1176379Sdas/*- 2176379Sdas * Copyright (c) 2008 David Schultz <das@FreeBSD.org> 3176379Sdas * All rights reserved. 4176379Sdas * 5176379Sdas * Redistribution and use in source and binary forms, with or without 6176379Sdas * modification, are permitted provided that the following conditions 7176379Sdas * are met: 8176379Sdas * 1. Redistributions of source code must retain the above copyright 9176379Sdas * notice, this list of conditions and the following disclaimer. 10176379Sdas * 2. Redistributions in binary form must reproduce the above copyright 11176379Sdas * notice, this list of conditions and the following disclaimer in the 12176379Sdas * documentation and/or other materials provided with the distribution. 13176379Sdas * 14176379Sdas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15176379Sdas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16176379Sdas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17176379Sdas * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18176379Sdas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19176379Sdas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20176379Sdas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21176379Sdas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22176379Sdas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23176379Sdas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24176379Sdas * SUCH DAMAGE. 25176379Sdas */ 26176379Sdas 27176379Sdas/* 28176379Sdas * Tests for corner cases in trigonometric functions. Some accuracy tests 29176379Sdas * are included as well, but these are very basic sanity checks, not 30176379Sdas * intended to be comprehensive. 31176379Sdas * 32176379Sdas * The program for generating representable numbers near multiples of pi is 33176379Sdas * available at http://www.cs.berkeley.edu/~wkahan/testpi/ . 34176379Sdas */ 35176379Sdas 36176379Sdas#include <sys/cdefs.h> 37176379Sdas__FBSDID("$FreeBSD: releng/11.0/lib/msun/tests/trig_test.c 292328 2015-12-16 09:11:11Z ngie $"); 38176379Sdas 39287297Srodrigc#include <sys/param.h> 40287297Srodrigc 41176379Sdas#include <assert.h> 42176379Sdas#include <fenv.h> 43176379Sdas#include <float.h> 44176379Sdas#include <math.h> 45176379Sdas#include <stdio.h> 46176379Sdas 47251241Sdas#include "test-utils.h" 48176379Sdas 49176379Sdas#pragma STDC FENV_ACCESS ON 50176379Sdas 51176379Sdas/* 52176379Sdas * Test that a function returns the correct value and sets the 53176379Sdas * exception flags correctly. The exceptmask specifies which 54176379Sdas * exceptions we should check. We need to be lenient for several 55176379Sdas * reasons, but mainly because on some architectures it's impossible 56176379Sdas * to raise FE_OVERFLOW without raising FE_INEXACT. 57176379Sdas * 58176379Sdas * These are macros instead of functions so that assert provides more 59176379Sdas * meaningful error messages. 60176379Sdas * 61176379Sdas * XXX The volatile here is to avoid gcc's bogus constant folding and work 62176379Sdas * around the lack of support for the FENV_ACCESS pragma. 63176379Sdas */ 64176379Sdas#define test(func, x, result, exceptmask, excepts) do { \ 65176379Sdas volatile long double _d = x; \ 66176379Sdas assert(feclearexcept(FE_ALL_EXCEPT) == 0); \ 67176748Sdas assert(fpequal((func)(_d), (result))); \ 68251241Sdas assert(((void)(func), fetestexcept(exceptmask) == (excepts))); \ 69176379Sdas} while (0) 70176379Sdas 71176379Sdas#define testall(prefix, x, result, exceptmask, excepts) do { \ 72176748Sdas test(prefix, x, (double)result, exceptmask, excepts); \ 73176379Sdas test(prefix##f, x, (float)result, exceptmask, excepts); \ 74176379Sdas test(prefix##l, x, result, exceptmask, excepts); \ 75176379Sdas} while (0) 76176379Sdas 77176748Sdas#define testdf(prefix, x, result, exceptmask, excepts) do { \ 78176748Sdas test(prefix, x, (double)result, exceptmask, excepts); \ 79176748Sdas test(prefix##f, x, (float)result, exceptmask, excepts); \ 80176748Sdas} while (0) 81176748Sdas 82176379Sdas/* 83176379Sdas * Test special cases in sin(), cos(), and tan(). 84176379Sdas */ 85176379Sdasstatic void 86176379Sdasrun_special_tests(void) 87176379Sdas{ 88176379Sdas 89176379Sdas /* Values at 0 should be exact. */ 90176379Sdas testall(tan, 0.0, 0.0, ALL_STD_EXCEPT, 0); 91176379Sdas testall(tan, -0.0, -0.0, ALL_STD_EXCEPT, 0); 92176379Sdas testall(cos, 0.0, 1.0, ALL_STD_EXCEPT, 0); 93176379Sdas testall(cos, -0.0, 1.0, ALL_STD_EXCEPT, 0); 94176379Sdas testall(sin, 0.0, 0.0, ALL_STD_EXCEPT, 0); 95176379Sdas testall(sin, -0.0, -0.0, ALL_STD_EXCEPT, 0); 96176379Sdas 97176379Sdas /* func(+-Inf) == NaN */ 98176379Sdas testall(tan, INFINITY, NAN, ALL_STD_EXCEPT, FE_INVALID); 99176379Sdas testall(sin, INFINITY, NAN, ALL_STD_EXCEPT, FE_INVALID); 100176379Sdas testall(cos, INFINITY, NAN, ALL_STD_EXCEPT, FE_INVALID); 101176379Sdas testall(tan, -INFINITY, NAN, ALL_STD_EXCEPT, FE_INVALID); 102176379Sdas testall(sin, -INFINITY, NAN, ALL_STD_EXCEPT, FE_INVALID); 103176379Sdas testall(cos, -INFINITY, NAN, ALL_STD_EXCEPT, FE_INVALID); 104176379Sdas 105176379Sdas /* func(NaN) == NaN */ 106176379Sdas testall(tan, NAN, NAN, ALL_STD_EXCEPT, 0); 107176379Sdas testall(sin, NAN, NAN, ALL_STD_EXCEPT, 0); 108176379Sdas testall(cos, NAN, NAN, ALL_STD_EXCEPT, 0); 109176379Sdas} 110176379Sdas 111176379Sdas/* 112176379Sdas * Tests to ensure argument reduction for large arguments is accurate. 113176379Sdas */ 114176379Sdasstatic void 115176379Sdasrun_reduction_tests(void) 116176379Sdas{ 117176379Sdas /* floats very close to odd multiples of pi */ 118176379Sdas static const float f_pi_odd[] = { 119176379Sdas 85563208.0f, 120176379Sdas 43998769152.0f, 121176379Sdas 9.2763667655669323e+25f, 122176379Sdas 1.5458357838905804e+29f, 123176379Sdas }; 124176379Sdas /* doubles very close to odd multiples of pi */ 125176379Sdas static const double d_pi_odd[] = { 126176379Sdas 3.1415926535897931, 127176379Sdas 91.106186954104004, 128176379Sdas 642615.9188844458, 129176379Sdas 3397346.5699258847, 130176379Sdas 6134899525417045.0, 131176379Sdas 3.0213551960457761e+43, 132176379Sdas 1.2646209897993783e+295, 133176379Sdas 6.2083625380677099e+307, 134176379Sdas }; 135176379Sdas /* long doubles very close to odd multiples of pi */ 136176379Sdas#if LDBL_MANT_DIG == 64 137176379Sdas static const long double ld_pi_odd[] = { 138176379Sdas 1.1891886960373841596e+101L, 139176379Sdas 1.07999475322710967206e+2087L, 140176379Sdas 6.522151627890431836e+2147L, 141176379Sdas 8.9368974898260328229e+2484L, 142176379Sdas 9.2961044110572205863e+2555L, 143176379Sdas 4.90208421886578286e+3189L, 144176379Sdas 1.5275546401232615884e+3317L, 145176379Sdas 1.7227465626338900093e+3565L, 146176379Sdas 2.4160090594000745334e+3808L, 147176379Sdas 9.8477555741888350649e+4314L, 148176379Sdas 1.6061597222105160737e+4326L, 149176379Sdas }; 150176379Sdas#elif LDBL_MANT_DIG == 113 151176379Sdas static const long double ld_pi_odd[] = { 152176379Sdas /* XXX */ 153176379Sdas }; 154176379Sdas#endif 155176379Sdas 156176379Sdas int i; 157176379Sdas 158287297Srodrigc for (i = 0; i < nitems(f_pi_odd); i++) { 159176379Sdas assert(fabs(sinf(f_pi_odd[i])) < FLT_EPSILON); 160176379Sdas assert(cosf(f_pi_odd[i]) == -1.0); 161176379Sdas assert(fabs(tan(f_pi_odd[i])) < FLT_EPSILON); 162176379Sdas 163176379Sdas assert(fabs(sinf(-f_pi_odd[i])) < FLT_EPSILON); 164176379Sdas assert(cosf(-f_pi_odd[i]) == -1.0); 165176379Sdas assert(fabs(tanf(-f_pi_odd[i])) < FLT_EPSILON); 166176379Sdas 167176379Sdas assert(fabs(sinf(f_pi_odd[i] * 2)) < FLT_EPSILON); 168176379Sdas assert(cosf(f_pi_odd[i] * 2) == 1.0); 169176379Sdas assert(fabs(tanf(f_pi_odd[i] * 2)) < FLT_EPSILON); 170176379Sdas 171176379Sdas assert(fabs(sinf(-f_pi_odd[i] * 2)) < FLT_EPSILON); 172176379Sdas assert(cosf(-f_pi_odd[i] * 2) == 1.0); 173176379Sdas assert(fabs(tanf(-f_pi_odd[i] * 2)) < FLT_EPSILON); 174176379Sdas } 175176379Sdas 176287297Srodrigc for (i = 0; i < nitems(d_pi_odd); i++) { 177176379Sdas assert(fabs(sin(d_pi_odd[i])) < 2 * DBL_EPSILON); 178176379Sdas assert(cos(d_pi_odd[i]) == -1.0); 179176379Sdas assert(fabs(tan(d_pi_odd[i])) < 2 * DBL_EPSILON); 180176379Sdas 181176379Sdas assert(fabs(sin(-d_pi_odd[i])) < 2 * DBL_EPSILON); 182176379Sdas assert(cos(-d_pi_odd[i]) == -1.0); 183176379Sdas assert(fabs(tan(-d_pi_odd[i])) < 2 * DBL_EPSILON); 184176379Sdas 185176379Sdas assert(fabs(sin(d_pi_odd[i] * 2)) < 2 * DBL_EPSILON); 186176379Sdas assert(cos(d_pi_odd[i] * 2) == 1.0); 187176379Sdas assert(fabs(tan(d_pi_odd[i] * 2)) < 2 * DBL_EPSILON); 188176379Sdas 189176379Sdas assert(fabs(sin(-d_pi_odd[i] * 2)) < 2 * DBL_EPSILON); 190176379Sdas assert(cos(-d_pi_odd[i] * 2) == 1.0); 191176379Sdas assert(fabs(tan(-d_pi_odd[i] * 2)) < 2 * DBL_EPSILON); 192176379Sdas } 193176379Sdas 194176379Sdas#if LDBL_MANT_DIG > 53 195287297Srodrigc for (i = 0; i < nitems(ld_pi_odd); i++) { 196176379Sdas assert(fabsl(sinl(ld_pi_odd[i])) < LDBL_EPSILON); 197176379Sdas assert(cosl(ld_pi_odd[i]) == -1.0); 198176379Sdas assert(fabsl(tanl(ld_pi_odd[i])) < LDBL_EPSILON); 199176379Sdas 200176379Sdas assert(fabsl(sinl(-ld_pi_odd[i])) < LDBL_EPSILON); 201176379Sdas assert(cosl(-ld_pi_odd[i]) == -1.0); 202176379Sdas assert(fabsl(tanl(-ld_pi_odd[i])) < LDBL_EPSILON); 203176379Sdas 204176379Sdas assert(fabsl(sinl(ld_pi_odd[i] * 2)) < LDBL_EPSILON); 205176379Sdas assert(cosl(ld_pi_odd[i] * 2) == 1.0); 206176379Sdas assert(fabsl(tanl(ld_pi_odd[i] * 2)) < LDBL_EPSILON); 207176379Sdas 208176379Sdas assert(fabsl(sinl(-ld_pi_odd[i] * 2)) < LDBL_EPSILON); 209176379Sdas assert(cosl(-ld_pi_odd[i] * 2) == 1.0); 210176379Sdas assert(fabsl(tanl(-ld_pi_odd[i] * 2)) < LDBL_EPSILON); 211176379Sdas } 212176379Sdas#endif 213176379Sdas} 214176379Sdas 215176379Sdas/* 216176379Sdas * Tests the accuracy of these functions over the primary range. 217176379Sdas */ 218176379Sdasstatic void 219176379Sdasrun_accuracy_tests(void) 220176379Sdas{ 221176379Sdas 222176379Sdas /* For small args, sin(x) = tan(x) = x, and cos(x) = 1. */ 223176379Sdas testall(sin, 0xd.50ee515fe4aea16p-114L, 0xd.50ee515fe4aea16p-114L, 224176379Sdas ALL_STD_EXCEPT, FE_INEXACT); 225176379Sdas testall(tan, 0xd.50ee515fe4aea16p-114L, 0xd.50ee515fe4aea16p-114L, 226176379Sdas ALL_STD_EXCEPT, FE_INEXACT); 227176379Sdas testall(cos, 0xd.50ee515fe4aea16p-114L, 1.0, 228176379Sdas ALL_STD_EXCEPT, FE_INEXACT); 229176379Sdas 230176379Sdas /* 231176379Sdas * These tests should pass for f32, d64, and ld80 as long as 232176379Sdas * the error is <= 0.75 ulp (round to nearest) 233176379Sdas */ 234176748Sdas#if LDBL_MANT_DIG <= 64 235176748Sdas#define testacc testall 236176748Sdas#else 237176748Sdas#define testacc testdf 238176748Sdas#endif 239176748Sdas testacc(sin, 0.17255452780841205174L, 0.17169949801444412683L, 240176379Sdas ALL_STD_EXCEPT, FE_INEXACT); 241176748Sdas testacc(sin, -0.75431944555904520893L, -0.68479288156557286353L, 242176379Sdas ALL_STD_EXCEPT, FE_INEXACT); 243176748Sdas testacc(cos, 0.70556358769838947292L, 0.76124620693117771850L, 244176379Sdas ALL_STD_EXCEPT, FE_INEXACT); 245176748Sdas testacc(cos, -0.34061437849088045332L, 0.94254960031831729956L, 246176379Sdas ALL_STD_EXCEPT, FE_INEXACT); 247176748Sdas testacc(tan, -0.15862817413325692897L, -0.15997221861309522115L, 248176379Sdas ALL_STD_EXCEPT, FE_INEXACT); 249176748Sdas testacc(tan, 0.38374784931303813530L, 0.40376500259976759951L, 250176379Sdas ALL_STD_EXCEPT, FE_INEXACT); 251176379Sdas 252176379Sdas /* 253176379Sdas * XXX missing: 254176379Sdas * - tests for ld128 255176379Sdas * - tests for other rounding modes (probably won't pass for now) 256176379Sdas * - tests for large numbers that get reduced to hi+lo with lo!=0 257176379Sdas */ 258176379Sdas} 259176379Sdas 260176379Sdasint 261176379Sdasmain(int argc, char *argv[]) 262176379Sdas{ 263176379Sdas 264176379Sdas printf("1..3\n"); 265176379Sdas 266176379Sdas run_special_tests(); 267176379Sdas printf("ok 1 - trig\n"); 268176379Sdas 269176379Sdas#ifndef __i386__ 270176379Sdas run_reduction_tests(); 271176379Sdas#endif 272176379Sdas printf("ok 2 - trig\n"); 273176379Sdas 274176379Sdas#ifndef __i386__ 275176379Sdas run_accuracy_tests(); 276176379Sdas#endif 277176379Sdas printf("ok 3 - trig\n"); 278176379Sdas 279176379Sdas return (0); 280176379Sdas} 281