]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/powerpc/fpu/fenv_libc.h
f8dd1b7a8b31c54d19843fb3d4e9fbcaa6644411
[thirdparty/glibc.git] / sysdeps / powerpc / fpu / fenv_libc.h
1 /* Internal libc stuff for floating point environment routines.
2 Copyright (C) 1997-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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #ifndef _FENV_LIBC_H
20 #define _FENV_LIBC_H 1
21
22 #include <fenv.h>
23 #include <ldsodefs.h>
24 #include <sysdep.h>
25
26 extern const fenv_t *__fe_nomask_env_priv (void);
27
28 extern const fenv_t *__fe_mask_env (void) attribute_hidden;
29
30 /* The sticky bits in the FPSCR indicating exceptions have occurred. */
31 #define FPSCR_STICKY_BITS ((FE_ALL_EXCEPT | FE_ALL_INVALID) & ~FE_INVALID)
32
33 /* Equivalent to fegetenv, but returns a fenv_t instead of taking a
34 pointer. */
35 #define fegetenv_register() __builtin_mffs()
36
37 /* Equivalent to fesetenv, but takes a fenv_t instead of a pointer. */
38 #define fesetenv_register(env) \
39 do { \
40 double d = (env); \
41 if(GLRO(dl_hwcap) & PPC_FEATURE_HAS_DFP) \
42 asm volatile (".machine push; " \
43 ".machine \"power6\"; " \
44 "mtfsf 0xff,%0,1,0; " \
45 ".machine pop" : : "f" (d)); \
46 else \
47 __builtin_mtfsf (0xff, d); \
48 } while(0)
49
50 /* This very handy macro:
51 - Sets the rounding mode to 'round to nearest';
52 - Sets the processor into IEEE mode; and
53 - Prevents exceptions from being raised for inexact results.
54 These things happen to be exactly what you need for typical elementary
55 functions. */
56 #define relax_fenv_state() \
57 do { \
58 if (GLRO(dl_hwcap) & PPC_FEATURE_HAS_DFP) \
59 asm (".machine push; .machine \"power6\"; " \
60 "mtfsfi 7,0,1; .machine pop"); \
61 asm ("mtfsfi 7,0"); \
62 } while(0)
63
64 /* Set/clear a particular FPSCR bit (for instance,
65 reset_fpscr_bit(FPSCR_VE);
66 prevents INVALID exceptions from being raised). */
67 #define set_fpscr_bit(x) asm volatile ("mtfsb1 %0" : : "i"(x))
68 #define reset_fpscr_bit(x) asm volatile ("mtfsb0 %0" : : "i"(x))
69
70 typedef union
71 {
72 fenv_t fenv;
73 unsigned long long l;
74 } fenv_union_t;
75
76
77 static inline int
78 __fesetround_inline (int round)
79 {
80 if ((unsigned int) round < 2)
81 {
82 asm volatile ("mtfsb0 30");
83 if ((unsigned int) round == 0)
84 asm volatile ("mtfsb0 31");
85 else
86 asm volatile ("mtfsb1 31");
87 }
88 else
89 {
90 asm volatile ("mtfsb1 30");
91 if ((unsigned int) round == 2)
92 asm volatile ("mtfsb0 31");
93 else
94 asm volatile ("mtfsb1 31");
95 }
96
97 return 0;
98 }
99
100 /* Same as __fesetround_inline, however without runtime check to use DFP
101 mtfsfi syntax (as relax_fenv_state) or if round value is valid. */
102 static inline void
103 __fesetround_inline_nocheck (const int round)
104 {
105 asm volatile ("mtfsfi 7,%0" : : "i" (round));
106 }
107
108 /* Definitions of all the FPSCR bit numbers */
109 enum {
110 FPSCR_FX = 0, /* exception summary */
111 FPSCR_FEX, /* enabled exception summary */
112 FPSCR_VX, /* invalid operation summary */
113 FPSCR_OX, /* overflow */
114 FPSCR_UX, /* underflow */
115 FPSCR_ZX, /* zero divide */
116 FPSCR_XX, /* inexact */
117 FPSCR_VXSNAN, /* invalid operation for sNaN */
118 FPSCR_VXISI, /* invalid operation for Inf-Inf */
119 FPSCR_VXIDI, /* invalid operation for Inf/Inf */
120 FPSCR_VXZDZ, /* invalid operation for 0/0 */
121 FPSCR_VXIMZ, /* invalid operation for Inf*0 */
122 FPSCR_VXVC, /* invalid operation for invalid compare */
123 FPSCR_FR, /* fraction rounded [fraction was incremented by round] */
124 FPSCR_FI, /* fraction inexact */
125 FPSCR_FPRF_C, /* result class descriptor */
126 FPSCR_FPRF_FL, /* result less than (usually, less than 0) */
127 FPSCR_FPRF_FG, /* result greater than */
128 FPSCR_FPRF_FE, /* result equal to */
129 FPSCR_FPRF_FU, /* result unordered */
130 FPSCR_20, /* reserved */
131 FPSCR_VXSOFT, /* invalid operation set by software */
132 FPSCR_VXSQRT, /* invalid operation for square root */
133 FPSCR_VXCVI, /* invalid operation for invalid integer convert */
134 FPSCR_VE, /* invalid operation exception enable */
135 FPSCR_OE, /* overflow exception enable */
136 FPSCR_UE, /* underflow exception enable */
137 FPSCR_ZE, /* zero divide exception enable */
138 FPSCR_XE, /* inexact exception enable */
139 #ifdef _ARCH_PWR6
140 FPSCR_29, /* Reserved in ISA 2.05 */
141 #else
142 FPSCR_NI /* non-IEEE mode (typically, no denormalised numbers) */
143 #endif /* _ARCH_PWR6 */
144 /* the remaining two least-significant bits keep the rounding mode */
145 };
146
147 static inline int
148 fenv_reg_to_exceptions (unsigned long long l)
149 {
150 int result = 0;
151 if (l & (1 << (31 - FPSCR_XE)))
152 result |= FE_INEXACT;
153 if (l & (1 << (31 - FPSCR_ZE)))
154 result |= FE_DIVBYZERO;
155 if (l & (1 << (31 - FPSCR_UE)))
156 result |= FE_UNDERFLOW;
157 if (l & (1 << (31 - FPSCR_OE)))
158 result |= FE_OVERFLOW;
159 if (l & (1 << (31 - FPSCR_VE)))
160 result |= FE_INVALID;
161 return result;
162 }
163
164 #ifdef _ARCH_PWR6
165 /* Not supported in ISA 2.05. Provided for source compat only. */
166 # define FPSCR_NI 29
167 #endif /* _ARCH_PWR6 */
168
169 /* This operation (i) sets the appropriate FPSCR bits for its
170 parameter, (ii) converts sNaN to the corresponding qNaN, and (iii)
171 otherwise passes its parameter through unchanged (in particular, -0
172 and +0 stay as they were). The `obvious' way to do this is optimised
173 out by gcc. */
174 #define f_wash(x) \
175 ({ double d; asm volatile ("fmul %0,%1,%2" \
176 : "=f"(d) \
177 : "f" (x), "f"((float)1.0)); d; })
178 #define f_washf(x) \
179 ({ float f; asm volatile ("fmuls %0,%1,%2" \
180 : "=f"(f) \
181 : "f" (x), "f"((float)1.0)); f; })
182
183 #endif /* fenv_libc.h */