stdint: avoid HP-UX 10.20 preprocessor bug
[gnulib.git] / tests / test-floor2.c
1 /* Test of rounding towards negative infinity.
2    Copyright (C) 2007-2010 Free Software Foundation, Inc.
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
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
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
34
35 /* The reference implementation, taken from lib/floor.c.  */
36
37 #define DOUBLE double
38 #define MANT_DIG DBL_MANT_DIG
39 #define L_(literal) literal
40
41 /* 2^(MANT_DIG-1).  */
42 static const DOUBLE TWO_MANT_DIG =
43   /* Assume MANT_DIG <= 5 * 31.
44      Use the identity
45        n = floor(n/5) + floor((n+1)/5) + ... + floor((n+4)/5).  */
46   (DOUBLE) (1U << ((MANT_DIG - 1) / 5))
47   * (DOUBLE) (1U << ((MANT_DIG - 1 + 1) / 5))
48   * (DOUBLE) (1U << ((MANT_DIG - 1 + 2) / 5))
49   * (DOUBLE) (1U << ((MANT_DIG - 1 + 3) / 5))
50   * (DOUBLE) (1U << ((MANT_DIG - 1 + 4) / 5));
51
52 DOUBLE
53 floor_reference (DOUBLE x)
54 {
55   /* The use of 'volatile' guarantees that excess precision bits are dropped
56      at each addition step and before the following comparison at the caller's
57      site.  It is necessary on x86 systems where double-floats are not IEEE
58      compliant by default, to avoid that the results become platform and compiler
59      option dependent.  'volatile' is a portable alternative to gcc's
60      -ffloat-store option.  */
61   volatile DOUBLE y = x;
62   volatile DOUBLE z = y;
63
64   if (z > L_(0.0))
65     {
66       /* For 0 < x < 1, return +0.0 even if the current rounding mode is
67          FE_DOWNWARD.  */
68       if (z < L_(1.0))
69         z = L_(0.0);
70       /* Avoid rounding errors for values near 2^k, where k >= MANT_DIG-1.  */
71       else if (z < TWO_MANT_DIG)
72         {
73           /* Round to the next integer (nearest or up or down, doesn't matter).  */
74           z += TWO_MANT_DIG;
75           z -= TWO_MANT_DIG;
76           /* Enforce rounding down.  */
77           if (z > y)
78             z -= L_(1.0);
79         }
80     }
81   else if (z < L_(0.0))
82     {
83       /* Work around ICC's desire to optimize denormal floats to 0.  */
84       if (z > -DBL_MIN)
85         return L_(-1.0);
86       /* Avoid rounding errors for values near -2^k, where k >= MANT_DIG-1.  */
87       if (z > - TWO_MANT_DIG)
88         {
89           /* Round to the next integer (nearest or up or down, doesn't matter).  */
90           z -= TWO_MANT_DIG;
91           z += TWO_MANT_DIG;
92           /* Enforce rounding down.  */
93           if (z > y)
94             z -= L_(1.0);
95         }
96     }
97   return z;
98 }
99
100
101 /* Test for equality.  */
102 static int
103 equal (DOUBLE x, DOUBLE y)
104 {
105   return (isnand (x) ? isnand (y) : x == y);
106 }
107
108 /* Test whether the result for a given argument is correct.  */
109 static bool
110 correct_result_p (DOUBLE x, DOUBLE result)
111 {
112   return
113     (x < 0 && x >= -1 ? result == - L_(1.0) :
114      x - 1 < x ? result <= x && result >= x - 1 && x - result < 1 :
115      equal (result, x));
116 }
117
118 /* Test the function for a given argument.  */
119 static int
120 check (double x)
121 {
122   /* If the reference implementation is incorrect, bail out immediately.  */
123   double reference = floor_reference (x);
124   ASSERT (correct_result_p (x, reference));
125   /* If the actual implementation is wrong, return an error code.  */
126   {
127     double result = floor (x);
128     if (correct_result_p (x, result))
129       return 0;
130     else
131       {
132 #if GNULIB_TEST_FPRINTF_POSIX
133         fprintf (stderr, "floor %g(%a) = %g(%a) or %g(%a)?\n",
134                  x, x, reference, reference, result, result);
135 #endif
136         return 1;
137       }
138   }
139 }
140
141 #define NUM_HIGHBITS 15
142 #define NUM_LOWBITS 4
143
144 int
145 main ()
146 {
147 #ifdef UINT64_MAX
148   unsigned int highbits;
149   unsigned int lowbits;
150   int error = 0;
151   for (highbits = 0; highbits < (1 << NUM_HIGHBITS); highbits++)
152     for (lowbits = 0; lowbits < (1 << NUM_LOWBITS); lowbits++)
153       {
154         /* Combine highbits and lowbits into a floating-point number,
155            sign-extending the lowbits to 64-NUM_HIGHBITS bits.  */
156         union { double f; uint64_t i; } janus;
157         janus.i = ((uint64_t) highbits << (64 - NUM_HIGHBITS))
158                   | ((uint64_t) ((int64_t) ((uint64_t) lowbits << (64 - NUM_LOWBITS))
159                                  >> (64 - NUM_LOWBITS - NUM_HIGHBITS))
160                      >> NUM_HIGHBITS);
161         error |= check (janus.f);
162       }
163   return (error ? 1 : 0);
164 #else
165   fprintf (stderr, "Skipping test: no 64-bit integer type available\n");
166   return 77;
167 #endif
168 }