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