]> git.ipfire.org Git - thirdparty/glibc.git/blame - math/atest-sincos.c
Update copyright notices with scripts/update-copyrights.
[thirdparty/glibc.git] / math / atest-sincos.c
CommitLineData
568035b7 1/* Copyright (C) 1997-2013 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
PE
16 License along with the GNU C Library; if not, see
17 <http://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)
34typedef mp_limb_t mp1[SZ], mp2[SZ * 2];
35
36/* These strings have exactly 100 hex digits in them. */
37static const char sin1[101] =
38"d76aa47848677020c6e9e909c50f3c3289e511132f518b4def"
39"b6ca5fd6c649bdfb0bd9ff1edcd4577655b5826a3d3b50c264";
40static const char cos1[101] =
41"8a51407da8345c91c2466d976871bd29a2373a894f96c3b7f2"
42"300240b760e6fa96a94430a52d0e9e43f3450e3b8ff99bc934";
43static const char hexdig[] = "0123456789abcdef";
44
767b6275 45static void
9a0a462c
UD
46print_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 61static void
9a0a462c
UD
62sincosx_mpn (mp1 si, mp1 co, mp1 xx, mp1 ix)
63{
64 int i;
65 mp2 s[4], c[4];
66 mp1 tmp, x;
67 mp_limb_t chk, round;
68
69 if (ix == NULL)
70 {
71 memset (si, 0, sizeof (mp1));
72 memset (co, 0, sizeof (mp1));
73 co[SZ-1] = 1;
74 memcpy (x, xx, sizeof (mp1));
75 }
76 else
77 mpn_sub_n (x, xx, ix, SZ);
78
79 for (i = 0; i < 1 << N; i++)
80 {
81#define add_shift_mulh(d,x,s1,s2,sh,n) \
82 /* d = (n ? -1 : 1) * (s1 + (s2>>sh)) * x / (1>>N); */ \
83 do { \
84 if (s2 != NULL) { \
85 if (sh > 0) { \
86 assert (sh < mpbpl); \
87 mpn_lshift (tmp, s1, SZ, sh); \
88 chk = (n ? mpn_sub_n : mpn_add_n)(tmp,tmp,s2+FRAC/mpbpl,SZ); \
89 } else \
90 chk = (n ? mpn_sub_n : mpn_add_n)(tmp,s1,s2+FRAC/mpbpl,SZ); \
91 /* assert(chk == 0); */ \
92 mpn_mul_n(d,tmp,x,SZ); \
93 } else \
94 mpn_mul_n(d,s1,x,SZ); \
95 /* assert(d[SZ*2-1] == 0); */ \
96 assert(N+sh < mpbpl); \
97 if (N+sh > 0) mpn_rshift(d,d,2*SZ,N+sh); \
98 } while(0)
99#define summ(d,ss,s,n) \
100 /* d = ss +/- (s[0]+2*s[1]+2*s[2]+s[3])/6; */ \
101 do { \
102 chk = mpn_add_n(tmp,s[1]+FRAC/mpbpl,s[2]+FRAC/mpbpl,SZ); \
103 mpn_lshift(tmp,tmp,SZ,1); \
104 chk |= mpn_add_n(tmp,tmp,s[0]+FRAC/mpbpl,SZ); \
105 chk |= mpn_add_n(tmp,tmp,s[3]+FRAC/mpbpl,SZ); \
106 round = mpn_divmod_1(tmp,tmp,SZ,6); \
107 /* chk |= mpn_add_1(tmp,tmp,SZ, (round > 3) ); */ \
108 chk |= (n ? mpn_sub_n : mpn_add_n)(d,ss,tmp,SZ); \
109 /* assert(chk == 0); */ \
110 } while (0)
111
112 add_shift_mulh (s[0], x, co, NULL, 0, 0); /* s0 = h * c; */
113 add_shift_mulh (c[0], x, si, NULL, 0, 0); /* c0 = h * s; */
114 add_shift_mulh (s[1], x, co, c[0], 1, 1); /* s1 = h * (c - c0/2); */
115 add_shift_mulh (c[1], x, si, s[0], 1, 0); /* c1 = h * (s + s0/2); */
116 add_shift_mulh (s[2], x, co, c[1], 1, 1); /* s2 = h * (c - c1/2); */
117 add_shift_mulh (c[2], x, si, s[1], 1, 0); /* c2 = h * (s + s1/2); */
118 add_shift_mulh (s[3], x, co, c[2], 0, 1); /* s3 = h * (c - c2); */
119 add_shift_mulh (c[3], x, si, s[2], 0, 0); /* c3 = h * (s + s2); */
120 summ (si, si, s, 0); /* s = s + (s0+2*s1+2*s2+s3)/6; */
121 summ (co, co, c, 1); /* c = c - (c0+2*c1+2*c2+c3)/6; */
122 }
123#undef add_shift_mulh
124#undef summ
125}
126
127static int
128mpn_bitsize (const mp_limb_t *SRC_PTR, mp_size_t SIZE)
129{
130 int i, j;
131 for (i = SIZE - 1; i > 0; i--)
132 if (SRC_PTR[i] != 0)
133 break;
b259e746
UD
134 for (j = mpbpl - 1; j >= 0; j--)
135 if ((SRC_PTR[i] & (mp_limb_t)1 << j) != 0)
9a0a462c
UD
136 break;
137
b259e746 138 return i * mpbpl + j;
9a0a462c
UD
139}
140
141int
142main (void)
143{
144 mp1 si, co, x, ox, xt, s2, c2, s3, c3;
145 int i;
146 int sin_errors = 0, cos_errors = 0;
147 int sin_failures = 0, cos_failures = 0;
148 mp1 sin_maxerror, cos_maxerror;
149 int sin_maxerror_s = 0, cos_maxerror_s = 0;
150 const double sf = pow (2, mpbpl);
151
152 /* assert(mpbpl == mp_bits_per_limb); */
153 assert(FRAC / mpbpl * mpbpl == FRAC);
154
155 memset (sin_maxerror, 0, sizeof (mp1));
156 memset (cos_maxerror, 0, sizeof (mp1));
157 memset (xt, 0, sizeof (mp1));
69bf5f75 158 xt[(FRAC - N2) / mpbpl] = (mp_limb_t)1 << (FRAC - N2) % mpbpl;
9a0a462c
UD
159
160 for (i = 0; i < 1 << N2; i++)
161 {
162 int s2s, s3s, c2s, c3s, j;
163 double ds2,dc2;
164
165 mpn_mul_1 (x, xt, SZ, i);
166 sincosx_mpn (si, co, x, i == 0 ? NULL : ox);
167 memcpy (ox, x, sizeof (mp1));
168 ds2 = sin (i / (double) (1 << N2));
169 dc2 = cos (i / (double) (1 << N2));
170 for (j = SZ-1; j >= 0; j--)
171 {
172 s2[j] = (mp_limb_t) ds2;
173 ds2 = (ds2 - s2[j]) * sf;
174 c2[j] = (mp_limb_t) dc2;
175 dc2 = (dc2 - c2[j]) * sf;
176 }
177 if (mpn_cmp (si, s2, SZ) >= 0)
178 mpn_sub_n (s3, si, s2, SZ);
179 else
180 mpn_sub_n (s3, s2, si, SZ);
181 if (mpn_cmp (co, c2, SZ) >= 0)
182 mpn_sub_n (c3, co, c2, SZ);
183 else
184 mpn_sub_n (c3, c2, co, SZ);
185
186 s2s = mpn_bitsize (s2, SZ);
187 s3s = mpn_bitsize (s3, SZ);
188 c2s = mpn_bitsize (c2, SZ);
189 c3s = mpn_bitsize (c3, SZ);
04795ad9
UD
190 if ((s3s >= 0 && s2s - s3s < 54)
191 || (c3s >= 0 && c2s - c3s < 54)
9a0a462c
UD
192 || 0)
193 {
194#if PRINT_ERRORS
195 printf ("%06x ", i * (0x100000 / (1 << N2)));
196 print_mpn_hex(si, (FRAC / 4) + 1);
197 putchar (' ');
198 print_mpn_hex (co, (FRAC / 4) + 1);
199 putchar ('\n');
200 fputs (" ", stdout);
201 print_mpn_hex (s2, (FRAC / 4) + 1);
202 putchar (' ');
203 print_mpn_hex (c2, (FRAC / 4) + 1);
204 putchar ('\n');
205 printf (" %c%c ",
b259e746
UD
206 s3s >= 0 && s2s-s3s < 54 ? s2s - s3s == 53 ? 'e' : 'F' : 'P',
207 c3s >= 0 && c2s-c3s < 54 ? c2s - c3s == 53 ? 'e' : 'F' : 'P');
9a0a462c
UD
208 print_mpn_hex (s3, (FRAC / 4) + 1);
209 putchar (' ');
210 print_mpn_hex (c3, (FRAC / 4) + 1);
211 putchar ('\n');
212#endif
213 sin_errors += s2s - s3s == 53;
214 cos_errors += c2s - c3s == 53;
215 sin_failures += s2s - s3s < 53;
216 cos_failures += c2s - c3s < 53;
217 }
218 if (s3s >= sin_maxerror_s
219 && mpn_cmp (s3, sin_maxerror, SZ) > 0)
220 {
221 memcpy (sin_maxerror, s3, sizeof (mp1));
222 sin_maxerror_s = s3s;
223 }
224 if (c3s >= cos_maxerror_s
225 && mpn_cmp (c3, cos_maxerror, SZ) > 0)
226 {
227 memcpy (cos_maxerror, c3, sizeof (mp1));
228 cos_maxerror_s = c3s;
229 }
230 }
231
232 /* Check Range-Kutta against precomputed values of sin(1) and cos(1). */
233 memset (x, 0, sizeof (mp1));
69bf5f75 234 x[FRAC / mpbpl] = (mp_limb_t)1 << FRAC % mpbpl;
9a0a462c
UD
235 sincosx_mpn (si, co, x, ox);
236
237 memset (s2, 0, sizeof (mp1));
238 memset (c2, 0, sizeof (mp1));
239 for (i = 0; i < 100 && i < FRAC / 4; i++)
240 {
11bf311e
UD
241 s2[(FRAC - i * 4 - 4) / mpbpl] |= ((mp_limb_t) (strchr (hexdig, sin1[i])
242 - hexdig)
243 << (FRAC - i * 4 - 4) % mpbpl);
244 c2[(FRAC - i * 4 - 4) / mpbpl] |= ((mp_limb_t) (strchr (hexdig, cos1[i])
245 - hexdig)
9a0a462c
UD
246 << (FRAC - i * 4 - 4) % mpbpl);
247 }
248
249 if (mpn_cmp (si, s2, SZ) >= 0)
250 mpn_sub_n (s3, si, s2, SZ);
251 else
252 mpn_sub_n (s3, s2, si, SZ);
253 if (mpn_cmp (co, c2, SZ) >= 0)
254 mpn_sub_n (c3, co, c2, SZ);
255 else
256 mpn_sub_n (c3, c2, co, SZ);
257
258 printf ("sin:\n");
259 printf ("%d failures; %d errors; error rate %0.2f%%\n",
260 sin_failures, sin_errors, sin_errors * 100.0 / (double) (1 << N2));
261 fputs ("maximum error: ", stdout);
262 print_mpn_hex (sin_maxerror, (FRAC / 4) + 1);
263 fputs ("\nerror in sin(1): ", stdout);
264 print_mpn_hex (s3, (FRAC / 4) + 1);
265
266 fputs ("\n\ncos:\n", stdout);
267 printf ("%d failures; %d errors; error rate %0.2f%%\n",
268 cos_failures, cos_errors, cos_errors * 100.0 / (double) (1 << N2));
269 fputs ("maximum error: ", stdout);
270 print_mpn_hex (cos_maxerror, (FRAC / 4) + 1);
271 fputs ("\nerror in cos(1): ", stdout);
272 print_mpn_hex (c3, (FRAC / 4) + 1);
273 putchar ('\n');
274
275 return (sin_failures == 0 && cos_failures == 0) ? 0 : 1;
276}