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