atest-sincos.c 8.5 KB

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