1 /* Test of rounding towards positive infinity.
2 Copyright (C) 2007-2011 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. */
19 /* When this test fails on some platform, build it together with the gnulib
20 module 'fprintf-posix' for optimal debugging output. */
31 #include "isnanf-nolibm.h"
32 #include "minus-zero.h"
36 /* The reference implementation, taken from lib/ceil.c. */
39 #define MANT_DIG FLT_MANT_DIG
40 #define L_(literal) literal##f
42 /* -0.0. See minus-zero.h. */
43 #define MINUS_ZERO minus_zerof
46 static const DOUBLE TWO_MANT_DIG =
47 /* Assume MANT_DIG <= 5 * 31.
49 n = floor(n/5) + floor((n+1)/5) + ... + floor((n+4)/5). */
50 (DOUBLE) (1U << ((MANT_DIG - 1) / 5))
51 * (DOUBLE) (1U << ((MANT_DIG - 1 + 1) / 5))
52 * (DOUBLE) (1U << ((MANT_DIG - 1 + 2) / 5))
53 * (DOUBLE) (1U << ((MANT_DIG - 1 + 3) / 5))
54 * (DOUBLE) (1U << ((MANT_DIG - 1 + 4) / 5));
57 ceilf_reference (DOUBLE x)
59 /* The use of 'volatile' guarantees that excess precision bits are dropped
60 at each addition step and before the following comparison at the caller's
61 site. It is necessary on x86 systems where double-floats are not IEEE
62 compliant by default, to avoid that the results become platform and compiler
63 option dependent. 'volatile' is a portable alternative to gcc's
64 -ffloat-store option. */
65 volatile DOUBLE y = x;
66 volatile DOUBLE z = y;
70 /* Work around ICC's desire to optimize denormal floats to 0. */
73 /* Avoid rounding errors for values near 2^k, where k >= MANT_DIG-1. */
76 /* Round to the next integer (nearest or up or down, doesn't matter). */
79 /* Enforce rounding up. */
86 /* For -1 < x < 0, return -0.0 regardless of the current rounding
90 /* Avoid rounding errors for values near -2^k, where k >= MANT_DIG-1. */
91 else if (z > - TWO_MANT_DIG)
93 /* Round to the next integer (nearest or up or down, doesn't matter). */
96 /* Enforce rounding up. */
105 /* Test for equality. */
107 equal (DOUBLE x, DOUBLE y)
109 return (isnanf (x) ? isnanf (y) : x == y);
112 /* Test whether the result for a given argument is correct. */
114 correct_result_p (DOUBLE x, DOUBLE result)
117 (x > 0 && x <= 1 ? result == L_(1.0) :
118 x + 1 > x ? result >= x && result <= x + 1 && result - x < 1 :
122 /* Test the function for a given argument. */
126 /* If the reference implementation is incorrect, bail out immediately. */
127 float reference = ceilf_reference (x);
128 ASSERT (correct_result_p (x, reference));
129 /* If the actual implementation is wrong, return an error code. */
131 float result = ceilf (x);
132 if (correct_result_p (x, result))
136 #if GNULIB_TEST_FPRINTF_POSIX
137 fprintf (stderr, "ceilf %g(%a) = %g(%a) or %g(%a)?\n",
138 x, x, reference, reference, result, result);
145 #define NUM_HIGHBITS 12
146 #define NUM_LOWBITS 4
151 unsigned int highbits;
152 unsigned int lowbits;
154 for (highbits = 0; highbits < (1 << NUM_HIGHBITS); highbits++)
155 for (lowbits = 0; lowbits < (1 << NUM_LOWBITS); lowbits++)
157 /* Combine highbits and lowbits into a floating-point number,
158 sign-extending the lowbits to 32-NUM_HIGHBITS bits. */
159 union { float f; uint32_t i; } janus;
160 janus.i = ((uint32_t) highbits << (32 - NUM_HIGHBITS))
161 | ((uint32_t) ((int32_t) ((uint32_t) lowbits << (32 - NUM_LOWBITS))
162 >> (32 - NUM_LOWBITS - NUM_HIGHBITS))
164 error |= check (janus.f);
166 return (error ? 1 : 0);