logarithm_test.c revision 175467
125401Sjb/*- 250476Speter * Copyright (c) 2008 David Schultz <das@FreeBSD.org> 37494Sjkh * All rights reserved. 4107052Sru * 57494Sjkh * Redistribution and use in source and binary forms, with or without 665324Sphantom * modification, are permitted provided that the following conditions 725401Sjb * are met: 8156613Sdeischen * 1. Redistributions of source code must retain the above copyright 9156613Sdeischen * notice, this list of conditions and the following disclaimer. 1074870Sru * 2. Redistributions in binary form must reproduce the above copyright 11189765Sgabor * notice, this list of conditions and the following disclaimer in the 12189765Sgabor * documentation and/or other materials provided with the distribution. 13189765Sgabor * 14189765Sgabor * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15189765Sgabor * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16190397Sgabor * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17189765Sgabor * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18189765Sgabor * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19189765Sgabor * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20189765Sgabor * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21189765Sgabor * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22189765Sgabor * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23197534Sgabor * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24189765Sgabor * SUCH DAMAGE. 25189765Sgabor */ 26199080Sume 27199081Sume/* 28189765Sgabor * Tests for corner cases in exp*(). 29189765Sgabor */ 30189765Sgabor 31189765Sgabor#include <sys/cdefs.h> 32189765Sgabor__FBSDID("$FreeBSD: head/tools/regression/lib/msun/test-exponential.c 175467 2008-01-18 22:10:57Z das $"); 33189765Sgabor 34189883Sgabor#include <assert.h> 35189765Sgabor#include <fenv.h> 36189765Sgabor#include <float.h> 37189765Sgabor#include <math.h> 38190193Sgabor#include <stdio.h> 39244757Sdelphij 40244757Sdelphij#ifdef __i386__ 41244756Sdelphij#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 test(exp2l, x, result, exceptmask, excepts); \ 76} while (0) 77 78/* Test all the functions that compute b^x - 1. */ 79#define testall1(x, result, exceptmask, excepts) do { \ 80 test(expm1, x, result, exceptmask, excepts); \ 81 test(expm1f, x, result, exceptmask, excepts); \ 82} while (0) 83 84/* 85 * Determine whether x and y are equal, with two special rules: 86 * +0.0 != -0.0 87 * NaN == NaN 88 */ 89int 90fpequal(long double x, long double y) 91{ 92 return ((x == y && signbit(x) == signbit(y)) || isnan(x) && isnan(y)); 93} 94 95void 96run_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 /* exp(big) == Inf, overflow exception */ 118 testall0(50000.0, INFINITY, ALL_STD_EXCEPT & ~FE_INEXACT, FE_OVERFLOW); 119 testall1(50000.0, INFINITY, ALL_STD_EXCEPT & ~FE_INEXACT, FE_OVERFLOW); 120 121 /* exp(small) == 0, underflow and inexact exceptions */ 122 testall0(-50000.0, 0.0, ALL_STD_EXCEPT, FE_UNDERFLOW | FE_INEXACT); 123 testall1(-50000.0, -1.0, ALL_STD_EXCEPT, FE_INEXACT); 124} 125 126void 127run_exp2_tests(void) 128{ 129 int i; 130 131 /* 132 * We should insist that exp2() return exactly the correct 133 * result and not raise an inexact exception for integer 134 * arguments. 135 */ 136 feclearexcept(FE_ALL_EXCEPT); 137 for (i = FLT_MIN_EXP - FLT_MANT_DIG; i < FLT_MAX_EXP; i++) { 138 assert(exp2f(i) == ldexpf(1.0, i)); 139 assert(fetestexcept(ALL_STD_EXCEPT) == 0); 140 } 141 for (i = DBL_MIN_EXP - DBL_MANT_DIG; i < DBL_MAX_EXP; i++) { 142 assert(exp2(i) == ldexp(1.0, i)); 143 assert(fetestexcept(ALL_STD_EXCEPT) == 0); 144 } 145 for (i = LDBL_MIN_EXP - LDBL_MANT_DIG; i < LDBL_MAX_EXP; i++) { 146 assert(exp2l(i) == ldexpl(1.0, i)); 147 assert(fetestexcept(ALL_STD_EXCEPT) == 0); 148 } 149} 150 151int 152main(int argc, char *argv[]) 153{ 154 155 printf("1..2\n"); 156 157 run_generic_tests(); 158 printf("ok 1 - exponential\n"); 159 160#ifdef __i386__ 161 fpsetprec(FP_PE); 162 run_generic_tests(); 163#endif 164 printf("ok 2 - exponential\n"); 165 166 run_exp2_tests(); 167 printf("ok 3 - exponential\n"); 168 169 return (0); 170} 171