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