autoupdate ylwrap
[gnulib.git] / tests / test-truncf2.c
1 /* Test of rounding towards zero.
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 "isnanf-nolibm.h"
32 #include "minus-zero.h"
33 #include "macros.h"
34
35
36 /* The reference implementation, taken from lib/trunc.c.  */
37
38 #define DOUBLE float
39 #define MANT_DIG FLT_MANT_DIG
40 #define L_(literal) literal##f
41
42 /* -0.0.  See minus-zero.h.  */
43 #define MINUS_ZERO minus_zerof
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 truncf_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       /* For 0 < x < 1, return +0.0 even if the current rounding mode is
71          FE_DOWNWARD.  */
72       if (z < L_(1.0))
73         z = L_(0.0);
74       /* Avoid rounding errors for values near 2^k, where k >= MANT_DIG-1.  */
75       else 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 down.  */
81           if (z > y)
82             z -= L_(1.0);
83         }
84     }
85   else if (z < L_(0.0))
86     {
87       /* For -1 < x < 0, return -0.0 regardless of the current rounding
88          mode.  */
89       if (z > L_(-1.0))
90         z = MINUS_ZERO;
91       /* Avoid rounding errors for values near -2^k, where k >= MANT_DIG-1.  */
92       else if (z > - TWO_MANT_DIG)
93         {
94           /* Round to the next integer (nearest or up or down, doesn't matter).  */
95           z -= TWO_MANT_DIG;
96           z += TWO_MANT_DIG;
97           /* Enforce rounding up.  */
98           if (z < y)
99             z += L_(1.0);
100         }
101     }
102   return z;
103 }
104
105
106 /* Test for equality.  */
107 static int
108 equal (DOUBLE x, DOUBLE y)
109 {
110   return (isnanf (x) ? isnanf (y) : x == y);
111 }
112
113 /* Test whether the result for a given argument is correct.  */
114 static bool
115 correct_result_p (DOUBLE x, DOUBLE result)
116 {
117   return
118     (x >= 0
119      ? (x < 1 ? result == L_(0.0) :
120         x - 1 < x ? result <= x && result >= x - 1 && x - result < 1 :
121         equal (result, x))
122      : (x > -1 ? result == L_(0.0) :
123         x + 1 > x ? result >= x && result <= x + 1 && result - x < 1 :
124         equal (result, x)));
125 }
126
127 /* Test the function for a given argument.  */
128 static int
129 check (float x)
130 {
131   /* If the reference implementation is incorrect, bail out immediately.  */
132   float reference = truncf_reference (x);
133   ASSERT (correct_result_p (x, reference));
134   /* If the actual implementation is wrong, return an error code.  */
135   {
136     float result = truncf (x);
137     if (correct_result_p (x, result))
138       return 0;
139     else
140       {
141 #if GNULIB_TEST_FPRINTF_POSIX
142         fprintf (stderr, "truncf %g(%a) = %g(%a) or %g(%a)?\n",
143                  x, x, reference, reference, result, result);
144 #endif
145         return 1;
146       }
147   }
148 }
149
150 #define NUM_HIGHBITS 12
151 #define NUM_LOWBITS 4
152
153 int
154 main ()
155 {
156   unsigned int highbits;
157   unsigned int lowbits;
158   int error = 0;
159   for (highbits = 0; highbits < (1 << NUM_HIGHBITS); highbits++)
160     for (lowbits = 0; lowbits < (1 << NUM_LOWBITS); lowbits++)
161       {
162         /* Combine highbits and lowbits into a floating-point number,
163            sign-extending the lowbits to 32-NUM_HIGHBITS bits.  */
164         union { float f; uint32_t i; } janus;
165         janus.i = ((uint32_t) highbits << (32 - NUM_HIGHBITS))
166                   | ((uint32_t) ((int32_t) ((uint32_t) lowbits << (32 - NUM_LOWBITS))
167                                  >> (32 - NUM_LOWBITS - NUM_HIGHBITS))
168                      >> NUM_HIGHBITS);
169         error |= check (janus.f);
170       }
171   return (error ? 1 : 0);
172 }