Code

Sync with the latest Gnulib code (177f525)
[nagiosplug.git] / gl / floor.c
1 /* Round towards negative infinity.
2    Copyright (C) 2007, 2009, 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 Bruno Haible <bruno@clisp.org>, 2007.  */
19 #include <config.h>
21 /* Specification.  */
22 #include <math.h>
24 #include <float.h>
26 #ifdef USE_LONG_DOUBLE
27 # define FUNC floorl
28 # define DOUBLE long double
29 # define MANT_DIG LDBL_MANT_DIG
30 # define L_(literal) literal##L
31 #elif ! defined USE_FLOAT
32 # define FUNC floor
33 # define DOUBLE double
34 # define MANT_DIG DBL_MANT_DIG
35 # define L_(literal) literal
36 #else /* defined USE_FLOAT */
37 # define FUNC floorf
38 # define DOUBLE float
39 # define MANT_DIG FLT_MANT_DIG
40 # define L_(literal) literal##f
41 #endif
43 /* 2^(MANT_DIG-1).  */
44 static const DOUBLE TWO_MANT_DIG =
45   /* Assume MANT_DIG <= 5 * 31.
46      Use the identity
47        n = floor(n/5) + floor((n+1)/5) + ... + floor((n+4)/5).  */
48   (DOUBLE) (1U << ((MANT_DIG - 1) / 5))
49   * (DOUBLE) (1U << ((MANT_DIG - 1 + 1) / 5))
50   * (DOUBLE) (1U << ((MANT_DIG - 1 + 2) / 5))
51   * (DOUBLE) (1U << ((MANT_DIG - 1 + 3) / 5))
52   * (DOUBLE) (1U << ((MANT_DIG - 1 + 4) / 5));
54 DOUBLE
55 FUNC (DOUBLE x)
56 {
57   /* The use of 'volatile' guarantees that excess precision bits are dropped
58      at each addition step and before the following comparison at the caller's
59      site.  It is necessary on x86 systems where double-floats are not IEEE
60      compliant by default, to avoid that the results become platform and compiler
61      option dependent.  'volatile' is a portable alternative to gcc's
62      -ffloat-store option.  */
63   volatile DOUBLE y = x;
64   volatile DOUBLE z = y;
66   if (z > L_(0.0))
67     {
68       /* Avoid rounding errors for values near 2^k, where k >= MANT_DIG-1.  */
69       if (z < TWO_MANT_DIG)
70         {
71           /* Round to the next integer (nearest or up or down, doesn't matter).  */
72           z += TWO_MANT_DIG;
73           z -= TWO_MANT_DIG;
74           /* Enforce rounding down.  */
75           if (z > y)
76             z -= L_(1.0);
77         }
78     }
79   else if (z < L_(0.0))
80     {
81       /* Avoid rounding errors for values near -2^k, where k >= MANT_DIG-1.  */
82       if (z > - TWO_MANT_DIG)
83         {
84           /* Round to the next integer (nearest or up or down, doesn't matter).  */
85           z -= TWO_MANT_DIG;
86           z += TWO_MANT_DIG;
87           /* Enforce rounding down.  */
88           if (z > y)
89             z -= L_(1.0);
90         }
91     }
92   return z;
93 }