| 1 |
/* Copyright (C) 2013-2025 Free Software Foundation, Inc. |
| 2 |
|
| 3 |
This file is part of the GNU Atomic Library (libatomic). |
| 4 |
|
| 5 |
Libatomic is free software; you can redistribute it and/or modify it |
| 6 |
under the terms of the GNU General Public License as published by |
| 7 |
the Free Software Foundation; either version 3 of the License, or |
| 8 |
(at your option) any later version. |
| 9 |
|
| 10 |
Libatomic is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 12 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 13 |
more details. |
| 14 |
|
| 15 |
Under Section 7 of GPL version 3, you are granted additional |
| 16 |
permissions described in the GCC Runtime Library Exception, version |
| 17 |
3.1, as published by the Free Software Foundation. |
| 18 |
|
| 19 |
You should have received a copy of the GNU General Public License and |
| 20 |
a copy of the GCC Runtime Library Exception along with this program; |
| 21 |
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 22 |
<http://www.gnu.org/licenses/>. */ |
| 23 |
|
| 24 |
#include "libatomic_i.h" |
| 25 |
|
| 26 |
#define FE_INVALID 0x01 |
| 27 |
#define FE_DENORM 0x02 |
| 28 |
#define FE_DIVBYZERO 0x04 |
| 29 |
#define FE_OVERFLOW 0x08 |
| 30 |
#define FE_UNDERFLOW 0x10 |
| 31 |
#define FE_INEXACT 0x20 |
| 32 |
|
| 33 |
struct fenv |
| 34 |
{ |
| 35 |
unsigned short int __control_word; |
| 36 |
unsigned short int __unused1; |
| 37 |
unsigned short int __status_word; |
| 38 |
unsigned short int __unused2; |
| 39 |
unsigned short int __tags; |
| 40 |
unsigned short int __unused3; |
| 41 |
unsigned int __eip; |
| 42 |
unsigned short int __cs_selector; |
| 43 |
unsigned int __opcode:11; |
| 44 |
unsigned int __unused4:5; |
| 45 |
unsigned int __data_offset; |
| 46 |
unsigned short int __data_selector; |
| 47 |
unsigned short int __unused5; |
| 48 |
} __attribute__ ((gcc_struct)); |
| 49 |
|
| 50 |
#ifdef __SSE_MATH__ |
| 51 |
# define __math_force_eval_div(x, y) \ |
| 52 |
do { asm ("" : "+x" (x)); asm volatile ("" : : "x" (x / y)); } while (0) |
| 53 |
#else |
| 54 |
# define __math_force_eval_div(x, y) \ |
| 55 |
do { asm ("" : "+t" (x)); asm volatile ("" : : "f" (x / y)); } while (0) |
| 56 |
#endif |
| 57 |
|
| 58 |
/* Raise the supported floating-point exceptions from EXCEPTS. Other |
| 59 |
bits in EXCEPTS are ignored. */ |
| 60 |
|
| 61 |
void |
| 62 |
__atomic_feraiseexcept (int excepts) |
| 63 |
{ |
| 64 |
struct fenv temp; |
| 65 |
|
| 66 |
if (excepts & FE_INVALID) |
| 67 |
{ |
| 68 |
float f = 0.0f; |
| 69 |
__math_force_eval_div (f, f); |
| 70 |
} |
| 71 |
if (excepts & FE_DENORM) |
| 72 |
{ |
| 73 |
asm volatile ("fnstenv\t%0" : "=m" (temp)); |
| 74 |
temp.__status_word |= FE_DENORM; |
| 75 |
asm volatile ("fldenv\t%0" : : "m" (temp)); |
| 76 |
asm volatile ("fwait"); |
| 77 |
} |
| 78 |
if (excepts & FE_DIVBYZERO) |
| 79 |
{ |
| 80 |
float f = 1.0f, g = 0.0f; |
| 81 |
__math_force_eval_div (f, g); |
| 82 |
} |
| 83 |
if (excepts & FE_OVERFLOW) |
| 84 |
{ |
| 85 |
asm volatile ("fnstenv\t%0" : "=m" (temp)); |
| 86 |
temp.__status_word |= FE_OVERFLOW; |
| 87 |
asm volatile ("fldenv\t%0" : : "m" (temp)); |
| 88 |
asm volatile ("fwait"); |
| 89 |
} |
| 90 |
if (excepts & FE_UNDERFLOW) |
| 91 |
{ |
| 92 |
asm volatile ("fnstenv\t%0" : "=m" (temp)); |
| 93 |
temp.__status_word |= FE_UNDERFLOW; |
| 94 |
asm volatile ("fldenv\t%0" : : "m" (temp)); |
| 95 |
asm volatile ("fwait"); |
| 96 |
} |
| 97 |
if (excepts & FE_INEXACT) |
| 98 |
{ |
| 99 |
float f = 1.0f, g = 3.0f; |
| 100 |
__math_force_eval_div (f, g); |
| 101 |
} |
| 102 |
} |