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