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 #include <fenv.h> 33 #include <float.h> 34 #include <math.h> 35 #include <stdio.h> 36 37 #ifdef __i386__ 38 #include <ieeefp.h> 39 #endif 40 41 #include "test-utils.h" 42 43 #pragma STDC FENV_ACCESS ON 44 45 /* 46 * Test that a function returns the correct value and sets the 47 * exception flags correctly. The exceptmask specifies which 48 * exceptions we should check. We need to be lenient for several 49 * reasoons, but mainly because on some architectures it's impossible 50 * to raise FE_OVERFLOW without raising FE_INEXACT. 51 * 52 * These are macros instead of functions so that assert provides more 53 * meaningful error messages. 54 * 55 * XXX The volatile here is to avoid gcc's bogus constant folding and work 56 * around the lack of support for the FENV_ACCESS pragma. 57 */ 58 #define test(func, x, result, exceptmask, excepts) do { \ 59 volatile long double _d = x; \ 60 ATF_REQUIRE_EQ(0, feclearexcept(FE_ALL_EXCEPT)); \ 61 CHECK_FPEQUAL((func)(_d), (result)); \ 62 CHECK_FP_EXCEPTIONS_MSG(excepts, exceptmask, "for %s(%s)", \ 63 #func, #x); \ 64 } while (0) 65 66 /* Test all the functions that compute b^x. */ 67 #define _testall0(x, result, exceptmask, excepts) do { \ 68 test(exp, x, result, exceptmask, excepts); \ 69 test(expf, x, result, exceptmask, excepts); \ 70 test(exp2, x, result, exceptmask, excepts); \ 71 test(exp2f, x, result, exceptmask, excepts); \ 72 } while (0) 73 74 /* Skip over exp2l on platforms that don't support it. */ 75 #if LDBL_PREC == 53 76 #define testall0 _testall0 77 #else 78 #define testall0(x, result, exceptmask, excepts) do { \ 79 _testall0(x, result, exceptmask, excepts); \ 80 test(exp2l, x, result, exceptmask, excepts); \ 81 } while (0) 82 #endif 83 84 /* Test all the functions that compute b^x - 1. */ 85 #define testall1(x, result, exceptmask, excepts) do { \ 86 test(expm1, x, result, exceptmask, excepts); \ 87 test(expm1f, x, result, exceptmask, excepts); \ 88 } while (0) 89 90 static void 91 run_generic_tests(void) 92 { 93 94 /* exp(0) == 1, no exceptions raised */ 95 testall0(0.0, 1.0, ALL_STD_EXCEPT, 0); 96 testall1(0.0, 0.0, ALL_STD_EXCEPT, 0); 97 testall0(-0.0, 1.0, ALL_STD_EXCEPT, 0); 98 testall1(-0.0, -0.0, ALL_STD_EXCEPT, 0); 99 100 /* exp(NaN) == NaN, no exceptions raised */ 101 testall0(NAN, NAN, ALL_STD_EXCEPT, 0); 102 testall1(NAN, NAN, ALL_STD_EXCEPT, 0); 103 104 /* exp(Inf) == Inf, no exceptions raised */ 105 testall0(INFINITY, INFINITY, ALL_STD_EXCEPT, 0); 106 testall1(INFINITY, INFINITY, ALL_STD_EXCEPT, 0); 107 108 /* exp(-Inf) == 0, no exceptions raised */ 109 testall0(-INFINITY, 0.0, ALL_STD_EXCEPT, 0); 110 testall1(-INFINITY, -1.0, ALL_STD_EXCEPT, 0); 111 112 #if !defined(__i386__) 113 /* exp(big) == Inf, overflow exception */ 114 testall0(50000.0, INFINITY, ALL_STD_EXCEPT & ~FE_INEXACT, FE_OVERFLOW); 115 testall1(50000.0, INFINITY, ALL_STD_EXCEPT & ~FE_INEXACT, FE_OVERFLOW); 116 117 /* exp(small) == 0, underflow and inexact exceptions */ 118 testall0(-50000.0, 0.0, ALL_STD_EXCEPT, FE_UNDERFLOW | FE_INEXACT); 119 #endif 120 testall1(-50000.0, -1.0, ALL_STD_EXCEPT, FE_INEXACT); 121 } 122 123 124 /* 125 * We should insist that exp2() return exactly the correct 126 * result and not raise an inexact exception for integer 127 * arguments. 128 */ 129 ATF_TC_WITHOUT_HEAD(exp2f); 130 ATF_TC_BODY(exp2f, tc) 131 { 132 ATF_REQUIRE_EQ(0, feclearexcept(FE_ALL_EXCEPT)); 133 for (int i = FLT_MIN_EXP - FLT_MANT_DIG; i < FLT_MAX_EXP; i++) { 134 ATF_CHECK_EQ(exp2f(i), ldexpf(1.0, i)); 135 CHECK_FP_EXCEPTIONS(0, ALL_STD_EXCEPT); 136 } 137 } 138 139 ATF_TC_WITHOUT_HEAD(exp2); 140 ATF_TC_BODY(exp2, tc) 141 { 142 ATF_REQUIRE_EQ(0, feclearexcept(FE_ALL_EXCEPT)); 143 for (int i = DBL_MIN_EXP - DBL_MANT_DIG; i < DBL_MAX_EXP; i++) { 144 ATF_CHECK_EQ(exp2(i), ldexp(1.0, i)); 145 CHECK_FP_EXCEPTIONS(0, ALL_STD_EXCEPT); 146 } 147 } 148 149 ATF_TC_WITHOUT_HEAD(exp2l); 150 ATF_TC_BODY(exp2l, tc) 151 { 152 ATF_REQUIRE_EQ(0, feclearexcept(FE_ALL_EXCEPT)); 153 for (int i = LDBL_MIN_EXP - LDBL_MANT_DIG; i < LDBL_MAX_EXP; i++) { 154 ATF_CHECK_EQ(exp2l(i), ldexpl(1.0, i)); 155 CHECK_FP_EXCEPTIONS(0, ALL_STD_EXCEPT); 156 } 157 } 158 159 ATF_TC_WITHOUT_HEAD(generic); 160 ATF_TC_BODY(generic, tc) 161 { 162 run_generic_tests(); 163 } 164 165 #ifdef __i386__ 166 ATF_TC_WITHOUT_HEAD(generic_fp_pe); 167 ATF_TC_BODY(generic_fp_pe, tc) 168 { 169 fpsetprec(FP_PE); 170 run_generic_tests(); 171 } 172 #endif 173 174 ATF_TP_ADD_TCS(tp) 175 { 176 ATF_TP_ADD_TC(tp, generic); 177 #ifdef __i386__ 178 ATF_TP_ADD_TC(tp, generic_fp_pe); 179 #endif 180 ATF_TP_ADD_TC(tp, exp2); 181 ATF_TP_ADD_TC(tp, exp2f); 182 ATF_TP_ADD_TC(tp, exp2l); 183 184 return (atf_no_error()); 185 } 186