atest-exp2.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. #include <stdlib.h>
  22. #define PRINT_ERRORS 0
  23. #define TOL 80
  24. #define N2 18
  25. #define FRAC (32*4)
  26. #define mpbpl (CHAR_BIT * sizeof (mp_limb_t))
  27. #define SZ (FRAC / mpbpl + 1)
  28. typedef mp_limb_t mp1[SZ], mp2[SZ * 2];
  29. #if BITS_PER_MP_LIMB == 64
  30. # define LIMB64(L, H) 0x ## H ## L
  31. #elif BITS_PER_MP_LIMB == 32
  32. # define LIMB64(L, H) 0x ## L, 0x ## H
  33. #else
  34. # error
  35. #endif
  36. /* Once upon a time these constants were generated to 400 bits.
  37. We only need FRAC bits (128) at present, but we retain 384 bits
  38. in the text Just In Case. */
  39. #define CONSTSZ(INT, F1, F2, F3, F4, F5, F6, F7, F8, F9, Fa, Fb, Fc) \
  40. LIMB64(F4, F3), LIMB64(F2, F1), INT
  41. static const mp1 mp_exp1 = {
  42. CONSTSZ (2, b7e15162, 8aed2a6a, bf715880, 9cf4f3c7, 62e7160f, 38b4da56,
  43. a784d904, 5190cfef, 324e7738, 926cfbe5, f4bf8d8d, 8c31d763)
  44. };
  45. static const mp1 mp_log2 = {
  46. CONSTSZ (0, b17217f7, d1cf79ab, c9e3b398, 03f2f6af, 40f34326, 7298b62d,
  47. 8a0d175b, 8baafa2b, e7b87620, 6debac98, 559552fb, 4afa1b10)
  48. };
  49. static void
  50. print_mpn_fp (const mp_limb_t *x, unsigned int dp, unsigned int base)
  51. {
  52. static const char hexdig[16] = "0123456789abcdef";
  53. unsigned int i;
  54. mp1 tx;
  55. memcpy (tx, x, sizeof (mp1));
  56. if (base == 16)
  57. fputs ("0x", stdout);
  58. assert (x[SZ-1] < base);
  59. fputc (hexdig[x[SZ - 1]], stdout);
  60. fputc ('.', stdout);
  61. for (i = 0; i < dp; i++)
  62. {
  63. tx[SZ - 1] = 0;
  64. mpn_mul_1 (tx, tx, SZ, base);
  65. assert (tx[SZ - 1] < base);
  66. fputc (hexdig[tx[SZ - 1]], stdout);
  67. }
  68. }
  69. /* Compute e^x. */
  70. static void
  71. exp_mpn (mp1 ex, mp1 x)
  72. {
  73. unsigned int n;
  74. mp1 xp;
  75. mp2 tmp;
  76. mp_limb_t chk __attribute__ ((unused));
  77. mp1 tol;
  78. memset (xp, 0, sizeof (mp1));
  79. memset (ex, 0, sizeof (mp1));
  80. xp[FRAC / mpbpl] = (mp_limb_t)1 << FRAC % mpbpl;
  81. memset (tol, 0, sizeof (mp1));
  82. tol[(FRAC - TOL) / mpbpl] = (mp_limb_t)1 << (FRAC - TOL) % mpbpl;
  83. n = 0;
  84. do
  85. {
  86. /* Calculate sum(x^n/n!) until the next term is sufficiently small. */
  87. mpn_mul_n (tmp, xp, x, SZ);
  88. assert(tmp[SZ * 2 - 1] == 0);
  89. if (n > 0)
  90. mpn_divmod_1 (xp, tmp + FRAC / mpbpl, SZ, n);
  91. chk = mpn_add_n (ex, ex, xp, SZ);
  92. assert (chk == 0);
  93. ++n;
  94. assert (n < 80); /* Catch too-high TOL. */
  95. }
  96. while (n < 10 || mpn_cmp (xp, tol, SZ) >= 0);
  97. }
  98. /* Calculate 2^x. */
  99. static void
  100. exp2_mpn (mp1 ex, mp1 x)
  101. {
  102. mp2 tmp;
  103. mpn_mul_n (tmp, x, mp_log2, SZ);
  104. assert(tmp[SZ * 2 - 1] == 0);
  105. exp_mpn (ex, tmp + FRAC / mpbpl);
  106. }
  107. static int
  108. mpn_bitsize(const mp_limb_t *SRC_PTR, mp_size_t SIZE)
  109. {
  110. int i, j;
  111. for (i = SIZE - 1; i > 0; --i)
  112. if (SRC_PTR[i] != 0)
  113. break;
  114. for (j = mpbpl - 1; j >= 0; --j)
  115. if ((SRC_PTR[i] & (mp_limb_t)1 << j) != 0)
  116. break;
  117. return i * mpbpl + j;
  118. }
  119. static int
  120. do_test (void)
  121. {
  122. mp1 ex, x, xt, e2, e3;
  123. int i;
  124. int errors = 0;
  125. int failures = 0;
  126. mp1 maxerror;
  127. int maxerror_s = 0;
  128. const double sf = pow (2, mpbpl);
  129. /* assert(mpbpl == mp_bits_per_limb); */
  130. assert(FRAC / mpbpl * mpbpl == FRAC);
  131. memset (maxerror, 0, sizeof (mp1));
  132. memset (xt, 0, sizeof (mp1));
  133. xt[(FRAC - N2) / mpbpl] = (mp_limb_t)1 << (FRAC - N2) % mpbpl;
  134. for (i = 0; i < (1 << N2); ++i)
  135. {
  136. int e2s, e3s, j;
  137. double de2;
  138. mpn_mul_1 (x, xt, SZ, i);
  139. exp2_mpn (ex, x);
  140. de2 = exp2 (i / (double) (1 << N2));
  141. for (j = SZ - 1; j >= 0; --j)
  142. {
  143. e2[j] = (mp_limb_t) de2;
  144. de2 = (de2 - e2[j]) * sf;
  145. }
  146. if (mpn_cmp (ex, e2, SZ) >= 0)
  147. mpn_sub_n (e3, ex, e2, SZ);
  148. else
  149. mpn_sub_n (e3, e2, ex, SZ);
  150. e2s = mpn_bitsize (e2, SZ);
  151. e3s = mpn_bitsize (e3, SZ);
  152. if (e3s >= 0 && e2s - e3s < 54)
  153. {
  154. #if PRINT_ERRORS
  155. printf ("%06x ", i * (0x100000 / (1 << N2)));
  156. print_mpn_fp (ex, (FRAC / 4) + 1, 16);
  157. putchar ('\n');
  158. fputs (" ",stdout);
  159. print_mpn_fp (e2, (FRAC / 4) + 1, 16);
  160. putchar ('\n');
  161. printf (" %c ",
  162. e2s - e3s < 54 ? e2s - e3s == 53 ? 'e' : 'F' : 'P');
  163. print_mpn_fp (e3, (FRAC / 4) + 1, 16);
  164. putchar ('\n');
  165. #endif
  166. errors += (e2s - e3s == 53);
  167. failures += (e2s - e3s < 53);
  168. }
  169. if (e3s >= maxerror_s
  170. && mpn_cmp (e3, maxerror, SZ) > 0)
  171. {
  172. memcpy (maxerror, e3, sizeof (mp1));
  173. maxerror_s = e3s;
  174. }
  175. }
  176. /* Check exp_mpn against precomputed value of exp(1). */
  177. memset (x, 0, sizeof (mp1));
  178. x[FRAC / mpbpl] = (mp_limb_t)1 << FRAC % mpbpl;
  179. exp_mpn (ex, x);
  180. if (mpn_cmp (ex, mp_exp1, SZ) >= 0)
  181. mpn_sub_n (e3, ex, mp_exp1, SZ);
  182. else
  183. mpn_sub_n (e3, mp_exp1, ex, SZ);
  184. printf ("%d failures; %d errors; error rate %0.2f%%\n", failures, errors,
  185. errors * 100.0 / (double) (1 << N2));
  186. fputs ("maximum error: ", stdout);
  187. print_mpn_fp (maxerror, (FRAC / 4) + 1, 16);
  188. putchar ('\n');
  189. fputs ("error in exp(1): ", stdout);
  190. print_mpn_fp (e3, (FRAC / 4) + 1, 16);
  191. putchar ('\n');
  192. return failures == 0 ? 0 : 1;
  193. }
  194. #define TIMEOUT 300
  195. #define TEST_FUNCTION do_test ()
  196. #include "../test-skeleton.c"