usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-floorf2.c
blobd6166ea7bd2ca4a7c98b03354d881266d8d9b011
1 /* Test of rounding towards negative infinity.
2 Copyright (C) 2007-2024 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 <https://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. */
22 #include <config.h>
24 #include <math.h>
26 #include <float.h>
27 #include <stdint.h>
28 #include <stdio.h>
30 #include "isnanf-nolibm.h"
31 #include "macros.h"
33 /* MSVC with option -fp:strict refuses to compile constant initializers that
34 contain floating-point operations. Pacify this compiler. */
35 #if defined _MSC_VER && !defined __clang__
36 # pragma fenv_access (off)
37 #endif
40 /* The reference implementation, taken from lib/floor.c. */
42 #define DOUBLE float
43 #define MANT_DIG FLT_MANT_DIG
44 #define L_(literal) literal##f
46 /* 2^(MANT_DIG-1). */
47 static const DOUBLE TWO_MANT_DIG =
48 /* Assume MANT_DIG <= 5 * 31.
49 Use the identity
50 n = floor(n/5) + floor((n+1)/5) + ... + floor((n+4)/5). */
51 (DOUBLE) (1U << ((MANT_DIG - 1) / 5))
52 * (DOUBLE) (1U << ((MANT_DIG - 1 + 1) / 5))
53 * (DOUBLE) (1U << ((MANT_DIG - 1 + 2) / 5))
54 * (DOUBLE) (1U << ((MANT_DIG - 1 + 3) / 5))
55 * (DOUBLE) (1U << ((MANT_DIG - 1 + 4) / 5));
57 DOUBLE
58 floorf_reference (DOUBLE x)
60 /* The use of 'volatile' guarantees that excess precision bits are dropped
61 at each addition step and before the following comparison at the caller's
62 site. It is necessary on x86 systems where double-floats are not IEEE
63 compliant by default, to avoid that the results become platform and compiler
64 option dependent. 'volatile' is a portable alternative to gcc's
65 -ffloat-store option. */
66 volatile DOUBLE y = x;
67 volatile DOUBLE z = y;
69 if (z > L_(0.0))
71 /* For 0 < x < 1, return +0.0 even if the current rounding mode is
72 FE_DOWNWARD. */
73 if (z < L_(1.0))
74 z = L_(0.0);
75 /* Avoid rounding errors for values near 2^k, where k >= MANT_DIG-1. */
76 else if (z < TWO_MANT_DIG)
78 /* Round to the next integer (nearest or up or down, doesn't matter). */
79 z += TWO_MANT_DIG;
80 z -= TWO_MANT_DIG;
81 /* Enforce rounding down. */
82 if (z > y)
83 z -= L_(1.0);
86 else if (z < L_(0.0))
88 /* Work around ICC's desire to optimize denormal floats to 0. */
89 if (z > -FLT_MIN)
90 return L_(-1.0);
91 /* Avoid rounding errors for values near -2^k, where k >= MANT_DIG-1. */
92 if (z > - TWO_MANT_DIG)
94 /* Round to the next integer (nearest or up or down, doesn't matter). */
95 z -= TWO_MANT_DIG;
96 z += TWO_MANT_DIG;
97 /* Enforce rounding down. */
98 if (z > y)
99 z -= L_(1.0);
102 return z;
106 /* Test for equality. */
107 static int
108 equal (DOUBLE x, DOUBLE y)
110 return (isnanf (x) ? isnanf (y) : x == y);
113 /* Test whether the result for a given argument is correct. */
114 static bool
115 correct_result_p (DOUBLE x, DOUBLE result)
117 return
118 (x < 0 && x >= -1 ? result == - L_(1.0) :
119 x - 1 < x ? result <= x && result >= x - 1 && x - result < 1 :
120 equal (result, x));
123 /* Test the function for a given argument. */
124 static int
125 check (float x)
127 /* If the reference implementation is incorrect, bail out immediately. */
128 float reference = floorf_reference (x);
129 ASSERT (correct_result_p (x, reference));
130 /* If the actual implementation is wrong, return an error code. */
132 float result = floorf (x);
133 if (correct_result_p (x, result))
134 return 0;
135 else
137 #if GNULIB_TEST_FPRINTF_POSIX
138 fprintf (stderr, "floorf %g(%a) = %g(%a) or %g(%a)?\n",
139 x, x, reference, reference, result, result);
140 #endif
141 return 1;
146 #define NUM_HIGHBITS 12
147 #define NUM_LOWBITS 4
150 main ()
152 unsigned int highbits;
153 unsigned int lowbits;
154 int error = 0;
155 for (highbits = 0; highbits < (1 << NUM_HIGHBITS); highbits++)
156 for (lowbits = 0; lowbits < (1 << NUM_LOWBITS); lowbits++)
158 /* Combine highbits and lowbits into a floating-point number,
159 sign-extending the lowbits to 32-NUM_HIGHBITS bits. */
160 union { float f; uint32_t i; } janus;
161 janus.i = ((uint32_t) highbits << (32 - NUM_HIGHBITS))
162 | ((uint32_t) ((int32_t) ((uint32_t) lowbits << (32 - NUM_LOWBITS))
163 >> (32 - NUM_LOWBITS - NUM_HIGHBITS))
164 >> NUM_HIGHBITS);
165 error |= check (janus.f);
167 return (error ? 1 : test_exit_status);