xref: /openbsd-src/regress/lib/libm/msun/nearbyint_test.c (revision 49a6e16f2c2c8e509184b1f777366d1a6f337e1c)
1 /*	$OpenBSD: nearbyint_test.c,v 1.2 2021/12/13 16:56:48 deraadt Exp $	*/
2 /*-
3  * Copyright (c) 2010 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  * Tests for nearbyint{,f,l}()
32  *
33  * TODO:
34  * - adapt tests for rint(3)
35  * - tests for harder values (more mantissa bits than float)
36  */
37 
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 
41 #include <sys/types.h>
42 #include <fenv.h>
43 #include <math.h>
44 #include <stdio.h>
45 
46 #include "test-utils.h"
47 
48 static const int rmodes[] = {
49 	FE_TONEAREST, FE_DOWNWARD, FE_UPWARD, FE_TOWARDZERO,
50 };
51 
52 /* Make sure we're testing the library, not some broken compiler built-ins. */
53 static double (*libnearbyint)(double) = nearbyint;
54 static float (*libnearbyintf)(float) = nearbyintf;
55 static long double (*libnearbyintl)(long double) = nearbyintl;
56 #define nearbyintf libnearbyintf
57 #define nearbyint libnearbyint
58 #define nearbyintl libnearbyintl
59 
60 static const struct {
61 	float in;
62 	float out[3];	/* one answer per rounding mode except towardzero */
63 } tests[] = {
64 /* input	output (expected) */
65     { 0.0,	{ 0.0, 0.0, 0.0 }},
66     { 0.5,	{ 0.0, 0.0, 1.0 }},
67     { M_PI,	{ 3.0, 3.0, 4.0 }},
68     { 65536.5,	{ 65536, 65536, 65537 }},
69     { INFINITY,	{ INFINITY, INFINITY, INFINITY }},
70     { NAN,	{ NAN, NAN, NAN }},
71 };
72 
73 /* Get the appropriate result for the current rounding mode. */
74 static float
75 get_output(int testindex, int rmodeindex, int negative)
76 {
77 	double out;
78 
79 	if (negative) {	/* swap downwards and upwards if input is negative */
80 		if (rmodeindex == 1)
81 			rmodeindex = 2;
82 		else if (rmodeindex == 2)
83 			rmodeindex = 1;
84 	}
85 	if (rmodeindex == 3) /* FE_TOWARDZERO uses the value for downwards */
86 		rmodeindex = 1;
87 	out = tests[testindex].out[rmodeindex];
88 	return (negative ? -out : out);
89 }
90 
91 static void
92 test_nearby(int testindex)
93 {
94 	float in, out;
95 	unsigned i;
96 
97 	for (i = 0; i < sizeof(rmodes) / sizeof(rmodes[0]); i++) {
98 		ATF_REQUIRE_EQ(0, fesetround(rmodes[i]));
99 		ATF_REQUIRE_EQ(0, feclearexcept(ALL_STD_EXCEPT));
100 
101 		in = tests[testindex].in;
102 		out = get_output(testindex, i, 0);
103 		CHECK_FPEQUAL(out, libnearbyintf(in));
104 		CHECK_FPEQUAL(out, nearbyint(in));
105 		CHECK_FPEQUAL(out, nearbyintl(in));
106 		CHECK_FP_EXCEPTIONS(0, ALL_STD_EXCEPT);
107 
108 		in = -tests[testindex].in;
109 		out = get_output(testindex, i, 1);
110 		CHECK_FPEQUAL(out, nearbyintf(in));
111 		CHECK_FPEQUAL(out, nearbyint(in));
112 		CHECK_FPEQUAL(out, nearbyintl(in));
113 		CHECK_FP_EXCEPTIONS(0, ALL_STD_EXCEPT);
114 	}
115 }
116 
117 static void
118 test_modf(int testindex)
119 {
120 	float in, out;
121 	float ipartf, ipart_expected;
122 	double ipart;
123 	long double ipartl;
124 	unsigned i;
125 
126 	for (i = 0; i < sizeof(rmodes) / sizeof(rmodes[0]); i++) {
127 		ATF_REQUIRE_EQ(0, fesetround(rmodes[i]));
128 		ATF_REQUIRE_EQ(0, feclearexcept(ALL_STD_EXCEPT));
129 
130 		in = tests[testindex].in;
131 		ipart_expected = tests[testindex].out[1];
132 		out = copysignf(
133 		    isinf(ipart_expected) ? 0.0 : in - ipart_expected, in);
134 		ipartl = ipart = ipartf = 42.0;
135 
136 		CHECK_FPEQUAL(out, modff(in, &ipartf));
137 		CHECK_FPEQUAL(ipart_expected, ipartf);
138 		CHECK_FPEQUAL(out, modf(in, &ipart));
139 		CHECK_FPEQUAL(ipart_expected, ipart);
140 		CHECK_FPEQUAL(out, modfl(in, &ipartl));
141 		CHECK_FPEQUAL(ipart_expected, ipartl);
142 		CHECK_FP_EXCEPTIONS(0, ALL_STD_EXCEPT);
143 
144 		in = -in;
145 		ipart_expected = -ipart_expected;
146 		out = -out;
147 		ipartl = ipart = ipartf = 42.0;
148 		CHECK_FPEQUAL(out, modff(in, &ipartf));
149 		CHECK_FPEQUAL(ipart_expected, ipartf);
150 		CHECK_FPEQUAL(out, modf(in, &ipart));
151 		CHECK_FPEQUAL(ipart_expected, ipart);
152 		CHECK_FPEQUAL(out, modfl(in, &ipartl));
153 		CHECK_FPEQUAL(ipart_expected, ipartl);
154 		CHECK_FP_EXCEPTIONS(0, ALL_STD_EXCEPT);
155 	}
156 }
157 
158 ATF_TC_WITHOUT_HEAD(nearbyint);
159 ATF_TC_BODY(nearbyint, tc)
160 {
161 	unsigned i;
162 
163 	for (i = 0; i < nitems(tests); i++) {
164 		test_nearby(i);
165 		test_modf(i);
166 	}
167 }
168 
169 ATF_TP_ADD_TCS(tp)
170 {
171 	ATF_TP_ADD_TC(tp, nearbyint);
172 
173 	return (atf_no_error());
174 }
175