]> git.ipfire.org Git - thirdparty/glibc.git/blame - math/atest-exp2.c
Fix some testsuite build warning fixes in libm
[thirdparty/glibc.git] / math / atest-exp2.c
CommitLineData
568035b7 1/* Copyright (C) 1997-2013 Free Software Foundation, Inc.
650425ce
UD
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
41bdb6e2
AJ
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.
650425ce
UD
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
41bdb6e2 13 Lesser General Public License for more details.
650425ce 14
41bdb6e2 15 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
650425ce
UD
18
19#include <stdio.h>
20#include <math.h>
48896b9d 21#include <gmp.h>
650425ce
UD
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)
35typedef mp_limb_t mp1[SZ], mp2[SZ * 2];
36
37/* This string has 101 hex digits. */
38static const char exp1[102] = "2" /* point */
39"b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da56a7"
40"84d9045190cfef324e7738926cfbe5f4bf8d8d8c31d763da07";
41static const char exp_m1[102] = "0" /* point */
42"5e2d58d8b3bcdf1abadec7829054f90dda9805aab56c773330"
43"24b9d0a507daedb16400bf472b4215b8245b669d90d27a5aea";
44
45static const char hexdig[] = "0123456789abcdef";
46
767b6275 47static void
650425ce
UD
48print_mpn_fp (const mp_limb_t *x, unsigned int dp, unsigned int base)
49{
50 unsigned int i;
51 mp1 tx;
52
53 memcpy (tx, x, sizeof (mp1));
54 if (base == 16)
55 fputs ("0x", stdout);
56 assert (x[SZ-1] < base);
57 fputc (hexdig[x[SZ - 1]], stdout);
58 fputc ('.', stdout);
59 for (i = 0; i < dp; i++)
60 {
61 tx[SZ - 1] = 0;
62 mpn_mul_1 (tx, tx, SZ, base);
63 assert (tx[SZ - 1] < base);
64 fputc (hexdig[tx[SZ - 1]], stdout);
65 }
66}
67
767b6275 68static void
650425ce
UD
69read_mpn_hex(mp_limb_t *x, const char *str)
70{
71 int i;
72
73 memset (x, 0, sizeof (mp1));
74 for (i = -1; i < 100 && i < FRAC / 4; ++i)
11bf311e
UD
75 x[(FRAC - i * 4 - 4) / mpbpl] |= ((mp_limb_t) (strchr (hexdig, str[i + 1])
76 - hexdig)
77 << (FRAC - i * 4 - 4) % mpbpl);
650425ce
UD
78}
79
80static mp_limb_t *get_log2(void) __attribute__((const));
81static mp_limb_t *
82get_log2(void)
83{
84 static mp1 log2_m;
85 static int log2_m_inited = 0;
86 static const char log2[102] = "0" /* point */
87 "b17217f7d1cf79abc9e3b39803f2f6af40f343267298b62d8a"
88 "0d175b8baafa2be7b876206debac98559552fb4afa1b10ed2e";
89
90 if (!log2_m_inited)
91 {
92 read_mpn_hex (log2_m, log2);
93 log2_m_inited = 1;
94 }
95 return log2_m;
96}
97
98/* Compute e^x. */
767b6275 99static void
650425ce
UD
100exp_mpn (mp1 ex, mp1 x)
101{
102 unsigned int n;
103 mp1 xp;
104 mp2 tmp;
e4f22324 105 mp_limb_t chk;
650425ce
UD
106 mp1 tol;
107
108 memset (xp, 0, sizeof (mp1));
109 memset (ex, 0, sizeof (mp1));
69bf5f75 110 xp[FRAC / mpbpl] = (mp_limb_t)1 << FRAC % mpbpl;
650425ce 111 memset (tol, 0, sizeof (mp1));
69bf5f75 112 tol[(FRAC - TOL) / mpbpl] = (mp_limb_t)1 << (FRAC - TOL) % mpbpl;
650425ce
UD
113
114 n = 0;
115
116 do
117 {
118 /* Calculate sum(x^n/n!) until the next term is sufficiently small. */
119
120 mpn_mul_n (tmp, xp, x, SZ);
121 assert(tmp[SZ * 2 - 1] == 0);
122 if (n > 0)
e4f22324 123 mpn_divmod_1 (xp, tmp + FRAC / mpbpl, SZ, n);
650425ce
UD
124 chk = mpn_add_n (ex, ex, xp, SZ);
125 assert (chk == 0);
126 ++n;
127 assert (n < 80); /* Catch too-high TOL. */
128 }
129 while (n < 10 || mpn_cmp (xp, tol, SZ) >= 0);
130}
131
132/* Calculate 2^x. */
767b6275 133static void
650425ce
UD
134exp2_mpn (mp1 ex, mp1 x)
135{
136 mp2 tmp;
137 mpn_mul_n (tmp, x, get_log2 (), SZ);
138 assert(tmp[SZ * 2 - 1] == 0);
139 exp_mpn (ex, tmp + FRAC / mpbpl);
140}
141
142
143static int
144mpn_bitsize(const mp_limb_t *SRC_PTR, mp_size_t SIZE)
145{
146 int i, j;
147 for (i = SIZE - 1; i > 0; --i)
148 if (SRC_PTR[i] != 0)
149 break;
b259e746
UD
150 for (j = mpbpl - 1; j >= 0; --j)
151 if ((SRC_PTR[i] & (mp_limb_t)1 << j) != 0)
650425ce
UD
152 break;
153
b259e746 154 return i * mpbpl + j;
650425ce
UD
155}
156
157int
158main (void)
159{
160 mp1 ex, x, xt, e2, e3;
161 int i;
162 int errors = 0;
163 int failures = 0;
164 mp1 maxerror;
165 int maxerror_s = 0;
166 const double sf = pow (2, mpbpl);
167
168 /* assert(mpbpl == mp_bits_per_limb); */
169 assert(FRAC / mpbpl * mpbpl == FRAC);
170
171 memset (maxerror, 0, sizeof (mp1));
172 memset (xt, 0, sizeof (mp1));
69bf5f75 173 xt[(FRAC - N2) / mpbpl] = (mp_limb_t)1 << (FRAC - N2) % mpbpl;
650425ce
UD
174
175 for (i = 0; i < (1 << N2); ++i)
176 {
177 int e2s, e3s, j;
178 double de2;
179
180 mpn_mul_1 (x, xt, SZ, i);
181 exp2_mpn (ex, x);
182 de2 = exp2 (i / (double) (1 << N2));
183 for (j = SZ - 1; j >= 0; --j)
184 {
185 e2[j] = (mp_limb_t) de2;
186 de2 = (de2 - e2[j]) * sf;
187 }
188 if (mpn_cmp (ex, e2, SZ) >= 0)
189 mpn_sub_n (e3, ex, e2, SZ);
190 else
191 mpn_sub_n (e3, e2, ex, SZ);
192
193 e2s = mpn_bitsize (e2, SZ);
194 e3s = mpn_bitsize (e3, SZ);
b259e746 195 if (e3s >= 0 && e2s - e3s < 54)
650425ce
UD
196 {
197#if PRINT_ERRORS
198 printf ("%06x ", i * (0x100000 / (1 << N2)));
199 print_mpn_fp (ex, (FRAC / 4) + 1, 16);
200 putchar ('\n');
201 fputs (" ",stdout);
202 print_mpn_fp (e2, (FRAC / 4) + 1, 16);
203 putchar ('\n');
204 printf (" %c ",
205 e2s - e3s < 54 ? e2s - e3s == 53 ? 'e' : 'F' : 'P');
206 print_mpn_fp (e3, (FRAC / 4) + 1, 16);
207 putchar ('\n');
208#endif
209 errors += (e2s - e3s == 53);
210 failures += (e2s - e3s < 53);
211 }
212 if (e3s >= maxerror_s
213 && mpn_cmp (e3, maxerror, SZ) > 0)
214 {
215 memcpy (maxerror, e3, sizeof (mp1));
216 maxerror_s = e3s;
217 }
218 }
219
220 /* Check exp_mpn against precomputed value of exp(1). */
221 memset (x, 0, sizeof (mp1));
69bf5f75 222 x[FRAC / mpbpl] = (mp_limb_t)1 << FRAC % mpbpl;
650425ce
UD
223 exp_mpn (ex, x);
224 read_mpn_hex (e2, exp1);
225 if (mpn_cmp (ex, e2, SZ) >= 0)
226 mpn_sub_n (e3, ex, e2, SZ);
227 else
228 mpn_sub_n (e3, e2, ex, SZ);
229
230 printf ("%d failures; %d errors; error rate %0.2f%%\n", failures, errors,
231 errors * 100.0 / (double) (1 << N2));
232 fputs ("maximum error: ", stdout);
233 print_mpn_fp (maxerror, (FRAC / 4) + 1, 16);
234 putchar ('\n');
235 fputs ("error in exp(1): ", stdout);
236 print_mpn_fp (e3, (FRAC / 4) + 1, 16);
237 putchar ('\n');
238
239 return failures == 0 ? 0 : 1;
240}