1 /* Test of rounding to nearest, breaking ties away from zero.
2 Copyright (C) 2007-2010 Free Software Foundation, Inc.
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.
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.
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/>. */
17 /* Written by Ben Pfaff <blp@gnu.org>, 2007.
18 Heavily based on code by Bruno Haible. */
20 /* Get the two reference implementations of round under the names
21 round_reference1 and round_reference2.
23 round.c will #include <config.h> for us. */
24 #define FLOOR_BASED_ROUND round_reference1
25 #define FLOOR_FREE_ROUND round_reference2
37 #ifdef USE_LONG_DOUBLE
38 # error Long double not supported.
39 #elif ! defined USE_FLOAT
40 # include "isnand-nolibm.h"
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"
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
57 /* Test for equality. */
59 equal (const char *message, DOUBLE x, DOUBLE y0, DOUBLE y1)
61 if (ISNAN (y0) ? ISNAN (y1) : y0 == y1)
65 fprintf (stderr, "%s: "FUNCTION"(%g(%a)) = %g(%a) or %g(%a)?\n",
66 message, x, x, y0, y0, y1, y1);
71 /* Test the function for a given argument. */
75 DOUBLE ref1 = round_reference1 (x);
76 DOUBLE ref2 = round_reference2 (x);
77 DOUBLE result = ROUND (x);
79 /* If the reference implementations disagree, bail out immediately. */
80 if (!equal ("reference implementations disagree", x, ref1, ref2))
83 /* If the actual implementation is wrong, return an error code. */
84 return equal ("bad round implementation", x, ref1, result);
90 DOUBLE_UINT highbits, lowbits;
92 for (highbits = 0; highbits < (1 << NUM_HIGHBITS); highbits++)
93 for (lowbits = 0; lowbits < (1 << NUM_LOWBITS); lowbits++)
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)
104 if (!check (janus.f))
107 return (error ? 1 : 0);