Back to index

glibc  2.9
atest-exp.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 
00027 #define PRINT_ERRORS 0
00028 
00029 #define TOL 80
00030 #define N2 17
00031 #define FRAC (32*4)
00032 
00033 #define mpbpl (CHAR_BIT * sizeof (mp_limb_t))
00034 #define SZ (FRAC / mpbpl + 1)
00035 typedef mp_limb_t mp1[SZ], mp2[SZ * 2];
00036 
00037 /* This string has 101 hex digits.  */
00038 static const char exp1[102] = "2" /* point */
00039 "b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da56a7"
00040 "84d9045190cfef324e7738926cfbe5f4bf8d8d8c31d763da07";
00041 static const char hexdig[] = "0123456789abcdef";
00042 
00043 static void
00044 print_mpn_hex (const mp_limb_t *x, unsigned size)
00045 {
00046    char value[size + 1];
00047    unsigned i;
00048    const unsigned final = (size * 4 > SZ * mpbpl) ? SZ * mpbpl / 4 : size;
00049 
00050    memset (value, '0', size);
00051 
00052    for (i = 0; i < final ; i++)
00053      value[size - 1 - i] = hexdig[x[i * 4 / mpbpl] >> (i * 4) % mpbpl & 0xf];
00054 
00055    value[size] = '\0';
00056    fputs (value, stdout);
00057 }
00058 
00059 static void
00060 exp_mpn (mp1 ex, mp1 x)
00061 {
00062    unsigned n;
00063    mp1 xp;
00064    mp2 tmp;
00065    mp_limb_t chk, round;
00066    mp1 tol;
00067 
00068    memset (xp, 0, sizeof (mp1));
00069    memset (ex, 0, sizeof (mp1));
00070    xp[FRAC / mpbpl] = (mp_limb_t)1 << FRAC % mpbpl;
00071    memset (tol,0, sizeof (mp1));
00072    tol[(FRAC - TOL) / mpbpl] = (mp_limb_t)1 << (FRAC - TOL) % mpbpl;
00073 
00074    n = 0;
00075 
00076    do
00077      {
00078        /* Calculate sum(x^n/n!) until the next term is sufficiently small.  */
00079 
00080        mpn_mul_n (tmp, xp, x, SZ);
00081        assert (tmp[SZ * 2 - 1] == 0);
00082        if (n > 0)
00083         round = mpn_divmod_1 (xp, tmp + FRAC / mpbpl, SZ, n);
00084        chk = mpn_add_n (ex, ex, xp, SZ);
00085        assert (chk == 0);
00086        n++;
00087        assert (n < 80); /* Catch too-high TOL.  */
00088      }
00089    while (n < 10 || mpn_cmp (xp, tol, SZ) >= 0);
00090 }
00091 
00092 static int
00093 mpn_bitsize(const mp_limb_t *SRC_PTR, mp_size_t SIZE)
00094 {
00095    int i, j;
00096    for (i = SIZE - 1; i > 0; i--)
00097      if (SRC_PTR[i] != 0)
00098        break;
00099    for (j = mpbpl - 1; j >= 0; j--)
00100      if ((SRC_PTR[i] & (mp_limb_t)1 << j) != 0)
00101        break;
00102 
00103    return i * mpbpl + j;
00104 }
00105 
00106 int
00107 main (void)
00108 {
00109    mp1 ex, x, xt, e2, e3;
00110    int i;
00111    int errors = 0;
00112    int failures = 0;
00113    mp1 maxerror;
00114    int maxerror_s = 0;
00115    const double sf = pow (2, mpbpl);
00116 
00117    /* assert (mpbpl == mp_bits_per_limb); */
00118    assert (FRAC / mpbpl * mpbpl == FRAC);
00119 
00120    memset (maxerror, 0, sizeof (mp1));
00121    memset (xt, 0, sizeof (mp1));
00122    xt[(FRAC - N2) / mpbpl] = (mp_limb_t)1 << (FRAC - N2) % mpbpl;
00123 
00124    for (i = 0; i < 1 << N2; i++)
00125    {
00126       int e2s, e3s, j;
00127       double de2;
00128 
00129       mpn_mul_1 (x,xt,SZ,i);
00130       exp_mpn (ex, x);
00131       de2 = exp (i / (double) (1 << N2));
00132       for (j = SZ-1; j >= 0; j--)
00133        {
00134          e2[j] = (mp_limb_t) de2;
00135          de2 = (de2 - e2[j]) * sf;
00136        }
00137       if (mpn_cmp (ex,e2,SZ) >= 0)
00138        mpn_sub_n (e3,ex,e2,SZ);
00139       else
00140        mpn_sub_n (e3,e2,ex,SZ);
00141 
00142       e2s = mpn_bitsize (e2,SZ);
00143       e3s = mpn_bitsize (e3,SZ);
00144       if (e3s >= 0 && e2s - e3s < 54)
00145        {
00146 #if PRINT_ERRORS
00147          printf ("%06x ", i * (0x100000 / (1 << N2)));
00148          print_mpn_hex (ex, (FRAC / 4) + 1);
00149          fputs ("\n       ",stdout);
00150          print_mpn_hex (e2, (FRAC / 4) + 1);
00151          printf ("\n %c     ",
00152                 e2s - e3s < 54 ? e2s - e3s == 53 ? 'e' : 'F' : 'P');
00153          print_mpn_hex (e3, (FRAC / 4) + 1);
00154          putchar ('\n');
00155 #endif
00156         errors += (e2s - e3s == 53);
00157         failures += (e2s - e3s < 53);
00158        }
00159       if (e3s >= maxerror_s
00160          && mpn_cmp (e3, maxerror, SZ) > 0)
00161        {
00162          memcpy (maxerror, e3, sizeof (mp1));
00163          maxerror_s = e3s;
00164        }
00165    }
00166 
00167    /* Check exp_mpn against precomputed value of exp(1).  */
00168    memset (x, '\0', sizeof (mp1));
00169    x[FRAC / mpbpl] = (mp_limb_t)1 << FRAC % mpbpl;
00170    exp_mpn (ex, x);
00171 
00172    memset (e2, '\0', sizeof (mp1));
00173    for (i = -1; i < 100 && i < FRAC / 4; i++)
00174      e2[(FRAC - i * 4 - 4) / mpbpl] |= ((mp_limb_t) (strchr (hexdig,
00175                                                       exp1[i + 1])
00176                                                - hexdig)
00177                                    << (FRAC - i * 4 - 4) % mpbpl);
00178 
00179    if (mpn_cmp (ex, e2, SZ) >= 0)
00180      mpn_sub_n (e3, ex, e2, SZ);
00181    else
00182      mpn_sub_n (e3, e2, ex, SZ);
00183 
00184    printf ("%d failures; %d errors; error rate %0.2f%%\n", failures, errors,
00185           errors * 100.0 / (double) (1 << N2));
00186    fputs ("maximum error:   ", stdout);
00187    print_mpn_hex (maxerror, (FRAC / 4) + 1);
00188    fputs ("\nerror in exp(1): ", stdout);
00189    print_mpn_hex (e3, (FRAC / 4) + 1);
00190    putchar ('\n');
00191 
00192    return failures == 0 ? 0 : 1;
00193 }