Back to index

glibc  2.9
atest-sincos.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 N 0
00030 #define N2 20
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 /* These strings have exactly 100 hex digits in them.  */
00038 static const char sin1[101] =
00039 "d76aa47848677020c6e9e909c50f3c3289e511132f518b4def"
00040 "b6ca5fd6c649bdfb0bd9ff1edcd4577655b5826a3d3b50c264";
00041 static const char cos1[101] =
00042 "8a51407da8345c91c2466d976871bd29a2373a894f96c3b7f2"
00043 "300240b760e6fa96a94430a52d0e9e43f3450e3b8ff99bc934";
00044 static const char hexdig[] = "0123456789abcdef";
00045 
00046 static void
00047 print_mpn_hex (const mp_limb_t *x, unsigned size)
00048 {
00049    char value[size + 1];
00050    unsigned i;
00051    const unsigned final = (size * 4 > SZ * mpbpl) ? SZ * mpbpl / 4 : size;
00052 
00053    memset (value, '0', size);
00054 
00055    for (i = 0; i < final ; i++)
00056      value[size-1-i] = hexdig[x[i * 4 / mpbpl] >> (i * 4) % mpbpl & 0xf];
00057 
00058    value[size] = '\0';
00059    fputs (value, stdout);
00060 }
00061 
00062 static void
00063 sincosx_mpn (mp1 si, mp1 co, mp1 xx, mp1 ix)
00064 {
00065    int i;
00066    mp2 s[4], c[4];
00067    mp1 tmp, x;
00068    mp_limb_t chk, round;
00069 
00070    if (ix == NULL)
00071      {
00072        memset (si, 0, sizeof (mp1));
00073        memset (co, 0, sizeof (mp1));
00074        co[SZ-1] = 1;
00075        memcpy (x, xx, sizeof (mp1));
00076      }
00077    else
00078       mpn_sub_n (x, xx, ix, SZ);
00079 
00080    for (i = 0; i < 1 << N; i++)
00081      {
00082 #define add_shift_mulh(d,x,s1,s2,sh,n) \
00083        /* d = (n ? -1 : 1) * (s1 + (s2>>sh)) * x / (1>>N); */               \
00084       do {                                                           \
00085         if (s2 != NULL) {                                            \
00086            if (sh > 0) {                                             \
00087               assert (sh < mpbpl);                                   \
00088               mpn_lshift (tmp, s1, SZ, sh);                                 \
00089               chk = (n ? mpn_sub_n : mpn_add_n)(tmp,tmp,s2+FRAC/mpbpl,SZ);   \
00090            } else                                                    \
00091               chk = (n ? mpn_sub_n : mpn_add_n)(tmp,s1,s2+FRAC/mpbpl,SZ);    \
00092            /* assert(chk == 0); */                                   \
00093            mpn_mul_n(d,tmp,x,SZ);                                    \
00094         } else                                                       \
00095            mpn_mul_n(d,s1,x,SZ);                                     \
00096         /* assert(d[SZ*2-1] == 0); */                                       \
00097         assert(N+sh < mpbpl);                                               \
00098         if (N+sh > 0) mpn_rshift(d,d,2*SZ,N+sh);                     \
00099       } while(0)
00100 #define summ(d,ss,s,n) \
00101       /* d = ss +/- (s[0]+2*s[1]+2*s[2]+s[3])/6; */                         \
00102       do {                                                           \
00103         chk = mpn_add_n(tmp,s[1]+FRAC/mpbpl,s[2]+FRAC/mpbpl,SZ);            \
00104         mpn_lshift(tmp,tmp,SZ,1);                                    \
00105         chk |= mpn_add_n(tmp,tmp,s[0]+FRAC/mpbpl,SZ);                       \
00106         chk |= mpn_add_n(tmp,tmp,s[3]+FRAC/mpbpl,SZ);                       \
00107         round = mpn_divmod_1(tmp,tmp,SZ,6);                                 \
00108         /* chk |= mpn_add_1(tmp,tmp,SZ, (round > 3) ); */                   \
00109          chk |= (n ? mpn_sub_n : mpn_add_n)(d,ss,tmp,SZ);                   \
00110         /* assert(chk == 0); */                                      \
00111       } while (0)
00112 
00113       add_shift_mulh (s[0], x, co, NULL, 0, 0); /* s0 = h * c; */
00114       add_shift_mulh (c[0], x, si, NULL, 0, 0); /* c0 = h * s; */
00115       add_shift_mulh (s[1], x, co, c[0], 1, 1); /* s1 = h * (c - c0/2); */
00116       add_shift_mulh (c[1], x, si, s[0], 1, 0); /* c1 = h * (s + s0/2); */
00117       add_shift_mulh (s[2], x, co, c[1], 1, 1); /* s2 = h * (c - c1/2); */
00118       add_shift_mulh (c[2], x, si, s[1], 1, 0); /* c2 = h * (s + s1/2); */
00119       add_shift_mulh (s[3], x, co, c[2], 0, 1); /* s3 = h * (c - c2); */
00120       add_shift_mulh (c[3], x, si, s[2], 0, 0); /* c3 = h * (s + s2); */
00121       summ (si, si, s, 0);        /* s = s + (s0+2*s1+2*s2+s3)/6; */
00122       summ (co, co, c, 1);        /* c = c - (c0+2*c1+2*c2+c3)/6; */
00123    }
00124 #undef add_shift_mulh
00125 #undef summ
00126 }
00127 
00128 static int
00129 mpn_bitsize (const mp_limb_t *SRC_PTR, mp_size_t SIZE)
00130 {
00131    int i, j;
00132    for (i = SIZE - 1; i > 0; i--)
00133      if (SRC_PTR[i] != 0)
00134        break;
00135    for (j = mpbpl - 1; j >= 0; j--)
00136      if ((SRC_PTR[i] & (mp_limb_t)1 << j) != 0)
00137        break;
00138 
00139    return i * mpbpl + j;
00140 }
00141 
00142 int
00143 main (void)
00144 {
00145   mp1 si, co, x, ox, xt, s2, c2, s3, c3;
00146   int i;
00147   int sin_errors = 0, cos_errors = 0;
00148   int sin_failures = 0, cos_failures = 0;
00149   mp1 sin_maxerror, cos_maxerror;
00150   int sin_maxerror_s = 0, cos_maxerror_s = 0;
00151   const double sf = pow (2, mpbpl);
00152 
00153   /* assert(mpbpl == mp_bits_per_limb);  */
00154   assert(FRAC / mpbpl * mpbpl == FRAC);
00155 
00156   memset (sin_maxerror, 0, sizeof (mp1));
00157   memset (cos_maxerror, 0, sizeof (mp1));
00158   memset (xt, 0, sizeof (mp1));
00159   xt[(FRAC - N2) / mpbpl] = (mp_limb_t)1 << (FRAC - N2) % mpbpl;
00160 
00161   for (i = 0; i < 1 << N2; i++)
00162     {
00163       int s2s, s3s, c2s, c3s, j;
00164       double ds2,dc2;
00165 
00166       mpn_mul_1 (x, xt, SZ, i);
00167       sincosx_mpn (si, co, x, i == 0 ? NULL : ox);
00168       memcpy (ox, x, sizeof (mp1));
00169       ds2 = sin (i / (double) (1 << N2));
00170       dc2 = cos (i / (double) (1 << N2));
00171       for (j = SZ-1; j >= 0; j--)
00172        {
00173          s2[j] = (mp_limb_t) ds2;
00174          ds2 = (ds2 - s2[j]) * sf;
00175          c2[j] = (mp_limb_t) dc2;
00176          dc2 = (dc2 - c2[j]) * sf;
00177        }
00178       if (mpn_cmp (si, s2, SZ) >= 0)
00179        mpn_sub_n (s3, si, s2, SZ);
00180       else
00181        mpn_sub_n (s3, s2, si, SZ);
00182       if (mpn_cmp (co, c2, SZ) >= 0)
00183        mpn_sub_n (c3, co, c2, SZ);
00184       else
00185        mpn_sub_n (c3, c2, co, SZ);
00186 
00187       s2s = mpn_bitsize (s2, SZ);
00188       s3s = mpn_bitsize (s3, SZ);
00189       c2s = mpn_bitsize (c2, SZ);
00190       c3s = mpn_bitsize (c3, SZ);
00191       if ((s3s >= 0 && s2s - s3s < 54)
00192          || (c3s >= 0 && c2s - c3s < 54)
00193          || 0)
00194        {
00195 #if PRINT_ERRORS
00196          printf ("%06x ", i * (0x100000 / (1 << N2)));
00197          print_mpn_hex(si, (FRAC / 4) + 1);
00198          putchar (' ');
00199          print_mpn_hex (co, (FRAC / 4) + 1);
00200          putchar ('\n');
00201          fputs ("       ", stdout);
00202          print_mpn_hex (s2, (FRAC / 4) + 1);
00203          putchar (' ');
00204          print_mpn_hex (c2, (FRAC / 4) + 1);
00205          putchar ('\n');
00206          printf (" %c%c    ",
00207                 s3s >= 0 && s2s-s3s < 54 ? s2s - s3s == 53 ? 'e' : 'F' : 'P',
00208                 c3s >= 0 && c2s-c3s < 54 ? c2s - c3s == 53 ? 'e' : 'F' : 'P');
00209          print_mpn_hex (s3, (FRAC / 4) + 1);
00210          putchar (' ');
00211          print_mpn_hex (c3, (FRAC / 4) + 1);
00212          putchar ('\n');
00213 #endif
00214          sin_errors += s2s - s3s == 53;
00215          cos_errors += c2s - c3s == 53;
00216          sin_failures += s2s - s3s < 53;
00217          cos_failures += c2s - c3s < 53;
00218        }
00219       if (s3s >= sin_maxerror_s
00220          && mpn_cmp (s3, sin_maxerror, SZ) > 0)
00221        {
00222          memcpy (sin_maxerror, s3, sizeof (mp1));
00223          sin_maxerror_s = s3s;
00224        }
00225       if (c3s >= cos_maxerror_s
00226          && mpn_cmp (c3, cos_maxerror, SZ) > 0)
00227        {
00228          memcpy (cos_maxerror, c3, sizeof (mp1));
00229          cos_maxerror_s = c3s;
00230        }
00231     }
00232 
00233    /* Check Range-Kutta against precomputed values of sin(1) and cos(1).  */
00234    memset (x, 0, sizeof (mp1));
00235    x[FRAC / mpbpl] = (mp_limb_t)1 << FRAC % mpbpl;
00236    sincosx_mpn (si, co, x, ox);
00237 
00238    memset (s2, 0, sizeof (mp1));
00239    memset (c2, 0, sizeof (mp1));
00240    for (i = 0; i < 100 && i < FRAC / 4; i++)
00241      {
00242        s2[(FRAC - i * 4 - 4) / mpbpl] |= ((mp_limb_t) (strchr (hexdig, sin1[i])
00243                                                  - hexdig)
00244                                      << (FRAC - i * 4 - 4) % mpbpl);
00245        c2[(FRAC - i * 4 - 4) / mpbpl] |= ((mp_limb_t) (strchr (hexdig, cos1[i])
00246                                                  - hexdig)
00247                                      << (FRAC - i * 4 - 4) % mpbpl);
00248      }
00249 
00250    if (mpn_cmp (si, s2, SZ) >= 0)
00251      mpn_sub_n (s3, si, s2, SZ);
00252    else
00253      mpn_sub_n (s3, s2, si, SZ);
00254    if (mpn_cmp (co, c2, SZ) >= 0)
00255       mpn_sub_n (c3, co, c2, SZ);
00256    else
00257      mpn_sub_n (c3, c2, co, SZ);
00258 
00259    printf ("sin:\n");
00260    printf ("%d failures; %d errors; error rate %0.2f%%\n",
00261           sin_failures, sin_errors, sin_errors * 100.0 / (double) (1 << N2));
00262    fputs ("maximum error:   ", stdout);
00263    print_mpn_hex (sin_maxerror, (FRAC / 4) + 1);
00264    fputs ("\nerror in sin(1): ", stdout);
00265    print_mpn_hex (s3, (FRAC / 4) + 1);
00266 
00267    fputs ("\n\ncos:\n", stdout);
00268    printf ("%d failures; %d errors; error rate %0.2f%%\n",
00269           cos_failures, cos_errors, cos_errors * 100.0 / (double) (1 << N2));
00270    fputs ("maximum error:   ", stdout);
00271    print_mpn_hex (cos_maxerror, (FRAC / 4) + 1);
00272    fputs ("\nerror in cos(1): ", stdout);
00273    print_mpn_hex (c3, (FRAC / 4) + 1);
00274    putchar ('\n');
00275 
00276    return (sin_failures == 0 && cos_failures == 0) ? 0 : 1;
00277 }