]>
Commit | Line | Data |
---|---|---|
04795ad9 | 1 | /* Copyright (C) 1997, 1998 Free Software Foundation, Inc. |
9a0a462c 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 | |
6 | modify it under the terms of the GNU Library General Public License as | |
7 | published by the Free Software Foundation; either version 2 of the | |
8 | License, or (at your option) any later version. | |
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 | |
13 | Library General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Library General Public | |
16 | License along with the GNU C Library; see the file COPYING.LIB. If not, | |
17 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
18 | Boston, MA 02111-1307, USA. */ | |
19 | ||
20 | #include <stdio.h> | |
21 | #include <math.h> | |
22 | #include <stdlib/gmp.h> | |
23 | #include <string.h> | |
24 | #include <limits.h> | |
25 | #include <assert.h> | |
26 | ||
27 | #define PRINT_ERRORS 0 | |
28 | ||
29 | #define N 0 | |
30 | #define N2 20 | |
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 | ||
37 | /* These strings have exactly 100 hex digits in them. */ | |
38 | static const char sin1[101] = | |
39 | "d76aa47848677020c6e9e909c50f3c3289e511132f518b4def" | |
40 | "b6ca5fd6c649bdfb0bd9ff1edcd4577655b5826a3d3b50c264"; | |
41 | static const char cos1[101] = | |
42 | "8a51407da8345c91c2466d976871bd29a2373a894f96c3b7f2" | |
43 | "300240b760e6fa96a94430a52d0e9e43f3450e3b8ff99bc934"; | |
44 | static const char hexdig[] = "0123456789abcdef"; | |
45 | ||
46 | void | |
47 | print_mpn_hex (const mp_limb_t *x, unsigned size) | |
48 | { | |
49 | char value[size + 1]; | |
50 | unsigned i; | |
51 | const unsigned final = (size * 4 > SZ * mpbpl) ? SZ * mpbpl / 4 : size; | |
52 | ||
53 | memset (value, '0', size); | |
54 | ||
55 | for (i = 0; i < final ; i++) | |
56 | value[size-1-i] = hexdig[x[i * 4 / mpbpl] >> (i * 4) % mpbpl & 0xf]; | |
57 | ||
58 | value[size] = '\0'; | |
59 | fputs (value, stdout); | |
60 | } | |
61 | ||
62 | void | |
63 | sincosx_mpn (mp1 si, mp1 co, mp1 xx, mp1 ix) | |
64 | { | |
65 | int i; | |
66 | mp2 s[4], c[4]; | |
67 | mp1 tmp, x; | |
68 | mp_limb_t chk, round; | |
69 | ||
70 | if (ix == NULL) | |
71 | { | |
72 | memset (si, 0, sizeof (mp1)); | |
73 | memset (co, 0, sizeof (mp1)); | |
74 | co[SZ-1] = 1; | |
75 | memcpy (x, xx, sizeof (mp1)); | |
76 | } | |
77 | else | |
78 | mpn_sub_n (x, xx, ix, SZ); | |
79 | ||
80 | for (i = 0; i < 1 << N; i++) | |
81 | { | |
82 | #define add_shift_mulh(d,x,s1,s2,sh,n) \ | |
83 | /* d = (n ? -1 : 1) * (s1 + (s2>>sh)) * x / (1>>N); */ \ | |
84 | do { \ | |
85 | if (s2 != NULL) { \ | |
86 | if (sh > 0) { \ | |
87 | assert (sh < mpbpl); \ | |
88 | mpn_lshift (tmp, s1, SZ, sh); \ | |
89 | chk = (n ? mpn_sub_n : mpn_add_n)(tmp,tmp,s2+FRAC/mpbpl,SZ); \ | |
90 | } else \ | |
91 | chk = (n ? mpn_sub_n : mpn_add_n)(tmp,s1,s2+FRAC/mpbpl,SZ); \ | |
92 | /* assert(chk == 0); */ \ | |
93 | mpn_mul_n(d,tmp,x,SZ); \ | |
94 | } else \ | |
95 | mpn_mul_n(d,s1,x,SZ); \ | |
96 | /* assert(d[SZ*2-1] == 0); */ \ | |
97 | assert(N+sh < mpbpl); \ | |
98 | if (N+sh > 0) mpn_rshift(d,d,2*SZ,N+sh); \ | |
99 | } while(0) | |
100 | #define summ(d,ss,s,n) \ | |
101 | /* d = ss +/- (s[0]+2*s[1]+2*s[2]+s[3])/6; */ \ | |
102 | do { \ | |
103 | chk = mpn_add_n(tmp,s[1]+FRAC/mpbpl,s[2]+FRAC/mpbpl,SZ); \ | |
104 | mpn_lshift(tmp,tmp,SZ,1); \ | |
105 | chk |= mpn_add_n(tmp,tmp,s[0]+FRAC/mpbpl,SZ); \ | |
106 | chk |= mpn_add_n(tmp,tmp,s[3]+FRAC/mpbpl,SZ); \ | |
107 | round = mpn_divmod_1(tmp,tmp,SZ,6); \ | |
108 | /* chk |= mpn_add_1(tmp,tmp,SZ, (round > 3) ); */ \ | |
109 | chk |= (n ? mpn_sub_n : mpn_add_n)(d,ss,tmp,SZ); \ | |
110 | /* assert(chk == 0); */ \ | |
111 | } while (0) | |
112 | ||
113 | add_shift_mulh (s[0], x, co, NULL, 0, 0); /* s0 = h * c; */ | |
114 | add_shift_mulh (c[0], x, si, NULL, 0, 0); /* c0 = h * s; */ | |
115 | add_shift_mulh (s[1], x, co, c[0], 1, 1); /* s1 = h * (c - c0/2); */ | |
116 | add_shift_mulh (c[1], x, si, s[0], 1, 0); /* c1 = h * (s + s0/2); */ | |
117 | add_shift_mulh (s[2], x, co, c[1], 1, 1); /* s2 = h * (c - c1/2); */ | |
118 | add_shift_mulh (c[2], x, si, s[1], 1, 0); /* c2 = h * (s + s1/2); */ | |
119 | add_shift_mulh (s[3], x, co, c[2], 0, 1); /* s3 = h * (c - c2); */ | |
120 | add_shift_mulh (c[3], x, si, s[2], 0, 0); /* c3 = h * (s + s2); */ | |
121 | summ (si, si, s, 0); /* s = s + (s0+2*s1+2*s2+s3)/6; */ | |
122 | summ (co, co, c, 1); /* c = c - (c0+2*c1+2*c2+c3)/6; */ | |
123 | } | |
124 | #undef add_shift_mulh | |
125 | #undef summ | |
126 | } | |
127 | ||
128 | static int | |
129 | mpn_bitsize (const mp_limb_t *SRC_PTR, mp_size_t SIZE) | |
130 | { | |
131 | int i, j; | |
132 | for (i = SIZE - 1; i > 0; i--) | |
133 | if (SRC_PTR[i] != 0) | |
134 | break; | |
b259e746 UD |
135 | for (j = mpbpl - 1; j >= 0; j--) |
136 | if ((SRC_PTR[i] & (mp_limb_t)1 << j) != 0) | |
9a0a462c UD |
137 | break; |
138 | ||
b259e746 | 139 | return i * mpbpl + j; |
9a0a462c UD |
140 | } |
141 | ||
142 | int | |
143 | main (void) | |
144 | { | |
145 | mp1 si, co, x, ox, xt, s2, c2, s3, c3; | |
146 | int i; | |
147 | int sin_errors = 0, cos_errors = 0; | |
148 | int sin_failures = 0, cos_failures = 0; | |
149 | mp1 sin_maxerror, cos_maxerror; | |
150 | int sin_maxerror_s = 0, cos_maxerror_s = 0; | |
151 | const double sf = pow (2, mpbpl); | |
152 | ||
153 | /* assert(mpbpl == mp_bits_per_limb); */ | |
154 | assert(FRAC / mpbpl * mpbpl == FRAC); | |
155 | ||
156 | memset (sin_maxerror, 0, sizeof (mp1)); | |
157 | memset (cos_maxerror, 0, sizeof (mp1)); | |
158 | memset (xt, 0, sizeof (mp1)); | |
69bf5f75 | 159 | xt[(FRAC - N2) / mpbpl] = (mp_limb_t)1 << (FRAC - N2) % mpbpl; |
9a0a462c UD |
160 | |
161 | for (i = 0; i < 1 << N2; i++) | |
162 | { | |
163 | int s2s, s3s, c2s, c3s, j; | |
164 | double ds2,dc2; | |
165 | ||
166 | mpn_mul_1 (x, xt, SZ, i); | |
167 | sincosx_mpn (si, co, x, i == 0 ? NULL : ox); | |
168 | memcpy (ox, x, sizeof (mp1)); | |
169 | ds2 = sin (i / (double) (1 << N2)); | |
170 | dc2 = cos (i / (double) (1 << N2)); | |
171 | for (j = SZ-1; j >= 0; j--) | |
172 | { | |
173 | s2[j] = (mp_limb_t) ds2; | |
174 | ds2 = (ds2 - s2[j]) * sf; | |
175 | c2[j] = (mp_limb_t) dc2; | |
176 | dc2 = (dc2 - c2[j]) * sf; | |
177 | } | |
178 | if (mpn_cmp (si, s2, SZ) >= 0) | |
179 | mpn_sub_n (s3, si, s2, SZ); | |
180 | else | |
181 | mpn_sub_n (s3, s2, si, SZ); | |
182 | if (mpn_cmp (co, c2, SZ) >= 0) | |
183 | mpn_sub_n (c3, co, c2, SZ); | |
184 | else | |
185 | mpn_sub_n (c3, c2, co, SZ); | |
186 | ||
187 | s2s = mpn_bitsize (s2, SZ); | |
188 | s3s = mpn_bitsize (s3, SZ); | |
189 | c2s = mpn_bitsize (c2, SZ); | |
190 | c3s = mpn_bitsize (c3, SZ); | |
04795ad9 UD |
191 | if ((s3s >= 0 && s2s - s3s < 54) |
192 | || (c3s >= 0 && c2s - c3s < 54) | |
9a0a462c UD |
193 | || 0) |
194 | { | |
195 | #if PRINT_ERRORS | |
196 | printf ("%06x ", i * (0x100000 / (1 << N2))); | |
197 | print_mpn_hex(si, (FRAC / 4) + 1); | |
198 | putchar (' '); | |
199 | print_mpn_hex (co, (FRAC / 4) + 1); | |
200 | putchar ('\n'); | |
201 | fputs (" ", stdout); | |
202 | print_mpn_hex (s2, (FRAC / 4) + 1); | |
203 | putchar (' '); | |
204 | print_mpn_hex (c2, (FRAC / 4) + 1); | |
205 | putchar ('\n'); | |
206 | printf (" %c%c ", | |
b259e746 UD |
207 | s3s >= 0 && s2s-s3s < 54 ? s2s - s3s == 53 ? 'e' : 'F' : 'P', |
208 | c3s >= 0 && c2s-c3s < 54 ? c2s - c3s == 53 ? 'e' : 'F' : 'P'); | |
9a0a462c UD |
209 | print_mpn_hex (s3, (FRAC / 4) + 1); |
210 | putchar (' '); | |
211 | print_mpn_hex (c3, (FRAC / 4) + 1); | |
212 | putchar ('\n'); | |
213 | #endif | |
214 | sin_errors += s2s - s3s == 53; | |
215 | cos_errors += c2s - c3s == 53; | |
216 | sin_failures += s2s - s3s < 53; | |
217 | cos_failures += c2s - c3s < 53; | |
218 | } | |
219 | if (s3s >= sin_maxerror_s | |
220 | && mpn_cmp (s3, sin_maxerror, SZ) > 0) | |
221 | { | |
222 | memcpy (sin_maxerror, s3, sizeof (mp1)); | |
223 | sin_maxerror_s = s3s; | |
224 | } | |
225 | if (c3s >= cos_maxerror_s | |
226 | && mpn_cmp (c3, cos_maxerror, SZ) > 0) | |
227 | { | |
228 | memcpy (cos_maxerror, c3, sizeof (mp1)); | |
229 | cos_maxerror_s = c3s; | |
230 | } | |
231 | } | |
232 | ||
233 | /* Check Range-Kutta against precomputed values of sin(1) and cos(1). */ | |
234 | memset (x, 0, sizeof (mp1)); | |
69bf5f75 | 235 | x[FRAC / mpbpl] = (mp_limb_t)1 << FRAC % mpbpl; |
9a0a462c UD |
236 | sincosx_mpn (si, co, x, ox); |
237 | ||
238 | memset (s2, 0, sizeof (mp1)); | |
239 | memset (c2, 0, sizeof (mp1)); | |
240 | for (i = 0; i < 100 && i < FRAC / 4; i++) | |
241 | { | |
04795ad9 | 242 | s2[(FRAC - i * 4 - 4) / mpbpl] |= ((strchr (hexdig, sin1[i]) - hexdig) |
9a0a462c | 243 | << (FRAC - i * 4 - 4) % mpbpl); |
04795ad9 | 244 | c2[(FRAC - i * 4 - 4) / mpbpl] |= ((strchr (hexdig, cos1[i]) - hexdig) |
9a0a462c UD |
245 | << (FRAC - i * 4 - 4) % mpbpl); |
246 | } | |
247 | ||
248 | if (mpn_cmp (si, s2, SZ) >= 0) | |
249 | mpn_sub_n (s3, si, s2, SZ); | |
250 | else | |
251 | mpn_sub_n (s3, s2, si, SZ); | |
252 | if (mpn_cmp (co, c2, SZ) >= 0) | |
253 | mpn_sub_n (c3, co, c2, SZ); | |
254 | else | |
255 | mpn_sub_n (c3, c2, co, SZ); | |
256 | ||
257 | printf ("sin:\n"); | |
258 | printf ("%d failures; %d errors; error rate %0.2f%%\n", | |
259 | sin_failures, sin_errors, sin_errors * 100.0 / (double) (1 << N2)); | |
260 | fputs ("maximum error: ", stdout); | |
261 | print_mpn_hex (sin_maxerror, (FRAC / 4) + 1); | |
262 | fputs ("\nerror in sin(1): ", stdout); | |
263 | print_mpn_hex (s3, (FRAC / 4) + 1); | |
264 | ||
265 | fputs ("\n\ncos:\n", stdout); | |
266 | printf ("%d failures; %d errors; error rate %0.2f%%\n", | |
267 | cos_failures, cos_errors, cos_errors * 100.0 / (double) (1 << N2)); | |
268 | fputs ("maximum error: ", stdout); | |
269 | print_mpn_hex (cos_maxerror, (FRAC / 4) + 1); | |
270 | fputs ("\nerror in cos(1): ", stdout); | |
271 | print_mpn_hex (c3, (FRAC / 4) + 1); | |
272 | putchar ('\n'); | |
273 | ||
274 | return (sin_failures == 0 && cos_failures == 0) ? 0 : 1; | |
275 | } |