Skip to content

Commit 804dc3d

Browse files
author
Siva Chandra Reddy
committed
[libc] Clear all exceptions before setting in fesetexceptflag.
Previously, exceptions from the flag were being added. This patch changes it such that only the exceptions in the flag will be set. Reviewed By: lntue Differential Revision: https://reviews.llvm.org/D105085
1 parent 0c2f40f commit 804dc3d

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

libc/src/fenv/fesetexceptflag.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ LLVM_LIBC_FUNCTION(int, fesetexceptflag,
2121
static_assert(sizeof(int) >= sizeof(fexcept_t),
2222
"fexcept_t value cannot fit in an int value.");
2323
int excepts_to_set = static_cast<const int>(*flagp) & excepts;
24+
fputil::clearExcept(FE_ALL_EXCEPT);
2425
return fputil::setExcept(excepts_to_set);
2526
}
2627

libc/test/src/fenv/exception_flags_test.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,15 @@ TEST(LlvmLibcFenvTest, GetExceptFlagAndSetExceptFlag) {
4242
// Cleanup
4343
__llvm_libc::fputil::clearExcept(e);
4444
}
45+
46+
// Next, we will raise one exception and save the flags.
47+
__llvm_libc::fputil::raiseExcept(FE_INVALID);
48+
fexcept_t eflags;
49+
__llvm_libc::fegetexceptflag(&eflags, FE_ALL_EXCEPT);
50+
// Clear all exceptions and raise two other exceptions.
51+
__llvm_libc::fputil::clearExcept(FE_ALL_EXCEPT);
52+
__llvm_libc::fputil::raiseExcept(FE_OVERFLOW | FE_INEXACT);
53+
// When we set the flags and test, we should only see FE_INVALID.
54+
__llvm_libc::fesetexceptflag(&eflags, FE_ALL_EXCEPT);
55+
EXPECT_EQ(__llvm_libc::fputil::testExcept(FE_ALL_EXCEPT), FE_INVALID);
4556
}

0 commit comments

Comments
 (0)