1 /* Copyright (C) 1997, 1998, 2000 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27 #define PRINT_ERRORS 0
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];
37 /* This string has 101 hex digits. */
38 static const char exp1
[102] = "2" /* point */
39 "b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da56a7"
40 "84d9045190cfef324e7738926cfbe5f4bf8d8d8c31d763da07";
41 static const char hexdig
[] = "0123456789abcdef";
44 print_mpn_hex (const mp_limb_t
*x
, unsigned size
)
48 const unsigned final
= (size
* 4 > SZ
* mpbpl
) ? SZ
* mpbpl
/ 4 : size
;
50 memset (value
, '0', size
);
52 for (i
= 0; i
< final
; i
++)
53 value
[size
- 1 - i
] = hexdig
[x
[i
* 4 / mpbpl
] >> (i
* 4) % mpbpl
& 0xf];
56 fputs (value
, stdout
);
60 exp_mpn (mp1 ex
, mp1 x
)
68 memset (xp
, 0, sizeof (mp1
));
69 memset (ex
, 0, sizeof (mp1
));
70 xp
[FRAC
/ mpbpl
] = (mp_limb_t
)1 << FRAC
% mpbpl
;
71 memset (tol
,0, sizeof (mp1
));
72 tol
[(FRAC
- TOL
) / mpbpl
] = (mp_limb_t
)1 << (FRAC
- TOL
) % mpbpl
;
78 /* Calculate sum(x^n/n!) until the next term is sufficiently small. */
80 mpn_mul_n (tmp
, xp
, x
, SZ
);
81 assert (tmp
[SZ
* 2 - 1] == 0);
83 round
= mpn_divmod_1 (xp
, tmp
+ FRAC
/ mpbpl
, SZ
, n
);
84 chk
= mpn_add_n (ex
, ex
, xp
, SZ
);
87 assert (n
< 80); /* Catch too-high TOL. */
89 while (n
< 10 || mpn_cmp (xp
, tol
, SZ
) >= 0);
93 mpn_bitsize(const mp_limb_t
*SRC_PTR
, mp_size_t SIZE
)
96 for (i
= SIZE
- 1; i
> 0; i
--)
99 for (j
= mpbpl
- 1; j
>= 0; j
--)
100 if ((SRC_PTR
[i
] & (mp_limb_t
)1 << j
) != 0)
103 return i
* mpbpl
+ j
;
109 mp1 ex
, x
, xt
, e2
, e3
;
115 const double sf
= pow (2, mpbpl
);
117 /* assert (mpbpl == mp_bits_per_limb); */
118 assert (FRAC
/ mpbpl
* mpbpl
== FRAC
);
120 memset (maxerror
, 0, sizeof (mp1
));
121 memset (xt
, 0, sizeof (mp1
));
122 xt
[(FRAC
- N2
) / mpbpl
] = (mp_limb_t
)1 << (FRAC
- N2
) % mpbpl
;
124 for (i
= 0; i
< 1 << N2
; i
++)
129 mpn_mul_1 (x
,xt
,SZ
,i
);
131 de2
= exp (i
/ (double) (1 << N2
));
132 for (j
= SZ
-1; j
>= 0; j
--)
134 e2
[j
] = (mp_limb_t
) de2
;
135 de2
= (de2
- e2
[j
]) * sf
;
137 if (mpn_cmp (ex
,e2
,SZ
) >= 0)
138 mpn_sub_n (e3
,ex
,e2
,SZ
);
140 mpn_sub_n (e3
,e2
,ex
,SZ
);
142 e2s
= mpn_bitsize (e2
,SZ
);
143 e3s
= mpn_bitsize (e3
,SZ
);
144 if (e3s
>= 0 && e2s
- e3s
< 54)
147 printf ("%06x ", i
* (0x100000 / (1 << N2
)));
148 print_mpn_hex (ex
, (FRAC
/ 4) + 1);
149 fputs ("\n ",stdout
);
150 print_mpn_hex (e2
, (FRAC
/ 4) + 1);
152 e2s
- e3s
< 54 ? e2s
- e3s
== 53 ? 'e' : 'F' : 'P');
153 print_mpn_hex (e3
, (FRAC
/ 4) + 1);
156 errors
+= (e2s
- e3s
== 53);
157 failures
+= (e2s
- e3s
< 53);
159 if (e3s
>= maxerror_s
160 && mpn_cmp (e3
, maxerror
, SZ
) > 0)
162 memcpy (maxerror
, e3
, sizeof (mp1
));
167 /* Check exp_mpn against precomputed value of exp(1). */
168 memset (x
, '\0', sizeof (mp1
));
169 x
[FRAC
/ mpbpl
] = (mp_limb_t
)1 << FRAC
% mpbpl
;
172 memset (e2
, '\0', sizeof (mp1
));
173 for (i
= -1; i
< 100 && i
< FRAC
/ 4; i
++)
174 e2
[(FRAC
- i
* 4 - 4) / mpbpl
] |= ((strchr (hexdig
, exp1
[i
+ 1]) - hexdig
)
175 << (FRAC
- i
* 4 - 4) % mpbpl
);
177 if (mpn_cmp (ex
, e2
, SZ
) >= 0)
178 mpn_sub_n (e3
, ex
, e2
, SZ
);
180 mpn_sub_n (e3
, e2
, ex
, SZ
);
182 printf ("%d failures; %d errors; error rate %0.2f%%\n", failures
, errors
,
183 errors
* 100.0 / (double) (1 << N2
));
184 fputs ("maximum error: ", stdout
);
185 print_mpn_hex (maxerror
, (FRAC
/ 4) + 1);
186 fputs ("\nerror in exp(1): ", stdout
);
187 print_mpn_hex (e3
, (FRAC
/ 4) + 1);
190 return failures
== 0 ? 0 : 1;