Remove --with-fp / --without-fp.
[glibc.git] / math / atest-exp.c
blob6029ae1fdfcfa5f24bd959a4bc60dd0d15b5bac3
1 /* Copyright (C) 1997-2017 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Geoffrey Keating <Geoff.Keating@anu.edu.au>, 1997.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <stdio.h>
20 #include <math.h>
21 #include <gmp.h>
22 #include <string.h>
23 #include <limits.h>
24 #include <assert.h>
26 #define PRINT_ERRORS 0
28 #define TOL 80
29 #define N2 17
30 #define FRAC (32*4)
32 #define mpbpl (CHAR_BIT * sizeof (mp_limb_t))
33 #define SZ (FRAC / mpbpl + 1)
34 typedef mp_limb_t mp1[SZ], mp2[SZ * 2];
36 /* This string has 101 hex digits. */
37 static const char exp1[102] = "2" /* point */
38 "b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da56a7"
39 "84d9045190cfef324e7738926cfbe5f4bf8d8d8c31d763da07";
40 static const char hexdig[] = "0123456789abcdef";
42 static void
43 print_mpn_hex (const mp_limb_t *x, unsigned size)
45 char value[size + 1];
46 unsigned i;
47 const unsigned final = (size * 4 > SZ * mpbpl) ? SZ * mpbpl / 4 : size;
49 memset (value, '0', size);
51 for (i = 0; i < final ; i++)
52 value[size - 1 - i] = hexdig[x[i * 4 / mpbpl] >> (i * 4) % mpbpl & 0xf];
54 value[size] = '\0';
55 fputs (value, stdout);
58 static void
59 exp_mpn (mp1 ex, mp1 x)
61 unsigned n;
62 mp1 xp;
63 mp2 tmp;
64 mp_limb_t chk __attribute__ ((unused));
65 mp1 tol;
67 memset (xp, 0, sizeof (mp1));
68 memset (ex, 0, sizeof (mp1));
69 xp[FRAC / mpbpl] = (mp_limb_t)1 << FRAC % mpbpl;
70 memset (tol,0, sizeof (mp1));
71 tol[(FRAC - TOL) / mpbpl] = (mp_limb_t)1 << (FRAC - TOL) % mpbpl;
73 n = 0;
77 /* Calculate sum(x^n/n!) until the next term is sufficiently small. */
79 mpn_mul_n (tmp, xp, x, SZ);
80 assert (tmp[SZ * 2 - 1] == 0);
81 if (n > 0)
82 mpn_divmod_1 (xp, tmp + FRAC / mpbpl, SZ, n);
83 chk = mpn_add_n (ex, ex, xp, SZ);
84 assert (chk == 0);
85 n++;
86 assert (n < 80); /* Catch too-high TOL. */
88 while (n < 10 || mpn_cmp (xp, tol, SZ) >= 0);
91 static int
92 mpn_bitsize(const mp_limb_t *SRC_PTR, mp_size_t SIZE)
94 int i, j;
95 for (i = SIZE - 1; i > 0; i--)
96 if (SRC_PTR[i] != 0)
97 break;
98 for (j = mpbpl - 1; j >= 0; j--)
99 if ((SRC_PTR[i] & (mp_limb_t)1 << j) != 0)
100 break;
102 return i * mpbpl + j;
105 static int
106 do_test (void)
108 mp1 ex, x, xt, e2, e3;
109 int i;
110 int errors = 0;
111 int failures = 0;
112 mp1 maxerror;
113 int maxerror_s = 0;
114 const double sf = pow (2, mpbpl);
116 /* assert (mpbpl == mp_bits_per_limb); */
117 assert (FRAC / mpbpl * mpbpl == FRAC);
119 memset (maxerror, 0, sizeof (mp1));
120 memset (xt, 0, sizeof (mp1));
121 xt[(FRAC - N2) / mpbpl] = (mp_limb_t)1 << (FRAC - N2) % mpbpl;
123 for (i = 0; i < 1 << N2; i++)
125 int e2s, e3s, j;
126 double de2;
128 mpn_mul_1 (x,xt,SZ,i);
129 exp_mpn (ex, x);
130 de2 = exp (i / (double) (1 << N2));
131 for (j = SZ-1; j >= 0; j--)
133 e2[j] = (mp_limb_t) de2;
134 de2 = (de2 - e2[j]) * sf;
136 if (mpn_cmp (ex,e2,SZ) >= 0)
137 mpn_sub_n (e3,ex,e2,SZ);
138 else
139 mpn_sub_n (e3,e2,ex,SZ);
141 e2s = mpn_bitsize (e2,SZ);
142 e3s = mpn_bitsize (e3,SZ);
143 if (e3s >= 0 && e2s - e3s < 54)
145 #if PRINT_ERRORS
146 printf ("%06x ", i * (0x100000 / (1 << N2)));
147 print_mpn_hex (ex, (FRAC / 4) + 1);
148 fputs ("\n ",stdout);
149 print_mpn_hex (e2, (FRAC / 4) + 1);
150 printf ("\n %c ",
151 e2s - e3s < 54 ? e2s - e3s == 53 ? 'e' : 'F' : 'P');
152 print_mpn_hex (e3, (FRAC / 4) + 1);
153 putchar ('\n');
154 #endif
155 errors += (e2s - e3s == 53);
156 failures += (e2s - e3s < 53);
158 if (e3s >= maxerror_s
159 && mpn_cmp (e3, maxerror, SZ) > 0)
161 memcpy (maxerror, e3, sizeof (mp1));
162 maxerror_s = e3s;
166 /* Check exp_mpn against precomputed value of exp(1). */
167 memset (x, '\0', sizeof (mp1));
168 x[FRAC / mpbpl] = (mp_limb_t)1 << FRAC % mpbpl;
169 exp_mpn (ex, x);
171 memset (e2, '\0', sizeof (mp1));
172 for (i = -1; i < 100 && i < FRAC / 4; i++)
173 e2[(FRAC - i * 4 - 4) / mpbpl] |= ((mp_limb_t) (strchr (hexdig,
174 exp1[i + 1])
175 - hexdig)
176 << (FRAC - i * 4 - 4) % mpbpl);
178 if (mpn_cmp (ex, e2, SZ) >= 0)
179 mpn_sub_n (e3, ex, e2, SZ);
180 else
181 mpn_sub_n (e3, e2, ex, SZ);
183 printf ("%d failures; %d errors; error rate %0.2f%%\n", failures, errors,
184 errors * 100.0 / (double) (1 << N2));
185 fputs ("maximum error: ", stdout);
186 print_mpn_hex (maxerror, (FRAC / 4) + 1);
187 fputs ("\nerror in exp(1): ", stdout);
188 print_mpn_hex (e3, (FRAC / 4) + 1);
189 putchar ('\n');
191 return failures == 0 ? 0 : 1;
194 #define TIMEOUT 200
195 #define TEST_FUNCTION do_test ()
196 #include "../test-skeleton.c"