update nearly all FSF copyright year lists to include 2009
[gnulib.git] / tests / test-round2.c
1 /* Test of rounding to nearest, breaking ties away from 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 Ben Pfaff <blp@gnu.org>, 2007.
18    Heavily based on code by Bruno Haible. */
19
20 /* Get the two reference implementations of round under the names
21    round_reference1 and round_reference2.
22
23    round.c will #include <config.h> for us. */
24 #define FLOOR_BASED_ROUND round_reference1
25 #define FLOOR_FREE_ROUND round_reference2
26 #include "round.c"
27
28 #include <math.h>
29 #include <float.h>
30 #include <stdbool.h>
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34
35 #include "verify.h"
36
37 #ifdef USE_LONG_DOUBLE
38 # error Long double not supported.
39 #elif ! defined USE_FLOAT
40 # include "isnand-nolibm.h"
41 # define ISNAN isnand
42 # define FUNCTION "round"
43 # define DOUBLE_UINT uint64_t
44 # define DOUBLE_BITS 64
45 # define NUM_HIGHBITS 13
46 # define NUM_LOWBITS 4
47 #else /* defined USE_FLOAT */
48 # include "isnanf-nolibm.h"
49 # define ISNAN isnanf
50 # define FUNCTION "roundf"
51 # define DOUBLE_UINT uint32_t
52 # define DOUBLE_BITS 32
53 # define NUM_HIGHBITS 12
54 # define NUM_LOWBITS 4
55 #endif
56
57 /* Test for equality.  */
58 static bool
59 equal (const char *message, DOUBLE x, DOUBLE y0, DOUBLE y1)
60 {
61   if (ISNAN (y0) ? ISNAN (y1) : y0 == y1)
62     return true;
63   else
64     {
65       fprintf (stderr, "%s: "FUNCTION"(%g(%a)) = %g(%a) or %g(%a)?\n",
66                message, x, x, y0, y0, y1, y1);
67       return false;
68     }
69 }
70
71 /* Test the function for a given argument.  */
72 static bool
73 check (DOUBLE x)
74 {
75   DOUBLE ref1 = round_reference1 (x);
76   DOUBLE ref2 = round_reference2 (x);
77   DOUBLE result = ROUND (x);
78
79   /* If the reference implementations disagree, bail out immediately.  */
80   if (!equal ("reference implementations disagree", x, ref1, ref2))
81     exit (EXIT_FAILURE);
82
83   /* If the actual implementation is wrong, return an error code.  */
84   return equal ("bad round implementation", x, ref1, result);
85 }
86
87 int
88 main (void)
89 {
90   DOUBLE_UINT highbits, lowbits;
91   int error = 0;
92   for (highbits = 0; highbits < (1 << NUM_HIGHBITS); highbits++)
93     for (lowbits = 0; lowbits < (1 << NUM_LOWBITS); lowbits++)
94       {
95         /* Combine highbits and lowbits into a floating-point number,
96            sign-extending the lowbits to DOUBLE_BITS-NUM_HIGHBITS bits.  */
97         union { DOUBLE f; DOUBLE_UINT i; } janus;
98         verify (sizeof janus.f == sizeof janus.i);
99         janus.i = lowbits | (highbits << (DOUBLE_BITS - NUM_HIGHBITS));
100         if (lowbits >> (NUM_LOWBITS - 1))
101           janus.i |= ((DOUBLE_UINT) -1
102                       >> (NUM_LOWBITS + NUM_HIGHBITS)
103                       << NUM_LOWBITS);
104         if (!check (janus.f))
105           error = true;
106       }
107   return (error ? 1 : 0);
108 }