finalise NEWS.stable
[gnulib.git] / lib / round.c
1 /* Round toward nearest, breaking ties away from zero.
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 /* Written by Ben Pfaff <blp@gnu.org>, 2007.
19    Based heavily on code by Bruno Haible. */
20
21 #if ! defined USE_LONG_DOUBLE
22 # include <config.h>
23 #endif
24
25 /* Specification.  */
26 #include <math.h>
27
28 #include <float.h>
29
30 #undef MIN
31
32 #ifdef USE_LONG_DOUBLE
33 # define ROUND roundl
34 # define FLOOR floorl
35 # define CEIL ceill
36 # define DOUBLE long double
37 # define MANT_DIG LDBL_MANT_DIG
38 # define MIN LDBL_MIN
39 # define L_(literal) literal##L
40 # define HAVE_FLOOR_AND_CEIL HAVE_FLOORL_AND_CEILL
41 #elif ! defined USE_FLOAT
42 # define ROUND round
43 # define FLOOR floor
44 # define CEIL ceil
45 # define DOUBLE double
46 # define MANT_DIG DBL_MANT_DIG
47 # define MIN DBL_MIN
48 # define L_(literal) literal
49 # define HAVE_FLOOR_AND_CEIL 1
50 #else /* defined USE_FLOAT */
51 # define ROUND roundf
52 # define FLOOR floorf
53 # define CEIL ceilf
54 # define DOUBLE float
55 # define MANT_DIG FLT_MANT_DIG
56 # define MIN FLT_MIN
57 # define L_(literal) literal##f
58 # define HAVE_FLOOR_AND_CEIL HAVE_FLOORF_AND_CEILF
59 #endif
60
61 /* -0.0.  See minus-zero.h.  */
62 #if defined __hpux || defined __sgi || defined __ICC
63 # define MINUS_ZERO (-MIN * MIN)
64 #else
65 # define MINUS_ZERO L_(-0.0)
66 #endif
67
68 /* If we're being included from test-round2[f].c, it already defined names for
69    our round implementations.  Otherwise, pick the preferred implementation for
70    this machine. */
71 #if !defined FLOOR_BASED_ROUND && !defined FLOOR_FREE_ROUND
72 # if HAVE_FLOOR_AND_CEIL
73 #  define FLOOR_BASED_ROUND ROUND
74 # else
75 #  define FLOOR_FREE_ROUND ROUND
76 # endif
77 #endif
78
79 #ifdef FLOOR_BASED_ROUND
80 /* An implementation of the C99 round function based on floor and ceil.  We use
81    this when floor and ceil are available, on the assumption that they are
82    faster than the open-coded versions below. */
83 DOUBLE
84 FLOOR_BASED_ROUND (DOUBLE x)
85 {
86   if (x >= L_(0.0))
87     {
88       DOUBLE y = FLOOR (x);
89       if (x - y >= L_(0.5))
90         y += L_(1.0);
91       return y;
92     }
93   else
94     {
95       DOUBLE y = CEIL (x);
96       if (y - x >= L_(0.5))
97         y -= L_(1.0);
98       return y;
99     }
100 }
101 #endif /* FLOOR_BASED_ROUND */
102
103 #ifdef FLOOR_FREE_ROUND
104 /* An implementation of the C99 round function without floor or ceil.
105    We use this when floor or ceil is missing. */
106 DOUBLE
107 FLOOR_FREE_ROUND (DOUBLE x)
108 {
109   /* 2^(MANT_DIG-1).  */
110   static const DOUBLE TWO_MANT_DIG =
111     /* Assume MANT_DIG <= 5 * 31.
112        Use the identity
113        n = floor(n/5) + floor((n+1)/5) + ... + floor((n+4)/5).  */
114     (DOUBLE) (1U << ((MANT_DIG - 1) / 5))
115     * (DOUBLE) (1U << ((MANT_DIG - 1 + 1) / 5))
116     * (DOUBLE) (1U << ((MANT_DIG - 1 + 2) / 5))
117     * (DOUBLE) (1U << ((MANT_DIG - 1 + 3) / 5))
118     * (DOUBLE) (1U << ((MANT_DIG - 1 + 4) / 5));
119
120   /* The use of 'volatile' guarantees that excess precision bits are dropped at
121      each addition step and before the following comparison at the caller's
122      site.  It is necessary on x86 systems where double-floats are not IEEE
123      compliant by default, to avoid that the results become platform and
124      compiler option dependent.  'volatile' is a portable alternative to gcc's
125      -ffloat-store option.  */
126   volatile DOUBLE y = x;
127   volatile DOUBLE z = y;
128
129   if (z > L_(0.0))
130     {
131       /* Avoid rounding error for x = 0.5 - 2^(-MANT_DIG-1).  */
132       if (z < L_(0.5))
133         z = L_(0.0);
134       /* Avoid rounding errors for values near 2^k, where k >= MANT_DIG-1.  */
135       else if (z < TWO_MANT_DIG)
136         {
137           /* Add 0.5 to the absolute value.  */
138           y = z += L_(0.5);
139           /* Round to the next integer (nearest or up or down, doesn't
140              matter).  */
141           z += TWO_MANT_DIG;
142           z -= TWO_MANT_DIG;
143           /* Enforce rounding down.  */
144           if (z > y)
145             z -= L_(1.0);
146         }
147     }
148   else if (z < L_(0.0))
149     {
150       /* Avoid rounding error for x = -(0.5 - 2^(-MANT_DIG-1)).  */
151       if (z > - L_(0.5))
152         z = MINUS_ZERO;
153       /* Avoid rounding errors for values near -2^k, where k >= MANT_DIG-1.  */
154       else if (z > -TWO_MANT_DIG)
155         {
156           /* Add 0.5 to the absolute value.  */
157           y = z -= L_(0.5);
158           /* Round to the next integer (nearest or up or down, doesn't
159              matter).  */
160           z -= TWO_MANT_DIG;
161           z += TWO_MANT_DIG;
162           /* Enforce rounding up.  */
163           if (z < y)
164             z += L_(1.0);
165         }
166     }
167   return z;
168 }
169 #endif /* FLOOR_FREE_ROUND */
170