1 /*
2  * Copyright (C) 2020 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 <cstring>
20 
21 #include "berberis/base/bit_util.h"
22 #include "berberis/guest_abi/guest_arguments.h"
23 
24 namespace berberis {
25 
26 namespace {
27 
TEST(NdkTest,GuestArgumentsAndResultTest)28 TEST(NdkTest, GuestArgumentsAndResultTest) {
29   union {
30     GuestArgumentBuffer buffer;
31     uint64_t padding[AlignUp(offsetof(GuestArgumentBuffer, stack_argv), sizeof(uint64_t)) /
32                          sizeof(uint64_t) +
33                      4];
34   } arguments = {{.argv = {1, 2, 3, 4, 5, 6, 7, 8},
35                   .simd_argv = {0x3ff0000000000000,
36                                 0x3fe0000000000000,
37                                 0x3fd5555555555555,
38                                 0x3fd0000000000000,
39                                 0x3fc999999999999a,
40                                 0x3fc5555555555555,
41                                 0x3fc2492492492492,
42                                 0x3fc0000000000000},
43                   .stack_argv = {0x3fbc71c71c71c71c}}};
44   arguments.buffer.stack_argv[1] = 0x3fb999999999999a;
45   arguments.buffer.stack_argv[2] = 0x3fb745d1745d1746;
46   arguments.buffer.stack_argv[3] = 0x3fb5555555555555;
47 
48   GuestArgumentsAndResult<double(int,
49                                  double,
50                                  int,
51                                  double,
52                                  int,
53                                  double,
54                                  int,
55                                  double,
56                                  int,
57                                  double,
58                                  int,
59                                  double,
60                                  int,
61                                  double,
62                                  int,
63                                  double,
64                                  int,
65                                  double,
66                                  int,
67                                  double)>
68       f1_args(&arguments.buffer);
69   EXPECT_EQ(1, f1_args.GuestArgument<0>());
70   EXPECT_EQ(1.0, f1_args.GuestArgument<1>());
71   EXPECT_EQ(2, f1_args.GuestArgument<2>());
72   EXPECT_EQ(1.0 / 2.0, f1_args.GuestArgument<3>());
73   EXPECT_EQ(3, f1_args.GuestArgument<4>());
74   EXPECT_EQ(1.0 / 3.0, f1_args.GuestArgument<5>());
75   EXPECT_EQ(4, f1_args.GuestArgument<6>());
76   EXPECT_EQ(1.0 / 4.0, f1_args.GuestArgument<7>());
77   EXPECT_EQ(5, f1_args.GuestArgument<8>());
78   EXPECT_EQ(1.0 / 5.0, f1_args.GuestArgument<9>());
79   EXPECT_EQ(6, f1_args.GuestArgument<10>());
80   EXPECT_EQ(1.0 / 6.0, f1_args.GuestArgument<11>());
81   EXPECT_EQ(7, f1_args.GuestArgument<12>());
82   EXPECT_EQ(1.0 / 7.0, f1_args.GuestArgument<13>());
83   EXPECT_EQ(8, f1_args.GuestArgument<14>());
84   EXPECT_EQ(1.0 / 8.0, f1_args.GuestArgument<15>());
85   EXPECT_EQ(0x1c71c71c, f1_args.GuestArgument<16>());
86   EXPECT_EQ(1.0 / 10.0, f1_args.GuestArgument<17>());
87   EXPECT_EQ(0x745d1746, f1_args.GuestArgument<18>());
88   EXPECT_EQ(1.0 / 12.0, f1_args.GuestArgument<19>());
89   EXPECT_EQ(1.0, f1_args.GuestResult());
90 
91   GuestArgumentsAndResult<int(double,
92                               int,
93                               double,
94                               int,
95                               double,
96                               int,
97                               double,
98                               int,
99                               double,
100                               int,
101                               double,
102                               int,
103                               double,
104                               int,
105                               double,
106                               int,
107                               double,
108                               int,
109                               double,
110                               int)>
111       f2_args(&arguments.buffer);
112   EXPECT_EQ(1.0, f2_args.GuestArgument<0>());
113   EXPECT_EQ(1, f2_args.GuestArgument<1>());
114   EXPECT_EQ(1.0 / 2.0, f2_args.GuestArgument<2>());
115   EXPECT_EQ(2, f2_args.GuestArgument<3>());
116   EXPECT_EQ(1.0 / 3.0, f2_args.GuestArgument<4>());
117   EXPECT_EQ(3, f2_args.GuestArgument<5>());
118   EXPECT_EQ(1.0 / 4.0, f2_args.GuestArgument<6>());
119   EXPECT_EQ(4, f2_args.GuestArgument<7>());
120   EXPECT_EQ(1.0 / 5.0, f2_args.GuestArgument<8>());
121   EXPECT_EQ(5, f2_args.GuestArgument<9>());
122   EXPECT_EQ(1.0 / 6.0, f2_args.GuestArgument<10>());
123   EXPECT_EQ(6, f2_args.GuestArgument<11>());
124   EXPECT_EQ(1.0 / 7.0, f2_args.GuestArgument<12>());
125   EXPECT_EQ(7, f2_args.GuestArgument<13>());
126   EXPECT_EQ(1.0 / 8.0, f2_args.GuestArgument<14>());
127   EXPECT_EQ(8, f2_args.GuestArgument<15>());
128   EXPECT_EQ(1.0 / 9.0, f2_args.GuestArgument<16>());
129   EXPECT_EQ(-0x66666666, f2_args.GuestArgument<17>());
130   EXPECT_EQ(1.0 / 11.0, f2_args.GuestArgument<18>());
131   EXPECT_EQ(0x55555555, f2_args.GuestArgument<19>());
132   EXPECT_EQ(1, f2_args.GuestResult());
133 }
134 
135 }  // namespace
136 
137 }  // namespace berberis
138