1*8aea95f3SMichał Górny //===-- RegisterContextTest.cpp -------------------------------------------===//
2*8aea95f3SMichał Górny //
3*8aea95f3SMichał Górny // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*8aea95f3SMichał Górny // See https://llvm.org/LICENSE.txt for license information.
5*8aea95f3SMichał Górny // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*8aea95f3SMichał Górny //
7*8aea95f3SMichał Górny //===----------------------------------------------------------------------===//
8*8aea95f3SMichał Górny 
9*8aea95f3SMichał Górny #include "gtest/gtest.h"
10*8aea95f3SMichał Górny 
11*8aea95f3SMichał Górny #include "Plugins/Process/Utility/RegisterContext_x86.h"
12*8aea95f3SMichał Górny 
13*8aea95f3SMichał Górny #include "llvm/ADT/STLExtras.h"
14*8aea95f3SMichał Górny #include "llvm/Support/FormatVariadic.h"
15*8aea95f3SMichał Górny 
16*8aea95f3SMichał Górny #include <array>
17*8aea95f3SMichał Górny 
18*8aea95f3SMichał Górny using namespace lldb_private;
19*8aea95f3SMichał Górny 
20*8aea95f3SMichał Górny struct TagWordTestVector {
21*8aea95f3SMichał Górny   uint16_t sw;
22*8aea95f3SMichał Górny   uint16_t tw;
23*8aea95f3SMichał Górny   uint8_t tw_abridged;
24*8aea95f3SMichał Górny   int st_reg_num;
25*8aea95f3SMichał Górny };
26*8aea95f3SMichał Górny 
st_from_comp(uint64_t mantissa,uint16_t sign_exp)27*8aea95f3SMichał Górny constexpr MMSReg st_from_comp(uint64_t mantissa, uint16_t sign_exp) {
28*8aea95f3SMichał Górny   MMSReg ret = {};
29*8aea95f3SMichał Górny   ret.comp.mantissa = mantissa;
30*8aea95f3SMichał Górny   ret.comp.sign_exp = sign_exp;
31*8aea95f3SMichał Górny   return ret;
32*8aea95f3SMichał Górny }
33*8aea95f3SMichał Górny 
34*8aea95f3SMichał Górny const std::array<MMSReg, 8> st_regs = {
35*8aea95f3SMichał Górny     st_from_comp(0x8000000000000000, 0x4000), // +2.0
36*8aea95f3SMichał Górny     st_from_comp(0x3f00000000000000, 0x0000), // 1.654785e-4932
37*8aea95f3SMichał Górny     st_from_comp(0x0000000000000000, 0x0000), // +0
38*8aea95f3SMichał Górny     st_from_comp(0x0000000000000000, 0x8000), // -0
39*8aea95f3SMichał Górny     st_from_comp(0x8000000000000000, 0x7fff), // +inf
40*8aea95f3SMichał Górny     st_from_comp(0x8000000000000000, 0xffff), // -inf
41*8aea95f3SMichał Górny     st_from_comp(0xc000000000000000, 0xffff), // nan
42*8aea95f3SMichał Górny     st_from_comp(0x8000000000000000, 0xc000), // -2.0
43*8aea95f3SMichał Górny };
44*8aea95f3SMichał Górny 
45*8aea95f3SMichał Górny const std::array<TagWordTestVector, 8> tag_word_test_vectors{
46*8aea95f3SMichał Górny     TagWordTestVector{0x3800, 0x3fff, 0x80, 1},
47*8aea95f3SMichał Górny     TagWordTestVector{0x3000, 0x2fff, 0xc0, 2},
48*8aea95f3SMichał Górny     TagWordTestVector{0x2800, 0x27ff, 0xe0, 3},
49*8aea95f3SMichał Górny     TagWordTestVector{0x2000, 0x25ff, 0xf0, 4},
50*8aea95f3SMichał Górny     TagWordTestVector{0x1800, 0x25bf, 0xf8, 5},
51*8aea95f3SMichał Górny     TagWordTestVector{0x1000, 0x25af, 0xfc, 6},
52*8aea95f3SMichał Górny     TagWordTestVector{0x0800, 0x25ab, 0xfe, 7},
53*8aea95f3SMichał Górny     TagWordTestVector{0x0000, 0x25a8, 0xff, 8},
54*8aea95f3SMichał Górny };
55*8aea95f3SMichał Górny 
TEST(RegisterContext_x86Test,AbridgedToFullTagWord)56*8aea95f3SMichał Górny TEST(RegisterContext_x86Test, AbridgedToFullTagWord) {
57*8aea95f3SMichał Górny   for (const auto &x : llvm::enumerate(tag_word_test_vectors)) {
58*8aea95f3SMichał Górny     SCOPED_TRACE(llvm::formatv("tag_word_test_vectors[{0}]", x.index()));
59*8aea95f3SMichał Górny     std::array<MMSReg, 8> test_regs;
60*8aea95f3SMichał Górny     for (int i = 0; i < x.value().st_reg_num; ++i)
61*8aea95f3SMichał Górny       test_regs[i] = st_regs[x.value().st_reg_num - i - 1];
62*8aea95f3SMichał Górny     EXPECT_EQ(
63*8aea95f3SMichał Górny         AbridgedToFullTagWord(x.value().tw_abridged, x.value().sw, test_regs),
64*8aea95f3SMichał Górny         x.value().tw);
65*8aea95f3SMichał Górny   }
66*8aea95f3SMichał Górny }
67*8aea95f3SMichał Górny 
TEST(RegisterContext_x86Test,FullToAbridgedTagWord)68*8aea95f3SMichał Górny TEST(RegisterContext_x86Test, FullToAbridgedTagWord) {
69*8aea95f3SMichał Górny   for (const auto &x : llvm::enumerate(tag_word_test_vectors)) {
70*8aea95f3SMichał Górny     SCOPED_TRACE(llvm::formatv("tag_word_test_vectors[{0}]", x.index()));
71*8aea95f3SMichał Górny     EXPECT_EQ(FullToAbridgedTagWord(x.value().tw), x.value().tw_abridged);
72*8aea95f3SMichał Górny   }
73*8aea95f3SMichał Górny }
74