1 /* Test of rounding towards negative infinity.
2 Copyright (C) 2007-2010 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. */
28 #include "isnanf-nolibm.h"
32 /* The reference implementation, taken from lib/floor.c. */
35 #define MANT_DIG FLT_MANT_DIG
36 #define L_(literal) literal##f
39 static const DOUBLE TWO_MANT_DIG =
40 /* Assume MANT_DIG <= 5 * 31.
42 n = floor(n/5) + floor((n+1)/5) + ... + floor((n+4)/5). */
43 (DOUBLE) (1U << ((MANT_DIG - 1) / 5))
44 * (DOUBLE) (1U << ((MANT_DIG - 1 + 1) / 5))
45 * (DOUBLE) (1U << ((MANT_DIG - 1 + 2) / 5))
46 * (DOUBLE) (1U << ((MANT_DIG - 1 + 3) / 5))
47 * (DOUBLE) (1U << ((MANT_DIG - 1 + 4) / 5));
50 floorf_reference (DOUBLE x)
52 /* The use of 'volatile' guarantees that excess precision bits are dropped
53 at each addition step and before the following comparison at the caller's
54 site. It is necessary on x86 systems where double-floats are not IEEE
55 compliant by default, to avoid that the results become platform and compiler
56 option dependent. 'volatile' is a portable alternative to gcc's
57 -ffloat-store option. */
58 volatile DOUBLE y = x;
59 volatile DOUBLE z = y;
63 /* Avoid rounding errors for values near 2^k, where k >= MANT_DIG-1. */
66 /* Round to the next integer (nearest or up or down, doesn't matter). */
69 /* Enforce rounding down. */
76 /* Avoid rounding errors for values near -2^k, where k >= MANT_DIG-1. */
77 if (z > - TWO_MANT_DIG)
79 /* Round to the next integer (nearest or up or down, doesn't matter). */
82 /* Enforce rounding down. */
91 /* Test for equality. */
93 equal (DOUBLE x, DOUBLE y)
95 return (isnanf (x) ? isnanf (y) : x == y);
98 /* Test whether the result for a given argument is correct. */
100 correct_result_p (DOUBLE x, DOUBLE result)
103 (x < 0 && x >= -1 ? result == - L_(1.0) :
104 x - 1 < x ? result <= x && result >= x - 1 && x - result < 1 :
108 /* Test the function for a given argument. */
112 /* If the reference implementation is incorrect, bail out immediately. */
113 float reference = floorf_reference (x);
114 ASSERT (correct_result_p (x, reference));
115 /* If the actual implementation is wrong, return an error code. */
117 float result = floorf (x);
118 if (correct_result_p (x, result))
122 fprintf (stderr, "floorf %g(%a) = %g(%a) or %g(%a)?\n",
123 x, x, reference, reference, result, result);
129 #define NUM_HIGHBITS 12
130 #define NUM_LOWBITS 4
135 unsigned int highbits;
136 unsigned int lowbits;
138 for (highbits = 0; highbits < (1 << NUM_HIGHBITS); highbits++)
139 for (lowbits = 0; lowbits < (1 << NUM_LOWBITS); lowbits++)
141 /* Combine highbits and lowbits into a floating-point number,
142 sign-extending the lowbits to 32-NUM_HIGHBITS bits. */
143 union { float f; uint32_t i; } janus;
144 janus.i = ((uint32_t) highbits << (32 - NUM_HIGHBITS))
145 | ((uint32_t) ((int32_t) ((uint32_t) lowbits << (32 - NUM_LOWBITS))
146 >> (32 - NUM_LOWBITS - NUM_HIGHBITS))
148 error |= check (janus.f);
150 return (error ? 1 : 0);