]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - tools/regression/lib/msun/test-exponential.c
MFstable/10 r292795:
[FreeBSD/stable/9.git] / tools / regression / lib / msun / test-exponential.c
1 /*-
2  * Copyright (c) 2008 David Schultz <das@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 /*
28  * Tests for corner cases in exp*().
29  */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include <assert.h>
35 #include <fenv.h>
36 #include <float.h>
37 #include <math.h>
38 #include <stdio.h>
39
40 #ifdef __i386__
41 #include <ieeefp.h>
42 #endif
43
44 #define ALL_STD_EXCEPT  (FE_DIVBYZERO | FE_INEXACT | FE_INVALID | \
45                          FE_OVERFLOW | FE_UNDERFLOW)
46
47 #pragma STDC FENV_ACCESS ON
48
49 /*
50  * Test that a function returns the correct value and sets the
51  * exception flags correctly. The exceptmask specifies which
52  * exceptions we should check. We need to be lenient for several
53  * reasoons, but mainly because on some architectures it's impossible
54  * to raise FE_OVERFLOW without raising FE_INEXACT.
55  *
56  * These are macros instead of functions so that assert provides more
57  * meaningful error messages.
58  *
59  * XXX The volatile here is to avoid gcc's bogus constant folding and work
60  *     around the lack of support for the FENV_ACCESS pragma.
61  */
62 #define test(func, x, result, exceptmask, excepts)      do {            \
63         volatile long double _d = x;                                    \
64         assert(feclearexcept(FE_ALL_EXCEPT) == 0);                      \
65         assert(fpequal((func)(_d), (result)));                           \
66         assert(((func), fetestexcept(exceptmask) == (excepts)));        \
67 } while (0)
68
69 /* Test all the functions that compute b^x. */
70 #define _testall0(x, result, exceptmask, excepts)       do {            \
71         test(exp, x, result, exceptmask, excepts);                      \
72         test(expf, x, result, exceptmask, excepts);                     \
73         test(exp2, x, result, exceptmask, excepts);                     \
74         test(exp2f, x, result, exceptmask, excepts);                    \
75 } while (0)
76
77 /* Skip over exp2l on platforms that don't support it. */
78 #if LDBL_PREC == 53
79 #define testall0        _testall0
80 #else
81 #define testall0(x, result, exceptmask, excepts)        do {            \
82         _testall0(x, result, exceptmask, excepts);                      \
83         test(exp2l, x, result, exceptmask, excepts);                    \
84 } while (0)
85 #endif
86
87 /* Test all the functions that compute b^x - 1. */
88 #define testall1(x, result, exceptmask, excepts)        do {            \
89         test(expm1, x, result, exceptmask, excepts);                    \
90         test(expm1f, x, result, exceptmask, excepts);                   \
91 } while (0)
92
93 /*
94  * Determine whether x and y are equal, with two special rules:
95  *      +0.0 != -0.0
96  *       NaN == NaN
97  */
98 int
99 fpequal(long double x, long double y)
100 {
101         return ((x == y && !signbit(x) == !signbit(y)) || isnan(x) && isnan(y));
102 }
103
104 void
105 run_generic_tests(void)
106 {
107
108         /* exp(0) == 1, no exceptions raised */
109         testall0(0.0, 1.0, ALL_STD_EXCEPT, 0);
110         testall1(0.0, 0.0, ALL_STD_EXCEPT, 0);
111         testall0(-0.0, 1.0, ALL_STD_EXCEPT, 0);
112         testall1(-0.0, -0.0, ALL_STD_EXCEPT, 0);
113
114         /* exp(NaN) == NaN, no exceptions raised */
115         testall0(NAN, NAN, ALL_STD_EXCEPT, 0);
116         testall1(NAN, NAN, ALL_STD_EXCEPT, 0);
117
118         /* exp(Inf) == Inf, no exceptions raised */
119         testall0(INFINITY, INFINITY, ALL_STD_EXCEPT, 0);
120         testall1(INFINITY, INFINITY, ALL_STD_EXCEPT, 0);
121
122         /* exp(-Inf) == 0, no exceptions raised */
123         testall0(-INFINITY, 0.0, ALL_STD_EXCEPT, 0);
124         testall1(-INFINITY, -1.0, ALL_STD_EXCEPT, 0);
125
126 #if !defined(__i386__)
127         /* exp(big) == Inf, overflow exception */
128         testall0(50000.0, INFINITY, ALL_STD_EXCEPT & ~FE_INEXACT, FE_OVERFLOW);
129         testall1(50000.0, INFINITY, ALL_STD_EXCEPT & ~FE_INEXACT, FE_OVERFLOW);
130
131         /* exp(small) == 0, underflow and inexact exceptions */
132         testall0(-50000.0, 0.0, ALL_STD_EXCEPT, FE_UNDERFLOW | FE_INEXACT);
133 #endif
134         testall1(-50000.0, -1.0, ALL_STD_EXCEPT, FE_INEXACT);
135 }
136
137 void
138 run_exp2_tests(void)
139 {
140         int i;
141
142         /*
143          * We should insist that exp2() return exactly the correct
144          * result and not raise an inexact exception for integer
145          * arguments.
146          */
147         feclearexcept(FE_ALL_EXCEPT);
148         for (i = FLT_MIN_EXP - FLT_MANT_DIG; i < FLT_MAX_EXP; i++) {
149                 assert(exp2f(i) == ldexpf(1.0, i));
150                 assert(fetestexcept(ALL_STD_EXCEPT) == 0);
151         }
152         for (i = DBL_MIN_EXP - DBL_MANT_DIG; i < DBL_MAX_EXP; i++) {
153                 assert(exp2(i) == ldexp(1.0, i));
154                 assert(fetestexcept(ALL_STD_EXCEPT) == 0);
155         }
156         for (i = LDBL_MIN_EXP - LDBL_MANT_DIG; i < LDBL_MAX_EXP; i++) {
157                 assert(exp2l(i) == ldexpl(1.0, i));
158                 assert(fetestexcept(ALL_STD_EXCEPT) == 0);
159         }
160 }
161
162 int
163 main(int argc, char *argv[])
164 {
165
166         printf("1..3\n");
167
168         run_generic_tests();
169         printf("ok 1 - exponential\n");
170
171 #ifdef __i386__
172         fpsetprec(FP_PE);
173         run_generic_tests();
174 #endif
175         printf("ok 2 - exponential\n");
176
177         run_exp2_tests();
178         printf("ok 3 - exponential\n");
179
180         return (0);
181 }