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