Back to index

glibc  2.9
atest-exp2.c
Go to the documentation of this file.
00001 /* Copyright (C) 1997, 1998, 2000, 2006 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Geoffrey Keating <Geoff.Keating@anu.edu.au>, 1997.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <stdio.h>
00021 #include <math.h>
00022 #include <gmp.h>
00023 #include <string.h>
00024 #include <limits.h>
00025 #include <assert.h>
00026 #include <stdlib.h>
00027 
00028 #define PRINT_ERRORS 0
00029 
00030 #define TOL 80
00031 #define N2 18
00032 #define FRAC (32*4)
00033 
00034 #define mpbpl (CHAR_BIT * sizeof (mp_limb_t))
00035 #define SZ (FRAC / mpbpl + 1)
00036 typedef mp_limb_t mp1[SZ], mp2[SZ * 2];
00037 
00038 /* This string has 101 hex digits.  */
00039 static const char exp1[102] = "2" /* point */
00040 "b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da56a7"
00041 "84d9045190cfef324e7738926cfbe5f4bf8d8d8c31d763da07";
00042 static const char exp_m1[102] = "0" /* point */
00043 "5e2d58d8b3bcdf1abadec7829054f90dda9805aab56c773330"
00044 "24b9d0a507daedb16400bf472b4215b8245b669d90d27a5aea";
00045 
00046 static const char hexdig[] = "0123456789abcdef";
00047 
00048 static void
00049 print_mpn_fp (const mp_limb_t *x, unsigned int dp, unsigned int base)
00050 {
00051    unsigned int i;
00052    mp1 tx;
00053 
00054    memcpy (tx, x, sizeof (mp1));
00055    if (base == 16)
00056      fputs ("0x", stdout);
00057    assert (x[SZ-1] < base);
00058    fputc (hexdig[x[SZ - 1]], stdout);
00059    fputc ('.', stdout);
00060    for (i = 0; i < dp; i++)
00061      {
00062        tx[SZ - 1] = 0;
00063        mpn_mul_1 (tx, tx, SZ, base);
00064        assert (tx[SZ - 1] < base);
00065        fputc (hexdig[tx[SZ - 1]], stdout);
00066      }
00067 }
00068 
00069 static void
00070 read_mpn_hex(mp_limb_t *x, const char *str)
00071 {
00072   int i;
00073 
00074   memset (x, 0, sizeof (mp1));
00075   for (i = -1; i < 100 && i < FRAC / 4; ++i)
00076     x[(FRAC - i * 4 - 4) / mpbpl] |= ((mp_limb_t) (strchr (hexdig, str[i + 1])
00077                                              - hexdig)
00078                                   << (FRAC - i * 4 - 4) % mpbpl);
00079 }
00080 
00081 static mp_limb_t *get_log2(void) __attribute__((const));
00082 static mp_limb_t *
00083 get_log2(void)
00084 {
00085   static mp1 log2_m;
00086   static int log2_m_inited = 0;
00087   static const char log2[102] = "0" /* point */
00088     "b17217f7d1cf79abc9e3b39803f2f6af40f343267298b62d8a"
00089     "0d175b8baafa2be7b876206debac98559552fb4afa1b10ed2e";
00090 
00091   if (!log2_m_inited)
00092     {
00093       read_mpn_hex (log2_m, log2);
00094       log2_m_inited = 1;
00095     }
00096   return log2_m;
00097 }
00098 
00099 /* Compute e^x.  */
00100 static void
00101 exp_mpn (mp1 ex, mp1 x)
00102 {
00103    unsigned int n;
00104    mp1 xp;
00105    mp2 tmp;
00106    mp_limb_t chk, round;
00107    mp1 tol;
00108 
00109    memset (xp, 0, sizeof (mp1));
00110    memset (ex, 0, sizeof (mp1));
00111    xp[FRAC / mpbpl] = (mp_limb_t)1 << FRAC % mpbpl;
00112    memset (tol, 0, sizeof (mp1));
00113    tol[(FRAC - TOL) / mpbpl] = (mp_limb_t)1 << (FRAC - TOL) % mpbpl;
00114 
00115    n = 0;
00116 
00117    do
00118      {
00119        /* Calculate sum(x^n/n!) until the next term is sufficiently small.  */
00120 
00121        mpn_mul_n (tmp, xp, x, SZ);
00122        assert(tmp[SZ * 2 - 1] == 0);
00123        if (n > 0)
00124         round = mpn_divmod_1 (xp, tmp + FRAC / mpbpl, SZ, n);
00125        chk = mpn_add_n (ex, ex, xp, SZ);
00126        assert (chk == 0);
00127        ++n;
00128        assert (n < 80); /* Catch too-high TOL.  */
00129      }
00130    while (n < 10 || mpn_cmp (xp, tol, SZ) >= 0);
00131 }
00132 
00133 /* Calculate 2^x.  */
00134 static void
00135 exp2_mpn (mp1 ex, mp1 x)
00136 {
00137   mp2 tmp;
00138   mpn_mul_n (tmp, x, get_log2 (), SZ);
00139   assert(tmp[SZ * 2 - 1] == 0);
00140   exp_mpn (ex, tmp + FRAC / mpbpl);
00141 }
00142 
00143 
00144 static int
00145 mpn_bitsize(const mp_limb_t *SRC_PTR, mp_size_t SIZE)
00146 {
00147   int i, j;
00148   for (i = SIZE - 1; i > 0; --i)
00149     if (SRC_PTR[i] != 0)
00150       break;
00151   for (j = mpbpl - 1; j >= 0; --j)
00152     if ((SRC_PTR[i] & (mp_limb_t)1 << j) != 0)
00153       break;
00154 
00155   return i * mpbpl + j;
00156 }
00157 
00158 int
00159 main (void)
00160 {
00161   mp1 ex, x, xt, e2, e3;
00162   int i;
00163   int errors = 0;
00164   int failures = 0;
00165   mp1 maxerror;
00166   int maxerror_s = 0;
00167   const double sf = pow (2, mpbpl);
00168 
00169   /* assert(mpbpl == mp_bits_per_limb); */
00170   assert(FRAC / mpbpl * mpbpl == FRAC);
00171 
00172   memset (maxerror, 0, sizeof (mp1));
00173   memset (xt, 0, sizeof (mp1));
00174   xt[(FRAC - N2) / mpbpl] = (mp_limb_t)1 << (FRAC - N2) % mpbpl;
00175 
00176   for (i = 0; i < (1 << N2); ++i)
00177     {
00178       int e2s, e3s, j;
00179       double de2;
00180 
00181       mpn_mul_1 (x, xt, SZ, i);
00182       exp2_mpn (ex, x);
00183       de2 = exp2 (i / (double) (1 << N2));
00184       for (j = SZ - 1; j >= 0; --j)
00185        {
00186          e2[j] = (mp_limb_t) de2;
00187          de2 = (de2 - e2[j]) * sf;
00188        }
00189       if (mpn_cmp (ex, e2, SZ) >= 0)
00190        mpn_sub_n (e3, ex, e2, SZ);
00191       else
00192        mpn_sub_n (e3, e2, ex, SZ);
00193 
00194       e2s = mpn_bitsize (e2, SZ);
00195       e3s = mpn_bitsize (e3, SZ);
00196       if (e3s >= 0 && e2s - e3s < 54)
00197        {
00198 #if PRINT_ERRORS
00199          printf ("%06x ", i * (0x100000 / (1 << N2)));
00200          print_mpn_fp (ex, (FRAC / 4) + 1, 16);
00201          putchar ('\n');
00202          fputs ("       ",stdout);
00203          print_mpn_fp (e2, (FRAC / 4) + 1, 16);
00204          putchar ('\n');
00205          printf (" %c     ",
00206                 e2s - e3s < 54 ? e2s - e3s == 53 ? 'e' : 'F' : 'P');
00207          print_mpn_fp (e3, (FRAC / 4) + 1, 16);
00208          putchar ('\n');
00209 #endif
00210          errors += (e2s - e3s == 53);
00211          failures += (e2s - e3s < 53);
00212        }
00213       if (e3s >= maxerror_s
00214          && mpn_cmp (e3, maxerror, SZ) > 0)
00215        {
00216          memcpy (maxerror, e3, sizeof (mp1));
00217          maxerror_s = e3s;
00218        }
00219     }
00220 
00221   /* Check exp_mpn against precomputed value of exp(1).  */
00222   memset (x, 0, sizeof (mp1));
00223   x[FRAC / mpbpl] = (mp_limb_t)1 << FRAC % mpbpl;
00224   exp_mpn (ex, x);
00225   read_mpn_hex (e2, exp1);
00226   if (mpn_cmp (ex, e2, SZ) >= 0)
00227     mpn_sub_n (e3, ex, e2, SZ);
00228   else
00229     mpn_sub_n (e3, e2, ex, SZ);
00230 
00231   printf ("%d failures; %d errors; error rate %0.2f%%\n", failures, errors,
00232          errors * 100.0 / (double) (1 << N2));
00233   fputs ("maximum error:   ", stdout);
00234   print_mpn_fp (maxerror, (FRAC / 4) + 1, 16);
00235   putchar ('\n');
00236   fputs ("error in exp(1): ", stdout);
00237   print_mpn_fp (e3, (FRAC / 4) + 1, 16);
00238   putchar ('\n');
00239 
00240   return failures == 0 ? 0 : 1;
00241 }