xref: /freebsd/lib/msun/tests/nearbyint_test.c (revision 1c05a6ea6b849ff95e539c31adea887c644a6a01)
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 <assert.h>
40 #include <fenv.h>
41 #include <math.h>
42 #include <stdio.h>
43 
44 #include "test-utils.h"
45 
46 static int testnum;
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 		fesetround(rmodes[i]);
99 		feclearexcept(ALL_STD_EXCEPT);
100 
101 		in = tests[testindex].in;
102 		out = get_output(testindex, i, 0);
103 		assert(fpequal(out, libnearbyintf(in)));
104 		assert(fpequal(out, nearbyint(in)));
105 		assert(fpequal(out, nearbyintl(in)));
106 		assert(fetestexcept(ALL_STD_EXCEPT) == 0);
107 
108 		in = -tests[testindex].in;
109 		out = get_output(testindex, i, 1);
110 		assert(fpequal(out, nearbyintf(in)));
111 		assert(fpequal(out, nearbyint(in)));
112 		assert(fpequal(out, nearbyintl(in)));
113 		assert(fetestexcept(ALL_STD_EXCEPT) == 0);
114 	}
115 
116 	printf("ok %d\t\t# nearbyint(+%g)\n", testnum++, in);
117 }
118 
119 static void
120 test_modf(int testindex)
121 {
122 	float in, out;
123 	float ipartf, ipart_expected;
124 	double ipart;
125 	long double ipartl;
126 	unsigned i;
127 
128 	for (i = 0; i < sizeof(rmodes) / sizeof(rmodes[0]); i++) {
129 		fesetround(rmodes[i]);
130 		feclearexcept(ALL_STD_EXCEPT);
131 
132 		in = tests[testindex].in;
133 		ipart_expected = tests[testindex].out[1];
134 		out = copysignf(
135 		    isinf(ipart_expected) ? 0.0 : in - ipart_expected, in);
136 		ipartl = ipart = ipartf = 42.0;
137 
138 		assert(fpequal(out, modff(in, &ipartf)));
139 		assert(fpequal(ipart_expected, ipartf));
140 		assert(fpequal(out, modf(in, &ipart)));
141 		assert(fpequal(ipart_expected, ipart));
142 		assert(fpequal(out, modfl(in, &ipartl)));
143 		assert(fpequal(ipart_expected, ipartl));
144 		assert(fetestexcept(ALL_STD_EXCEPT) == 0);
145 
146 		in = -in;
147 		ipart_expected = -ipart_expected;
148 		out = -out;
149 		ipartl = ipart = ipartf = 42.0;
150 		assert(fpequal(out, modff(in, &ipartf)));
151 		assert(fpequal(ipart_expected, ipartf));
152 		assert(fpequal(out, modf(in, &ipart)));
153 		assert(fpequal(ipart_expected, ipart));
154 		assert(fpequal(out, modfl(in, &ipartl)));
155 		assert(fpequal(ipart_expected, ipartl));
156 		assert(fetestexcept(ALL_STD_EXCEPT) == 0);
157 	}
158 
159 	printf("ok %d\t\t# modf(+%g)\n", testnum++, in);
160 }
161 
162 int
163 main(void)
164 {
165 	unsigned i;
166 
167 	printf("1..%zu\n", (size_t)(nitems(tests) * 2));
168 	testnum = 1;
169 	for (i = 0; i < nitems(tests); i++) {
170 		test_nearby(i);
171 		test_modf(i);
172 	}
173 
174 	return (0);
175 }
176