1b7e05c87SSiva Chandra Reddy //===-- Unittests for fegetenv and fesetenv -------------------------------===// 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 975bbf4ddSJob Henandez Lara #include "hdr/types/fenv_t.h" 10b7e05c87SSiva Chandra Reddy #include "src/fenv/fegetenv.h" 115c3c716bSSiva Chandra Reddy #include "src/fenv/fegetround.h" 12b7e05c87SSiva Chandra Reddy #include "src/fenv/fesetenv.h" 135c3c716bSSiva Chandra Reddy #include "src/fenv/fesetround.h" 14b7e05c87SSiva Chandra Reddy 1576ec69a9STue Ly #include "src/__support/FPUtil/FEnvImpl.h" 16*ded08015SSirui Mu #include "src/__support/macros/properties/os.h" 17837dab96SRoland McGrath #include "test/UnitTest/FEnvSafeTest.h" 18af1315c2SSiva Chandra Reddy #include "test/UnitTest/Test.h" 19b7e05c87SSiva Chandra Reddy 20837dab96SRoland McGrath #include "excepts.h" 21837dab96SRoland McGrath 22837dab96SRoland McGrath using LlvmLibcFEnvTest = LIBC_NAMESPACE::testing::FEnvSafeTest; 23837dab96SRoland McGrath 24*ded08015SSirui Mu #ifndef LIBC_TARGET_OS_IS_WINDOWS 25837dab96SRoland McGrath TEST_F(LlvmLibcFEnvTest, GetEnvAndSetEnv) { 26b7e05c87SSiva Chandra Reddy // We will disable all exceptions to prevent invocation of the exception 27b7e05c87SSiva Chandra Reddy // handler. 28b6bc9d72SGuillaume Chatelet LIBC_NAMESPACE::fputil::disable_except(FE_ALL_EXCEPT); 29b7e05c87SSiva Chandra Reddy 30837dab96SRoland McGrath for (int e : EXCEPTS) { 31b6bc9d72SGuillaume Chatelet LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT); 32b7e05c87SSiva Chandra Reddy 33b7e05c87SSiva Chandra Reddy // Save the cleared environment. 34b7e05c87SSiva Chandra Reddy fenv_t env; 35b6bc9d72SGuillaume Chatelet ASSERT_EQ(LIBC_NAMESPACE::fegetenv(&env), 0); 36b7e05c87SSiva Chandra Reddy 37b6bc9d72SGuillaume Chatelet LIBC_NAMESPACE::fputil::raise_except(e); 38b7e05c87SSiva Chandra Reddy // Make sure that the exception is raised. 39b6bc9d72SGuillaume Chatelet ASSERT_NE(LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT) & e, 0); 40b7e05c87SSiva Chandra Reddy 41b6bc9d72SGuillaume Chatelet ASSERT_EQ(LIBC_NAMESPACE::fesetenv(&env), 0); 42b6bc9d72SGuillaume Chatelet ASSERT_EQ(LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT) & e, 0); 43b7e05c87SSiva Chandra Reddy } 44b7e05c87SSiva Chandra Reddy } 455c3c716bSSiva Chandra Reddy 46392ca648SJordan Rupprecht TEST_F(LlvmLibcFEnvTest, Set_FE_DFL_ENV) { 475c3c716bSSiva Chandra Reddy // We will disable all exceptions to prevent invocation of the exception 485c3c716bSSiva Chandra Reddy // handler. 49b6bc9d72SGuillaume Chatelet LIBC_NAMESPACE::fputil::disable_except(FE_ALL_EXCEPT); 505c3c716bSSiva Chandra Reddy 515c3c716bSSiva Chandra Reddy int excepts[] = {FE_DIVBYZERO, FE_INVALID, FE_INEXACT, FE_OVERFLOW, 525c3c716bSSiva Chandra Reddy FE_UNDERFLOW}; 535c3c716bSSiva Chandra Reddy 545c3c716bSSiva Chandra Reddy for (int e : excepts) { 55b6bc9d72SGuillaume Chatelet LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT); 565c3c716bSSiva Chandra Reddy 575c3c716bSSiva Chandra Reddy // Save the cleared environment. 585c3c716bSSiva Chandra Reddy fenv_t env; 59b6bc9d72SGuillaume Chatelet ASSERT_EQ(LIBC_NAMESPACE::fegetenv(&env), 0); 605c3c716bSSiva Chandra Reddy 61b6bc9d72SGuillaume Chatelet LIBC_NAMESPACE::fputil::raise_except(e); 625c3c716bSSiva Chandra Reddy // Make sure that the exception is raised. 63b6bc9d72SGuillaume Chatelet ASSERT_NE(LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT) & e, 0); 645c3c716bSSiva Chandra Reddy 65b6bc9d72SGuillaume Chatelet ASSERT_EQ(LIBC_NAMESPACE::fesetenv(FE_DFL_ENV), 0); 665c3c716bSSiva Chandra Reddy // Setting the default env should clear all exceptions. 67b6bc9d72SGuillaume Chatelet ASSERT_EQ(LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT) & e, 0); 685c3c716bSSiva Chandra Reddy } 695c3c716bSSiva Chandra Reddy 70b6bc9d72SGuillaume Chatelet ASSERT_EQ(LIBC_NAMESPACE::fesetround(FE_DOWNWARD), 0); 71b6bc9d72SGuillaume Chatelet ASSERT_EQ(LIBC_NAMESPACE::fesetenv(FE_DFL_ENV), 0); 725c3c716bSSiva Chandra Reddy // Setting the default env should set rounding mode to FE_TONEAREST. 73b6bc9d72SGuillaume Chatelet int rm = LIBC_NAMESPACE::fegetround(); 745c3c716bSSiva Chandra Reddy EXPECT_EQ(rm, FE_TONEAREST); 755c3c716bSSiva Chandra Reddy } 76*ded08015SSirui Mu #endif 77805899e6SMichael Jones 78*ded08015SSirui Mu #ifdef LIBC_TARGET_OS_IS_WINDOWS 79837dab96SRoland McGrath TEST_F(LlvmLibcFEnvTest, Windows_Set_Get_Test) { 80805899e6SMichael Jones // If a valid fenv_t is written, then reading it back out should be identical. 81805899e6SMichael Jones fenv_t setEnv = {0x7e00053e, 0x0f00000f}; 82805899e6SMichael Jones fenv_t getEnv; 83b6bc9d72SGuillaume Chatelet ASSERT_EQ(LIBC_NAMESPACE::fesetenv(&setEnv), 0); 84b6bc9d72SGuillaume Chatelet ASSERT_EQ(LIBC_NAMESPACE::fegetenv(&getEnv), 0); 85805899e6SMichael Jones 86805899e6SMichael Jones ASSERT_EQ(setEnv._Fe_ctl, getEnv._Fe_ctl); 87805899e6SMichael Jones ASSERT_EQ(setEnv._Fe_stat, getEnv._Fe_stat); 88805899e6SMichael Jones } 89805899e6SMichael Jones #endif 90