Mercurial > hg > octave-kai > gnulib-hg
annotate lib/round.c @ 17342:c75939cb6254
merge with default branch
author | Michael Goffioul <michael.goffioul@gmail.com> |
---|---|
date | Thu, 21 Feb 2013 14:57:31 +0000 |
parents | e542fd46ad6f |
children |
rev | line source |
---|---|
9375
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
1 /* Round toward nearest, breaking ties away from zero. |
17249
e542fd46ad6f
maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents:
16506
diff
changeset
|
2 Copyright (C) 2007, 2010-2013 Free Software Foundation, Inc. |
9375
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
3 |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
4 This program is free software; you can redistribute it and/or modify |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
5 it under the terms of the GNU General Public License as published by |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
6 the Free Software Foundation; either version 2, or (at your option) |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
7 any later version. |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
8 |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
9 This program is distributed in the hope that it will be useful, |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
10 but WITHOUT ANY WARRANTY; without even the implied warranty of |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
12 GNU General Public License for more details. |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
13 |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
14 You should have received a copy of the GNU General Public License along |
16366
bb182ee4a09d
maint: replace FSF snail-mail addresses with URLs
Paul Eggert <eggert@cs.ucla.edu>
parents:
16297
diff
changeset
|
15 with this program; if not, see <http://www.gnu.org/licenses/>. */ |
9375
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
16 |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
17 /* Written by Ben Pfaff <blp@gnu.org>, 2007. |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
18 Based heavily on code by Bruno Haible. */ |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
19 |
15927
afd47902c4b4
roundl: Simplify for platforms where 'long double' == 'double'.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
20 #if ! defined USE_LONG_DOUBLE |
afd47902c4b4
roundl: Simplify for platforms where 'long double' == 'double'.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
21 # include <config.h> |
afd47902c4b4
roundl: Simplify for platforms where 'long double' == 'double'.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
22 #endif |
9375
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
23 |
14018
ac9f59901721
ceil, trunc, round: Fix gcc warnings.
Bruno Haible <bruno@clisp.org>
parents:
13994
diff
changeset
|
24 /* Specification. */ |
ac9f59901721
ceil, trunc, round: Fix gcc warnings.
Bruno Haible <bruno@clisp.org>
parents:
13994
diff
changeset
|
25 #include <math.h> |
ac9f59901721
ceil, trunc, round: Fix gcc warnings.
Bruno Haible <bruno@clisp.org>
parents:
13994
diff
changeset
|
26 |
9375
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
27 #include <float.h> |
14018
ac9f59901721
ceil, trunc, round: Fix gcc warnings.
Bruno Haible <bruno@clisp.org>
parents:
13994
diff
changeset
|
28 |
ac9f59901721
ceil, trunc, round: Fix gcc warnings.
Bruno Haible <bruno@clisp.org>
parents:
13994
diff
changeset
|
29 #undef MIN |
9375
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
30 |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
31 #ifdef USE_LONG_DOUBLE |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
32 # define ROUND roundl |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
33 # define FLOOR floorl |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
34 # define CEIL ceill |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
35 # define DOUBLE long double |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
36 # define MANT_DIG LDBL_MANT_DIG |
13994
b91bc81df379
round: Implement result sign according to IEEE 754.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
37 # define MIN LDBL_MIN |
9375
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
38 # define L_(literal) literal##L |
9466
1013cba29012
Handle the case that floorl and roundl are declared but do not exist.
Bruno Haible <bruno@clisp.org>
parents:
9375
diff
changeset
|
39 # define HAVE_FLOOR_AND_CEIL HAVE_FLOORL_AND_CEILL |
9375
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
40 #elif ! defined USE_FLOAT |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
41 # define ROUND round |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
42 # define FLOOR floor |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
43 # define CEIL ceil |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
44 # define DOUBLE double |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
45 # define MANT_DIG DBL_MANT_DIG |
13994
b91bc81df379
round: Implement result sign according to IEEE 754.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
46 # define MIN DBL_MIN |
9375
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
47 # define L_(literal) literal |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
48 # define HAVE_FLOOR_AND_CEIL 1 |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
49 #else /* defined USE_FLOAT */ |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
50 # define ROUND roundf |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
51 # define FLOOR floorf |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
52 # define CEIL ceilf |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
53 # define DOUBLE float |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
54 # define MANT_DIG FLT_MANT_DIG |
13994
b91bc81df379
round: Implement result sign according to IEEE 754.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
55 # define MIN FLT_MIN |
9375
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
56 # define L_(literal) literal##f |
9466
1013cba29012
Handle the case that floorl and roundl are declared but do not exist.
Bruno Haible <bruno@clisp.org>
parents:
9375
diff
changeset
|
57 # define HAVE_FLOOR_AND_CEIL HAVE_FLOORF_AND_CEILF |
9375
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
58 #endif |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
59 |
13994
b91bc81df379
round: Implement result sign according to IEEE 754.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
60 /* -0.0. See minus-zero.h. */ |
b91bc81df379
round: Implement result sign according to IEEE 754.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
61 #if defined __hpux || defined __sgi || defined __ICC |
b91bc81df379
round: Implement result sign according to IEEE 754.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
62 # define MINUS_ZERO (-MIN * MIN) |
b91bc81df379
round: Implement result sign according to IEEE 754.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
63 #else |
b91bc81df379
round: Implement result sign according to IEEE 754.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
64 # define MINUS_ZERO L_(-0.0) |
b91bc81df379
round: Implement result sign according to IEEE 754.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
65 #endif |
b91bc81df379
round: Implement result sign according to IEEE 754.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
66 |
16506
fa4e9b981eb4
Avoid compilation errors with MSVC option -fp:strict.
Bruno Haible <bruno@clisp.org>
parents:
16366
diff
changeset
|
67 /* MSVC with option -fp:strict refuses to compile constant initializers that |
fa4e9b981eb4
Avoid compilation errors with MSVC option -fp:strict.
Bruno Haible <bruno@clisp.org>
parents:
16366
diff
changeset
|
68 contain floating-point operations. Pacify this compiler. */ |
fa4e9b981eb4
Avoid compilation errors with MSVC option -fp:strict.
Bruno Haible <bruno@clisp.org>
parents:
16366
diff
changeset
|
69 #ifdef _MSC_VER |
fa4e9b981eb4
Avoid compilation errors with MSVC option -fp:strict.
Bruno Haible <bruno@clisp.org>
parents:
16366
diff
changeset
|
70 # pragma fenv_access (off) |
fa4e9b981eb4
Avoid compilation errors with MSVC option -fp:strict.
Bruno Haible <bruno@clisp.org>
parents:
16366
diff
changeset
|
71 #endif |
fa4e9b981eb4
Avoid compilation errors with MSVC option -fp:strict.
Bruno Haible <bruno@clisp.org>
parents:
16366
diff
changeset
|
72 |
9375
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
73 /* If we're being included from test-round2[f].c, it already defined names for |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
74 our round implementations. Otherwise, pick the preferred implementation for |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
75 this machine. */ |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
76 #if !defined FLOOR_BASED_ROUND && !defined FLOOR_FREE_ROUND |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
77 # if HAVE_FLOOR_AND_CEIL |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
78 # define FLOOR_BASED_ROUND ROUND |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
79 # else |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
80 # define FLOOR_FREE_ROUND ROUND |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
81 # endif |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
82 #endif |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
83 |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
84 #ifdef FLOOR_BASED_ROUND |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
85 /* An implementation of the C99 round function based on floor and ceil. We use |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
86 this when floor and ceil are available, on the assumption that they are |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
87 faster than the open-coded versions below. */ |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
88 DOUBLE |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
89 FLOOR_BASED_ROUND (DOUBLE x) |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
90 { |
10092 | 91 if (x >= L_(0.0)) |
9375
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
92 { |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
93 DOUBLE y = FLOOR (x); |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
94 if (x - y >= L_(0.5)) |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
95 y += L_(1.0); |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
96 return y; |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
97 } |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
98 else |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
99 { |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
100 DOUBLE y = CEIL (x); |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
101 if (y - x >= L_(0.5)) |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
102 y -= L_(1.0); |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
103 return y; |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
104 } |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
105 } |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
106 #endif /* FLOOR_BASED_ROUND */ |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
107 |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
108 #ifdef FLOOR_FREE_ROUND |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
109 /* An implementation of the C99 round function without floor or ceil. |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
110 We use this when floor or ceil is missing. */ |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
111 DOUBLE |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
112 FLOOR_FREE_ROUND (DOUBLE x) |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
113 { |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
114 /* 2^(MANT_DIG-1). */ |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
115 static const DOUBLE TWO_MANT_DIG = |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
116 /* Assume MANT_DIG <= 5 * 31. |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
117 Use the identity |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
118 n = floor(n/5) + floor((n+1)/5) + ... + floor((n+4)/5). */ |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
119 (DOUBLE) (1U << ((MANT_DIG - 1) / 5)) |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
120 * (DOUBLE) (1U << ((MANT_DIG - 1 + 1) / 5)) |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
121 * (DOUBLE) (1U << ((MANT_DIG - 1 + 2) / 5)) |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
122 * (DOUBLE) (1U << ((MANT_DIG - 1 + 3) / 5)) |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
123 * (DOUBLE) (1U << ((MANT_DIG - 1 + 4) / 5)); |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
124 |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
125 /* The use of 'volatile' guarantees that excess precision bits are dropped at |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
126 each addition step and before the following comparison at the caller's |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
127 site. It is necessary on x86 systems where double-floats are not IEEE |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
128 compliant by default, to avoid that the results become platform and |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
129 compiler option dependent. 'volatile' is a portable alternative to gcc's |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
130 -ffloat-store option. */ |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
131 volatile DOUBLE y = x; |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
132 volatile DOUBLE z = y; |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
133 |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
134 if (z > L_(0.0)) |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
135 { |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
136 /* Avoid rounding error for x = 0.5 - 2^(-MANT_DIG-1). */ |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
137 if (z < L_(0.5)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10092
diff
changeset
|
138 z = L_(0.0); |
9375
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
139 /* Avoid rounding errors for values near 2^k, where k >= MANT_DIG-1. */ |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
140 else if (z < TWO_MANT_DIG) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10092
diff
changeset
|
141 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10092
diff
changeset
|
142 /* Add 0.5 to the absolute value. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10092
diff
changeset
|
143 y = z += L_(0.5); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10092
diff
changeset
|
144 /* Round to the next integer (nearest or up or down, doesn't |
9375
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
145 matter). */ |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10092
diff
changeset
|
146 z += TWO_MANT_DIG; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10092
diff
changeset
|
147 z -= TWO_MANT_DIG; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10092
diff
changeset
|
148 /* Enforce rounding down. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10092
diff
changeset
|
149 if (z > y) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10092
diff
changeset
|
150 z -= L_(1.0); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10092
diff
changeset
|
151 } |
9375
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
152 } |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
153 else if (z < L_(0.0)) |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
154 { |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
155 /* Avoid rounding error for x = -(0.5 - 2^(-MANT_DIG-1)). */ |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
156 if (z > - L_(0.5)) |
13994
b91bc81df379
round: Implement result sign according to IEEE 754.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
157 z = MINUS_ZERO; |
9375
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
158 /* Avoid rounding errors for values near -2^k, where k >= MANT_DIG-1. */ |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
159 else if (z > -TWO_MANT_DIG) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10092
diff
changeset
|
160 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10092
diff
changeset
|
161 /* Add 0.5 to the absolute value. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10092
diff
changeset
|
162 y = z -= L_(0.5); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10092
diff
changeset
|
163 /* Round to the next integer (nearest or up or down, doesn't |
9375
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
164 matter). */ |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10092
diff
changeset
|
165 z -= TWO_MANT_DIG; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10092
diff
changeset
|
166 z += TWO_MANT_DIG; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10092
diff
changeset
|
167 /* Enforce rounding up. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10092
diff
changeset
|
168 if (z < y) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10092
diff
changeset
|
169 z += L_(1.0); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10092
diff
changeset
|
170 } |
9375
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
171 } |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
172 return z; |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
173 } |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
174 #endif /* FLOOR_FREE_ROUND */ |