elf: Ignore LD_LIBRARY_PATH and debug env var for setuid for static
[glibc.git] / sysdeps / powerpc / fpu / round_to_integer.h
blobcb257b41fb9196166febc6108573c6ccbccdd585
1 /* Round to integer generic implementation.
2 Copyright (C) 2019-2023 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 #ifndef _ROUND_TO_INTEGER_H
20 #define _ROUND_TO_INTEGER_H
22 #include <fenv_private.h>
24 enum round_mode
26 CEIL,
27 FLOOR,
28 ROUND,
29 TRUNC,
30 NEARBYINT,
31 RINT
34 static inline fenv_t
35 set_fenv_mode (enum round_mode mode)
37 fenv_t fe = 0;
38 if (mode != RINT)
39 /* Save current FPU rounding mode and inexact state. */
40 fe = fegetenv_register ();
42 switch (mode)
44 case CEIL:
45 __fesetround_inline_nocheck (FE_UPWARD);
46 break;
47 case FLOOR:
48 __fesetround_inline_nocheck (FE_DOWNWARD);
49 break;
50 case TRUNC:
51 case ROUND:
52 __fesetround_inline_nocheck (FE_TOWARDZERO);
53 break;
54 case NEARBYINT:
55 /* Disable FE_INEXACT exception */
56 reset_fpscr_bit (FPSCR_XE);
57 break;
58 case RINT:
59 break;
61 return fe;
64 static inline void
65 reset_fenv_mode (fenv_t fe, enum round_mode mode)
67 switch (mode)
69 default:
70 __builtin_mtfsf (0xff, fe);
71 break;
72 case RINT:
73 break;
77 static inline float
78 round_to_integer_float (enum round_mode mode, float x)
80 /* Ensure sNaN input is converted to qNaN. */
81 if (__glibc_unlikely (isnan (x)))
82 return x + x;
84 if (fabs (x) > 0x1p+23)
85 return x;
87 float r = x;
89 fenv_t fe = set_fenv_mode (mode);
90 if (x > 0.0)
92 /* IEEE 1003.1 round function. IEEE specifies "round to the nearest
93 integer value, rounding halfway cases away from zero, regardless of
94 the current rounding mode." However PowerPC Architecture defines
95 "Round to Nearest" as "Choose the best approximation. In case of a
96 tie, choose the one that is even (least significant bit o).".
97 So we can't use the PowerPC "Round to Nearest" mode. Instead we set
98 "Round toward Zero" mode and round by adding +-0.5 before rounding
99 to the integer value. */
100 if (mode == ROUND)
101 r += 0.5f;
102 r += 0x1p+23;
103 r -= 0x1p+23;
104 r = fabs (r);
106 else if (x < 0.0)
108 if (mode == ROUND)
109 r -= 0.5f;
110 r -= 0x1p+23;
111 r += 0x1p+23;
112 r = -fabs (r);
114 reset_fenv_mode (fe, mode);
116 return r;
119 static inline double
120 round_to_integer_double (enum round_mode mode, double x)
122 /* Ensure sNaN input is converted to qNaN. */
123 if (__glibc_unlikely (isnan (x)))
124 return x + x;
126 if (fabs (x) > 0x1p+52)
127 return x;
129 double r = x;
131 /* Save current FPU rounding mode and inexact state. */
132 fenv_t fe = set_fenv_mode (mode);
133 if (x > 0.0)
135 if (mode == ROUND)
136 r += 0.5;
137 r += 0x1p+52;
138 r -= 0x1p+52;
139 r = fabs (r);
141 else if (x < 0.0)
143 if (mode == ROUND)
144 r -= 0.5;
145 r -= 0x1p+52;
146 r += 0x1p+52;
147 r = -fabs (r);
149 reset_fenv_mode (fe, mode);
151 return r;
154 #endif