register_state_check.h 5.91 KB
Newer Older
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3
 *
4 5 6 7 8 9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 11
 */

Yaowu Xu's avatar
Yaowu Xu committed
12 13
#ifndef TEST_REGISTER_STATE_CHECK_H_
#define TEST_REGISTER_STATE_CHECK_H_
14

15
#include "third_party/googletest/src/include/gtest/gtest.h"
Adrian Grange's avatar
Adrian Grange committed
16 17
#include "./aom_config.h"
#include "aom/aom_integer.h"
18 19 20 21 22 23 24 25 26 27 28 29 30

// ASM_REGISTER_STATE_CHECK(asm_function)
//   Minimally validates the environment pre & post function execution. This
//   variant should be used with assembly functions which are not expected to
//   fully restore the system state. See platform implementations of
//   RegisterStateCheck for details.
//
// API_REGISTER_STATE_CHECK(api_function)
//   Performs all the checks done by ASM_REGISTER_STATE_CHECK() and any
//   additional checks to ensure the environment is in a consistent state pre &
//   post function execution. This variant should be used with API functions.
//   See platform implementations of RegisterStateCheckXXX for details.
//
31 32

#if defined(_WIN64)
33

34
#undef NOMINMAX
35 36
#define NOMINMAX
#define WIN32_LEAN_AND_MEAN
37 38 39 40 41 42 43 44 45 46 47 48 49
#include <windows.h>
#include <winnt.h>

namespace testing {
namespace internal {

inline bool operator==(const M128A& lhs, const M128A& rhs) {
  return (lhs.Low == rhs.Low && lhs.High == rhs.High);
}

}  // namespace internal
}  // namespace testing

Yaowu Xu's avatar
Yaowu Xu committed
50
namespace libaom_test {
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 88 89

// Compares the state of xmm[6-15] at construction with their state at
// destruction. These registers should be preserved by the callee on
// Windows x64.
class RegisterStateCheck {
 public:
  RegisterStateCheck() { initialized_ = StoreRegisters(&pre_context_); }
  ~RegisterStateCheck() { EXPECT_TRUE(Check()); }

 private:
  static bool StoreRegisters(CONTEXT* const context) {
    const HANDLE this_thread = GetCurrentThread();
    EXPECT_TRUE(this_thread != NULL);
    context->ContextFlags = CONTEXT_FLOATING_POINT;
    const bool context_saved = GetThreadContext(this_thread, context) == TRUE;
    EXPECT_TRUE(context_saved) << "GetLastError: " << GetLastError();
    return context_saved;
  }

  // Compares the register state. Returns true if the states match.
  bool Check() const {
    if (!initialized_) return false;
    CONTEXT post_context;
    if (!StoreRegisters(&post_context)) return false;

    const M128A* xmm_pre = &pre_context_.Xmm6;
    const M128A* xmm_post = &post_context.Xmm6;
    for (int i = 6; i <= 15; ++i) {
      EXPECT_EQ(*xmm_pre, *xmm_post) << "xmm" << i << " has been modified!";
      ++xmm_pre;
      ++xmm_post;
    }
    return !testing::Test::HasNonfatalFailure();
  }

  bool initialized_;
  CONTEXT pre_context_;
};

clang-format's avatar
clang-format committed
90 91
#define ASM_REGISTER_STATE_CHECK(statement)    \
  do {                                         \
Yaowu Xu's avatar
Yaowu Xu committed
92
    libaom_test::RegisterStateCheck reg_check; \
clang-format's avatar
clang-format committed
93 94
    statement;                                 \
  } while (false)
95

Yaowu Xu's avatar
Yaowu Xu committed
96
}  // namespace libaom_test
97

clang-format's avatar
clang-format committed
98
#elif defined(CONFIG_SHARED) && defined(HAVE_NEON_ASM) && !CONFIG_SHARED && \
99
    HAVE_NEON_ASM && CONFIG_AV1
100 101 102

extern "C" {
// Save the d8-d15 registers into store.
Adrian Grange's avatar
Adrian Grange committed
103
void aom_push_neon(int64_t *store);
104 105
}

Yaowu Xu's avatar
Yaowu Xu committed
106
namespace libaom_test {
107 108 109 110 111 112 113 114 115 116 117

// Compares the state of d8-d15 at construction with their state at
// destruction. These registers should be preserved by the callee on
// arm platform.
class RegisterStateCheck {
 public:
  RegisterStateCheck() { initialized_ = StoreRegisters(pre_store_); }
  ~RegisterStateCheck() { EXPECT_TRUE(Check()); }

 private:
  static bool StoreRegisters(int64_t store[8]) {
Adrian Grange's avatar
Adrian Grange committed
118
    aom_push_neon(store);
119 120 121 122 123 124 125
    return true;
  }

  // Compares the register state. Returns true if the states match.
  bool Check() const {
    if (!initialized_) return false;
    int64_t post_store[8];
Adrian Grange's avatar
Adrian Grange committed
126
    aom_push_neon(post_store);
127
    for (int i = 0; i < 8; ++i) {
clang-format's avatar
clang-format committed
128 129
      EXPECT_EQ(pre_store_[i], post_store[i]) << "d" << i + 8
                                              << " has been modified";
130 131 132 133 134 135 136 137
    }
    return !testing::Test::HasNonfatalFailure();
  }

  bool initialized_;
  int64_t pre_store_[8];
};

clang-format's avatar
clang-format committed
138 139
#define ASM_REGISTER_STATE_CHECK(statement)    \
  do {                                         \
Yaowu Xu's avatar
Yaowu Xu committed
140
    libaom_test::RegisterStateCheck reg_check; \
clang-format's avatar
clang-format committed
141 142
    statement;                                 \
  } while (false)
143

Yaowu Xu's avatar
Yaowu Xu committed
144
}  // namespace libaom_test
145 146

#else
147

Yaowu Xu's avatar
Yaowu Xu committed
148
namespace libaom_test {
149 150

class RegisterStateCheck {};
151
#define ASM_REGISTER_STATE_CHECK(statement) statement
152

Yaowu Xu's avatar
Yaowu Xu committed
153
}  // namespace libaom_test
154 155 156

#endif  // _WIN64

157 158 159
#if ARCH_X86 || ARCH_X86_64
#if defined(__GNUC__)

Yaowu Xu's avatar
Yaowu Xu committed
160
namespace libaom_test {
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

// Checks the FPU tag word pre/post execution to ensure emms has been called.
class RegisterStateCheckMMX {
 public:
  RegisterStateCheckMMX() {
    __asm__ volatile("fstenv %0" : "=rm"(pre_fpu_env_));
  }
  ~RegisterStateCheckMMX() { EXPECT_TRUE(Check()); }

 private:
  // Checks the FPU tag word pre/post execution, returning false if not cleared
  // to 0xffff.
  bool Check() const {
    EXPECT_EQ(0xffff, pre_fpu_env_[4])
        << "FPU was in an inconsistent state prior to call";

    uint16_t post_fpu_env[14];
    __asm__ volatile("fstenv %0" : "=rm"(post_fpu_env));
    EXPECT_EQ(0xffff, post_fpu_env[4])
        << "FPU was left in an inconsistent state after call";
    return !testing::Test::HasNonfatalFailure();
  }

  uint16_t pre_fpu_env_[14];
};

clang-format's avatar
clang-format committed
187 188
#define API_REGISTER_STATE_CHECK(statement)       \
  do {                                            \
Yaowu Xu's avatar
Yaowu Xu committed
189
    libaom_test::RegisterStateCheckMMX reg_check; \
clang-format's avatar
clang-format committed
190 191
    ASM_REGISTER_STATE_CHECK(statement);          \
  } while (false)
192

Yaowu Xu's avatar
Yaowu Xu committed
193
}  // namespace libaom_test
194 195 196 197 198 199 200 201

#endif  // __GNUC__
#endif  // ARCH_X86 || ARCH_X86_64

#ifndef API_REGISTER_STATE_CHECK
#define API_REGISTER_STATE_CHECK ASM_REGISTER_STATE_CHECK
#endif

Yaowu Xu's avatar
Yaowu Xu committed
202
#endif  // TEST_REGISTER_STATE_CHECK_H_