c32snrtombs: Add tests.
[gnulib.git] / tests / test-floorf2.c
blob5d6c51ffca3288131fbc82443aafa0418fa29138
1 /* Test of rounding towards negative infinity.
2 Copyright (C) 2007-2020 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 <stdbool.h>
28 #include <stdint.h>
29 #include <stdio.h>
31 #include "isnanf-nolibm.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 #ifdef _MSC_VER
37 # pragma fenv_access (off)
38 #endif
41 /* The reference implementation, taken from lib/floor.c. */
43 #define DOUBLE float
44 #define MANT_DIG FLT_MANT_DIG
45 #define L_(literal) literal##f
47 /* 2^(MANT_DIG-1). */
48 static const DOUBLE TWO_MANT_DIG =
49 /* Assume MANT_DIG <= 5 * 31.
50 Use the identity
51 n = floor(n/5) + floor((n+1)/5) + ... + floor((n+4)/5). */
52 (DOUBLE) (1U << ((MANT_DIG - 1) / 5))
53 * (DOUBLE) (1U << ((MANT_DIG - 1 + 1) / 5))
54 * (DOUBLE) (1U << ((MANT_DIG - 1 + 2) / 5))
55 * (DOUBLE) (1U << ((MANT_DIG - 1 + 3) / 5))
56 * (DOUBLE) (1U << ((MANT_DIG - 1 + 4) / 5));
58 DOUBLE
59 floorf_reference (DOUBLE x)
61 /* The use of 'volatile' guarantees that excess precision bits are dropped
62 at each addition step and before the following comparison at the caller's
63 site. It is necessary on x86 systems where double-floats are not IEEE
64 compliant by default, to avoid that the results become platform and compiler
65 option dependent. 'volatile' is a portable alternative to gcc's
66 -ffloat-store option. */
67 volatile DOUBLE y = x;
68 volatile DOUBLE z = y;
70 if (z > L_(0.0))
72 /* For 0 < x < 1, return +0.0 even if the current rounding mode is
73 FE_DOWNWARD. */
74 if (z < L_(1.0))
75 z = L_(0.0);
76 /* Avoid rounding errors for values near 2^k, where k >= MANT_DIG-1. */
77 else if (z < TWO_MANT_DIG)
79 /* Round to the next integer (nearest or up or down, doesn't matter). */
80 z += TWO_MANT_DIG;
81 z -= TWO_MANT_DIG;
82 /* Enforce rounding down. */
83 if (z > y)
84 z -= L_(1.0);
87 else if (z < L_(0.0))
89 /* Work around ICC's desire to optimize denormal floats to 0. */
90 if (z > -FLT_MIN)
91 return L_(-1.0);
92 /* Avoid rounding errors for values near -2^k, where k >= MANT_DIG-1. */
93 if (z > - TWO_MANT_DIG)
95 /* Round to the next integer (nearest or up or down, doesn't matter). */
96 z -= TWO_MANT_DIG;
97 z += TWO_MANT_DIG;
98 /* Enforce rounding down. */
99 if (z > y)
100 z -= L_(1.0);
103 return z;
107 /* Test for equality. */
108 static int
109 equal (DOUBLE x, DOUBLE y)
111 return (isnanf (x) ? isnanf (y) : x == y);
114 /* Test whether the result for a given argument is correct. */
115 static bool
116 correct_result_p (DOUBLE x, DOUBLE result)
118 return
119 (x < 0 && x >= -1 ? result == - L_(1.0) :
120 x - 1 < x ? result <= x && result >= x - 1 && x - result < 1 :
121 equal (result, x));
124 /* Test the function for a given argument. */
125 static int
126 check (float x)
128 /* If the reference implementation is incorrect, bail out immediately. */
129 float reference = floorf_reference (x);
130 ASSERT (correct_result_p (x, reference));
131 /* If the actual implementation is wrong, return an error code. */
133 float result = floorf (x);
134 if (correct_result_p (x, result))
135 return 0;
136 else
138 #if GNULIB_TEST_FPRINTF_POSIX
139 fprintf (stderr, "floorf %g(%a) = %g(%a) or %g(%a)?\n",
140 x, x, reference, reference, result, result);
141 #endif
142 return 1;
147 #define NUM_HIGHBITS 12
148 #define NUM_LOWBITS 4
151 main ()
153 unsigned int highbits;
154 unsigned int lowbits;
155 int error = 0;
156 for (highbits = 0; highbits < (1 << NUM_HIGHBITS); highbits++)
157 for (lowbits = 0; lowbits < (1 << NUM_LOWBITS); lowbits++)
159 /* Combine highbits and lowbits into a floating-point number,
160 sign-extending the lowbits to 32-NUM_HIGHBITS bits. */
161 union { float f; uint32_t i; } janus;
162 janus.i = ((uint32_t) highbits << (32 - NUM_HIGHBITS))
163 | ((uint32_t) ((int32_t) ((uint32_t) lowbits << (32 - NUM_LOWBITS))
164 >> (32 - NUM_LOWBITS - NUM_HIGHBITS))
165 >> NUM_HIGHBITS);
166 error |= check (janus.f);
168 return (error ? 1 : 0);