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 trigonometric functions. Some accuracy tests
29 * are included as well, but these are very basic sanity checks, not
30 * intended to be comprehensive.
31 *
32 * The program for generating representable numbers near multiples of pi is
33 * available at http://www.cs.berkeley.edu/~wkahan/testpi/ .
34 */
35
36 #include <sys/param.h>
37
38 #include <fenv.h>
39 #include <float.h>
40 #include <math.h>
41 #include <stdio.h>
42
43 #include "test-utils.h"
44
45 #pragma STDC FENV_ACCESS ON
46
47 /*
48 * Test that a function returns the correct value and sets the
49 * exception flags correctly. The exceptmask specifies which
50 * exceptions we should check. We need to be lenient for several
51 * reasons, but mainly because on some architectures it's impossible
52 * to raise FE_OVERFLOW without raising FE_INEXACT.
53 *
54 * These are macros instead of functions so that assert provides more
55 * meaningful error messages.
56 *
57 * XXX The volatile here is to avoid gcc's bogus constant folding and work
58 * around the lack of support for the FENV_ACCESS pragma.
59 */
60 #define test(func, x, result, exceptmask, excepts) do { \
61 volatile long double _d = x; \
62 ATF_CHECK(feclearexcept(FE_ALL_EXCEPT) == 0); \
63 CHECK_FPEQUAL((func)(_d), (result)); \
64 CHECK_FP_EXCEPTIONS_MSG(excepts, exceptmask, "for %s(%s)", \
65 #func, #x); \
66 } while (0)
67
68 #define testall(prefix, x, result, exceptmask, excepts) do { \
69 test(prefix, x, (double)result, exceptmask, excepts); \
70 test(prefix##f, x, (float)result, exceptmask, excepts); \
71 test(prefix##l, x, result, exceptmask, excepts); \
72 } while (0)
73
74 #define testdf(prefix, x, result, exceptmask, excepts) do { \
75 test(prefix, x, (double)result, exceptmask, excepts); \
76 test(prefix##f, x, (float)result, exceptmask, excepts); \
77 } while (0)
78
79 ATF_TC(special);
ATF_TC_HEAD(special,tc)80 ATF_TC_HEAD(special, tc)
81 {
82
83 atf_tc_set_md_var(tc, "descr",
84 "test special cases in sin(), cos(), and tan()");
85 }
ATF_TC_BODY(special,tc)86 ATF_TC_BODY(special, tc)
87 {
88 #if defined(__aarch64__) || defined(__riscv)
89 atf_tc_expect_fail("https://bugs.freebsd.org/290099");
90 #endif
91 /* Values at 0 should be exact. */
92 testall(tan, 0.0, 0.0, ALL_STD_EXCEPT, 0);
93 testall(tan, -0.0, -0.0, ALL_STD_EXCEPT, 0);
94 testall(cos, 0.0, 1.0, ALL_STD_EXCEPT, 0);
95 testall(cos, -0.0, 1.0, ALL_STD_EXCEPT, 0);
96 testall(sin, 0.0, 0.0, ALL_STD_EXCEPT, 0);
97 testall(sin, -0.0, -0.0, ALL_STD_EXCEPT, 0);
98
99 /* func(+-Inf) == NaN */
100 testall(tan, INFINITY, NAN, ALL_STD_EXCEPT, FE_INVALID);
101 testall(sin, INFINITY, NAN, ALL_STD_EXCEPT, FE_INVALID);
102 testall(cos, INFINITY, NAN, ALL_STD_EXCEPT, FE_INVALID);
103 testall(tan, -INFINITY, NAN, ALL_STD_EXCEPT, FE_INVALID);
104 testall(sin, -INFINITY, NAN, ALL_STD_EXCEPT, FE_INVALID);
105 testall(cos, -INFINITY, NAN, ALL_STD_EXCEPT, FE_INVALID);
106
107 /* func(NaN) == NaN */
108 testall(tan, NAN, NAN, ALL_STD_EXCEPT, 0);
109 testall(sin, NAN, NAN, ALL_STD_EXCEPT, 0);
110 testall(cos, NAN, NAN, ALL_STD_EXCEPT, 0);
111 }
112
113 #ifndef __i386__
114 ATF_TC(reduction);
ATF_TC_HEAD(reduction,tc)115 ATF_TC_HEAD(reduction, tc)
116 {
117
118 atf_tc_set_md_var(tc, "descr",
119 "tests to ensure argument reduction for large arguments is accurate");
120 }
ATF_TC_BODY(reduction,tc)121 ATF_TC_BODY(reduction, tc)
122 {
123 /* floats very close to odd multiples of pi */
124 static const float f_pi_odd[] = {
125 85563208.0f,
126 43998769152.0f,
127 9.2763667655669323e+25f,
128 1.5458357838905804e+29f,
129 };
130 /* doubles very close to odd multiples of pi */
131 static const double d_pi_odd[] = {
132 3.1415926535897931,
133 91.106186954104004,
134 642615.9188844458,
135 3397346.5699258847,
136 6134899525417045.0,
137 3.0213551960457761e+43,
138 1.2646209897993783e+295,
139 6.2083625380677099e+307,
140 };
141 /* long doubles very close to odd multiples of pi */
142 #if LDBL_MANT_DIG == 64
143 static const long double ld_pi_odd[] = {
144 1.1891886960373841596e+101L,
145 1.07999475322710967206e+2087L,
146 6.522151627890431836e+2147L,
147 8.9368974898260328229e+2484L,
148 9.2961044110572205863e+2555L,
149 4.90208421886578286e+3189L,
150 1.5275546401232615884e+3317L,
151 1.7227465626338900093e+3565L,
152 2.4160090594000745334e+3808L,
153 9.8477555741888350649e+4314L,
154 1.6061597222105160737e+4326L,
155 };
156 #endif
157
158 unsigned i;
159
160 #if defined(__amd64__) && defined(__clang__) && __clang_major__ >= 7 && \
161 __clang_major__ < 10 && __FreeBSD_cc_version < 1300002
162 atf_tc_expect_fail("test fails with clang 7-9 - bug 234040");
163 #endif
164
165 for (i = 0; i < nitems(f_pi_odd); i++) {
166 ATF_CHECK(fabs(sinf(f_pi_odd[i])) < FLT_EPSILON);
167 ATF_CHECK(cosf(f_pi_odd[i]) == -1.0);
168 ATF_CHECK(fabs(tan(f_pi_odd[i])) < FLT_EPSILON);
169
170 ATF_CHECK(fabs(sinf(-f_pi_odd[i])) < FLT_EPSILON);
171 ATF_CHECK(cosf(-f_pi_odd[i]) == -1.0);
172 ATF_CHECK(fabs(tanf(-f_pi_odd[i])) < FLT_EPSILON);
173
174 ATF_CHECK(fabs(sinf(f_pi_odd[i] * 2)) < FLT_EPSILON);
175 ATF_CHECK(cosf(f_pi_odd[i] * 2) == 1.0);
176 ATF_CHECK(fabs(tanf(f_pi_odd[i] * 2)) < FLT_EPSILON);
177
178 ATF_CHECK(fabs(sinf(-f_pi_odd[i] * 2)) < FLT_EPSILON);
179 ATF_CHECK(cosf(-f_pi_odd[i] * 2) == 1.0);
180 ATF_CHECK(fabs(tanf(-f_pi_odd[i] * 2)) < FLT_EPSILON);
181 }
182
183 for (i = 0; i < nitems(d_pi_odd); i++) {
184 ATF_CHECK(fabs(sin(d_pi_odd[i])) < 2 * DBL_EPSILON);
185 ATF_CHECK(cos(d_pi_odd[i]) == -1.0);
186 ATF_CHECK(fabs(tan(d_pi_odd[i])) < 2 * DBL_EPSILON);
187
188 ATF_CHECK(fabs(sin(-d_pi_odd[i])) < 2 * DBL_EPSILON);
189 ATF_CHECK(cos(-d_pi_odd[i]) == -1.0);
190 ATF_CHECK(fabs(tan(-d_pi_odd[i])) < 2 * DBL_EPSILON);
191
192 ATF_CHECK(fabs(sin(d_pi_odd[i] * 2)) < 2 * DBL_EPSILON);
193 ATF_CHECK(cos(d_pi_odd[i] * 2) == 1.0);
194 ATF_CHECK(fabs(tan(d_pi_odd[i] * 2)) < 2 * DBL_EPSILON);
195
196 ATF_CHECK(fabs(sin(-d_pi_odd[i] * 2)) < 2 * DBL_EPSILON);
197 ATF_CHECK(cos(-d_pi_odd[i] * 2) == 1.0);
198 ATF_CHECK(fabs(tan(-d_pi_odd[i] * 2)) < 2 * DBL_EPSILON);
199 }
200
201 #if LDBL_MANT_DIG == 64 /* XXX: || LDBL_MANT_DIG == 113 */
202 for (i = 0; i < nitems(ld_pi_odd); i++) {
203 ATF_CHECK(fabsl(sinl(ld_pi_odd[i])) < LDBL_EPSILON);
204 ATF_CHECK(cosl(ld_pi_odd[i]) == -1.0);
205 ATF_CHECK(fabsl(tanl(ld_pi_odd[i])) < LDBL_EPSILON);
206
207 ATF_CHECK(fabsl(sinl(-ld_pi_odd[i])) < LDBL_EPSILON);
208 ATF_CHECK(cosl(-ld_pi_odd[i]) == -1.0);
209 ATF_CHECK(fabsl(tanl(-ld_pi_odd[i])) < LDBL_EPSILON);
210
211 ATF_CHECK(fabsl(sinl(ld_pi_odd[i] * 2)) < LDBL_EPSILON);
212 ATF_CHECK(cosl(ld_pi_odd[i] * 2) == 1.0);
213 ATF_CHECK(fabsl(tanl(ld_pi_odd[i] * 2)) < LDBL_EPSILON);
214
215 ATF_CHECK(fabsl(sinl(-ld_pi_odd[i] * 2)) < LDBL_EPSILON);
216 ATF_CHECK(cosl(-ld_pi_odd[i] * 2) == 1.0);
217 ATF_CHECK(fabsl(tanl(-ld_pi_odd[i] * 2)) < LDBL_EPSILON);
218 }
219 #endif
220 }
221
222 ATF_TC(accuracy);
ATF_TC_HEAD(accuracy,tc)223 ATF_TC_HEAD(accuracy, tc)
224 {
225
226 atf_tc_set_md_var(tc, "descr",
227 "tests the accuracy of these functions over the primary range");
228 }
ATF_TC_BODY(accuracy,tc)229 ATF_TC_BODY(accuracy, tc)
230 {
231
232 /* For small args, sin(x) = tan(x) = x, and cos(x) = 1. */
233 testall(sin, 0xd.50ee515fe4aea16p-114L, 0xd.50ee515fe4aea16p-114L,
234 ALL_STD_EXCEPT, FE_INEXACT);
235 testall(tan, 0xd.50ee515fe4aea16p-114L, 0xd.50ee515fe4aea16p-114L,
236 ALL_STD_EXCEPT, FE_INEXACT);
237 testall(cos, 0xd.50ee515fe4aea16p-114L, 1.0,
238 ALL_STD_EXCEPT, FE_INEXACT);
239
240 /*
241 * These tests should pass for f32, d64, and ld80 as long as
242 * the error is <= 0.75 ulp (round to nearest)
243 */
244 #if LDBL_MANT_DIG <= 64
245 #define testacc testall
246 #else
247 #define testacc testdf
248 #endif
249 testacc(sin, 0.17255452780841205174L, 0.17169949801444412683L,
250 ALL_STD_EXCEPT, FE_INEXACT);
251 testacc(sin, -0.75431944555904520893L, -0.68479288156557286353L,
252 ALL_STD_EXCEPT, FE_INEXACT);
253 testacc(cos, 0.70556358769838947292L, 0.76124620693117771850L,
254 ALL_STD_EXCEPT, FE_INEXACT);
255 testacc(cos, -0.34061437849088045332L, 0.94254960031831729956L,
256 ALL_STD_EXCEPT, FE_INEXACT);
257 testacc(tan, -0.15862817413325692897L, -0.15997221861309522115L,
258 ALL_STD_EXCEPT, FE_INEXACT);
259 testacc(tan, 0.38374784931303813530L, 0.40376500259976759951L,
260 ALL_STD_EXCEPT, FE_INEXACT);
261
262 /*
263 * XXX missing:
264 * - tests for ld128
265 * - tests for other rounding modes (probably won't pass for now)
266 * - tests for large numbers that get reduced to hi+lo with lo!=0
267 */
268 }
269 #endif
270
ATF_TP_ADD_TCS(tp)271 ATF_TP_ADD_TCS(tp)
272 {
273
274 ATF_TP_ADD_TC(tp, special);
275
276 #ifndef __i386__
277 ATF_TP_ADD_TC(tp, accuracy);
278 ATF_TP_ADD_TC(tp, reduction);
279 #endif
280
281 return (atf_no_error());
282 }
283