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