mirror of
https://sourceware.org/git/glibc.git
synced 2024-11-22 21:10:07 +00:00
3c1766ea10
Since fe{en,dis}ableexcept() and fesetmode() read-modify-write just the "mode" (exception enable and rounding mode) bits of the Floating Point Status Control Register (FPSCR), the lighter weight 'mffsl' instruction can be used to read the FPSCR (enables and rounding mode), and 'mtfsf 0b00000011' can be used to write just those bits back to the FPSCR. The net is better performance. In addition, fe{en,dis}ableexcept() read the FPSCR again after writing it, or they determine that it doesn't need to be written because it is not changing. In either case, the local variable holds the current values of the enable bits in the FPSCR. This local variable can be used instead of again reading the FPSCR. Also, that value of the FPSCR which is read the second time is validated against the requested enables. Since the write can't fail, this validation step is unnecessary, and can be removed. Instead, the exceptions to be enabled (or disabled) are transformed into available bits in the FPSCR, then validated after being transformed back, to ensure that all requested bits are actually being set. For example, FE_INVALID_SQRT can be requested, but cannot actually be set. This bit is not mapped during the transformations, so a test for that bit being set before and after transformations will show the bit would not be set, and the function will return -1 for failure. Finally, convert the local macros in fesetmode.c to more generally useful macros in fenv_libc.h.
51 lines
1.5 KiB
C
51 lines
1.5 KiB
C
/* Disable floating-point exceptions.
|
|
Copyright (C) 2000-2019 Free Software Foundation, Inc.
|
|
This file is part of the GNU C Library.
|
|
Contributed by Geoffrey Keating <geoffk@geoffk.org>, 2000.
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
|
modify it under the terms of the GNU Lesser General Public
|
|
License as published by the Free Software Foundation; either
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
License along with the GNU C Library; if not, see
|
|
<http://www.gnu.org/licenses/>. */
|
|
|
|
#include <fenv_libc.h>
|
|
|
|
int
|
|
fedisableexcept (int excepts)
|
|
{
|
|
fenv_union_t fe, curr;
|
|
int result, new;
|
|
|
|
/* Get current exception mask to return. */
|
|
fe.fenv = curr.fenv = fegetenv_status ();
|
|
result = fenv_reg_to_exceptions (fe.l);
|
|
|
|
if ((excepts & FE_ALL_INVALID) == FE_ALL_INVALID)
|
|
excepts = (excepts | FE_INVALID) & ~ FE_ALL_INVALID;
|
|
|
|
new = fenv_exceptions_to_reg (excepts);
|
|
|
|
if (fenv_reg_to_exceptions (new) != excepts)
|
|
return -1;
|
|
|
|
/* Sets the new exception mask. */
|
|
fe.l &= ~new;
|
|
|
|
if (fe.l != curr.l)
|
|
fesetenv_mode (fe.fenv);
|
|
|
|
if (new == 0 && result != 0)
|
|
(void)__fe_mask_env ();
|
|
|
|
return result;
|
|
}
|