]>
Commit | Line | Data |
---|---|---|
dee07df1 AZ |
1 | /* Round to nearest integer. PowerPC64 version. |
2 | Copyright (C) 2019 Free Software Foundation, Inc. | |
3 | This file is part of the GNU C Library | |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
6 | modify it under the terms of the GNU Library General Public License as | |
7 | published by the Free Software Foundation; either version 2 of the | |
8 | License, or (at your option) any later version. | |
9 | ||
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | Library General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Library General Public | |
16 | License along with the GNU C Library; see the file COPYING.LIB. If | |
5a82c748 | 17 | not, see <https://www.gnu.org/licenses/>. */ |
dee07df1 AZ |
18 | |
19 | #define NO_MATH_REDIRECT | |
20 | #define lroundf __redirect_llround | |
21 | #define __lroundf __redirect___lround | |
22 | #include <math.h> | |
23 | #undef lroundf | |
24 | #undef __lroundf | |
25 | #include <libm-alias-float.h> | |
26 | #include <math-barriers.h> | |
27 | ||
28 | long long int | |
29 | __llroundf (float x) | |
30 | { | |
31 | #ifdef _ARCH_PWR5X | |
32 | double r = __builtin_round (x); | |
33 | /* Prevent gcc from calling llround directly when compiled with | |
34 | -fno-math-errno by inserting a barrier. */ | |
35 | math_opt_barrier (r); | |
36 | return r; | |
37 | #else | |
38 | /* IEEE 1003.1 llroundf function. IEEE specifies "roundf to the nearest | |
39 | integer value, rounding halfway cases away from zero, regardless of | |
40 | the current rounding mode." However PowerPC Architecture defines | |
41 | "roundf to Nearest" as "Choose the best approximation. In case of a | |
42 | tie, choose the one that is even (least significant bit o).". | |
43 | So we can't use the PowerPC "round to Nearest" mode. Instead we set | |
44 | "round toward Zero" mode and round by adding +-0.5 before rounding | |
45 | to the integer value. | |
46 | ||
47 | It is necessary to detect when x is (+-)0x1.fffffffffffffp-2 | |
48 | because adding +-0.5 in this case will cause an erroneous shift, | |
49 | carry and round. We simply return 0 if 0.5 > x > -0.5. Likewise | |
50 | if x is and odd number between +-(2^23 and 2^24-1) a shift and | |
51 | carry will erroneously round if biased with +-0.5. Therefore if x | |
52 | is greater/less than +-2^23 we don't need to bias the number with | |
53 | +-0.5. */ | |
54 | ||
55 | float ax = fabsf (x); | |
56 | ||
57 | if (ax < 0.5f) | |
58 | return 0; | |
59 | ||
60 | if (ax < 0x1p+23f) | |
61 | { | |
62 | /* Test whether an integer to avoid spurious "inexact". */ | |
63 | float t = ax + 0x1p+23f; | |
64 | t = t - 0x1p+23f; | |
65 | if (ax != t) | |
66 | { | |
67 | ax = ax + 0.5f; | |
68 | if (x < 0.0f) | |
69 | ax = -fabs (ax); | |
70 | x = ax; | |
71 | } | |
72 | } | |
73 | ||
74 | long int ret; | |
75 | __asm__ ("fctidz %0, %1" : "=d" (ret) : "d" (x)); | |
76 | return ret; | |
77 | #endif | |
78 | } | |
79 | #ifndef __llroundf | |
80 | strong_alias (__llroundf, __lroundf) | |
81 | libm_alias_float (__llround, lround) | |
82 | libm_alias_float (__llround, llround) | |
83 | #endif |