]>
Commit | Line | Data |
---|---|---|
f964490f RM |
1 | /* Truncate (toward zero) long double floating-point values. |
2 | IBM extended format long double version. | |
8db21882 | 3 | Copyright (C) 2006, 2007, 2012 Free Software Foundation, Inc. |
f964490f RM |
4 | This file is part of the GNU C Library. |
5 | ||
6 | The GNU C Library is free software; you can redistribute it and/or | |
7 | modify it under the terms of the GNU Lesser General Public | |
8 | License as published by the Free Software Foundation; either | |
9 | version 2.1 of the License, or (at your option) any later version. | |
10 | ||
11 | The GNU C Library is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public | |
59ba27a6 PE |
17 | License along with the GNU C Library; if not, see |
18 | <http://www.gnu.org/licenses/>. */ | |
f964490f RM |
19 | |
20 | /* This has been coded in assembler because GCC makes such a mess of it | |
21 | when it's coded in C. */ | |
22 | ||
23 | #include <math.h> | |
f964490f RM |
24 | #include <math_ldbl_opt.h> |
25 | #include <float.h> | |
26 | #include <ieee754.h> | |
27 | ||
28 | ||
f964490f RM |
29 | long double |
30 | __truncl (long double x) | |
f964490f | 31 | { |
5c68d401 | 32 | double xh, xl, hi, lo; |
f964490f | 33 | |
5c68d401 | 34 | ldbl_unpack (x, &xh, &xl); |
f964490f | 35 | |
5c68d401 RM |
36 | /* Return Inf, Nan, +/-0 unchanged. */ |
37 | if (__builtin_expect (xh != 0.0 | |
38 | && __builtin_isless (__builtin_fabs (xh), | |
39 | __builtin_inf ()), 1)) | |
f964490f | 40 | { |
5c68d401 | 41 | double orig_xh; |
5c68d401 RM |
42 | |
43 | /* Long double arithmetic, including the canonicalisation below, | |
44 | only works in round-to-nearest mode. */ | |
5c68d401 RM |
45 | |
46 | /* Convert the high double to integer. */ | |
47 | orig_xh = xh; | |
48 | hi = ldbl_nearbyint (xh); | |
49 | ||
50 | /* Subtract integral high part from the value. */ | |
51 | xh -= hi; | |
52 | ldbl_canonicalize (&xh, &xl); | |
53 | ||
54 | /* Now convert the low double, adjusted for any remainder from the | |
55 | high double. */ | |
56 | lo = ldbl_nearbyint (xh); | |
57 | ||
58 | /* Adjust the result when the remainder is non-zero. nearbyint | |
59 | rounds values to the nearest integer, and values halfway | |
60 | between integers to the nearest even integer. floorl must | |
61 | round towards -Inf. */ | |
62 | xh -= lo; | |
63 | ldbl_canonicalize (&xh, &xl); | |
64 | ||
65 | if (orig_xh < 0.0) | |
f964490f | 66 | { |
5c68d401 RM |
67 | if (xh > 0.0 || (xh == 0.0 && xl > 0.0)) |
68 | lo += 1.0; | |
f964490f | 69 | } |
5c68d401 | 70 | else |
f964490f | 71 | { |
5c68d401 RM |
72 | if (xh < 0.0 || (xh == 0.0 && xl < 0.0)) |
73 | lo -= 1.0; | |
f964490f | 74 | } |
5c68d401 RM |
75 | |
76 | /* Ensure the final value is canonical. In certain cases, | |
77 | rounding causes hi,lo calculated so far to be non-canonical. */ | |
78 | xh = hi; | |
79 | xl = lo; | |
80 | ldbl_canonicalize (&xh, &xl); | |
81 | ||
82 | /* Ensure we return -0 rather than +0 when appropriate. */ | |
83 | if (orig_xh < 0.0) | |
84 | xh = -__builtin_fabs (xh); | |
f964490f RM |
85 | } |
86 | ||
5c68d401 | 87 | return ldbl_pack (xh, xl); |
f964490f RM |
88 | } |
89 | ||
90 | long_double_symbol (libm, __truncl, truncl); |