1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20: #include <stdio.h>
21: #include <math.h>
22: #include <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:
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: static 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: static 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: \
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: \
93: mpn_mul_n(d,tmp,x,SZ); \
94: } else \
95: mpn_mul_n(d,s1,x,SZ); \
96: \
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: \
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: \
109: chk |= (n ? mpn_sub_n : mpn_add_n)(d,ss,tmp,SZ); \
110: \
111: } while (0)
112:
113: add_shift_mulh (s[0], x, co, NULL, 0, 0);
114: add_shift_mulh (c[0], x, si, NULL, 0, 0);
115: add_shift_mulh (s[1], x, co, c[0], 1, 1);
116: add_shift_mulh (c[1], x, si, s[0], 1, 0);
117: add_shift_mulh (s[2], x, co, c[1], 1, 1);
118: add_shift_mulh (c[2], x, si, s[1], 1, 0);
119: add_shift_mulh (s[3], x, co, c[2], 0, 1);
120: add_shift_mulh (c[3], x, si, s[2], 0, 0);
121: summ (si, si, s, 0);
122: summ (co, co, c, 1);
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;
135: for (j = mpbpl - 1; j >= 0; j--)
136: if ((SRC_PTR[i] & (mp_limb_t)1 << j) != 0)
137: break;
138:
139: return i * mpbpl + j;
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:
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));
159: xt[(FRAC - N2) / mpbpl] = (mp_limb_t)1 << (FRAC - N2) % mpbpl;
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);
191: if ((s3s >= 0 && s2s - s3s < 54)
192: || (c3s >= 0 && c2s - c3s < 54)
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 ",
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;
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:
234: memset (x, 0, sizeof (mp1));
235: x[FRAC / mpbpl] = (mp_limb_t)1 << FRAC % mpbpl;
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: {
242: s2[(FRAC - i * 4 - 4) / mpbpl] |= ((mp_limb_t) (strchr (hexdig, sin1[i])
243: - hexdig)
244: << (FRAC - i * 4 - 4) % mpbpl);
245: c2[(FRAC - i * 4 - 4) / mpbpl] |= ((mp_limb_t) (strchr (hexdig, cos1[i])
246: - hexdig)
247: << (FRAC - i * 4 - 4) % mpbpl);
248: }
249:
250: if (mpn_cmp (si, s2, SZ) >= 0)
251: mpn_sub_n (s3, si, s2, SZ);
252: else
253: mpn_sub_n (s3, s2, si, SZ);
254: if (mpn_cmp (co, c2, SZ) >= 0)
255: mpn_sub_n (c3, co, c2, SZ);
256: else
257: mpn_sub_n (c3, c2, co, SZ);
258:
259: printf ("sin:\n");
260: printf ("%d failures; %d errors; error rate %0.2f%%\n",
261: sin_failures, sin_errors, sin_errors * 100.0 / (double) (1 << N2));
262: fputs ("maximum error: ", stdout);
263: print_mpn_hex (sin_maxerror, (FRAC / 4) + 1);
264: fputs ("\nerror in sin(1): ", stdout);
265: print_mpn_hex (s3, (FRAC / 4) + 1);
266:
267: fputs ("\n\ncos:\n", stdout);
268: printf ("%d failures; %d errors; error rate %0.2f%%\n",
269: cos_failures, cos_errors, cos_errors * 100.0 / (double) (1 << N2));
270: fputs ("maximum error: ", stdout);
271: print_mpn_hex (cos_maxerror, (FRAC / 4) + 1);
272: fputs ("\nerror in cos(1): ", stdout);
273: print_mpn_hex (c3, (FRAC / 4) + 1);
274: putchar ('\n');
275:
276: return (sin_failures == 0 && cos_failures == 0) ? 0 : 1;
277: }