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

source code of glibc/math/atest-sincos.c