xref: /openbsd-src/regress/lib/libm/msun/next_test.c (revision 498c7b5e98c82ca5a0055f676f2a6e4214e2d9a8)
1 /*	$OpenBSD: next_test.c,v 1.2 2021/12/13 18:04:28 deraadt Exp $	*/
2 /*-
3  * Copyright (c) 2005 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  * Test the correctness of nextafter{,f,l} and nexttoward{,f,l}.
32  */
33 
34 #include <fenv.h>
35 #include <float.h>
36 #include <math.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 
40 #ifdef	__i386__
41 #include <ieeefp.h>
42 #endif
43 
44 #include "test-utils.h"
45 
46 #define	test(exp, ans, ex)	do {			\
47 	double __ans = (ans);				\
48 	feclearexcept(ALL_STD_EXCEPT);			\
49 	_testl(#exp, __LINE__, (exp), __ans, (ex));	\
50 } while (0)
51 #define	testf(exp, ans, ex)	do {			\
52 	float __ans = (ans);				\
53 	feclearexcept(ALL_STD_EXCEPT);			\
54 	_testl(#exp, __LINE__, (exp), __ans, (ex));	\
55 } while (0)
56 #define	testl(exp, ans, ex)	do {			\
57 	long double __ans = (ans);			\
58 	feclearexcept(ALL_STD_EXCEPT);			\
59 	_testl(#exp, __LINE__, (exp), __ans, (ex));	\
60 } while (0)
61 #define	testboth(arg1, arg2, ans, ex, prec)	do {			\
62 	test##prec(nextafter##prec((arg1), (arg2)), (ans), (ex));	\
63 	test##prec(nexttoward##prec((arg1), (arg2)), (ans), (ex));	\
64 } while (0)
65 #define	testall(arg1, arg2, ans, ex)	do {		\
66 	testboth((arg1), (arg2), (ans), (ex), );	\
67 	testboth((arg1), (arg2), (ans), (ex), f);	\
68 	testboth((arg1), (arg2), (ans), (ex), l);	\
69 } while (0)
70 
71 static void _testl(const char *, int, long double, long double, int);
72 static double idd(double);
73 static float idf(float);
74 
75 static const int ex_under = FE_UNDERFLOW | FE_INEXACT;	/* shorthand */
76 static const int ex_over = FE_OVERFLOW | FE_INEXACT;
77 static const long double ldbl_eps = LDBL_EPSILON;
78 
79 
80 
81 ATF_TC_WITHOUT_HEAD(zeros);
ATF_TC_BODY(zeros,tc)82 ATF_TC_BODY(zeros, tc)
83 {
84 	long double ldbl_small;
85 
86 #ifndef	__OpenBSD__
87 #ifdef	__i386__
88 	fpsetprec(FP_PE);
89 #endif
90 #endif
91 	/*
92 	 * We can't use a compile-time constant here because gcc on
93 	 * FreeBSD/i386 assumes long doubles are truncated to the
94 	 * double format.
95 	 */
96 	ldbl_small = ldexpl(1.0, LDBL_MIN_EXP - LDBL_MANT_DIG);
97 
98 	/*
99 	 * Special cases involving zeroes.
100 	 */
101 #define	ztest(prec)							      \
102 	test##prec(copysign##prec(1.0, nextafter##prec(0.0, -0.0)), -1.0, 0); \
103 	test##prec(copysign##prec(1.0, nextafter##prec(-0.0, 0.0)), 1.0, 0);  \
104 	test##prec(copysign##prec(1.0, nexttoward##prec(0.0, -0.0)), -1.0, 0);\
105 	test##prec(copysign##prec(1.0, nexttoward##prec(-0.0, 0.0)), 1.0, 0)
106 
107 	ztest();
108 	ztest(f);
109 	ztest(l);
110 #undef	ztest
111 
112 #define	stest(next, eps, prec)					\
113 	test##prec(next(-0.0, 42.0), eps, ex_under);		\
114 	test##prec(next(0.0, -42.0), -eps, ex_under);		\
115 	test##prec(next(0.0, INFINITY), eps, ex_under);		\
116 	test##prec(next(-0.0, -INFINITY), -eps, ex_under)
117 
118 	stest(nextafter, 0x1p-1074, );
119 	stest(nextafterf, 0x1p-149f, f);
120 	stest(nextafterl, ldbl_small, l);
121 	stest(nexttoward, 0x1p-1074, );
122 	stest(nexttowardf, 0x1p-149f, f);
123 	stest(nexttowardl, ldbl_small, l);
124 #undef	stest
125 }
126 
127 ATF_TC_WITHOUT_HEAD(eq_and_nan);
ATF_TC_BODY(eq_and_nan,tc)128 ATF_TC_BODY(eq_and_nan, tc)
129 {
130 	/*
131 	 * `x == y' and NaN tests
132 	 */
133 	testall(42.0, 42.0, 42.0, 0);
134 	testall(-42.0, -42.0, -42.0, 0);
135 	testall(INFINITY, INFINITY, INFINITY, 0);
136 	testall(-INFINITY, -INFINITY, -INFINITY, 0);
137 	testall(NAN, 42.0, NAN, 0);
138 	testall(42.0, NAN, NAN, 0);
139 	testall(NAN, NAN, NAN, 0);
140 }
141 
142 ATF_TC_WITHOUT_HEAD(ordinary);
ATF_TC_BODY(ordinary,tc)143 ATF_TC_BODY(ordinary, tc)
144 {
145 	/*
146 	 * Tests where x is an ordinary normalized number
147 	 */
148 	testboth(1.0, 2.0, 1.0 + DBL_EPSILON, 0, );
149 	testboth(1.0, -INFINITY, 1.0 - DBL_EPSILON / 2, 0, );
150 	testboth(1.0, 2.0, 1.0 + FLT_EPSILON, 0, f);
151 	testboth(1.0, -INFINITY, 1.0 - FLT_EPSILON / 2, 0, f);
152 	testboth(1.0, 2.0, 1.0 + ldbl_eps, 0, l);
153 	testboth(1.0, -INFINITY, 1.0 - ldbl_eps / 2, 0, l);
154 
155 	testboth(-1.0, 2.0, -1.0 + DBL_EPSILON / 2, 0, );
156 	testboth(-1.0, -INFINITY, -1.0 - DBL_EPSILON, 0, );
157 	testboth(-1.0, 2.0, -1.0 + FLT_EPSILON / 2, 0, f);
158 	testboth(-1.0, -INFINITY, -1.0 - FLT_EPSILON, 0, f);
159 	testboth(-1.0, 2.0, -1.0 + ldbl_eps / 2, 0, l);
160 	testboth(-1.0, -INFINITY, -1.0 - ldbl_eps, 0, l);
161 
162 	/* Cases where nextafter(...) != nexttoward(...) */
163 	test(nexttoward(1.0, 1.0 + ldbl_eps), 1.0 + DBL_EPSILON, 0);
164 	testf(nexttowardf(1.0, 1.0 + ldbl_eps), 1.0 + FLT_EPSILON, 0);
165 	testl(nexttowardl(1.0, 1.0 + ldbl_eps), 1.0 + ldbl_eps, 0);
166 }
167 
168 ATF_TC_WITHOUT_HEAD(boundaries);
ATF_TC_BODY(boundaries,tc)169 ATF_TC_BODY(boundaries, tc)
170 {
171 	/*
172 	 * Tests at word boundaries, normalization boundaries, etc.
173 	 */
174 	testboth(0x1.87654ffffffffp+0, INFINITY, 0x1.87655p+0, 0, );
175 	testboth(0x1.87655p+0, -INFINITY, 0x1.87654ffffffffp+0, 0, );
176 	testboth(0x1.fffffffffffffp+0, INFINITY, 0x1p1, 0, );
177 	testboth(0x1p1, -INFINITY, 0x1.fffffffffffffp+0, 0, );
178 	testboth(0x0.fffffffffffffp-1022, INFINITY, 0x1p-1022, 0, );
179 	testboth(0x1p-1022, -INFINITY, 0x0.fffffffffffffp-1022, ex_under, );
180 
181 	testboth(0x1.fffffep0f, INFINITY, 0x1p1, 0, f);
182 	testboth(0x1p1, -INFINITY, 0x1.fffffep0f, 0, f);
183 	testboth(0x0.fffffep-126f, INFINITY, 0x1p-126f, 0, f);
184 	testboth(0x1p-126f, -INFINITY, 0x0.fffffep-126f, ex_under, f);
185 
186 #if LDBL_MANT_DIG == 53
187 	testboth(0x1.87654ffffffffp+0L, INFINITY, 0x1.87655p+0L, 0, l);
188 	testboth(0x1.87655p+0L, -INFINITY, 0x1.87654ffffffffp+0L, 0, l);
189 	testboth(0x1.fffffffffffffp+0L, INFINITY, 0x1p1L, 0, l);
190 	testboth(0x1p1L, -INFINITY, 0x1.fffffffffffffp+0L, 0, l);
191 	testboth(0x0.fffffffffffffp-1022L, INFINITY, 0x1p-1022L, 0, l);
192 	testboth(0x1p-1022L, -INFINITY, 0x0.fffffffffffffp-1022L, ex_under, l);
193 #elif LDBL_MANT_DIG == 64 && !defined(__i386)
194 	testboth(0x1.87654321fffffffep+0L, INFINITY, 0x1.87654322p+0L, 0, l);
195 	testboth(0x1.87654322p+0L, -INFINITY, 0x1.87654321fffffffep+0L, 0, l);
196 	testboth(0x1.fffffffffffffffep0L, INFINITY, 0x1p1L, 0, l);
197 	testboth(0x1p1L, -INFINITY, 0x1.fffffffffffffffep0L, 0, l);
198 	testboth(0x0.fffffffffffffffep-16382L, INFINITY, 0x1p-16382L, 0, l);
199 	testboth(0x1p-16382L, -INFINITY,
200 	    0x0.fffffffffffffffep-16382L, ex_under, l);
201 #elif LDBL_MANT_DIG == 113
202 	testboth(0x1.876543210987ffffffffffffffffp+0L, INFINITY,
203 	    0x1.876543210988p+0, 0, l);
204 	testboth(0x1.876543210988p+0L, -INFINITY,
205 	    0x1.876543210987ffffffffffffffffp+0L, 0, l);
206 	testboth(0x1.ffffffffffffffffffffffffffffp0L, INFINITY, 0x1p1L, 0, l);
207 	testboth(0x1p1L, -INFINITY, 0x1.ffffffffffffffffffffffffffffp0L, 0, l);
208 	testboth(0x0.ffffffffffffffffffffffffffffp-16382L, INFINITY,
209 	    0x1p-16382L, 0, l);
210 	testboth(0x1p-16382L, -INFINITY,
211 	    0x0.ffffffffffffffffffffffffffffp-16382L, ex_under, l);
212 #endif
213 }
214 
215 ATF_TC_WITHOUT_HEAD(overflow);
ATF_TC_BODY(overflow,tc)216 ATF_TC_BODY(overflow, tc)
217 {
218 	long double ldbl_max;
219 	/*
220 	 * We can't use a compile-time constant here because gcc on
221 	 * FreeBSD/i386 assumes long doubles are truncated to the
222 	 * double format.
223 	 */
224 	ldbl_max = ldexpl(1.0 - ldbl_eps / 2, LDBL_MAX_EXP);
225 
226 	/*
227 	 * Overflow tests
228 	 */
229 	test(idd(nextafter(DBL_MAX, INFINITY)), INFINITY, ex_over);
230 	test(idd(nextafter(INFINITY, 0.0)), DBL_MAX, 0);
231 	test(idd(nexttoward(DBL_MAX, DBL_MAX * 2.0L)), INFINITY, ex_over);
232 #if LDBL_MANT_DIG > 53
233 	test(idd(nexttoward(INFINITY, DBL_MAX * 2.0L)), DBL_MAX, 0);
234 #endif
235 
236 	testf(idf(nextafterf(FLT_MAX, INFINITY)), INFINITY, ex_over);
237 	testf(idf(nextafterf(INFINITY, 0.0)), FLT_MAX, 0);
238 	testf(idf(nexttowardf(FLT_MAX, FLT_MAX * 2.0)), INFINITY, ex_over);
239 	testf(idf(nexttowardf(INFINITY, FLT_MAX * 2.0)), FLT_MAX, 0);
240 
241 	testboth(ldbl_max, INFINITY, INFINITY, ex_over, l);
242 	testboth(INFINITY, 0.0, ldbl_max, 0, l);
243 }
244 
245 static void
_testl(const char * exp,int line,long double actual,long double expected,int except)246 _testl(const char *exp, int line, long double actual, long double expected,
247     int except)
248 {
249 	int actual_except;
250 
251 	actual_except = fetestexcept(ALL_STD_EXCEPT);
252 	if (!fpequal_cs(actual, expected, true)) {
253 		atf_tc_fail_check(__FILE__, line,
254 		    "%s returned %La, expecting %La\n", exp, actual, expected);
255 	}
256 	if (actual_except != except) {
257 		atf_tc_fail_check(__FILE__, line,
258 		    "%s raised 0x%x, expecting 0x%x\n", exp, actual_except,
259 		    except);
260 	}
261 }
262 
263 /*
264  * The idd() and idf() routines ensure that doubles and floats are
265  * converted to their respective types instead of stored in the FPU
266  * with extra precision.
267  */
268 static double
idd(double x)269 idd(double x)
270 {
271 	return (x);
272 }
273 
274 static float
idf(float x)275 idf(float x)
276 {
277 	return (x);
278 }
279 
ATF_TP_ADD_TCS(tp)280 ATF_TP_ADD_TCS(tp)
281 {
282 	ATF_TP_ADD_TC(tp, zeros);
283 	ATF_TP_ADD_TC(tp, ordinary);
284 	ATF_TP_ADD_TC(tp, eq_and_nan);
285 	ATF_TP_ADD_TC(tp, boundaries);
286 	ATF_TP_ADD_TC(tp, overflow);
287 
288 	return (atf_no_error());
289 }
290