• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "gtest/gtest.h"
18 
19 #include <cstdint>
20 
21 #include "berberis/guest_abi/guest_abi.h"
22 
23 namespace berberis {
24 
25 namespace {
26 
TEST(GuestAbi_riscv64,GuestArgumentInt8)27 TEST(GuestAbi_riscv64, GuestArgumentInt8) {
28   uint64_t value = 0;
29   auto& param = *reinterpret_cast<GuestAbi::GuestArgument<int8_t>*>(&value);
30 
31   value = 0xffff'ffff'ffff'fff9U;
32   EXPECT_EQ(param, -7);
33 
34   value = 7;
35   EXPECT_EQ(param, 7);
36 
37   param = -123;
38   EXPECT_EQ(value, 0xffff'ffff'ffff'ff85U);
39 
40   param = 127;
41   EXPECT_EQ(value, 0x0000'0000'0000'007fU);
42 }
43 
TEST(GuestAbi_riscv64,GuestArgumentUInt8)44 TEST(GuestAbi_riscv64, GuestArgumentUInt8) {
45   uint64_t value = 0;
46   auto& param = *reinterpret_cast<GuestAbi::GuestArgument<uint8_t>*>(&value);
47 
48   value = 0x0000'0000'0000'00f9U;
49   EXPECT_EQ(param, 249);
50 
51   value = 7;
52   EXPECT_EQ(param, 7);
53 
54   param = 123;
55   EXPECT_EQ(value, 0x0000'0000'0000'007bU);
56 
57   param = 255;
58   EXPECT_EQ(value, 0x0000'0000'0000'00ffU);
59 }
60 
TEST(GuestAbi_riscv64,GuestArgumentUInt32)61 TEST(GuestAbi_riscv64, GuestArgumentUInt32) {
62   uint64_t value = 0;
63   auto& param = *reinterpret_cast<GuestAbi::GuestArgument<uint32_t>*>(&value);
64 
65   value = 0xffff'ffff'ffff'ffffU;
66   EXPECT_EQ(param, 0xffff'ffffU);
67 
68   value = 7;
69   EXPECT_EQ(param, 7U);
70 
71   param = 0xf123'4567U;
72   EXPECT_EQ(value, 0xffff'ffff'f123'4567U);
73 
74   param += 1;
75   EXPECT_EQ(value, 0xffff'ffff'f123'4568U);
76 }
77 
TEST(GuestAbi_riscv64,GuestArgumentEnumUInt32)78 TEST(GuestAbi_riscv64, GuestArgumentEnumUInt32) {
79   enum class Enum : uint32_t {
80     kA = 0xffff'ffffU,
81     kB = 7,
82     kC = 0xf123'4567U,
83   };
84 
85   uint64_t value = 0;
86   auto& param = *reinterpret_cast<GuestAbi::GuestArgument<Enum>*>(&value);
87 
88   value = 0xffff'ffff'ffff'ffffU;
89   EXPECT_EQ(static_cast<Enum>(param), Enum::kA);
90 
91   value = 7;
92   EXPECT_EQ(static_cast<Enum>(param), Enum::kB);
93 
94   param = Enum::kC;
95   EXPECT_EQ(value, 0xffff'ffff'f123'4567U);
96 }
97 
TEST(GuestAbi_riscv64_lp64,GuestArgumentFloat32)98 TEST(GuestAbi_riscv64_lp64, GuestArgumentFloat32) {
99   uint64_t value = 0;
100   auto& param = *reinterpret_cast<GuestAbi::GuestArgument<float, GuestAbi::kLp64>*>(&value);
101 
102   value = 0x0000'0000'3f00'0000;
103   EXPECT_FLOAT_EQ(param, 0.5f);
104 
105   param = 7.125f;
106   EXPECT_EQ(value, 0x0000'0000'40e4'0000U);
107 }
108 
TEST(GuestAbi_riscv64_lp64,GuestArgumentFloat64)109 TEST(GuestAbi_riscv64_lp64, GuestArgumentFloat64) {
110   uint64_t value = 0;
111   auto& param = *reinterpret_cast<GuestAbi::GuestArgument<double, GuestAbi::kLp64>*>(&value);
112 
113   value = 0x3fd5'c28f'5c28'f5c3;
114   EXPECT_DOUBLE_EQ(param, 0.34);
115 
116   param = 0.125f;
117   EXPECT_EQ(value, 0x3fc0'0000'0000'0000U);
118 }
119 
TEST(GuestAbi_riscv64_lp64d,GuestArgumentFloat32)120 TEST(GuestAbi_riscv64_lp64d, GuestArgumentFloat32) {
121   uint64_t value = 0;
122   auto& param = *reinterpret_cast<GuestAbi::GuestArgument<float, GuestAbi::kLp64d>*>(&value);
123 
124   value = 0xffff'ffff'3f00'0000;
125   EXPECT_FLOAT_EQ(param, 0.5f);
126 
127   param = 7.125f;
128   EXPECT_EQ(value, 0xffff'ffff'40e4'0000U);
129 }
130 
TEST(GuestAbi_riscv64_lp64d,GuestArgumentFloat64)131 TEST(GuestAbi_riscv64_lp64d, GuestArgumentFloat64) {
132   uint64_t value = 0;
133   auto& param = *reinterpret_cast<GuestAbi::GuestArgument<double, GuestAbi::kLp64d>*>(&value);
134 
135   value = 0x3fd5'c28f'5c28'f5c3;
136   EXPECT_DOUBLE_EQ(param, 0.34);
137 
138   param = 0.125f;
139   EXPECT_EQ(value, 0x3fc0'0000'0000'0000U);
140 }
141 
142 }  // namespace
143 
144 }  // namespace berberis
145