/glibc-2.18/math/atest-exp.c

https://github.com/attliaLin/Learn-SourceCode · C · 192 lines · 144 code · 27 blank · 21 comment · 26 complexity · 0db9c1d2e5e4a5139158417438604545 MD5 · raw file

  1. /* Copyright (C) 1997-2013 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 TOL 80
  23. #define N2 17
  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. /* This string has 101 hex digits. */
  29. static const char exp1[102] = "2" /* point */
  30. "b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da56a7"
  31. "84d9045190cfef324e7738926cfbe5f4bf8d8d8c31d763da07";
  32. static const char hexdig[] = "0123456789abcdef";
  33. static void
  34. print_mpn_hex (const mp_limb_t *x, unsigned size)
  35. {
  36. char value[size + 1];
  37. unsigned i;
  38. const unsigned final = (size * 4 > SZ * mpbpl) ? SZ * mpbpl / 4 : size;
  39. memset (value, '0', size);
  40. for (i = 0; i < final ; i++)
  41. value[size - 1 - i] = hexdig[x[i * 4 / mpbpl] >> (i * 4) % mpbpl & 0xf];
  42. value[size] = '\0';
  43. fputs (value, stdout);
  44. }
  45. static void
  46. exp_mpn (mp1 ex, mp1 x)
  47. {
  48. unsigned n;
  49. mp1 xp;
  50. mp2 tmp;
  51. mp_limb_t chk;
  52. mp1 tol;
  53. memset (xp, 0, sizeof (mp1));
  54. memset (ex, 0, sizeof (mp1));
  55. xp[FRAC / mpbpl] = (mp_limb_t)1 << FRAC % mpbpl;
  56. memset (tol,0, sizeof (mp1));
  57. tol[(FRAC - TOL) / mpbpl] = (mp_limb_t)1 << (FRAC - TOL) % mpbpl;
  58. n = 0;
  59. do
  60. {
  61. /* Calculate sum(x^n/n!) until the next term is sufficiently small. */
  62. mpn_mul_n (tmp, xp, x, SZ);
  63. assert (tmp[SZ * 2 - 1] == 0);
  64. if (n > 0)
  65. mpn_divmod_1 (xp, tmp + FRAC / mpbpl, SZ, n);
  66. chk = mpn_add_n (ex, ex, xp, SZ);
  67. assert (chk == 0);
  68. n++;
  69. assert (n < 80); /* Catch too-high TOL. */
  70. }
  71. while (n < 10 || mpn_cmp (xp, tol, SZ) >= 0);
  72. }
  73. static int
  74. mpn_bitsize(const mp_limb_t *SRC_PTR, mp_size_t SIZE)
  75. {
  76. int i, j;
  77. for (i = SIZE - 1; i > 0; i--)
  78. if (SRC_PTR[i] != 0)
  79. break;
  80. for (j = mpbpl - 1; j >= 0; j--)
  81. if ((SRC_PTR[i] & (mp_limb_t)1 << j) != 0)
  82. break;
  83. return i * mpbpl + j;
  84. }
  85. int
  86. main (void)
  87. {
  88. mp1 ex, x, xt, e2, e3;
  89. int i;
  90. int errors = 0;
  91. int failures = 0;
  92. mp1 maxerror;
  93. int maxerror_s = 0;
  94. const double sf = pow (2, mpbpl);
  95. /* assert (mpbpl == mp_bits_per_limb); */
  96. assert (FRAC / mpbpl * mpbpl == FRAC);
  97. memset (maxerror, 0, sizeof (mp1));
  98. memset (xt, 0, sizeof (mp1));
  99. xt[(FRAC - N2) / mpbpl] = (mp_limb_t)1 << (FRAC - N2) % mpbpl;
  100. for (i = 0; i < 1 << N2; i++)
  101. {
  102. int e2s, e3s, j;
  103. double de2;
  104. mpn_mul_1 (x,xt,SZ,i);
  105. exp_mpn (ex, x);
  106. de2 = exp (i / (double) (1 << N2));
  107. for (j = SZ-1; j >= 0; j--)
  108. {
  109. e2[j] = (mp_limb_t) de2;
  110. de2 = (de2 - e2[j]) * sf;
  111. }
  112. if (mpn_cmp (ex,e2,SZ) >= 0)
  113. mpn_sub_n (e3,ex,e2,SZ);
  114. else
  115. mpn_sub_n (e3,e2,ex,SZ);
  116. e2s = mpn_bitsize (e2,SZ);
  117. e3s = mpn_bitsize (e3,SZ);
  118. if (e3s >= 0 && e2s - e3s < 54)
  119. {
  120. #if PRINT_ERRORS
  121. printf ("%06x ", i * (0x100000 / (1 << N2)));
  122. print_mpn_hex (ex, (FRAC / 4) + 1);
  123. fputs ("\n ",stdout);
  124. print_mpn_hex (e2, (FRAC / 4) + 1);
  125. printf ("\n %c ",
  126. e2s - e3s < 54 ? e2s - e3s == 53 ? 'e' : 'F' : 'P');
  127. print_mpn_hex (e3, (FRAC / 4) + 1);
  128. putchar ('\n');
  129. #endif
  130. errors += (e2s - e3s == 53);
  131. failures += (e2s - e3s < 53);
  132. }
  133. if (e3s >= maxerror_s
  134. && mpn_cmp (e3, maxerror, SZ) > 0)
  135. {
  136. memcpy (maxerror, e3, sizeof (mp1));
  137. maxerror_s = e3s;
  138. }
  139. }
  140. /* Check exp_mpn against precomputed value of exp(1). */
  141. memset (x, '\0', sizeof (mp1));
  142. x[FRAC / mpbpl] = (mp_limb_t)1 << FRAC % mpbpl;
  143. exp_mpn (ex, x);
  144. memset (e2, '\0', sizeof (mp1));
  145. for (i = -1; i < 100 && i < FRAC / 4; i++)
  146. e2[(FRAC - i * 4 - 4) / mpbpl] |= ((mp_limb_t) (strchr (hexdig,
  147. exp1[i + 1])
  148. - hexdig)
  149. << (FRAC - i * 4 - 4) % mpbpl);
  150. if (mpn_cmp (ex, e2, SZ) >= 0)
  151. mpn_sub_n (e3, ex, e2, SZ);
  152. else
  153. mpn_sub_n (e3, e2, ex, SZ);
  154. printf ("%d failures; %d errors; error rate %0.2f%%\n", failures, errors,
  155. errors * 100.0 / (double) (1 << N2));
  156. fputs ("maximum error: ", stdout);
  157. print_mpn_hex (maxerror, (FRAC / 4) + 1);
  158. fputs ("\nerror in exp(1): ", stdout);
  159. print_mpn_hex (e3, (FRAC / 4) + 1);
  160. putchar ('\n');
  161. return failures == 0 ? 0 : 1;
  162. }