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