aboutsummaryrefslogtreecommitdiff
path: root/libc/test/src/fenv/feenableexcept_test.cpp
blob: c27a2bff6a0387205cc0695de563c315602e3f1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//===-- Unittests for feenableexcept  -------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/__support/macros/properties/architectures.h"
#include "src/fenv/fedisableexcept.h"
#include "src/fenv/feenableexcept.h"
#include "src/fenv/fegetexcept.h"

#include "test/UnitTest/Test.h"

#include <fenv.h>

TEST(LlvmLibcFEnvTest, EnableTest) {
#if defined(LIBC_TARGET_ARCH_IS_ANY_ARM) || defined(LIBC_TARGET_ARCH_IS_RISCV64)
  // Few Arm HW implementations do not trap exceptions. We skip this test
  // completely on such HW.
  //
  // Whether HW supports trapping exceptions or not is deduced by enabling an
  // exception and reading back to see if the exception got enabled. If the
  // exception did not get enabled, then it means that the HW does not support
  // trapping exceptions.
  __llvm_libc::fedisableexcept(FE_ALL_EXCEPT);
  __llvm_libc::feenableexcept(FE_DIVBYZERO);
  if (__llvm_libc::fegetexcept() == 0)
    return;
#endif // Architectures where exception trapping is not supported

  int excepts[] = {FE_DIVBYZERO, FE_INVALID, FE_INEXACT, FE_OVERFLOW,
                   FE_UNDERFLOW};
  __llvm_libc::fedisableexcept(FE_ALL_EXCEPT);
  ASSERT_EQ(0, __llvm_libc::fegetexcept());

  for (int e : excepts) {
    __llvm_libc::feenableexcept(e);
    ASSERT_EQ(e, __llvm_libc::fegetexcept());
    __llvm_libc::fedisableexcept(e);
  }

  for (int e1 : excepts) {
    for (int e2 : excepts) {
      __llvm_libc::feenableexcept(e1 | e2);
      ASSERT_EQ(e1 | e2, __llvm_libc::fegetexcept());
      __llvm_libc::fedisableexcept(e1 | e2);
    }
  }

  for (int e1 : excepts) {
    for (int e2 : excepts) {
      for (int e3 : excepts) {
        __llvm_libc::feenableexcept(e1 | e2 | e3);
        ASSERT_EQ(e1 | e2 | e3, __llvm_libc::fegetexcept());
        __llvm_libc::fedisableexcept(e1 | e2 | e3);
      }
    }
  }

  for (int e1 : excepts) {
    for (int e2 : excepts) {
      for (int e3 : excepts) {
        for (int e4 : excepts) {
          __llvm_libc::feenableexcept(e1 | e2 | e3 | e4);
          ASSERT_EQ(e1 | e2 | e3 | e4, __llvm_libc::fegetexcept());
          __llvm_libc::fedisableexcept(e1 | e2 | e3 | e4);
        }
      }
    }
  }

  for (int e1 : excepts) {
    for (int e2 : excepts) {
      for (int e3 : excepts) {
        for (int e4 : excepts) {
          for (int e5 : excepts) {
            __llvm_libc::feenableexcept(e1 | e2 | e3 | e4 | e5);
            ASSERT_EQ(e1 | e2 | e3 | e4 | e5, __llvm_libc::fegetexcept());
            __llvm_libc::fedisableexcept(e1 | e2 | e3 | e4 | e5);
          }
        }
      }
    }
  }
}