]>
Commit | Line | Data |
---|---|---|
d4697bc9 | 1 | /* Copyright (C) 1997-2014 Free Software Foundation, Inc. |
650425ce UD |
2 | This file is part of the GNU C Library. |
3 | Contributed by Geoffrey Keating <Geoff.Keating@anu.edu.au>, 1997. | |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
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. | |
650425ce UD |
9 | |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
650425ce | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
650425ce UD |
18 | |
19 | #include <stdio.h> | |
20 | #include <math.h> | |
48896b9d | 21 | #include <gmp.h> |
650425ce UD |
22 | #include <string.h> |
23 | #include <limits.h> | |
24 | #include <assert.h> | |
25 | #include <stdlib.h> | |
26 | ||
27 | #define PRINT_ERRORS 0 | |
28 | ||
29 | #define TOL 80 | |
30 | #define N2 18 | |
31 | #define FRAC (32*4) | |
32 | ||
33 | #define mpbpl (CHAR_BIT * sizeof (mp_limb_t)) | |
34 | #define SZ (FRAC / mpbpl + 1) | |
35 | typedef mp_limb_t mp1[SZ], mp2[SZ * 2]; | |
36 | ||
e6e49e59 RH |
37 | #if BITS_PER_MP_LIMB == 64 |
38 | # define LIMB64(L, H) 0x ## H ## L | |
39 | #elif BITS_PER_MP_LIMB == 32 | |
40 | # define LIMB64(L, H) 0x ## L, 0x ## H | |
41 | #else | |
42 | # error | |
43 | #endif | |
44 | ||
45 | /* Once upon a time these constants were generated to 400 bits. | |
46 | We only need FRAC bits (128) at present, but we retain 384 bits | |
47 | in the text Just In Case. */ | |
48 | #define CONSTSZ(INT, F1, F2, F3, F4, F5, F6, F7, F8, F9, Fa, Fb, Fc) \ | |
49 | LIMB64(F4, F3), LIMB64(F2, F1), INT | |
50 | ||
51 | static const mp1 mp_exp1 = { | |
52 | CONSTSZ (2, b7e15162, 8aed2a6a, bf715880, 9cf4f3c7, 62e7160f, 38b4da56, | |
53 | a784d904, 5190cfef, 324e7738, 926cfbe5, f4bf8d8d, 8c31d763) | |
54 | }; | |
650425ce | 55 | |
e6e49e59 RH |
56 | static const mp1 mp_exp_m1 = { |
57 | CONSTSZ (0, 5e2d58d8, b3bcdf1a, badec782, 9054f90d, da9805aa, b56c7733, | |
58 | 3024b9d0, a507daed, b16400bf, 472b4215, b8245b66, 9d90d27a) | |
59 | }; | |
60 | ||
61 | static const mp1 mp_log2 = { | |
62 | CONSTSZ (0, b17217f7, d1cf79ab, c9e3b398, 03f2f6af, 40f34326, 7298b62d, | |
63 | 8a0d175b, 8baafa2b, e7b87620, 6debac98, 559552fb, 4afa1b10) | |
64 | }; | |
650425ce | 65 | |
767b6275 | 66 | static void |
650425ce UD |
67 | print_mpn_fp (const mp_limb_t *x, unsigned int dp, unsigned int base) |
68 | { | |
e6e49e59 | 69 | static const char hexdig[16] = "0123456789abcdef"; |
650425ce UD |
70 | unsigned int i; |
71 | mp1 tx; | |
72 | ||
73 | memcpy (tx, x, sizeof (mp1)); | |
74 | if (base == 16) | |
75 | fputs ("0x", stdout); | |
76 | assert (x[SZ-1] < base); | |
77 | fputc (hexdig[x[SZ - 1]], stdout); | |
78 | fputc ('.', stdout); | |
79 | for (i = 0; i < dp; i++) | |
80 | { | |
81 | tx[SZ - 1] = 0; | |
82 | mpn_mul_1 (tx, tx, SZ, base); | |
83 | assert (tx[SZ - 1] < base); | |
84 | fputc (hexdig[tx[SZ - 1]], stdout); | |
85 | } | |
86 | } | |
87 | ||
650425ce | 88 | /* Compute e^x. */ |
767b6275 | 89 | static void |
650425ce UD |
90 | exp_mpn (mp1 ex, mp1 x) |
91 | { | |
92 | unsigned int n; | |
93 | mp1 xp; | |
94 | mp2 tmp; | |
e4f22324 | 95 | mp_limb_t chk; |
650425ce UD |
96 | mp1 tol; |
97 | ||
98 | memset (xp, 0, sizeof (mp1)); | |
99 | memset (ex, 0, sizeof (mp1)); | |
69bf5f75 | 100 | xp[FRAC / mpbpl] = (mp_limb_t)1 << FRAC % mpbpl; |
650425ce | 101 | memset (tol, 0, sizeof (mp1)); |
69bf5f75 | 102 | tol[(FRAC - TOL) / mpbpl] = (mp_limb_t)1 << (FRAC - TOL) % mpbpl; |
650425ce UD |
103 | |
104 | n = 0; | |
105 | ||
106 | do | |
107 | { | |
108 | /* Calculate sum(x^n/n!) until the next term is sufficiently small. */ | |
109 | ||
110 | mpn_mul_n (tmp, xp, x, SZ); | |
111 | assert(tmp[SZ * 2 - 1] == 0); | |
112 | if (n > 0) | |
e4f22324 | 113 | mpn_divmod_1 (xp, tmp + FRAC / mpbpl, SZ, n); |
650425ce UD |
114 | chk = mpn_add_n (ex, ex, xp, SZ); |
115 | assert (chk == 0); | |
116 | ++n; | |
117 | assert (n < 80); /* Catch too-high TOL. */ | |
118 | } | |
119 | while (n < 10 || mpn_cmp (xp, tol, SZ) >= 0); | |
120 | } | |
121 | ||
122 | /* Calculate 2^x. */ | |
767b6275 | 123 | static void |
650425ce UD |
124 | exp2_mpn (mp1 ex, mp1 x) |
125 | { | |
126 | mp2 tmp; | |
e6e49e59 | 127 | mpn_mul_n (tmp, x, mp_log2, SZ); |
650425ce UD |
128 | assert(tmp[SZ * 2 - 1] == 0); |
129 | exp_mpn (ex, tmp + FRAC / mpbpl); | |
130 | } | |
131 | ||
132 | ||
133 | static int | |
134 | mpn_bitsize(const mp_limb_t *SRC_PTR, mp_size_t SIZE) | |
135 | { | |
136 | int i, j; | |
137 | for (i = SIZE - 1; i > 0; --i) | |
138 | if (SRC_PTR[i] != 0) | |
139 | break; | |
b259e746 UD |
140 | for (j = mpbpl - 1; j >= 0; --j) |
141 | if ((SRC_PTR[i] & (mp_limb_t)1 << j) != 0) | |
650425ce UD |
142 | break; |
143 | ||
b259e746 | 144 | return i * mpbpl + j; |
650425ce UD |
145 | } |
146 | ||
29955b5d AS |
147 | static int |
148 | do_test (void) | |
650425ce UD |
149 | { |
150 | mp1 ex, x, xt, e2, e3; | |
151 | int i; | |
152 | int errors = 0; | |
153 | int failures = 0; | |
154 | mp1 maxerror; | |
155 | int maxerror_s = 0; | |
156 | const double sf = pow (2, mpbpl); | |
157 | ||
158 | /* assert(mpbpl == mp_bits_per_limb); */ | |
159 | assert(FRAC / mpbpl * mpbpl == FRAC); | |
160 | ||
161 | memset (maxerror, 0, sizeof (mp1)); | |
162 | memset (xt, 0, sizeof (mp1)); | |
69bf5f75 | 163 | xt[(FRAC - N2) / mpbpl] = (mp_limb_t)1 << (FRAC - N2) % mpbpl; |
650425ce UD |
164 | |
165 | for (i = 0; i < (1 << N2); ++i) | |
166 | { | |
167 | int e2s, e3s, j; | |
168 | double de2; | |
169 | ||
170 | mpn_mul_1 (x, xt, SZ, i); | |
171 | exp2_mpn (ex, x); | |
172 | de2 = exp2 (i / (double) (1 << N2)); | |
173 | for (j = SZ - 1; j >= 0; --j) | |
174 | { | |
175 | e2[j] = (mp_limb_t) de2; | |
176 | de2 = (de2 - e2[j]) * sf; | |
177 | } | |
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); | |
182 | ||
183 | e2s = mpn_bitsize (e2, SZ); | |
184 | e3s = mpn_bitsize (e3, SZ); | |
b259e746 | 185 | if (e3s >= 0 && e2s - e3s < 54) |
650425ce UD |
186 | { |
187 | #if PRINT_ERRORS | |
188 | printf ("%06x ", i * (0x100000 / (1 << N2))); | |
189 | print_mpn_fp (ex, (FRAC / 4) + 1, 16); | |
190 | putchar ('\n'); | |
191 | fputs (" ",stdout); | |
192 | print_mpn_fp (e2, (FRAC / 4) + 1, 16); | |
193 | putchar ('\n'); | |
194 | printf (" %c ", | |
195 | e2s - e3s < 54 ? e2s - e3s == 53 ? 'e' : 'F' : 'P'); | |
196 | print_mpn_fp (e3, (FRAC / 4) + 1, 16); | |
197 | putchar ('\n'); | |
198 | #endif | |
199 | errors += (e2s - e3s == 53); | |
200 | failures += (e2s - e3s < 53); | |
201 | } | |
202 | if (e3s >= maxerror_s | |
203 | && mpn_cmp (e3, maxerror, SZ) > 0) | |
204 | { | |
205 | memcpy (maxerror, e3, sizeof (mp1)); | |
206 | maxerror_s = e3s; | |
207 | } | |
208 | } | |
209 | ||
210 | /* Check exp_mpn against precomputed value of exp(1). */ | |
211 | memset (x, 0, sizeof (mp1)); | |
69bf5f75 | 212 | x[FRAC / mpbpl] = (mp_limb_t)1 << FRAC % mpbpl; |
650425ce | 213 | exp_mpn (ex, x); |
e6e49e59 RH |
214 | if (mpn_cmp (ex, mp_exp1, SZ) >= 0) |
215 | mpn_sub_n (e3, ex, mp_exp1, SZ); | |
650425ce | 216 | else |
e6e49e59 | 217 | mpn_sub_n (e3, mp_exp1, ex, SZ); |
650425ce UD |
218 | |
219 | printf ("%d failures; %d errors; error rate %0.2f%%\n", failures, errors, | |
220 | errors * 100.0 / (double) (1 << N2)); | |
221 | fputs ("maximum error: ", stdout); | |
222 | print_mpn_fp (maxerror, (FRAC / 4) + 1, 16); | |
223 | putchar ('\n'); | |
224 | fputs ("error in exp(1): ", stdout); | |
225 | print_mpn_fp (e3, (FRAC / 4) + 1, 16); | |
226 | putchar ('\n'); | |
227 | ||
228 | return failures == 0 ? 0 : 1; | |
229 | } | |
29955b5d | 230 | |
4ef91cdc | 231 | #define TIMEOUT 10 |
29955b5d AS |
232 | #define TEST_FUNCTION do_test () |
233 | #include "../test-skeleton.c" |