summaryrefslogtreecommitdiffstats
path: root/gl/floor.c
diff options
context:
space:
mode:
authorThomas Guyot-Sionnest <dermoth@users.sourceforge.net>2008-02-12 12:03:58 (GMT)
committerThomas Guyot-Sionnest <dermoth@users.sourceforge.net>2008-02-12 12:03:58 (GMT)
commitabbad00edd7f5f3d33ae77a9d5ac338e5bea5fb3 (patch)
tree0d55fb4d8e5ad7a9376d1bccdea31104cbecacfe /gl/floor.c
parentbd7029a99b0c2974265c6665638ef14a052f42ab (diff)
downloadmonitoring-plugins-abbad00edd7f5f3d33ae77a9d5ac338e5bea5fb3.tar.gz
Import Gnulib floorf and base64 and removed our old base64 library.
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1926 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'gl/floor.c')
-rw-r--r--gl/floor.c93
1 files changed, 93 insertions, 0 deletions
diff --git a/gl/floor.c b/gl/floor.c
new file mode 100644
index 0000000..05a6591
--- /dev/null
+++ b/gl/floor.c
@@ -0,0 +1,93 @@
1/* Round towards negative infinity.
2 Copyright (C) 2007 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 3 of the License, or
7 (at your option) 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
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
16
17/* Written by Bruno Haible <bruno@clisp.org>, 2007. */
18
19#include <config.h>
20
21/* Specification. */
22#include <math.h>
23
24#include <float.h>
25
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
42
43/* 2^(MANT_DIG-1). */
44static 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));
53
54DOUBLE
55FUNC (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;
65
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}