xref: /freebsd/lib/msun/tests/rem_test.c (revision 15c433351f54e7cd5bec8d36c8e89e6a7fa55b26)
1 /*-
2  * Copyright (c) 2005-2008 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 for remainder functions: remainder, remainderf, remainderl,
29  * remquo, remquof, and remquol.
30  * Missing tests: fmod, fmodf.
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include <assert.h>
37 #include <float.h>
38 #include <math.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <strings.h>
42 
43 static void test_invalid(long double, long double);
44 static void testl(long double, long double, long double, int);
45 static void testd(double, double, double, int);
46 static void testf(float, float, float, int);
47 
48 #define	test(x, y, e_r, e_q) do {	\
49 	testl(x, y, e_r, e_q);		\
50 	testd(x, y, e_r, e_q);		\
51 	testf(x, y, e_r, e_q);		\
52 } while (0)
53 
54 int
55 main(int argc, char *argv[])
56 {
57 
58 	printf("1..3\n");
59 
60 	test_invalid(0.0, 0.0);
61 	test_invalid(1.0, 0.0);
62 	test_invalid(INFINITY, 0.0);
63 	test_invalid(INFINITY, 1.0);
64 	test_invalid(-INFINITY, 1.0);
65 	test_invalid(NAN, 1.0);
66 	test_invalid(1.0, NAN);
67 
68 	test(4, 4, 0, 1);
69 	test(0, 3.0, 0, 0);
70 	testd(0x1p-1074, 1, 0x1p-1074, 0);
71 	testf(0x1p-149, 1, 0x1p-149, 0);
72 	test(3.0, 4, -1, 1);
73 	test(3.0, -4, -1, -1);
74 	testd(275 * 1193040, 275, 0, 1193040);
75 	test(4.5 * 7.5, 4.5, -2.25, 8);	/* we should get the even one */
76 	testf(0x1.9044f6p-1, 0x1.ce662ep-1, -0x1.f109cp-4, 1);
77 #if LDBL_MANT_DIG > 53
78 	testl(-0x1.23456789abcdefp-2000L, 0x1.fedcba987654321p-2000L,
79 	      0x1.b72ea61d950c862p-2001L, -1);
80 #endif
81 
82 	printf("ok 1 - rem\n");
83 
84 	/*
85 	 * The actual quotient here is 864062210.50000003..., but
86 	 * double-precision division gets -8.64062210.5, which rounds
87 	 * the wrong way.  This test ensures that remquo() is smart
88 	 * enough to get the low-order bit right.
89 	 */
90 	testd(-0x1.98260f22fc6dep-302, 0x1.fb3167c430a13p-332,
91 	    0x1.fb3165b82de72p-333, -864062211);
92 	/* Even harder cases with greater exponent separation */
93 	test(0x1.fp100, 0x1.ep-40, -0x1.cp-41, 143165577);
94 	testd(-0x1.abcdefp120, 0x1.87654321p-120,
95 	    -0x1.69c78ec4p-121, -63816414);
96 
97 	printf("ok 2 - rem\n");
98 
99 	test(0x1.66666cp+120, 0x1p+71, 0.0, 1476395008);
100 	testd(-0x1.0000000000003p+0, 0x1.0000000000003p+0, -0.0, -1);
101 	testl(-0x1.0000000000003p+0, 0x1.0000000000003p+0, -0.0, -1);
102 	testd(-0x1.0000000000001p-749, 0x1.4p-1072, 0x1p-1074, -1288490189);
103 	testl(-0x1.0000000000001p-749, 0x1.4p-1072, 0x1p-1074, -1288490189);
104 
105 	printf("ok 3 - rem\n");
106 
107 	return (0);
108 }
109 
110 static void
111 test_invalid(long double x, long double y)
112 {
113 	int q;
114 
115 	q = 0xdeadbeef;
116 
117 	assert(isnan(remainder(x, y)));
118 	assert(isnan(remquo(x, y, &q)));
119 #ifdef STRICT
120 	assert(q == 0xdeadbeef);
121 #endif
122 
123 	assert(isnan(remainderf(x, y)));
124 	assert(isnan(remquof(x, y, &q)));
125 #ifdef STRICT
126 	assert(q == 0xdeadbeef);
127 #endif
128 
129 	assert(isnan(remainderl(x, y)));
130 	assert(isnan(remquol(x, y, &q)));
131 #ifdef STRICT
132 	assert(q == 0xdeadbeef);
133 #endif
134 }
135 
136 /* 0x012345 ==> 0x01ffff */
137 static inline int
138 mask(int x)
139 {
140 	return ((unsigned)~0 >> (32 - fls(x)));
141 }
142 
143 static void
144 testl(long double x, long double y, long double expected_rem, int expected_quo)
145 {
146 	int q;
147 	long double rem;
148 
149 	q = random();
150 	rem = remainderl(x, y);
151 	assert(rem == expected_rem);
152 	assert(!signbit(rem) == !signbit(expected_rem));
153 	rem = remquol(x, y, &q);
154 	assert(rem == expected_rem);
155 	assert(!signbit(rem) == !signbit(expected_rem));
156 	assert((q ^ expected_quo) >= 0); /* sign(q) == sign(expected_quo) */
157 	assert((q & 0x7) == (expected_quo & 0x7));
158 	if (q != 0) {
159 		assert((q > 0) ^ !(expected_quo > 0));
160 		q = abs(q);
161 		assert(q == (abs(expected_quo) & mask(q)));
162 	}
163 }
164 
165 static void
166 testd(double x, double y, double expected_rem, int expected_quo)
167 {
168 	int q;
169 	double rem;
170 
171 	q = random();
172 	rem = remainder(x, y);
173 	assert(rem == expected_rem);
174 	assert(!signbit(rem) == !signbit(expected_rem));
175 	rem = remquo(x, y, &q);
176 	assert(rem == expected_rem);
177 	assert(!signbit(rem) == !signbit(expected_rem));
178 	assert((q ^ expected_quo) >= 0); /* sign(q) == sign(expected_quo) */
179 	assert((q & 0x7) == (expected_quo & 0x7));
180 	if (q != 0) {
181 		assert((q > 0) ^ !(expected_quo > 0));
182 		q = abs(q);
183 		assert(q == (abs(expected_quo) & mask(q)));
184 	}
185 }
186 
187 static void
188 testf(float x, float y, float expected_rem, int expected_quo)
189 {
190 	int q;
191 	float rem;
192 
193 	q = random();
194 	rem = remainderf(x, y);
195 	assert(rem == expected_rem);
196 	assert(!signbit(rem) == !signbit(expected_rem));
197 	rem = remquof(x, y, &q);
198 	assert(rem == expected_rem);
199 	assert(!signbit(rem) == !signbit(expected_rem));
200 	assert((q ^ expected_quo) >= 0); /* sign(q) == sign(expected_quo) */
201 	assert((q & 0x7) == (expected_quo & 0x7));
202 	if (q != 0) {
203 		assert((q > 0) ^ !(expected_quo > 0));
204 		q = abs(q);
205 		assert((q & mask(q)) == (abs(expected_quo) & mask(q)));
206 	}
207 }
208