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