]>
git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/powerpc/powerpc64/fpu/s_llroundf.c
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
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.
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.
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
17 not, see <https://www.gnu.org/licenses/>. */
19 #define NO_MATH_REDIRECT
20 #define lroundf __redirect_llround
21 #define __lroundf __redirect___lround
25 #include <libm-alias-float.h>
26 #include <math-barriers.h>
32 double r
= __builtin_round (x
);
33 /* Prevent gcc from calling llround directly when compiled with
34 -fno-math-errno by inserting a barrier. */
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
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
62 /* Test whether an integer to avoid spurious "inexact". */
63 float t
= ax
+ 0x1p
+23f
;
75 __asm__ ("fctidz %0, %1" : "=d" (ret
) : "d" (x
));
80 strong_alias (__llroundf
, __lroundf
)
81 libm_alias_float (__llround
, lround
)
82 libm_alias_float (__llround
, llround
)