1b7e05c87SSiva Chandra Reddy //===-- Unittests for fegetexceptflag and fesetexceptflag -----------------===//
2b7e05c87SSiva Chandra Reddy //
3b7e05c87SSiva Chandra Reddy // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4b7e05c87SSiva Chandra Reddy // See https://llvm.org/LICENSE.txt for license information.
5b7e05c87SSiva Chandra Reddy // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6b7e05c87SSiva Chandra Reddy //
7b7e05c87SSiva Chandra Reddy //===----------------------------------------------------------------------===//
8b7e05c87SSiva Chandra Reddy 
9b7e05c87SSiva Chandra Reddy #include "src/fenv/fegetexceptflag.h"
10b7e05c87SSiva Chandra Reddy #include "src/fenv/fesetexceptflag.h"
11b7e05c87SSiva Chandra Reddy 
12*76ec69a9STue Ly #include "src/__support/FPUtil/FEnvImpl.h"
13b7e05c87SSiva Chandra Reddy #include "utils/UnitTest/Test.h"
14b7e05c87SSiva Chandra Reddy 
15b7e05c87SSiva Chandra Reddy #include <fenv.h>
16b7e05c87SSiva Chandra Reddy 
TEST(LlvmLibcFenvTest,GetExceptFlagAndSetExceptFlag)17b7e05c87SSiva Chandra Reddy TEST(LlvmLibcFenvTest, GetExceptFlagAndSetExceptFlag) {
18b7e05c87SSiva Chandra Reddy   // We will disable all exceptions to prevent invocation of the exception
19b7e05c87SSiva Chandra Reddy   // handler.
201c92911eSMichael Jones   __llvm_libc::fputil::disable_except(FE_ALL_EXCEPT);
211c92911eSMichael Jones   __llvm_libc::fputil::clear_except(FE_ALL_EXCEPT);
22b7e05c87SSiva Chandra Reddy 
23b7e05c87SSiva Chandra Reddy   int excepts[] = {FE_DIVBYZERO, FE_INVALID, FE_INEXACT, FE_OVERFLOW,
24b7e05c87SSiva Chandra Reddy                    FE_UNDERFLOW};
25b7e05c87SSiva Chandra Reddy 
26b7e05c87SSiva Chandra Reddy   for (int e : excepts) {
27b7e05c87SSiva Chandra Reddy     // The overall idea is to raise an except and save the exception flags.
28b7e05c87SSiva Chandra Reddy     // Next, clear the flags and then set the saved exception flags. This
29b7e05c87SSiva Chandra Reddy     // should set the flag corresponding to the previously raised exception.
301c92911eSMichael Jones     __llvm_libc::fputil::raise_except(e);
31b7e05c87SSiva Chandra Reddy     // Make sure that the exception flag is set.
321c92911eSMichael Jones     ASSERT_NE(__llvm_libc::fputil::test_except(FE_ALL_EXCEPT) & e, 0);
33b7e05c87SSiva Chandra Reddy 
34b7e05c87SSiva Chandra Reddy     fexcept_t eflags;
35b7e05c87SSiva Chandra Reddy     ASSERT_EQ(__llvm_libc::fegetexceptflag(&eflags, FE_ALL_EXCEPT), 0);
36b7e05c87SSiva Chandra Reddy 
371c92911eSMichael Jones     __llvm_libc::fputil::clear_except(e);
381c92911eSMichael Jones     ASSERT_EQ(__llvm_libc::fputil::test_except(FE_ALL_EXCEPT) & e, 0);
39b7e05c87SSiva Chandra Reddy 
40b7e05c87SSiva Chandra Reddy     ASSERT_EQ(__llvm_libc::fesetexceptflag(&eflags, FE_ALL_EXCEPT), 0);
411c92911eSMichael Jones     ASSERT_NE(__llvm_libc::fputil::test_except(FE_ALL_EXCEPT) & e, 0);
42b7e05c87SSiva Chandra Reddy 
43578a4cfeSSiva Chandra     // Cleanup. We clear all excepts as raising excepts like FE_OVERFLOW
44578a4cfeSSiva Chandra     // can also raise FE_INEXACT.
451c92911eSMichael Jones     __llvm_libc::fputil::clear_except(FE_ALL_EXCEPT);
46b7e05c87SSiva Chandra Reddy   }
47804dc3dcSSiva Chandra Reddy 
48804dc3dcSSiva Chandra Reddy   // Next, we will raise one exception and save the flags.
491c92911eSMichael Jones   __llvm_libc::fputil::raise_except(FE_INVALID);
50804dc3dcSSiva Chandra Reddy   fexcept_t eflags;
51804dc3dcSSiva Chandra Reddy   __llvm_libc::fegetexceptflag(&eflags, FE_ALL_EXCEPT);
52804dc3dcSSiva Chandra Reddy   // Clear all exceptions and raise two other exceptions.
531c92911eSMichael Jones   __llvm_libc::fputil::clear_except(FE_ALL_EXCEPT);
541c92911eSMichael Jones   __llvm_libc::fputil::raise_except(FE_OVERFLOW | FE_INEXACT);
55804dc3dcSSiva Chandra Reddy   // When we set the flags and test, we should only see FE_INVALID.
56804dc3dcSSiva Chandra Reddy   __llvm_libc::fesetexceptflag(&eflags, FE_ALL_EXCEPT);
571c92911eSMichael Jones   EXPECT_EQ(__llvm_libc::fputil::test_except(FE_ALL_EXCEPT), FE_INVALID);
58b7e05c87SSiva Chandra Reddy }
59