Mercurial > gnulib
annotate tests/test-floor2.c @ 40196:e63f5d3edab5
relocatable-prog: Update documentation.
* doc/relocatable-maint.texi (Supporting Relocation): Update to match
the recent changes.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Sun, 24 Feb 2019 01:49:15 +0100 |
parents | b06060465f09 |
children |
rev | line source |
---|---|
13974 | 1 /* Test of rounding towards negative infinity. |
40057
b06060465f09
maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
2 Copyright (C) 2007-2019 Free Software Foundation, Inc. |
13974 | 3 |
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. | |
8 | |
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. | |
13 | |
14 You should have received a copy of the GNU General Public License | |
19190 | 15 along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
13974 | 16 |
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */ | |
18 | |
19 /* When this test fails on some platform, build it together with the gnulib | |
20 module 'fprintf-posix' for optimal debugging output. */ | |
21 | |
22 #include <config.h> | |
23 | |
24 #include <math.h> | |
25 | |
26 #include <float.h> | |
27 #include <stdbool.h> | |
28 #include <stdint.h> | |
29 #include <stdio.h> | |
30 | |
31 #include "isnand-nolibm.h" | |
32 #include "macros.h" | |
33 | |
16506
fa4e9b981eb4
Avoid compilation errors with MSVC option -fp:strict.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
34 /* MSVC with option -fp:strict refuses to compile constant initializers that |
fa4e9b981eb4
Avoid compilation errors with MSVC option -fp:strict.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
35 contain floating-point operations. Pacify this compiler. */ |
fa4e9b981eb4
Avoid compilation errors with MSVC option -fp:strict.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
36 #ifdef _MSC_VER |
fa4e9b981eb4
Avoid compilation errors with MSVC option -fp:strict.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
37 # pragma fenv_access (off) |
fa4e9b981eb4
Avoid compilation errors with MSVC option -fp:strict.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
38 #endif |
fa4e9b981eb4
Avoid compilation errors with MSVC option -fp:strict.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
39 |
13974 | 40 |
41 /* The reference implementation, taken from lib/floor.c. */ | |
42 | |
43 #define DOUBLE double | |
44 #define MANT_DIG DBL_MANT_DIG | |
45 #define L_(literal) literal | |
46 | |
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)); | |
57 | |
58 DOUBLE | |
59 floor_reference (DOUBLE x) | |
60 { | |
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; | |
69 | |
70 if (z > L_(0.0)) | |
71 { | |
13991
525bc52bc59f
floor: Implement result sign according to IEEE 754.
Bruno Haible <bruno@clisp.org>
parents:
13974
diff
changeset
|
72 /* For 0 < x < 1, return +0.0 even if the current rounding mode is |
525bc52bc59f
floor: Implement result sign according to IEEE 754.
Bruno Haible <bruno@clisp.org>
parents:
13974
diff
changeset
|
73 FE_DOWNWARD. */ |
525bc52bc59f
floor: Implement result sign according to IEEE 754.
Bruno Haible <bruno@clisp.org>
parents:
13974
diff
changeset
|
74 if (z < L_(1.0)) |
525bc52bc59f
floor: Implement result sign according to IEEE 754.
Bruno Haible <bruno@clisp.org>
parents:
13974
diff
changeset
|
75 z = L_(0.0); |
13974 | 76 /* Avoid rounding errors for values near 2^k, where k >= MANT_DIG-1. */ |
13991
525bc52bc59f
floor: Implement result sign according to IEEE 754.
Bruno Haible <bruno@clisp.org>
parents:
13974
diff
changeset
|
77 else if (z < TWO_MANT_DIG) |
13974 | 78 { |
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); | |
85 } | |
86 } | |
87 else if (z < L_(0.0)) | |
88 { | |
89 /* Work around ICC's desire to optimize denormal floats to 0. */ | |
90 if (z > -DBL_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) | |
94 { | |
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); | |
101 } | |
102 } | |
103 return z; | |
104 } | |
105 | |
106 | |
107 /* Test for equality. */ | |
108 static int | |
109 equal (DOUBLE x, DOUBLE y) | |
110 { | |
111 return (isnand (x) ? isnand (y) : x == y); | |
112 } | |
113 | |
114 /* Test whether the result for a given argument is correct. */ | |
115 static bool | |
116 correct_result_p (DOUBLE x, DOUBLE result) | |
117 { | |
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)); | |
122 } | |
123 | |
124 /* Test the function for a given argument. */ | |
125 static int | |
126 check (double x) | |
127 { | |
128 /* If the reference implementation is incorrect, bail out immediately. */ | |
129 double reference = floor_reference (x); | |
130 ASSERT (correct_result_p (x, reference)); | |
131 /* If the actual implementation is wrong, return an error code. */ | |
132 { | |
133 double result = floor (x); | |
134 if (correct_result_p (x, result)) | |
135 return 0; | |
136 else | |
137 { | |
138 #if GNULIB_TEST_FPRINTF_POSIX | |
139 fprintf (stderr, "floor %g(%a) = %g(%a) or %g(%a)?\n", | |
140 x, x, reference, reference, result, result); | |
141 #endif | |
142 return 1; | |
143 } | |
144 } | |
145 } | |
146 | |
147 #define NUM_HIGHBITS 15 | |
148 #define NUM_LOWBITS 4 | |
149 | |
150 int | |
151 main () | |
152 { | |
14015
41026f5e29c1
stdint: avoid HP-UX 10.20 preprocessor bug
Eric Blake <eblake@redhat.com>
parents:
13991
diff
changeset
|
153 #ifdef UINT64_MAX |
13974 | 154 unsigned int highbits; |
155 unsigned int lowbits; | |
156 int error = 0; | |
157 for (highbits = 0; highbits < (1 << NUM_HIGHBITS); highbits++) | |
158 for (lowbits = 0; lowbits < (1 << NUM_LOWBITS); lowbits++) | |
159 { | |
160 /* Combine highbits and lowbits into a floating-point number, | |
161 sign-extending the lowbits to 64-NUM_HIGHBITS bits. */ | |
162 union { double f; uint64_t i; } janus; | |
163 janus.i = ((uint64_t) highbits << (64 - NUM_HIGHBITS)) | |
164 | ((uint64_t) ((int64_t) ((uint64_t) lowbits << (64 - NUM_LOWBITS)) | |
165 >> (64 - NUM_LOWBITS - NUM_HIGHBITS)) | |
166 >> NUM_HIGHBITS); | |
167 error |= check (janus.f); | |
168 } | |
169 return (error ? 1 : 0); | |
170 #else | |
171 fprintf (stderr, "Skipping test: no 64-bit integer type available\n"); | |
172 return 77; | |
173 #endif | |
174 } |