1 /* Test of rounding towards zero.
2 Copyright (C) 2007-2008 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
31 #define ASSERT(expr) \
36 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
43 /* The reference implementation, taken from lib/trunc.c. */
46 #define MANT_DIG DBL_MANT_DIG
47 #define L_(literal) literal
50 static const DOUBLE TWO_MANT_DIG =
51 /* Assume MANT_DIG <= 5 * 31.
53 n = floor(n/5) + floor((n+1)/5) + ... + floor((n+4)/5). */
54 (DOUBLE) (1U << ((MANT_DIG - 1) / 5))
55 * (DOUBLE) (1U << ((MANT_DIG - 1 + 1) / 5))
56 * (DOUBLE) (1U << ((MANT_DIG - 1 + 2) / 5))
57 * (DOUBLE) (1U << ((MANT_DIG - 1 + 3) / 5))
58 * (DOUBLE) (1U << ((MANT_DIG - 1 + 4) / 5));
61 trunc_reference (DOUBLE x)
63 /* The use of 'volatile' guarantees that excess precision bits are dropped
64 at each addition step and before the following comparison at the caller's
65 site. It is necessary on x86 systems where double-floats are not IEEE
66 compliant by default, to avoid that the results become platform and compiler
67 option dependent. 'volatile' is a portable alternative to gcc's
68 -ffloat-store option. */
69 volatile DOUBLE y = x;
70 volatile DOUBLE z = y;
74 /* Avoid rounding errors for values near 2^k, where k >= MANT_DIG-1. */
77 /* Round to the next integer (nearest or up or down, doesn't matter). */
80 /* Enforce rounding down. */
87 /* Avoid rounding errors for values near -2^k, where k >= MANT_DIG-1. */
88 if (z > - TWO_MANT_DIG)
90 /* Round to the next integer (nearest or up or down, doesn't matter). */
93 /* Enforce rounding up. */
102 /* Test for equality. */
104 equal (DOUBLE x, DOUBLE y)
106 return (isnand (x) ? isnand (y) : x == y);
109 /* Test whether the result for a given argument is correct. */
111 correct_result_p (DOUBLE x, DOUBLE result)
115 ? (x < 1 ? result == L_(0.0) :
116 x - 1 < x ? result <= x && result >= x - 1 && x - result < 1 :
118 : (x > -1 ? result == L_(0.0) :
119 x + 1 > x ? result >= x && result <= x + 1 && result - x < 1 :
123 /* Test the function for a given argument. */
127 /* If the reference implementation is incorrect, bail out immediately. */
128 double reference = trunc_reference (x);
129 ASSERT (correct_result_p (x, reference));
130 /* If the actual implementation is wrong, return an error code. */
132 double result = trunc (x);
133 if (correct_result_p (x, result))
137 fprintf (stderr, "trunc %g(%a) = %g(%a) or %g(%a)?\n",
138 x, x, reference, reference, result, result);
144 #define NUM_HIGHBITS 13
145 #define NUM_LOWBITS 4
150 unsigned int highbits;
151 unsigned int lowbits;
153 for (highbits = 0; highbits < (1 << NUM_HIGHBITS); highbits++)
154 for (lowbits = 0; lowbits < (1 << NUM_LOWBITS); lowbits++)
156 /* Combine highbits and lowbits into a floating-point number,
157 sign-extending the lowbits to 32-NUM_HIGHBITS bits. */
158 union { double f; uint64_t i; } janus;
159 janus.i = ((uint64_t) highbits << (64 - NUM_HIGHBITS))
160 | ((uint64_t) ((int64_t) ((uint64_t) lowbits << (64 - NUM_LOWBITS))
161 >> (64 - NUM_LOWBITS - NUM_HIGHBITS))
163 error |= check (janus.f);
165 return (error ? 1 : 0);