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