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.
35 #ifndef WIN32_LEAN_AND_MEAN
36 #define WIN32_LEAN_AND_MEAN
41 inline bool operator==(const M128A &lhs, const M128A &rhs) {
42 return (lhs.Low == rhs.Low && lhs.High == rhs.High);
45 namespace libvpx_test {
47 // Compares the state of xmm[6-15] at construction with their state at
48 // destruction. These registers should be preserved by the callee on
50 class RegisterStateCheck {
52 RegisterStateCheck() { initialized_ = StoreRegisters(&pre_context_); }
53 ~RegisterStateCheck() { Check(); }
56 static bool StoreRegisters(CONTEXT *const context) {
57 const HANDLE this_thread = GetCurrentThread();
58 EXPECT_TRUE(this_thread != NULL);
59 context->ContextFlags = CONTEXT_FLOATING_POINT;
60 const bool context_saved = GetThreadContext(this_thread, context) == TRUE;
61 EXPECT_TRUE(context_saved) << "GetLastError: " << GetLastError();
65 // Compares the register state. Returns true if the states match.
67 ASSERT_TRUE(initialized_);
69 ASSERT_TRUE(StoreRegisters(&post_context));
71 const M128A *xmm_pre = &pre_context_.Xmm6;
72 const M128A *xmm_post = &post_context.Xmm6;
73 for (int i = 6; i <= 15; ++i) {
74 EXPECT_EQ(*xmm_pre, *xmm_post) << "xmm" << i << " has been modified!";
84 #define ASM_REGISTER_STATE_CHECK(statement) \
86 libvpx_test::RegisterStateCheck reg_check; \
90 } // namespace libvpx_test
92 #elif defined(CONFIG_SHARED) && defined(HAVE_NEON_ASM) && \
93 defined(CONFIG_VP9) && !CONFIG_SHARED && HAVE_NEON_ASM && CONFIG_VP9
96 // Save the d8-d15 registers into store.
97 void vpx_push_neon(int64_t *store);
100 namespace libvpx_test {
102 // Compares the state of d8-d15 at construction with their state at
103 // destruction. These registers should be preserved by the callee on
105 class RegisterStateCheck {
107 RegisterStateCheck() { vpx_push_neon(pre_store_); }
108 ~RegisterStateCheck() { Check(); }
111 // Compares the register state. Returns true if the states match.
113 int64_t post_store[8];
114 vpx_push_neon(post_store);
115 for (int i = 0; i < 8; ++i) {
116 EXPECT_EQ(pre_store_[i], post_store[i]) << "d" << i + 8
117 << " has been modified";
121 int64_t pre_store_[8];
124 #define ASM_REGISTER_STATE_CHECK(statement) \
126 libvpx_test::RegisterStateCheck reg_check; \
130 } // namespace libvpx_test
134 namespace libvpx_test {
136 class RegisterStateCheck {};
137 #define ASM_REGISTER_STATE_CHECK(statement) statement
139 } // namespace libvpx_test
143 #if ARCH_X86 || ARCH_X86_64
144 #if defined(__GNUC__)
146 namespace libvpx_test {
148 // Checks the FPU tag word pre/post execution to ensure emms has been called.
149 class RegisterStateCheckMMX {
151 RegisterStateCheckMMX() {
152 __asm__ volatile("fstenv %0" : "=rm"(pre_fpu_env_));
154 ~RegisterStateCheckMMX() { Check(); }
157 // Checks the FPU tag word pre/post execution, returning false if not cleared
160 EXPECT_EQ(0xffff, pre_fpu_env_[4])
161 << "FPU was in an inconsistent state prior to call";
163 uint16_t post_fpu_env[14];
164 __asm__ volatile("fstenv %0" : "=rm"(post_fpu_env));
165 EXPECT_EQ(0xffff, post_fpu_env[4])
166 << "FPU was left in an inconsistent state after call";
169 uint16_t pre_fpu_env_[14];
172 #define API_REGISTER_STATE_CHECK(statement) \
174 libvpx_test::RegisterStateCheckMMX reg_check; \
175 ASM_REGISTER_STATE_CHECK(statement); \
178 } // namespace libvpx_test
181 #endif // ARCH_X86 || ARCH_X86_64
183 #ifndef API_REGISTER_STATE_CHECK
184 #define API_REGISTER_STATE_CHECK ASM_REGISTER_STATE_CHECK
187 #endif // TEST_REGISTER_STATE_CHECK_H_