Merge branch 'stable'
[gnulib.git] / lib / rint.c
1 /* Round according to the current rounding mode.
2    Copyright (C) 2007, 2010-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 2, or (at your option)
7    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 along
15    with this program; if not, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 #if ! defined USE_LONG_DOUBLE
19 # include <config.h>
20 #endif
21
22 /* Specification.  */
23 #include <math.h>
24
25 #include <float.h>
26 #include <stdlib.h>
27
28 #undef MIN
29
30 #ifdef USE_LONG_DOUBLE
31 # define RINT rintl
32 # define DOUBLE long double
33 # define MANT_DIG LDBL_MANT_DIG
34 # define MIN LDBL_MIN
35 # define L_(literal) literal##L
36 #elif ! defined USE_FLOAT
37 # define RINT rint
38 # define DOUBLE double
39 # define MANT_DIG DBL_MANT_DIG
40 # define MIN DBL_MIN
41 # define L_(literal) literal
42 #else /* defined USE_FLOAT */
43 # define RINT rintf
44 # define DOUBLE float
45 # define MANT_DIG FLT_MANT_DIG
46 # define MIN FLT_MIN
47 # define L_(literal) literal##f
48 #endif
49
50 /* -0.0.  See minus-zero.h.  */
51 #if defined __hpux || defined __sgi || defined __ICC
52 # define MINUS_ZERO (-MIN * MIN)
53 #else
54 # define MINUS_ZERO L_(-0.0)
55 #endif
56
57 DOUBLE
58 RINT (DOUBLE x)
59 {
60   /* 2^(MANT_DIG-1).  */
61   static const DOUBLE TWO_MANT_DIG =
62     /* Assume MANT_DIG <= 5 * 31.
63        Use the identity
64        n = floor(n/5) + floor((n+1)/5) + ... + floor((n+4)/5).  */
65     (DOUBLE) (1U << ((MANT_DIG - 1) / 5))
66     * (DOUBLE) (1U << ((MANT_DIG - 1 + 1) / 5))
67     * (DOUBLE) (1U << ((MANT_DIG - 1 + 2) / 5))
68     * (DOUBLE) (1U << ((MANT_DIG - 1 + 3) / 5))
69     * (DOUBLE) (1U << ((MANT_DIG - 1 + 4) / 5));
70
71   /* The use of 'volatile' guarantees that excess precision bits are dropped at
72      each addition step and before the following comparison at the caller's
73      site.  It is necessary on x86 systems where double-floats are not IEEE
74      compliant by default, to avoid that the results become platform and
75      compiler option dependent.  'volatile' is a portable alternative to gcc's
76      -ffloat-store option.  */
77   volatile DOUBLE z = x;
78
79   /* Consider the current rounding mode, cf.
80      <http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/float.h.html>.
81      This implementation supports only rounds-to-nearest.  */
82   if (FLT_ROUNDS != 1)
83     abort ();
84
85   if (z > L_(0.0))
86     {
87       /* Avoid rounding error for x = 0.5 - 2^(-MANT_DIG-1).  */
88       if (z < L_(0.5))
89         z = L_(0.0);
90       /* Avoid rounding errors for values near 2^k, where k >= MANT_DIG-1.  */
91       else if (z < TWO_MANT_DIG)
92         {
93           /* Round to the next integer.  */
94           z += TWO_MANT_DIG;
95           z -= TWO_MANT_DIG;
96         }
97     }
98   else if (z < L_(0.0))
99     {
100       /* Avoid rounding error for x = -(0.5 - 2^(-MANT_DIG-1)).  */
101       if (z > - L_(0.5))
102         z = MINUS_ZERO;
103       /* Avoid rounding errors for values near -2^k, where k >= MANT_DIG-1.  */
104       else if (z > -TWO_MANT_DIG)
105         {
106           /* Round to the next integer.  */
107           z -= TWO_MANT_DIG;
108           z += TWO_MANT_DIG;
109         }
110     }
111   return z;
112 }