| xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Copyright (C) 1997-2016 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 |  | 
|  | 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. | 
|  | 9 |  | 
|  | 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. | 
|  | 14 |  | 
|  | 15 | You should have received a copy of the GNU Lesser General Public | 
|  | 16 | License along with the GNU C Library; if not, see | 
|  | 17 | <http://www.gnu.org/licenses/>.  */ | 
|  | 18 |  | 
|  | 19 | #include <stdio.h> | 
|  | 20 | #include <math.h> | 
|  | 21 | #include <gmp.h> | 
|  | 22 | #include <string.h> | 
|  | 23 | #include <limits.h> | 
|  | 24 | #include <assert.h> | 
|  | 25 | #include <stdlib.h> | 
|  | 26 |  | 
|  | 27 | #define PRINT_ERRORS 0 | 
|  | 28 |  | 
|  | 29 | #define TOL 80 | 
|  | 30 | #define N2 18 | 
|  | 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 | #if BITS_PER_MP_LIMB == 64 | 
|  | 38 | # define LIMB64(L, H) 0x ## H ## L | 
|  | 39 | #elif BITS_PER_MP_LIMB == 32 | 
|  | 40 | # define LIMB64(L, H) 0x ## L, 0x ## H | 
|  | 41 | #else | 
|  | 42 | # error | 
|  | 43 | #endif | 
|  | 44 |  | 
|  | 45 | /* Once upon a time these constants were generated to 400 bits. | 
|  | 46 | We only need FRAC bits (128) at present, but we retain 384 bits | 
|  | 47 | in the text Just In Case.  */ | 
|  | 48 | #define CONSTSZ(INT, F1, F2, F3, F4, F5, F6, F7, F8, F9, Fa, Fb, Fc) \ | 
|  | 49 | LIMB64(F4, F3), LIMB64(F2, F1), INT | 
|  | 50 |  | 
|  | 51 | static const mp1 mp_exp1 = { | 
|  | 52 | CONSTSZ (2, b7e15162, 8aed2a6a, bf715880, 9cf4f3c7, 62e7160f, 38b4da56, | 
|  | 53 | a784d904, 5190cfef, 324e7738, 926cfbe5, f4bf8d8d, 8c31d763) | 
|  | 54 | }; | 
|  | 55 |  | 
|  | 56 | static const mp1 mp_log2 = { | 
|  | 57 | CONSTSZ (0, b17217f7, d1cf79ab, c9e3b398, 03f2f6af, 40f34326, 7298b62d, | 
|  | 58 | 8a0d175b, 8baafa2b, e7b87620, 6debac98, 559552fb, 4afa1b10) | 
|  | 59 | }; | 
|  | 60 |  | 
|  | 61 | static void | 
|  | 62 | print_mpn_fp (const mp_limb_t *x, unsigned int dp, unsigned int base) | 
|  | 63 | { | 
|  | 64 | static const char hexdig[16] = "0123456789abcdef"; | 
|  | 65 | unsigned int i; | 
|  | 66 | mp1 tx; | 
|  | 67 |  | 
|  | 68 | memcpy (tx, x, sizeof (mp1)); | 
|  | 69 | if (base == 16) | 
|  | 70 | fputs ("0x", stdout); | 
|  | 71 | assert (x[SZ-1] < base); | 
|  | 72 | fputc (hexdig[x[SZ - 1]], stdout); | 
|  | 73 | fputc ('.', stdout); | 
|  | 74 | for (i = 0; i < dp; i++) | 
|  | 75 | { | 
|  | 76 | tx[SZ - 1] = 0; | 
|  | 77 | mpn_mul_1 (tx, tx, SZ, base); | 
|  | 78 | assert (tx[SZ - 1] < base); | 
|  | 79 | fputc (hexdig[tx[SZ - 1]], stdout); | 
|  | 80 | } | 
|  | 81 | } | 
|  | 82 |  | 
|  | 83 | /* Compute e^x.  */ | 
|  | 84 | static void | 
|  | 85 | exp_mpn (mp1 ex, mp1 x) | 
|  | 86 | { | 
|  | 87 | unsigned int n; | 
|  | 88 | mp1 xp; | 
|  | 89 | mp2 tmp; | 
|  | 90 | mp_limb_t chk; | 
|  | 91 | mp1 tol; | 
|  | 92 |  | 
|  | 93 | memset (xp, 0, sizeof (mp1)); | 
|  | 94 | memset (ex, 0, sizeof (mp1)); | 
|  | 95 | xp[FRAC / mpbpl] = (mp_limb_t)1 << FRAC % mpbpl; | 
|  | 96 | memset (tol, 0, sizeof (mp1)); | 
|  | 97 | tol[(FRAC - TOL) / mpbpl] = (mp_limb_t)1 << (FRAC - TOL) % mpbpl; | 
|  | 98 |  | 
|  | 99 | n = 0; | 
|  | 100 |  | 
|  | 101 | do | 
|  | 102 | { | 
|  | 103 | /* Calculate sum(x^n/n!) until the next term is sufficiently small.  */ | 
|  | 104 |  | 
|  | 105 | mpn_mul_n (tmp, xp, x, SZ); | 
|  | 106 | assert(tmp[SZ * 2 - 1] == 0); | 
|  | 107 | if (n > 0) | 
|  | 108 | mpn_divmod_1 (xp, tmp + FRAC / mpbpl, SZ, n); | 
|  | 109 | chk = mpn_add_n (ex, ex, xp, SZ); | 
|  | 110 | assert (chk == 0); | 
|  | 111 | ++n; | 
|  | 112 | assert (n < 80); /* Catch too-high TOL.  */ | 
|  | 113 | } | 
|  | 114 | while (n < 10 || mpn_cmp (xp, tol, SZ) >= 0); | 
|  | 115 | } | 
|  | 116 |  | 
|  | 117 | /* Calculate 2^x.  */ | 
|  | 118 | static void | 
|  | 119 | exp2_mpn (mp1 ex, mp1 x) | 
|  | 120 | { | 
|  | 121 | mp2 tmp; | 
|  | 122 | mpn_mul_n (tmp, x, mp_log2, SZ); | 
|  | 123 | assert(tmp[SZ * 2 - 1] == 0); | 
|  | 124 | exp_mpn (ex, tmp + FRAC / mpbpl); | 
|  | 125 | } | 
|  | 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 | static int | 
|  | 143 | do_test (void) | 
|  | 144 | { | 
|  | 145 | mp1 ex, x, xt, e2, e3; | 
|  | 146 | int i; | 
|  | 147 | int errors = 0; | 
|  | 148 | int failures = 0; | 
|  | 149 | mp1 maxerror; | 
|  | 150 | int maxerror_s = 0; | 
|  | 151 | const double sf = pow (2, mpbpl); | 
|  | 152 |  | 
|  | 153 | /* assert(mpbpl == mp_bits_per_limb); */ | 
|  | 154 | assert(FRAC / mpbpl * mpbpl == FRAC); | 
|  | 155 |  | 
|  | 156 | memset (maxerror, 0, sizeof (mp1)); | 
|  | 157 | memset (xt, 0, sizeof (mp1)); | 
|  | 158 | xt[(FRAC - N2) / mpbpl] = (mp_limb_t)1 << (FRAC - N2) % mpbpl; | 
|  | 159 |  | 
|  | 160 | for (i = 0; i < (1 << N2); ++i) | 
|  | 161 | { | 
|  | 162 | int e2s, e3s, j; | 
|  | 163 | double de2; | 
|  | 164 |  | 
|  | 165 | mpn_mul_1 (x, xt, SZ, i); | 
|  | 166 | exp2_mpn (ex, x); | 
|  | 167 | de2 = exp2 (i / (double) (1 << N2)); | 
|  | 168 | for (j = SZ - 1; j >= 0; --j) | 
|  | 169 | { | 
|  | 170 | e2[j] = (mp_limb_t) de2; | 
|  | 171 | de2 = (de2 - e2[j]) * sf; | 
|  | 172 | } | 
|  | 173 | if (mpn_cmp (ex, e2, SZ) >= 0) | 
|  | 174 | mpn_sub_n (e3, ex, e2, SZ); | 
|  | 175 | else | 
|  | 176 | mpn_sub_n (e3, e2, ex, SZ); | 
|  | 177 |  | 
|  | 178 | e2s = mpn_bitsize (e2, SZ); | 
|  | 179 | e3s = mpn_bitsize (e3, SZ); | 
|  | 180 | if (e3s >= 0 && e2s - e3s < 54) | 
|  | 181 | { | 
|  | 182 | #if PRINT_ERRORS | 
|  | 183 | printf ("%06x ", i * (0x100000 / (1 << N2))); | 
|  | 184 | print_mpn_fp (ex, (FRAC / 4) + 1, 16); | 
|  | 185 | putchar ('\n'); | 
|  | 186 | fputs ("       ",stdout); | 
|  | 187 | print_mpn_fp (e2, (FRAC / 4) + 1, 16); | 
|  | 188 | putchar ('\n'); | 
|  | 189 | printf (" %c     ", | 
|  | 190 | e2s - e3s < 54 ? e2s - e3s == 53 ? 'e' : 'F' : 'P'); | 
|  | 191 | print_mpn_fp (e3, (FRAC / 4) + 1, 16); | 
|  | 192 | putchar ('\n'); | 
|  | 193 | #endif | 
|  | 194 | errors += (e2s - e3s == 53); | 
|  | 195 | failures += (e2s - e3s < 53); | 
|  | 196 | } | 
|  | 197 | if (e3s >= maxerror_s | 
|  | 198 | && mpn_cmp (e3, maxerror, SZ) > 0) | 
|  | 199 | { | 
|  | 200 | memcpy (maxerror, e3, sizeof (mp1)); | 
|  | 201 | maxerror_s = e3s; | 
|  | 202 | } | 
|  | 203 | } | 
|  | 204 |  | 
|  | 205 | /* Check exp_mpn against precomputed value of exp(1).  */ | 
|  | 206 | memset (x, 0, sizeof (mp1)); | 
|  | 207 | x[FRAC / mpbpl] = (mp_limb_t)1 << FRAC % mpbpl; | 
|  | 208 | exp_mpn (ex, x); | 
|  | 209 | if (mpn_cmp (ex, mp_exp1, SZ) >= 0) | 
|  | 210 | mpn_sub_n (e3, ex, mp_exp1, SZ); | 
|  | 211 | else | 
|  | 212 | mpn_sub_n (e3, mp_exp1, ex, SZ); | 
|  | 213 |  | 
|  | 214 | printf ("%d failures; %d errors; error rate %0.2f%%\n", failures, errors, | 
|  | 215 | errors * 100.0 / (double) (1 << N2)); | 
|  | 216 | fputs ("maximum error:   ", stdout); | 
|  | 217 | print_mpn_fp (maxerror, (FRAC / 4) + 1, 16); | 
|  | 218 | putchar ('\n'); | 
|  | 219 | fputs ("error in exp(1): ", stdout); | 
|  | 220 | print_mpn_fp (e3, (FRAC / 4) + 1, 16); | 
|  | 221 | putchar ('\n'); | 
|  | 222 |  | 
|  | 223 | return failures == 0 ? 0 : 1; | 
|  | 224 | } | 
|  | 225 |  | 
|  | 226 | #define TIMEOUT 300 | 
|  | 227 | #define TEST_FUNCTION do_test () | 
|  | 228 | #include "../test-skeleton.c" |