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 //===----------------------------------------------------------------------===//
8 
9 #include "src/fenv/fedisableexcept.h"
10 #include "src/fenv/feenableexcept.h"
11 #include "src/fenv/fegetexcept.h"
12 
13 #include "utils/UnitTest/Test.h"
14 
15 #include <fenv.h>
16 
17 TEST(LlvmLibcFEnvTest, EnableTest) {
18 #ifdef __aarch64__
19   // Few aarch64 HW implementations do not trap exceptions. We skip this test
20   // completely on such HW.
21   //
22   // Whether HW supports trapping exceptions or not is deduced by enabling an
23   // exception and reading back to see if the exception got enabled. If the
24   // exception did not get enabled, then it means that the HW does not support
25   // trapping exceptions.
26   __llvm_libc::fedisableexcept(FE_ALL_EXCEPT);
27   __llvm_libc::feenableexcept(FE_DIVBYZERO);
28   if (__llvm_libc::fegetexcept() == 0)
29     return;
30 #endif
31 
32   int excepts[] = {FE_DIVBYZERO, FE_INVALID, FE_INEXACT, FE_OVERFLOW,
33                    FE_UNDERFLOW};
34   __llvm_libc::fedisableexcept(FE_ALL_EXCEPT);
35   ASSERT_EQ(0, __llvm_libc::fegetexcept());
36 
37   for (int e : excepts) {
38     __llvm_libc::feenableexcept(e);
39     ASSERT_EQ(e, __llvm_libc::fegetexcept());
40     __llvm_libc::fedisableexcept(e);
41   }
42 
43   for (int e1 : excepts) {
44     for (int e2 : excepts) {
45       __llvm_libc::feenableexcept(e1 | e2);
46       ASSERT_EQ(e1 | e2, __llvm_libc::fegetexcept());
47       __llvm_libc::fedisableexcept(e1 | e2);
48     }
49   }
50 
51   for (int e1 : excepts) {
52     for (int e2 : excepts) {
53       for (int e3 : excepts) {
54         __llvm_libc::feenableexcept(e1 | e2 | e3);
55         ASSERT_EQ(e1 | e2 | e3, __llvm_libc::fegetexcept());
56         __llvm_libc::fedisableexcept(e1 | e2 | e3);
57       }
58     }
59   }
60 
61   for (int e1 : excepts) {
62     for (int e2 : excepts) {
63       for (int e3 : excepts) {
64         for (int e4 : excepts) {
65           __llvm_libc::feenableexcept(e1 | e2 | e3 | e4);
66           ASSERT_EQ(e1 | e2 | e3 | e4, __llvm_libc::fegetexcept());
67           __llvm_libc::fedisableexcept(e1 | e2 | e3 | e4);
68         }
69       }
70     }
71   }
72 
73   for (int e1 : excepts) {
74     for (int e2 : excepts) {
75       for (int e3 : excepts) {
76         for (int e4 : excepts) {
77           for (int e5 : excepts) {
78             __llvm_libc::feenableexcept(e1 | e2 | e3 | e4 | e5);
79             ASSERT_EQ(e1 | e2 | e3 | e4 | e5, __llvm_libc::fegetexcept());
80             __llvm_libc::fedisableexcept(e1 | e2 | e3 | e4 | e5);
81           }
82         }
83       }
84     }
85   }
86 }
87