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