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