1 | //===-- FEnvSafeTest.cpp ---------------------------------------*- C++ -*-===// |
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 "FEnvSafeTest.h" |
10 | |
11 | #include "src/__support/FPUtil/FEnvImpl.h" |
12 | #include "src/__support/macros/properties/architectures.h" |
13 | |
14 | namespace LIBC_NAMESPACE::testing { |
15 | |
16 | void FEnvSafeTest::PreserveFEnv::check() { |
17 | fenv_t after; |
18 | test.get_fenv(fenv&: after); |
19 | test.expect_fenv_eq(before_fenv: before, after_fenv: after); |
20 | } |
21 | |
22 | void FEnvSafeTest::TearDown() { |
23 | if (!should_be_unchanged) { |
24 | restore_fenv(); |
25 | } |
26 | } |
27 | |
28 | void FEnvSafeTest::get_fenv(fenv_t &fenv) { |
29 | ASSERT_EQ(LIBC_NAMESPACE::fputil::get_env(&fenv), 0); |
30 | } |
31 | |
32 | void FEnvSafeTest::set_fenv(const fenv_t &fenv) { |
33 | ASSERT_EQ(LIBC_NAMESPACE::fputil::set_env(&fenv), 0); |
34 | } |
35 | |
36 | void FEnvSafeTest::expect_fenv_eq(const fenv_t &before_fenv, |
37 | const fenv_t &after_fenv) { |
38 | #if defined(LIBC_TARGET_ARCH_IS_AARCH64) |
39 | using FPState = LIBC_NAMESPACE::fputil::FEnv::FPState; |
40 | const FPState &before_state = reinterpret_cast<const FPState &>(before_fenv); |
41 | const FPState &after_state = reinterpret_cast<const FPState &>(after_fenv); |
42 | |
43 | EXPECT_EQ(before_state.ControlWord, after_state.ControlWord); |
44 | EXPECT_EQ(before_state.StatusWord, after_state.StatusWord); |
45 | |
46 | #elif defined(LIBC_TARGET_ARCH_IS_X86) && !defined(__APPLE__) |
47 | using LIBC_NAMESPACE::fputil::internal::FPState; |
48 | const FPState &before_state = reinterpret_cast<const FPState &>(before_fenv); |
49 | const FPState &after_state = reinterpret_cast<const FPState &>(after_fenv); |
50 | |
51 | #if defined(_WIN32) |
52 | EXPECT_EQ(before_state.control_word, after_state.control_word); |
53 | EXPECT_EQ(before_state.status_word, after_state.status_word); |
54 | #elif defined(__APPLE__) |
55 | EXPECT_EQ(before_state.control_word, after_state.control_word); |
56 | EXPECT_EQ(before_state.status_word, after_state.status_word); |
57 | EXPECT_EQ(before_state.mxcsr, after_state.mxcsr); |
58 | #else |
59 | EXPECT_EQ(before_state.x87_status.control_word, |
60 | after_state.x87_status.control_word); |
61 | EXPECT_EQ(before_state.x87_status.status_word, |
62 | after_state.x87_status.status_word); |
63 | EXPECT_EQ(before_state.mxcsr, after_state.mxcsr); |
64 | #endif |
65 | |
66 | #elif defined(LIBC_TARGET_ARCH_IS_ARM) && defined(__ARM_FP) |
67 | using LIBC_NAMESPACE::fputil::FEnv; |
68 | const FEnv &before_state = reinterpret_cast<const FEnv &>(before_fenv); |
69 | const FEnv &after_state = reinterpret_cast<const FEnv &>(after_fenv); |
70 | |
71 | EXPECT_EQ(before_state.fpscr, after_state.fpscr); |
72 | |
73 | #elif defined(LIBC_TARGET_ARCH_IS_ANY_RISCV) |
74 | const uint32_t &before_fcsr = reinterpret_cast<const uint32_t &>(before_fenv); |
75 | const uint32_t &after_fcsr = reinterpret_cast<const uint32_t &>(after_fenv); |
76 | EXPECT_EQ(before_fcsr, after_fcsr); |
77 | |
78 | #else |
79 | // No arch-specific `fenv_t` support, so nothing to compare. |
80 | |
81 | #endif |
82 | } |
83 | |
84 | } // namespace LIBC_NAMESPACE::testing |
85 | |