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