annotate lib/trunc.c @ 15926:23d150bba2eb

truncl: Simplify for platforms where 'long double' == 'double'. * lib/truncl.c: Include <config.h>. (truncl) [HAVE_SAME_LONG_DOUBLE_AS_DOUBLE]: New function. * lib/trunc.c [!USE_LONG_DOUBLE]: Don't include <config.h> a second time. * m4/truncl.m4 (gl_FUNC_TRUNCL): Require gl_LONG_DOUBLE_VS_DOUBLE. Determine TRUNCL_LIBM according to HAVE_SAME_LONG_DOUBLE_AS_DOUBLE. * modules/truncl (Depends-on): Add trunc. Update conditions.
author Bruno Haible <bruno@clisp.org>
date Mon, 10 Oct 2011 01:05:53 +0200
parents 97fc9a21a8fb
children 8250f2777afc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9282
9a26e94f946a New module 'trunc'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Round towards zero.
14079
97fc9a21a8fb maint: update almost all copyright ranges to include 2011
Jim Meyering <meyering@redhat.com>
parents: 14018
diff changeset
2 Copyright (C) 2007, 2010-2011 Free Software Foundation, Inc.
9282
9a26e94f946a New module 'trunc'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 9285
diff changeset
4 This program is free software: you can redistribute it and/or modify
9282
9a26e94f946a New module 'trunc'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 9285
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 9285
diff changeset
7 (at your option) any later version.
9282
9a26e94f946a New module 'trunc'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8
9a26e94f946a New module 'trunc'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
9a26e94f946a New module 'trunc'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
9a26e94f946a New module 'trunc'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9a26e94f946a New module 'trunc'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 GNU General Public License for more details.
9a26e94f946a New module 'trunc'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 9285
diff changeset
14 You should have received a copy of the GNU General Public License
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 9285
diff changeset
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
9282
9a26e94f946a New module 'trunc'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16
9a26e94f946a New module 'trunc'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
9a26e94f946a New module 'trunc'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
15926
23d150bba2eb truncl: Simplify for platforms where 'long double' == 'double'.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
19 #if ! defined USE_LONG_DOUBLE
23d150bba2eb truncl: Simplify for platforms where 'long double' == 'double'.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
20 # include <config.h>
23d150bba2eb truncl: Simplify for platforms where 'long double' == 'double'.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
21 #endif
9282
9a26e94f946a New module 'trunc'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22
9a26e94f946a New module 'trunc'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 /* Specification. */
9a26e94f946a New module 'trunc'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 #include <math.h>
9a26e94f946a New module 'trunc'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25
9a26e94f946a New module 'trunc'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 #include <float.h>
9a26e94f946a New module 'trunc'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27
14018
ac9f59901721 ceil, trunc, round: Fix gcc warnings.
Bruno Haible <bruno@clisp.org>
parents: 13993
diff changeset
28 #undef MIN
ac9f59901721 ceil, trunc, round: Fix gcc warnings.
Bruno Haible <bruno@clisp.org>
parents: 13993
diff changeset
29
9285
79e67a783774 New module 'truncf'.
Bruno Haible <bruno@clisp.org>
parents: 9282
diff changeset
30 #ifdef USE_LONG_DOUBLE
79e67a783774 New module 'truncf'.
Bruno Haible <bruno@clisp.org>
parents: 9282
diff changeset
31 # define FUNC truncl
79e67a783774 New module 'truncf'.
Bruno Haible <bruno@clisp.org>
parents: 9282
diff changeset
32 # define DOUBLE long double
79e67a783774 New module 'truncf'.
Bruno Haible <bruno@clisp.org>
parents: 9282
diff changeset
33 # define MANT_DIG LDBL_MANT_DIG
13993
9e9aaf4c9464 trunc: Implement result sign according to IEEE 754.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
34 # define MIN LDBL_MIN
9285
79e67a783774 New module 'truncf'.
Bruno Haible <bruno@clisp.org>
parents: 9282
diff changeset
35 # define L_(literal) literal##L
79e67a783774 New module 'truncf'.
Bruno Haible <bruno@clisp.org>
parents: 9282
diff changeset
36 #elif ! defined USE_FLOAT
79e67a783774 New module 'truncf'.
Bruno Haible <bruno@clisp.org>
parents: 9282
diff changeset
37 # define FUNC trunc
79e67a783774 New module 'truncf'.
Bruno Haible <bruno@clisp.org>
parents: 9282
diff changeset
38 # define DOUBLE double
79e67a783774 New module 'truncf'.
Bruno Haible <bruno@clisp.org>
parents: 9282
diff changeset
39 # define MANT_DIG DBL_MANT_DIG
13993
9e9aaf4c9464 trunc: Implement result sign according to IEEE 754.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
40 # define MIN DBL_MIN
9285
79e67a783774 New module 'truncf'.
Bruno Haible <bruno@clisp.org>
parents: 9282
diff changeset
41 # define L_(literal) literal
79e67a783774 New module 'truncf'.
Bruno Haible <bruno@clisp.org>
parents: 9282
diff changeset
42 #else /* defined USE_FLOAT */
79e67a783774 New module 'truncf'.
Bruno Haible <bruno@clisp.org>
parents: 9282
diff changeset
43 # define FUNC truncf
79e67a783774 New module 'truncf'.
Bruno Haible <bruno@clisp.org>
parents: 9282
diff changeset
44 # define DOUBLE float
79e67a783774 New module 'truncf'.
Bruno Haible <bruno@clisp.org>
parents: 9282
diff changeset
45 # define MANT_DIG FLT_MANT_DIG
13993
9e9aaf4c9464 trunc: Implement result sign according to IEEE 754.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
46 # define MIN FLT_MIN
9285
79e67a783774 New module 'truncf'.
Bruno Haible <bruno@clisp.org>
parents: 9282
diff changeset
47 # define L_(literal) literal##f
79e67a783774 New module 'truncf'.
Bruno Haible <bruno@clisp.org>
parents: 9282
diff changeset
48 #endif
79e67a783774 New module 'truncf'.
Bruno Haible <bruno@clisp.org>
parents: 9282
diff changeset
49
13993
9e9aaf4c9464 trunc: Implement result sign according to IEEE 754.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
50 /* -0.0. See minus-zero.h. */
9e9aaf4c9464 trunc: Implement result sign according to IEEE 754.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
51 #if defined __hpux || defined __sgi || defined __ICC
9e9aaf4c9464 trunc: Implement result sign according to IEEE 754.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
52 # define MINUS_ZERO (-MIN * MIN)
9e9aaf4c9464 trunc: Implement result sign according to IEEE 754.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
53 #else
9e9aaf4c9464 trunc: Implement result sign according to IEEE 754.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
54 # define MINUS_ZERO L_(-0.0)
9e9aaf4c9464 trunc: Implement result sign according to IEEE 754.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
55 #endif
9e9aaf4c9464 trunc: Implement result sign according to IEEE 754.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
56
9285
79e67a783774 New module 'truncf'.
Bruno Haible <bruno@clisp.org>
parents: 9282
diff changeset
57 /* 2^(MANT_DIG-1). */
9313
59fc463c9868 Fix type of TWO_MANT_DIG.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
58 static const DOUBLE TWO_MANT_DIG =
9285
79e67a783774 New module 'truncf'.
Bruno Haible <bruno@clisp.org>
parents: 9282
diff changeset
59 /* Assume MANT_DIG <= 5 * 31.
9282
9a26e94f946a New module 'trunc'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 Use the identity
9285
79e67a783774 New module 'truncf'.
Bruno Haible <bruno@clisp.org>
parents: 9282
diff changeset
61 n = floor(n/5) + floor((n+1)/5) + ... + floor((n+4)/5). */
79e67a783774 New module 'truncf'.
Bruno Haible <bruno@clisp.org>
parents: 9282
diff changeset
62 (DOUBLE) (1U << ((MANT_DIG - 1) / 5))
79e67a783774 New module 'truncf'.
Bruno Haible <bruno@clisp.org>
parents: 9282
diff changeset
63 * (DOUBLE) (1U << ((MANT_DIG - 1 + 1) / 5))
79e67a783774 New module 'truncf'.
Bruno Haible <bruno@clisp.org>
parents: 9282
diff changeset
64 * (DOUBLE) (1U << ((MANT_DIG - 1 + 2) / 5))
79e67a783774 New module 'truncf'.
Bruno Haible <bruno@clisp.org>
parents: 9282
diff changeset
65 * (DOUBLE) (1U << ((MANT_DIG - 1 + 3) / 5))
79e67a783774 New module 'truncf'.
Bruno Haible <bruno@clisp.org>
parents: 9282
diff changeset
66 * (DOUBLE) (1U << ((MANT_DIG - 1 + 4) / 5));
9282
9a26e94f946a New module 'trunc'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67
9285
79e67a783774 New module 'truncf'.
Bruno Haible <bruno@clisp.org>
parents: 9282
diff changeset
68 DOUBLE
79e67a783774 New module 'truncf'.
Bruno Haible <bruno@clisp.org>
parents: 9282
diff changeset
69 FUNC (DOUBLE x)
9282
9a26e94f946a New module 'trunc'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 {
9a26e94f946a New module 'trunc'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 /* The use of 'volatile' guarantees that excess precision bits are dropped
9a26e94f946a New module 'trunc'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 at each addition step and before the following comparison at the caller's
9a26e94f946a New module 'trunc'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 site. It is necessary on x86 systems where double-floats are not IEEE
9a26e94f946a New module 'trunc'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 compliant by default, to avoid that the results become platform and compiler
9a26e94f946a New module 'trunc'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 option dependent. 'volatile' is a portable alternative to gcc's
9a26e94f946a New module 'trunc'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 -ffloat-store option. */
9285
79e67a783774 New module 'truncf'.
Bruno Haible <bruno@clisp.org>
parents: 9282
diff changeset
77 volatile DOUBLE y = x;
79e67a783774 New module 'truncf'.
Bruno Haible <bruno@clisp.org>
parents: 9282
diff changeset
78 volatile DOUBLE z = y;
9282
9a26e94f946a New module 'trunc'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79
9285
79e67a783774 New module 'truncf'.
Bruno Haible <bruno@clisp.org>
parents: 9282
diff changeset
80 if (z > L_(0.0))
9282
9a26e94f946a New module 'trunc'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 {
13993
9e9aaf4c9464 trunc: Implement result sign according to IEEE 754.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
82 /* For 0 < x < 1, return +0.0 even if the current rounding mode is
9e9aaf4c9464 trunc: Implement result sign according to IEEE 754.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
83 FE_DOWNWARD. */
9e9aaf4c9464 trunc: Implement result sign according to IEEE 754.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
84 if (z < L_(1.0))
9e9aaf4c9464 trunc: Implement result sign according to IEEE 754.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
85 z = L_(0.0);
9327
5437ef3873b5 Fix incorrect rounding of trunc, truncf, truncl in some cases. Add a new test.
Bruno Haible <bruno@clisp.org>
parents: 9313
diff changeset
86 /* Avoid rounding errors for values near 2^k, where k >= MANT_DIG-1. */
13993
9e9aaf4c9464 trunc: Implement result sign according to IEEE 754.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
87 else if (z < TWO_MANT_DIG)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9327
diff changeset
88 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9327
diff changeset
89 /* Round to the next integer (nearest or up or down, doesn't matter). */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9327
diff changeset
90 z += TWO_MANT_DIG;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9327
diff changeset
91 z -= TWO_MANT_DIG;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9327
diff changeset
92 /* Enforce rounding down. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9327
diff changeset
93 if (z > y)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9327
diff changeset
94 z -= L_(1.0);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9327
diff changeset
95 }
9282
9a26e94f946a New module 'trunc'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96 }
9285
79e67a783774 New module 'truncf'.
Bruno Haible <bruno@clisp.org>
parents: 9282
diff changeset
97 else if (z < L_(0.0))
9282
9a26e94f946a New module 'trunc'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98 {
13993
9e9aaf4c9464 trunc: Implement result sign according to IEEE 754.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
99 /* For -1 < x < 0, return -0.0 regardless of the current rounding
9e9aaf4c9464 trunc: Implement result sign according to IEEE 754.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
100 mode. */
9e9aaf4c9464 trunc: Implement result sign according to IEEE 754.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
101 if (z > L_(-1.0))
9e9aaf4c9464 trunc: Implement result sign according to IEEE 754.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
102 z = MINUS_ZERO;
9327
5437ef3873b5 Fix incorrect rounding of trunc, truncf, truncl in some cases. Add a new test.
Bruno Haible <bruno@clisp.org>
parents: 9313
diff changeset
103 /* Avoid rounding errors for values near -2^k, where k >= MANT_DIG-1. */
13993
9e9aaf4c9464 trunc: Implement result sign according to IEEE 754.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
104 else if (z > - TWO_MANT_DIG)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9327
diff changeset
105 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9327
diff changeset
106 /* Round to the next integer (nearest or up or down, doesn't matter). */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9327
diff changeset
107 z -= TWO_MANT_DIG;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9327
diff changeset
108 z += TWO_MANT_DIG;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9327
diff changeset
109 /* Enforce rounding up. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9327
diff changeset
110 if (z < y)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9327
diff changeset
111 z += L_(1.0);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9327
diff changeset
112 }
9282
9a26e94f946a New module 'trunc'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
113 }
9a26e94f946a New module 'trunc'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
114 return z;
9a26e94f946a New module 'trunc'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
115 }