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