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