Mercurial > hg > octave-kai > gnulib-hg
annotate lib/round.c @ 9466:1013cba29012
Handle the case that floorl and roundl are declared but do not exist.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Sun, 11 Nov 2007 14:08:25 +0100 |
parents | 96fea5b2eb11 |
children | ae7bf97ee30c |
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. |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
2 Copyright (C) 2007 Free Software Foundation, Inc. |
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 |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
15 with this program; if not, write to the Free Software Foundation, |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
17 |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
18 /* Written by Ben Pfaff <blp@gnu.org>, 2007. |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
19 Based heavily on code by Bruno Haible. */ |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
20 |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
21 #include <config.h> |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
22 |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
23 #include <float.h> |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
24 #include <math.h> |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
25 |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
26 #ifdef USE_LONG_DOUBLE |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
27 # define ROUND roundl |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
28 # define FLOOR floorl |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
29 # define CEIL ceill |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
30 # define DOUBLE long double |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
31 # define MANT_DIG LDBL_MANT_DIG |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
32 # 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
|
33 # 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
|
34 #elif ! defined USE_FLOAT |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
35 # define ROUND round |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
36 # define FLOOR floor |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
37 # define CEIL ceil |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
38 # define DOUBLE double |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
39 # define MANT_DIG DBL_MANT_DIG |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
40 # define L_(literal) literal |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
41 # define HAVE_FLOOR_AND_CEIL 1 |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
42 #else /* defined USE_FLOAT */ |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
43 # define ROUND roundf |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
44 # define FLOOR floorf |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
45 # define CEIL ceilf |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
46 # define DOUBLE float |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
47 # define MANT_DIG FLT_MANT_DIG |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
48 # 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
|
49 # 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
|
50 #endif |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
51 |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
52 /* 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
|
53 our round implementations. Otherwise, pick the preferred implementation for |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
54 this machine. */ |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
55 #if !defined FLOOR_BASED_ROUND && !defined FLOOR_FREE_ROUND |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
56 # if HAVE_FLOOR_AND_CEIL |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
57 # define FLOOR_BASED_ROUND ROUND |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
58 # else |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
59 # define FLOOR_FREE_ROUND ROUND |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
60 # endif |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
61 #endif |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
62 |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
63 #ifdef FLOOR_BASED_ROUND |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
64 /* 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
|
65 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
|
66 faster than the open-coded versions below. */ |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
67 DOUBLE |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
68 FLOOR_BASED_ROUND (DOUBLE x) |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
69 { |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
70 if (x >= L_(0.0)) |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
71 { |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
72 DOUBLE y = FLOOR (x); |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
73 if (x - y >= L_(0.5)) |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
74 y += L_(1.0); |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
75 return y; |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
76 } |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
77 else |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
78 { |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
79 DOUBLE y = CEIL (x); |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
80 if (y - x >= L_(0.5)) |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
81 y -= L_(1.0); |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
82 return y; |
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 } |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
85 #endif /* FLOOR_BASED_ROUND */ |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
86 |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
87 #ifdef FLOOR_FREE_ROUND |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
88 /* 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
|
89 We use this when floor or ceil is missing. */ |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
90 DOUBLE |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
91 FLOOR_FREE_ROUND (DOUBLE x) |
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 /* 2^(MANT_DIG-1). */ |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
94 static const DOUBLE TWO_MANT_DIG = |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
95 /* Assume MANT_DIG <= 5 * 31. |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
96 Use the identity |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
97 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
|
98 (DOUBLE) (1U << ((MANT_DIG - 1) / 5)) |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
99 * (DOUBLE) (1U << ((MANT_DIG - 1 + 1) / 5)) |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
100 * (DOUBLE) (1U << ((MANT_DIG - 1 + 2) / 5)) |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
101 * (DOUBLE) (1U << ((MANT_DIG - 1 + 3) / 5)) |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
102 * (DOUBLE) (1U << ((MANT_DIG - 1 + 4) / 5)); |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
103 |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
104 /* 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
|
105 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
|
106 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
|
107 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
|
108 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
|
109 -ffloat-store option. */ |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
110 volatile DOUBLE y = x; |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
111 volatile DOUBLE z = y; |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
112 |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
113 if (z > L_(0.0)) |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
114 { |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
115 /* 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
|
116 if (z < L_(0.5)) |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
117 z = L_(0.0); |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
118 /* 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
|
119 else if (z < TWO_MANT_DIG) |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
120 { |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
121 /* Add 0.5 to the absolute value. */ |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
122 y = z += L_(0.5); |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
123 /* Round to the next integer (nearest or up or down, doesn't |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
124 matter). */ |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
125 z += TWO_MANT_DIG; |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
126 z -= TWO_MANT_DIG; |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
127 /* Enforce rounding down. */ |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
128 if (z > y) |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
129 z -= L_(1.0); |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
130 } |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
131 } |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
132 else if (z < L_(0.0)) |
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 /* 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
|
135 if (z > - L_(0.5)) |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
136 z = L_(0.0); |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
137 /* 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
|
138 else if (z > -TWO_MANT_DIG) |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
139 { |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
140 /* Add 0.5 to the absolute value. */ |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
141 y = z -= L_(0.5); |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
142 /* Round to the next integer (nearest or up or down, doesn't |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
143 matter). */ |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
144 z -= TWO_MANT_DIG; |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
145 z += TWO_MANT_DIG; |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
146 /* Enforce rounding up. */ |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
147 if (z < y) |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
148 z += L_(1.0); |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
149 } |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
150 } |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
151 return z; |
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 #endif /* FLOOR_FREE_ROUND */ |
96fea5b2eb11
Implement 'round', 'roundf', 'roundl' modules.
Ben Pfaff <blp@cs.stanford.edu>
parents:
diff
changeset
|
154 |