2 * Copyright (c) 2012 The WebM project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
11 #ifndef TEST_REGISTER_STATE_CHECK_H_
12 #define TEST_REGISTER_STATE_CHECK_H_
14 #include "third_party/googletest/src/include/gtest/gtest.h"
15 #include "./vpx_config.h"
16 #include "vpx/vpx_integer.h"
18 // ASM_REGISTER_STATE_CHECK(asm_function)
19 // Minimally validates the environment pre & post function execution. This
20 // variant should be used with assembly functions which are not expected to
21 // fully restore the system state. See platform implementations of
22 // RegisterStateCheck for details.
24 // API_REGISTER_STATE_CHECK(api_function)
25 // Performs all the checks done by ASM_REGISTER_STATE_CHECK() and any
26 // additional checks to ensure the environment is in a consistent state pre &
27 // post function execution. This variant should be used with API functions.
28 // See platform implementations of RegisterStateCheckXXX for details.
33 #define _WIN32_LEAN_AND_MEAN
40 inline bool operator==(const M128A& lhs, const M128A& rhs) {
41 return (lhs.Low == rhs.Low && lhs.High == rhs.High);
44 } // namespace internal
45 } // namespace testing
47 namespace libvpx_test {
49 // Compares the state of xmm[6-15] at construction with their state at
50 // destruction. These registers should be preserved by the callee on
52 class RegisterStateCheck {
54 RegisterStateCheck() { initialized_ = StoreRegisters(&pre_context_); }
55 ~RegisterStateCheck() { EXPECT_TRUE(Check()); }
58 static bool StoreRegisters(CONTEXT* const context) {
59 const HANDLE this_thread = GetCurrentThread();
60 EXPECT_TRUE(this_thread != NULL);
61 context->ContextFlags = CONTEXT_FLOATING_POINT;
62 const bool context_saved = GetThreadContext(this_thread, context) == TRUE;
63 EXPECT_TRUE(context_saved) << "GetLastError: " << GetLastError();
67 // Compares the register state. Returns true if the states match.
69 if (!initialized_) return false;
71 if (!StoreRegisters(&post_context)) return false;
73 const M128A* xmm_pre = &pre_context_.Xmm6;
74 const M128A* xmm_post = &post_context.Xmm6;
75 for (int i = 6; i <= 15; ++i) {
76 EXPECT_EQ(*xmm_pre, *xmm_post) << "xmm" << i << " has been modified!";
80 return !testing::Test::HasNonfatalFailure();
87 #define ASM_REGISTER_STATE_CHECK(statement) do { \
88 libvpx_test::RegisterStateCheck reg_check; \
92 } // namespace libvpx_test
94 #elif defined(CONFIG_SHARED) && defined(HAVE_NEON_ASM) && defined(CONFIG_VP9) \
95 && !CONFIG_SHARED && HAVE_NEON_ASM && CONFIG_VP9
98 // Save the d8-d15 registers into store.
99 void vp9_push_neon(int64_t *store);
102 namespace libvpx_test {
104 // Compares the state of d8-d15 at construction with their state at
105 // destruction. These registers should be preserved by the callee on
107 class RegisterStateCheck {
109 RegisterStateCheck() { initialized_ = StoreRegisters(pre_store_); }
110 ~RegisterStateCheck() { EXPECT_TRUE(Check()); }
113 static bool StoreRegisters(int64_t store[8]) {
114 vp9_push_neon(store);
118 // Compares the register state. Returns true if the states match.
120 if (!initialized_) return false;
121 int64_t post_store[8];
122 vp9_push_neon(post_store);
123 for (int i = 0; i < 8; ++i) {
124 EXPECT_EQ(pre_store_[i], post_store[i]) << "d"
125 << i + 8 << " has been modified";
127 return !testing::Test::HasNonfatalFailure();
131 int64_t pre_store_[8];
134 #define ASM_REGISTER_STATE_CHECK(statement) do { \
135 libvpx_test::RegisterStateCheck reg_check; \
139 } // namespace libvpx_test
143 namespace libvpx_test {
145 class RegisterStateCheck {};
146 #define ASM_REGISTER_STATE_CHECK(statement) statement
148 } // namespace libvpx_test
152 #if ARCH_X86 || ARCH_X86_64
153 #if defined(__GNUC__)
155 namespace libvpx_test {
157 // Checks the FPU tag word pre/post execution to ensure emms has been called.
158 class RegisterStateCheckMMX {
160 RegisterStateCheckMMX() {
161 __asm__ volatile("fstenv %0" : "=rm"(pre_fpu_env_));
163 ~RegisterStateCheckMMX() { EXPECT_TRUE(Check()); }
166 // Checks the FPU tag word pre/post execution, returning false if not cleared
169 EXPECT_EQ(0xffff, pre_fpu_env_[4])
170 << "FPU was in an inconsistent state prior to call";
172 uint16_t post_fpu_env[14];
173 __asm__ volatile("fstenv %0" : "=rm"(post_fpu_env));
174 EXPECT_EQ(0xffff, post_fpu_env[4])
175 << "FPU was left in an inconsistent state after call";
176 return !testing::Test::HasNonfatalFailure();
179 uint16_t pre_fpu_env_[14];
182 #define API_REGISTER_STATE_CHECK(statement) do { \
183 libvpx_test::RegisterStateCheckMMX reg_check; \
184 ASM_REGISTER_STATE_CHECK(statement); \
187 } // namespace libvpx_test
190 #endif // ARCH_X86 || ARCH_X86_64
192 #ifndef API_REGISTER_STATE_CHECK
193 #define API_REGISTER_STATE_CHECK ASM_REGISTER_STATE_CHECK
196 #endif // TEST_REGISTER_STATE_CHECK_H_