1 /* Copyright (C) 1997-2013 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/>. */
26 #define PRINT_ERRORS 0
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";
43 print_mpn_hex (const mp_limb_t
*x
, unsigned size
)
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];
55 fputs (value
, stdout
);
59 exp_mpn (mp1 ex
, mp1 x
)
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
;
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);
82 mpn_divmod_1 (xp
, tmp
+ FRAC
/ mpbpl
, SZ
, n
);
83 chk
= mpn_add_n (ex
, ex
, xp
, SZ
);
86 assert (n
< 80); /* Catch too-high TOL. */
88 while (n
< 10 || mpn_cmp (xp
, tol
, SZ
) >= 0);
92 mpn_bitsize(const mp_limb_t
*SRC_PTR
, mp_size_t SIZE
)
95 for (i
= SIZE
- 1; i
> 0; i
--)
98 for (j
= mpbpl
- 1; j
>= 0; j
--)
99 if ((SRC_PTR
[i
] & (mp_limb_t
)1 << j
) != 0)
102 return i
* mpbpl
+ j
;
108 mp1 ex
, x
, xt
, e2
, e3
;
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
++)
128 mpn_mul_1 (x
,xt
,SZ
,i
);
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
);
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)
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);
151 e2s
- e3s
< 54 ? e2s
- e3s
== 53 ? 'e' : 'F' : 'P');
152 print_mpn_hex (e3
, (FRAC
/ 4) + 1);
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
));
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
;
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
,
176 << (FRAC
- i
* 4 - 4) % mpbpl
);
178 if (mpn_cmp (ex
, e2
, SZ
) >= 0)
179 mpn_sub_n (e3
, ex
, e2
, SZ
);
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);
191 return failures
== 0 ? 0 : 1;