(_dl_powerpc_cap_flags): Add 4 new cap
[glibc/history.git] / math / atest-sincos.c
blob911aaec2754e25b1c4537b3f7ac87ea1ace3db3e
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
18 02111-1307 USA. */
20 #include <stdio.h>
21 #include <math.h>
22 #include <gmp.h>
23 #include <string.h>
24 #include <limits.h>
25 #include <assert.h>
27 #define PRINT_ERRORS 0
29 #define N 0
30 #define N2 20
31 #define FRAC (32 * 4)
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 /* 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";
46 static void
47 print_mpn_hex (const mp_limb_t *x, unsigned size)
49 char value[size + 1];
50 unsigned i;
51 const unsigned final = (size * 4 > SZ * mpbpl) ? SZ * mpbpl / 4 : size;
53 memset (value, '0', size);
55 for (i = 0; i < final ; i++)
56 value[size-1-i] = hexdig[x[i * 4 / mpbpl] >> (i * 4) % mpbpl & 0xf];
58 value[size] = '\0';
59 fputs (value, stdout);
62 static void
63 sincosx_mpn (mp1 si, mp1 co, mp1 xx, mp1 ix)
65 int i;
66 mp2 s[4], c[4];
67 mp1 tmp, x;
68 mp_limb_t chk, round;
70 if (ix == NULL)
72 memset (si, 0, sizeof (mp1));
73 memset (co, 0, sizeof (mp1));
74 co[SZ-1] = 1;
75 memcpy (x, xx, sizeof (mp1));
77 else
78 mpn_sub_n (x, xx, ix, SZ);
80 for (i = 0; i < 1 << N; i++)
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)
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; */
124 #undef add_shift_mulh
125 #undef summ
128 static int
129 mpn_bitsize (const mp_limb_t *SRC_PTR, mp_size_t SIZE)
131 int i, j;
132 for (i = SIZE - 1; i > 0; i--)
133 if (SRC_PTR[i] != 0)
134 break;
135 for (j = mpbpl - 1; j >= 0; j--)
136 if ((SRC_PTR[i] & (mp_limb_t)1 << j) != 0)
137 break;
139 return i * mpbpl + j;
143 main (void)
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);
153 /* assert(mpbpl == mp_bits_per_limb); */
154 assert(FRAC / mpbpl * mpbpl == FRAC);
156 memset (sin_maxerror, 0, sizeof (mp1));
157 memset (cos_maxerror, 0, sizeof (mp1));
158 memset (xt, 0, sizeof (mp1));
159 xt[(FRAC - N2) / mpbpl] = (mp_limb_t)1 << (FRAC - N2) % mpbpl;
161 for (i = 0; i < 1 << N2; i++)
163 int s2s, s3s, c2s, c3s, j;
164 double ds2,dc2;
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--)
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;
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);
187 s2s = mpn_bitsize (s2, SZ);
188 s3s = mpn_bitsize (s3, SZ);
189 c2s = mpn_bitsize (c2, SZ);
190 c3s = mpn_bitsize (c3, SZ);
191 if ((s3s >= 0 && s2s - s3s < 54)
192 || (c3s >= 0 && c2s - c3s < 54)
193 || 0)
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 ",
207 s3s >= 0 && s2s-s3s < 54 ? s2s - s3s == 53 ? 'e' : 'F' : 'P',
208 c3s >= 0 && c2s-c3s < 54 ? c2s - c3s == 53 ? 'e' : 'F' : 'P');
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;
219 if (s3s >= sin_maxerror_s
220 && mpn_cmp (s3, sin_maxerror, SZ) > 0)
222 memcpy (sin_maxerror, s3, sizeof (mp1));
223 sin_maxerror_s = s3s;
225 if (c3s >= cos_maxerror_s
226 && mpn_cmp (c3, cos_maxerror, SZ) > 0)
228 memcpy (cos_maxerror, c3, sizeof (mp1));
229 cos_maxerror_s = c3s;
233 /* Check Range-Kutta against precomputed values of sin(1) and cos(1). */
234 memset (x, 0, sizeof (mp1));
235 x[FRAC / mpbpl] = (mp_limb_t)1 << FRAC % mpbpl;
236 sincosx_mpn (si, co, x, ox);
238 memset (s2, 0, sizeof (mp1));
239 memset (c2, 0, sizeof (mp1));
240 for (i = 0; i < 100 && i < FRAC / 4; i++)
242 s2[(FRAC - i * 4 - 4) / mpbpl] |= ((strchr (hexdig, sin1[i]) - hexdig)
243 << (FRAC - i * 4 - 4) % mpbpl);
244 c2[(FRAC - i * 4 - 4) / mpbpl] |= ((strchr (hexdig, cos1[i]) - hexdig)
245 << (FRAC - i * 4 - 4) % mpbpl);
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);
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);
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');
274 return (sin_failures == 0 && cos_failures == 0) ? 0 : 1;