| 1 |
/* FPU-related code for PowerPC. |
| 2 |
Copyright (C) 2023-2025 Free Software Foundation, Inc. |
| 3 |
Contributed by Sergey Fedorov <[email protected]> |
| 4 |
|
| 5 |
This file is part of the GNU Fortran runtime library (libgfortran). |
| 6 |
|
| 7 |
Libgfortran is free software; you can redistribute it and/or |
| 8 |
modify it under the terms of the GNU General Public |
| 9 |
License as published by the Free Software Foundation; either |
| 10 |
version 3 of the License, or (at your option) any later version. |
| 11 |
|
| 12 |
Libgfortran is distributed in the hope that it will be useful, |
| 13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 15 |
the GNU General Public License for more details. |
| 16 |
|
| 17 |
Under Section 7 of GPL version 3, you are granted additional |
| 18 |
permissions described in the GCC Runtime Library Exception, version 3.1, |
| 19 |
as published by the Free Software Foundation. |
| 20 |
|
| 21 |
You should have received a copy of the GNU General Public License and |
| 22 |
a copy of the GCC Runtime Library Exception along with this program; |
| 23 |
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 24 |
<http://www.gnu.org/licenses/>. */ |
| 25 |
|
| 26 |
/* While some of the definitions and functions used here are available |
| 27 |
with Apple libm, we on purpose avoid pulling it in: to avoid potential |
| 28 |
conflicts and not self-impose unnecessary constraints. */ |
| 29 |
|
| 30 |
/* FP exception flags */ |
| 31 |
#define FE_INEXACT 0x02000000 |
| 32 |
#define FE_DIVBYZERO 0x04000000 |
| 33 |
#define FE_UNDERFLOW 0x08000000 |
| 34 |
#define FE_OVERFLOW 0x10000000 |
| 35 |
#define FE_INVALID 0x20000000 |
| 36 |
|
| 37 |
#define FE_ALL_EXCEPT 0x3E000000 |
| 38 |
#define FE_NO_EXCEPT 0xC1FFFFFF |
| 39 |
|
| 40 |
/* Extra invalid flags */ |
| 41 |
#define FE_INVALID_SNAN 0x01000000 |
| 42 |
#define FE_INVALID_ISI 0x00800000 |
| 43 |
#define FE_INVALID_IDI 0x00400000 |
| 44 |
#define FE_INVALID_ZDZ 0x00200000 |
| 45 |
#define FE_INVALID_IMZ 0x00100000 |
| 46 |
#define FE_INVALID_XVC 0x00080000 |
| 47 |
#define FE_INVALID_SOFT 0x00000400 |
| 48 |
#define FE_INVALID_SQRT 0x00000200 |
| 49 |
#define FE_INVALID_CVI 0x00000100 |
| 50 |
|
| 51 |
/* Rounding modes */ |
| 52 |
#define FE_TONEAREST 0x00000000 |
| 53 |
#define FE_TOWARDZERO 0x00000001 |
| 54 |
#define FE_UPWARD 0x00000002 |
| 55 |
#define FE_DOWNWARD 0x00000003 |
| 56 |
|
| 57 |
/* There is no consistency re what is to be included in all_invalid. |
| 58 |
Apple libm has 0x01f80300, Openlibm/FreeBSD/NetBSD has 0x21f80700 |
| 59 |
and OpenBSD has 0x01f80700. In particular, FE_INVALID_SOFT and |
| 60 |
FE_INVALID are contested. */ |
| 61 |
#define FE_ALL_INVALID 0x01F80300 |
| 62 |
#define FE_NO_INVALID 0xFE07FCFF |
| 63 |
|
| 64 |
#define FE_ALL_FLAGS 0xFFF80300 |
| 65 |
#define FE_NO_FLAGS 0x0007FCFF |
| 66 |
|
| 67 |
#define FE_ALL_RND 0x00000003 |
| 68 |
#define FE_NO_RND 0xFFFFFFFC |
| 69 |
|
| 70 |
/* Floating-point exception summary (FX) bit. */ |
| 71 |
#define FE_SET_FX 0x80000000 |
| 72 |
#define FE_CLR_FX 0x7FFFFFFF |
| 73 |
|
| 74 |
/* Some implementations use FE_INVALID_SOFT here. */ |
| 75 |
#define SET_INVALID 0x01000000 |
| 76 |
|
| 77 |
#define FE_EXCEPT_SHIFT 22 |
| 78 |
#define EXCEPT_MASK FE_ALL_EXCEPT >> FE_EXCEPT_SHIFT |
| 79 |
|
| 80 |
typedef unsigned int fenv_t; |
| 81 |
typedef unsigned int fexcept_t; |
| 82 |
|
| 83 |
/* default environment object */ |
| 84 |
extern const fenv_t _FE_DFL_ENV; |
| 85 |
/* pointer to default environment */ |
| 86 |
#define FE_DFL_ENV &_FE_DFL_ENV |
| 87 |
|
| 88 |
typedef union { |
| 89 |
struct { |
| 90 |
unsigned int hi; |
| 91 |
fenv_t lo; |
| 92 |
} i; |
| 93 |
double d; |
| 94 |
} hexdouble; |
| 95 |
|
| 96 |
#define HEXDOUBLE(hi, lo) {{ hi, lo }} |
| 97 |
|
| 98 |
|
| 99 |
/* Check we can actually store the FPU state in the allocated size. */ |
| 100 |
_Static_assert (sizeof(fenv_t) <= (size_t) GFC_FPE_STATE_BUFFER_SIZE, |
| 101 |
"GFC_FPE_STATE_BUFFER_SIZE is too small"); |
| 102 |
|
| 103 |
|
| 104 |
static inline int fegetexcept(void) |
| 105 |
{ |
| 106 |
hexdouble fe; |
| 107 |
|
| 108 |
fe.d = __builtin_mffs(); |
| 109 |
return ((fe.i.lo & EXCEPT_MASK) << FE_EXCEPT_SHIFT); |
| 110 |
} |
| 111 |
|
| 112 |
static inline int feclearexcept(int excepts) |
| 113 |
{ |
| 114 |
hexdouble fe; |
| 115 |
|
| 116 |
if (excepts & FE_INVALID) |
| 117 |
excepts |= FE_ALL_INVALID; |
| 118 |
fe.d = __builtin_mffs(); |
| 119 |
fe.i.lo &= ~excepts; |
| 120 |
if ((fe.i.lo & FE_ALL_EXCEPT) == 0) |
| 121 |
fe.i.lo &= FE_CLR_FX; |
| 122 |
__builtin_mtfsf(0xFF, fe.d); |
| 123 |
return 0; |
| 124 |
} |
| 125 |
|
| 126 |
/* It is not possible to set VX bit directly. */ |
| 127 |
static inline int feraiseexcept(int excepts) |
| 128 |
{ |
| 129 |
hexdouble fe; |
| 130 |
|
| 131 |
if (excepts & FE_INVALID) |
| 132 |
excepts |= SET_INVALID; |
| 133 |
fe.d = __builtin_mffs(); |
| 134 |
fe.i.lo |= excepts; |
| 135 |
__builtin_mtfsf(0xFF, fe.d); |
| 136 |
return 0; |
| 137 |
} |
| 138 |
|
| 139 |
static inline int fetestexcept(int excepts) |
| 140 |
{ |
| 141 |
hexdouble fe; |
| 142 |
|
| 143 |
excepts &= FE_ALL_EXCEPT; |
| 144 |
fe.d = __builtin_mffs(); |
| 145 |
return (fe.i.lo & (excepts & FE_ALL_EXCEPT)); |
| 146 |
} |
| 147 |
|
| 148 |
static inline int feenableexcept(int mask) |
| 149 |
{ |
| 150 |
hexdouble fe; |
| 151 |
fenv_t oldmask; |
| 152 |
|
| 153 |
fe.d = __builtin_mffs(); |
| 154 |
oldmask = fe.i.lo; |
| 155 |
fe.i.lo |= (mask & FE_ALL_EXCEPT) >> FE_EXCEPT_SHIFT; |
| 156 |
__builtin_mtfsf(0xFF, fe.d); |
| 157 |
return ((oldmask & EXCEPT_MASK) << FE_EXCEPT_SHIFT); |
| 158 |
} |
| 159 |
|
| 160 |
static inline int fedisableexcept(int mask) |
| 161 |
{ |
| 162 |
hexdouble fe; |
| 163 |
fenv_t oldmask; |
| 164 |
|
| 165 |
fe.d = __builtin_mffs(); |
| 166 |
oldmask = fe.i.lo; |
| 167 |
fe.i.lo &= ~((mask & FE_ALL_EXCEPT) >> FE_EXCEPT_SHIFT); |
| 168 |
__builtin_mtfsf(0xFF, fe.d); |
| 169 |
return ((oldmask & EXCEPT_MASK) << FE_EXCEPT_SHIFT); |
| 170 |
} |
| 171 |
|
| 172 |
static inline int fegetround(void) |
| 173 |
{ |
| 174 |
hexdouble fe; |
| 175 |
|
| 176 |
fe.d = __builtin_mffs(); |
| 177 |
return (fe.i.lo & FE_ALL_RND); |
| 178 |
} |
| 179 |
|
| 180 |
static inline int fesetround(int round) |
| 181 |
{ |
| 182 |
hexdouble fe; |
| 183 |
|
| 184 |
if (round & FE_NO_RND) |
| 185 |
return (-1); |
| 186 |
fe.d = __builtin_mffs(); |
| 187 |
fe.i.lo = (fe.i.lo & FE_NO_RND) | round; |
| 188 |
__builtin_mtfsf(0xFF, fe.d); |
| 189 |
return 0; |
| 190 |
} |
| 191 |
|
| 192 |
static inline int fegetenv(fenv_t *envp) |
| 193 |
{ |
| 194 |
hexdouble fe; |
| 195 |
|
| 196 |
fe.d = __builtin_mffs(); |
| 197 |
*envp = fe.i.lo; |
| 198 |
return 0; |
| 199 |
} |
| 200 |
|
| 201 |
static inline int fesetenv(const fenv_t *envp) |
| 202 |
{ |
| 203 |
hexdouble fe; |
| 204 |
|
| 205 |
fe.i.lo = *envp; |
| 206 |
__builtin_mtfsf(0xFF, fe.d); |
| 207 |
return 0; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
int get_fpu_trap_exceptions (void) |
| 212 |
{ |
| 213 |
int exceptions = fegetexcept(); |
| 214 |
int res = 0; |
| 215 |
|
| 216 |
if (exceptions & FE_INVALID) res |= GFC_FPE_INVALID; |
| 217 |
if (exceptions & FE_DIVBYZERO) res |= GFC_FPE_ZERO; |
| 218 |
if (exceptions & FE_OVERFLOW) res |= GFC_FPE_OVERFLOW; |
| 219 |
if (exceptions & FE_UNDERFLOW) res |= GFC_FPE_UNDERFLOW; |
| 220 |
if (exceptions & FE_INEXACT) res |= GFC_FPE_INEXACT; |
| 221 |
|
| 222 |
return res; |
| 223 |
} |
| 224 |
|
| 225 |
void set_fpu (void) |
| 226 |
{ |
| 227 |
if (options.fpe & GFC_FPE_DENORMAL) |
| 228 |
estr_write ("Fortran runtime warning: Floating point 'denormal operand' " |
| 229 |
"exception not supported.\n"); |
| 230 |
|
| 231 |
set_fpu_trap_exceptions (options.fpe, 0); |
| 232 |
} |
| 233 |
|
| 234 |
void set_fpu_trap_exceptions (int trap, int notrap) |
| 235 |
{ |
| 236 |
unsigned int mode_set = 0, mode_clr = 0; |
| 237 |
|
| 238 |
if (trap & GFC_FPE_INVALID) |
| 239 |
mode_set |= FE_INVALID; |
| 240 |
if (notrap & GFC_FPE_INVALID) |
| 241 |
mode_clr |= FE_INVALID; |
| 242 |
|
| 243 |
if (trap & GFC_FPE_ZERO) |
| 244 |
mode_set |= FE_DIVBYZERO; |
| 245 |
if (notrap & GFC_FPE_ZERO) |
| 246 |
mode_clr |= FE_DIVBYZERO; |
| 247 |
|
| 248 |
if (trap & GFC_FPE_OVERFLOW) |
| 249 |
mode_set |= FE_OVERFLOW; |
| 250 |
if (notrap & GFC_FPE_OVERFLOW) |
| 251 |
mode_clr |= FE_OVERFLOW; |
| 252 |
|
| 253 |
if (trap & GFC_FPE_UNDERFLOW) |
| 254 |
mode_set |= FE_UNDERFLOW; |
| 255 |
if (notrap & GFC_FPE_UNDERFLOW) |
| 256 |
mode_clr |= FE_UNDERFLOW; |
| 257 |
|
| 258 |
if (trap & GFC_FPE_INEXACT) |
| 259 |
mode_set |= FE_INEXACT; |
| 260 |
if (notrap & GFC_FPE_INEXACT) |
| 261 |
mode_clr |= FE_INEXACT; |
| 262 |
|
| 263 |
/* Clear stalled exception flags. */ |
| 264 |
feclearexcept (FE_ALL_EXCEPT); |
| 265 |
|
| 266 |
feenableexcept (mode_set); |
| 267 |
fedisableexcept (mode_clr); |
| 268 |
} |
| 269 |
|
| 270 |
int get_fpu_except_flags (void) |
| 271 |
{ |
| 272 |
int result, set_excepts; |
| 273 |
result = 0; |
| 274 |
set_excepts = fetestexcept (FE_ALL_EXCEPT); |
| 275 |
|
| 276 |
if (set_excepts & FE_INVALID) |
| 277 |
result |= GFC_FPE_INVALID; |
| 278 |
if (set_excepts & FE_DIVBYZERO) |
| 279 |
result |= GFC_FPE_ZERO; |
| 280 |
if (set_excepts & FE_OVERFLOW) |
| 281 |
result |= GFC_FPE_OVERFLOW; |
| 282 |
if (set_excepts & FE_UNDERFLOW) |
| 283 |
result |= GFC_FPE_UNDERFLOW; |
| 284 |
if (set_excepts & FE_INEXACT) |
| 285 |
result |= GFC_FPE_INEXACT; |
| 286 |
|
| 287 |
return result; |
| 288 |
} |
| 289 |
|
| 290 |
void set_fpu_except_flags (int set, int clear) |
| 291 |
{ |
| 292 |
unsigned int exc_set = 0, exc_clr = 0; |
| 293 |
|
| 294 |
if (set & GFC_FPE_INVALID) |
| 295 |
exc_set |= FE_INVALID; |
| 296 |
else if (clear & GFC_FPE_INVALID) |
| 297 |
exc_clr |= FE_INVALID; |
| 298 |
|
| 299 |
if (set & GFC_FPE_ZERO) |
| 300 |
exc_set |= FE_DIVBYZERO; |
| 301 |
else if (clear & GFC_FPE_ZERO) |
| 302 |
exc_clr |= FE_DIVBYZERO; |
| 303 |
|
| 304 |
if (set & GFC_FPE_OVERFLOW) |
| 305 |
exc_set |= FE_OVERFLOW; |
| 306 |
else if (clear & GFC_FPE_OVERFLOW) |
| 307 |
exc_clr |= FE_OVERFLOW; |
| 308 |
|
| 309 |
if (set & GFC_FPE_UNDERFLOW) |
| 310 |
exc_set |= FE_UNDERFLOW; |
| 311 |
else if (clear & GFC_FPE_UNDERFLOW) |
| 312 |
exc_clr |= FE_UNDERFLOW; |
| 313 |
|
| 314 |
if (set & GFC_FPE_INEXACT) |
| 315 |
exc_set |= FE_INEXACT; |
| 316 |
else if (clear & GFC_FPE_INEXACT) |
| 317 |
exc_clr |= FE_INEXACT; |
| 318 |
|
| 319 |
feclearexcept (exc_clr); |
| 320 |
feraiseexcept (exc_set); |
| 321 |
} |
| 322 |
|
| 323 |
void get_fpu_state (void *state) |
| 324 |
{ |
| 325 |
fegetenv (state); |
| 326 |
} |
| 327 |
|
| 328 |
void set_fpu_state (void *state) |
| 329 |
{ |
| 330 |
fesetenv (state); |
| 331 |
} |
| 332 |
|
| 333 |
int get_fpu_rounding_mode (void) |
| 334 |
{ |
| 335 |
int rnd_mode; |
| 336 |
rnd_mode = fegetround(); |
| 337 |
|
| 338 |
switch (rnd_mode) |
| 339 |
{ |
| 340 |
case FE_TONEAREST: |
| 341 |
return GFC_FPE_TONEAREST; |
| 342 |
case FE_UPWARD: |
| 343 |
return GFC_FPE_UPWARD; |
| 344 |
case FE_DOWNWARD: |
| 345 |
return GFC_FPE_DOWNWARD; |
| 346 |
case FE_TOWARDZERO: |
| 347 |
return GFC_FPE_TOWARDZERO; |
| 348 |
default: |
| 349 |
return 0; /* Should be unreachable. */ |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
void set_fpu_rounding_mode (int round) |
| 354 |
{ |
| 355 |
int rnd_mode; |
| 356 |
|
| 357 |
switch (round) |
| 358 |
{ |
| 359 |
case GFC_FPE_TONEAREST: |
| 360 |
rnd_mode = FE_TONEAREST; |
| 361 |
break; |
| 362 |
case GFC_FPE_UPWARD: |
| 363 |
rnd_mode = FE_UPWARD; |
| 364 |
break; |
| 365 |
case GFC_FPE_DOWNWARD: |
| 366 |
rnd_mode = FE_DOWNWARD; |
| 367 |
break; |
| 368 |
case GFC_FPE_TOWARDZERO: |
| 369 |
rnd_mode = FE_TOWARDZERO; |
| 370 |
break; |
| 371 |
default: |
| 372 |
return; /* Should be unreachable. */ |
| 373 |
} |
| 374 |
|
| 375 |
fesetround (rnd_mode); |
| 376 |
} |
| 377 |
|
| 378 |
int support_fpu_flag (int flag) |
| 379 |
{ |
| 380 |
if (flag & GFC_FPE_DENORMAL) |
| 381 |
return 0; |
| 382 |
|
| 383 |
return 1; |
| 384 |
} |
| 385 |
|
| 386 |
int support_fpu_trap (int flag) |
| 387 |
{ |
| 388 |
if (flag & GFC_FPE_DENORMAL) |
| 389 |
return 0; |
| 390 |
|
| 391 |
return 1; |
| 392 |
} |
| 393 |
|
| 394 |
int support_fpu_rounding_mode(int mode __attribute__((unused))) |
| 395 |
{ |
| 396 |
return 1; |
| 397 |
} |
| 398 |
|
| 399 |
/* The following are not supported. */ |
| 400 |
|
| 401 |
int support_fpu_underflow_control(int kind __attribute__((unused))) |
| 402 |
{ |
| 403 |
return 0; |
| 404 |
} |
| 405 |
|
| 406 |
int get_fpu_underflow_mode(void) |
| 407 |
{ |
| 408 |
return 0; |
| 409 |
} |
| 410 |
|
| 411 |
void set_fpu_underflow_mode(int gradual __attribute__((unused))) |
| 412 |
{ |
| 413 |
} |