Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / test / src / fenv / feenableexcept_test.cpp
blob41c1945368ed5a9be5a9efb239dbd63017874694
1 //===-- Unittests for feenableexcept -------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "src/__support/macros/properties/architectures.h"
10 #include "src/fenv/fedisableexcept.h"
11 #include "src/fenv/feenableexcept.h"
12 #include "src/fenv/fegetexcept.h"
14 #include "test/UnitTest/Test.h"
16 #include <fenv.h>
18 TEST(LlvmLibcFEnvTest, EnableTest) {
19 #if defined(LIBC_TARGET_ARCH_IS_ANY_ARM) || \
20 defined(LIBC_TARGET_ARCH_IS_ANY_RISCV)
21 // Few Arm HW implementations do not trap exceptions. We skip this test
22 // completely on such HW.
24 // Whether HW supports trapping exceptions or not is deduced by enabling an
25 // exception and reading back to see if the exception got enabled. If the
26 // exception did not get enabled, then it means that the HW does not support
27 // trapping exceptions.
28 LIBC_NAMESPACE::fedisableexcept(FE_ALL_EXCEPT);
29 LIBC_NAMESPACE::feenableexcept(FE_DIVBYZERO);
30 if (LIBC_NAMESPACE::fegetexcept() == 0)
31 return;
32 #endif // Architectures where exception trapping is not supported
34 int excepts[] = {FE_DIVBYZERO, FE_INVALID, FE_INEXACT, FE_OVERFLOW,
35 FE_UNDERFLOW};
36 LIBC_NAMESPACE::fedisableexcept(FE_ALL_EXCEPT);
37 ASSERT_EQ(0, LIBC_NAMESPACE::fegetexcept());
39 for (int e : excepts) {
40 LIBC_NAMESPACE::feenableexcept(e);
41 ASSERT_EQ(e, LIBC_NAMESPACE::fegetexcept());
42 LIBC_NAMESPACE::fedisableexcept(e);
45 for (int e1 : excepts) {
46 for (int e2 : excepts) {
47 LIBC_NAMESPACE::feenableexcept(e1 | e2);
48 ASSERT_EQ(e1 | e2, LIBC_NAMESPACE::fegetexcept());
49 LIBC_NAMESPACE::fedisableexcept(e1 | e2);
53 for (int e1 : excepts) {
54 for (int e2 : excepts) {
55 for (int e3 : excepts) {
56 LIBC_NAMESPACE::feenableexcept(e1 | e2 | e3);
57 ASSERT_EQ(e1 | e2 | e3, LIBC_NAMESPACE::fegetexcept());
58 LIBC_NAMESPACE::fedisableexcept(e1 | e2 | e3);
63 for (int e1 : excepts) {
64 for (int e2 : excepts) {
65 for (int e3 : excepts) {
66 for (int e4 : excepts) {
67 LIBC_NAMESPACE::feenableexcept(e1 | e2 | e3 | e4);
68 ASSERT_EQ(e1 | e2 | e3 | e4, LIBC_NAMESPACE::fegetexcept());
69 LIBC_NAMESPACE::fedisableexcept(e1 | e2 | e3 | e4);
75 for (int e1 : excepts) {
76 for (int e2 : excepts) {
77 for (int e3 : excepts) {
78 for (int e4 : excepts) {
79 for (int e5 : excepts) {
80 LIBC_NAMESPACE::feenableexcept(e1 | e2 | e3 | e4 | e5);
81 ASSERT_EQ(e1 | e2 | e3 | e4 | e5, LIBC_NAMESPACE::fegetexcept());
82 LIBC_NAMESPACE::fedisableexcept(e1 | e2 | e3 | e4 | e5);