openat: don’t close (-1)
[gnulib.git] / tests / test-ceilf2.c
blob0892cbf4beae24ffccfdf04bcddb4378b452dff6
1 /* Test of rounding towards positive 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 "minus-zero.h"
32 #include "macros.h"
34 /* MSVC with option -fp:strict refuses to compile constant initializers that
35 contain floating-point operations. Pacify this compiler. */
36 #if defined _MSC_VER && !defined __clang__
37 # pragma fenv_access (off)
38 #endif
41 /* The reference implementation, taken from lib/ceil.c. */
43 #define DOUBLE float
44 #define MANT_DIG FLT_MANT_DIG
45 #define L_(literal) literal##f
47 /* -0.0. See minus-zero.h. */
48 #define MINUS_ZERO minus_zerof
50 /* 2^(MANT_DIG-1). */
51 static const DOUBLE TWO_MANT_DIG =
52 /* Assume MANT_DIG <= 5 * 31.
53 Use the identity
54 n = floor(n/5) + floor((n+1)/5) + ... + floor((n+4)/5). */
55 (DOUBLE) (1U << ((MANT_DIG - 1) / 5))
56 * (DOUBLE) (1U << ((MANT_DIG - 1 + 1) / 5))
57 * (DOUBLE) (1U << ((MANT_DIG - 1 + 2) / 5))
58 * (DOUBLE) (1U << ((MANT_DIG - 1 + 3) / 5))
59 * (DOUBLE) (1U << ((MANT_DIG - 1 + 4) / 5));
61 DOUBLE
62 ceilf_reference (DOUBLE x)
64 /* The use of 'volatile' guarantees that excess precision bits are dropped
65 at each addition step and before the following comparison at the caller's
66 site. It is necessary on x86 systems where double-floats are not IEEE
67 compliant by default, to avoid that the results become platform and compiler
68 option dependent. 'volatile' is a portable alternative to gcc's
69 -ffloat-store option. */
70 volatile DOUBLE y = x;
71 volatile DOUBLE z = y;
73 if (z > L_(0.0))
75 /* Work around ICC's desire to optimize denormal floats to 0. */
76 if (z < FLT_MIN)
77 return L_(1.0);
78 /* Avoid rounding errors for values near 2^k, where k >= MANT_DIG-1. */
79 if (z < TWO_MANT_DIG)
81 /* Round to the next integer (nearest or up or down, doesn't matter). */
82 z += TWO_MANT_DIG;
83 z -= TWO_MANT_DIG;
84 /* Enforce rounding up. */
85 if (z < y)
86 z += L_(1.0);
89 else if (z < L_(0.0))
91 /* For -1 < x < 0, return -0.0 regardless of the current rounding
92 mode. */
93 if (z > L_(-1.0))
94 z = MINUS_ZERO;
95 /* Avoid rounding errors for values near -2^k, where k >= MANT_DIG-1. */
96 else if (z > - TWO_MANT_DIG)
98 /* Round to the next integer (nearest or up or down, doesn't matter). */
99 z -= TWO_MANT_DIG;
100 z += TWO_MANT_DIG;
101 /* Enforce rounding up. */
102 if (z < y)
103 z += L_(1.0);
106 return z;
110 /* Test for equality. */
111 static int
112 equal (DOUBLE x, DOUBLE y)
114 return (isnanf (x) ? isnanf (y) : x == y);
117 /* Test whether the result for a given argument is correct. */
118 static bool
119 correct_result_p (DOUBLE x, DOUBLE result)
121 return
122 (x > 0 && x <= 1 ? result == L_(1.0) :
123 x + 1 > x ? result >= x && result <= x + 1 && result - x < 1 :
124 equal (result, x));
127 /* Test the function for a given argument. */
128 static int
129 check (float x)
131 /* If the reference implementation is incorrect, bail out immediately. */
132 float reference = ceilf_reference (x);
133 ASSERT (correct_result_p (x, reference));
134 /* If the actual implementation is wrong, return an error code. */
136 float result = ceilf (x);
137 if (correct_result_p (x, result))
138 return 0;
139 else
141 #if GNULIB_TEST_FPRINTF_POSIX
142 fprintf (stderr, "ceilf %g(%a) = %g(%a) or %g(%a)?\n",
143 x, x, reference, reference, result, result);
144 #endif
145 return 1;
150 #define NUM_HIGHBITS 12
151 #define NUM_LOWBITS 4
154 main ()
156 unsigned int highbits;
157 unsigned int lowbits;
158 int error = 0;
159 for (highbits = 0; highbits < (1 << NUM_HIGHBITS); highbits++)
160 for (lowbits = 0; lowbits < (1 << NUM_LOWBITS); lowbits++)
162 /* Combine highbits and lowbits into a floating-point number,
163 sign-extending the lowbits to 32-NUM_HIGHBITS bits. */
164 union { float f; uint32_t i; } janus;
165 janus.i = ((uint32_t) highbits << (32 - NUM_HIGHBITS))
166 | ((uint32_t) ((int32_t) ((uint32_t) lowbits << (32 - NUM_LOWBITS))
167 >> (32 - NUM_LOWBITS - NUM_HIGHBITS))
168 >> NUM_HIGHBITS);
169 error |= check (janus.f);
171 return (error ? 1 : test_exit_status);