Tests for module 'exp2'.
[gnulib.git] / tests / test-exp2.h
1 /* Test of exp2*() function family.
2    Copyright (C) 2012 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 static void
18 test_function (void)
19 {
20   int i;
21   int j;
22   const DOUBLE TWO_MANT_DIG =
23     /* Assume MANT_DIG <= 5 * 31.
24        Use the identity
25          n = floor(n/5) + floor((n+1)/5) + ... + floor((n+4)/5).  */
26     (DOUBLE) (1U << ((MANT_DIG - 1) / 5))
27     * (DOUBLE) (1U << ((MANT_DIG - 1 + 1) / 5))
28     * (DOUBLE) (1U << ((MANT_DIG - 1 + 2) / 5))
29     * (DOUBLE) (1U << ((MANT_DIG - 1 + 3) / 5))
30     * (DOUBLE) (1U << ((MANT_DIG - 1 + 4) / 5));
31
32   /* Small integral arguments.  */
33   {
34     DOUBLE x = L_(0.0);
35     DOUBLE y = EXP2 (x);
36     ASSERT (y == L_(1.0));
37   }
38   {
39     DOUBLE x = L_(1.0);
40     DOUBLE y = EXP2 (x);
41     ASSERT (y == L_(2.0));
42   }
43   {
44     DOUBLE x = L_(2.0);
45     DOUBLE y = EXP2 (x);
46     ASSERT (y == L_(4.0));
47   }
48   /* <http://sourceware.org/bugzilla/show_bug.cgi?id=13824> */
49 #if !(defined __linux__ && defined __sparc__)
50   {
51     DOUBLE x = L_(3.0);
52     DOUBLE y = EXP2 (x);
53     ASSERT (y == L_(8.0));
54   }
55   {
56     DOUBLE x = L_(4.0);
57     DOUBLE y = EXP2 (x);
58     ASSERT (y == L_(16.0));
59   }
60 #endif
61   {
62     DOUBLE x = - L_(1.0);
63     DOUBLE y = EXP2 (x);
64     ASSERT (y == L_(0.5));
65   }
66   {
67     DOUBLE x = - L_(2.0);
68     DOUBLE y = EXP2 (x);
69     ASSERT (y == L_(0.25));
70   }
71
72   /* Randomized tests.  */
73   {
74     /* Error bound, in ulps.  */
75     const DOUBLE err_bound =
76       (sizeof (DOUBLE) == sizeof (long double) ?
77 #if defined __i386__ && defined __FreeBSD__
78        /* On FreeBSD/x86 6.4, the 'long double' type really has only 53 bits of
79           precision in the compiler but 64 bits of precision at runtime.  See
80           <http://lists.gnu.org/archive/html/bug-gnulib/2008-07/msg00063.html>.
81           The compiler has truncated all 'long double' literals in exp2l.c to
82           53 bits of precision.  */
83        L_(1350.0)
84 #else
85        L_(3.0)
86 #endif
87        : L_(3.0));
88
89     for (i = 0; i < SIZEOF (RANDOM); i++)
90       {
91         DOUBLE x = L_(16.0) * RANDOM[i]; /* 0.0 <= x <= 16.0 */
92         DOUBLE y = EXP2 (x);
93         DOUBLE z = EXP2 (- x);
94         DOUBLE err = y * z - L_(1.0);
95         ASSERT (y > L_(0.0));
96         ASSERT (z > L_(0.0));
97         ASSERT (err > - err_bound / TWO_MANT_DIG
98                 && err < err_bound / TWO_MANT_DIG);
99       }
100   }
101
102   {
103     /* Error bound, in ulps.  */
104     const DOUBLE err_bound =
105       (sizeof (DOUBLE) == sizeof (long double) ?
106 #if defined __i386__ && defined __FreeBSD__
107        L_(2300.0)
108 #else
109        L_(29.0)
110 #endif
111        : L_(11.0));
112
113     for (i = 0; i < SIZEOF (RANDOM) / 5; i++)
114       for (j = 0; j < SIZEOF (RANDOM) / 5; j++)
115         {
116           DOUBLE x = L_(32.0) * RANDOM[i] - L_(16.0); /* -16.0 <= x <= 16.0 */
117           DOUBLE y = L_(32.0) * RANDOM[j] - L_(16.0); /* -16.0 <= y <= 16.0 */
118           DOUBLE z = - x - y;
119           /* Approximately  x + y + z = 0.  */
120           DOUBLE err = EXP2 (x) * EXP2 (y) * EXP2 (z) - L_(1.0);
121           ASSERT (err > - err_bound / TWO_MANT_DIG
122                   && err < err_bound / TWO_MANT_DIG);
123         }
124   }
125 }
126
127 volatile DOUBLE x;
128 DOUBLE y;