1 // Copyright (c) 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "source/opt/constants.h"
16
17 #include <gtest/gtest-param-test.h>
18
19 #include "gmock/gmock.h"
20 #include "gtest/gtest.h"
21 #include "source/opt/types.h"
22
23 namespace spvtools {
24 namespace opt {
25 namespace analysis {
26 namespace {
27
28 using ConstantTest = ::testing::Test;
29 using ::testing::ValuesIn;
30
31 template <typename T>
32 struct GetExtendedValueCase {
33 bool is_signed;
34 int width;
35 std::vector<uint32_t> words;
36 T expected_value;
37 };
38
39 using GetSignExtendedValueCase = GetExtendedValueCase<int64_t>;
40 using GetZeroExtendedValueCase = GetExtendedValueCase<uint64_t>;
41
42 using GetSignExtendedValueTest =
43 ::testing::TestWithParam<GetSignExtendedValueCase>;
44 using GetZeroExtendedValueTest =
45 ::testing::TestWithParam<GetZeroExtendedValueCase>;
46
TEST_P(GetSignExtendedValueTest,Case)47 TEST_P(GetSignExtendedValueTest, Case) {
48 Integer type(GetParam().width, GetParam().is_signed);
49 IntConstant value(&type, GetParam().words);
50
51 EXPECT_EQ(GetParam().expected_value, value.GetSignExtendedValue());
52 }
53
TEST_P(GetZeroExtendedValueTest,Case)54 TEST_P(GetZeroExtendedValueTest, Case) {
55 Integer type(GetParam().width, GetParam().is_signed);
56 IntConstant value(&type, GetParam().words);
57
58 EXPECT_EQ(GetParam().expected_value, value.GetZeroExtendedValue());
59 }
60
61 const uint32_t k32ones = ~uint32_t(0);
62 const uint64_t k64ones = ~uint64_t(0);
63 const int64_t kSBillion = 1000 * 1000 * 1000;
64 const uint64_t kUBillion = 1000 * 1000 * 1000;
65
66 INSTANTIATE_TEST_SUITE_P(AtMost32Bits, GetSignExtendedValueTest,
67 ValuesIn(std::vector<GetSignExtendedValueCase>{
68 // 4 bits
69 {false, 4, {0}, 0},
70 {false, 4, {7}, 7},
71 {false, 4, {15}, 15},
72 {true, 4, {0}, 0},
73 {true, 4, {7}, 7},
74 {true, 4, {0xfffffff8}, -8},
75 {true, 4, {k32ones}, -1},
76 // 16 bits
77 {false, 16, {0}, 0},
78 {false, 16, {32767}, 32767},
79 {false, 16, {32768}, 32768},
80 {false, 16, {65000}, 65000},
81 {true, 16, {0}, 0},
82 {true, 16, {32767}, 32767},
83 {true, 16, {0xfffffff8}, -8},
84 {true, 16, {k32ones}, -1},
85 // 32 bits
86 {false, 32, {0}, 0},
87 {false, 32, {1000000}, 1000000},
88 {true, 32, {0xfffffff8}, -8},
89 {true, 32, {k32ones}, -1},
90 }));
91
92 INSTANTIATE_TEST_SUITE_P(AtMost64Bits, GetSignExtendedValueTest,
93 ValuesIn(std::vector<GetSignExtendedValueCase>{
94 // 48 bits
95 {false, 48, {0, 0}, 0},
96 {false, 48, {5, 0}, 5},
97 {false, 48, {0xfffffff8, k32ones}, -8},
98 {false, 48, {k32ones, k32ones}, -1},
99 {false, 48, {0xdcd65000, 1}, 8 * kSBillion},
100 {true, 48, {0xfffffff8, k32ones}, -8},
101 {true, 48, {k32ones, k32ones}, -1},
102 {true, 48, {0xdcd65000, 1}, 8 * kSBillion},
103
104 // 64 bits
105 {false, 64, {12, 0}, 12},
106 {false, 64, {0xdcd65000, 1}, 8 * kSBillion},
107 {false, 48, {0xfffffff8, k32ones}, -8},
108 {false, 64, {k32ones, k32ones}, -1},
109 {true, 64, {12, 0}, 12},
110 {true, 64, {0xdcd65000, 1}, 8 * kSBillion},
111 {true, 48, {0xfffffff8, k32ones}, -8},
112 {true, 64, {k32ones, k32ones}, -1},
113 }));
114
115 INSTANTIATE_TEST_SUITE_P(AtMost32Bits, GetZeroExtendedValueTest,
116 ValuesIn(std::vector<GetZeroExtendedValueCase>{
117 // 4 bits
118 {false, 4, {0}, 0},
119 {false, 4, {7}, 7},
120 {false, 4, {15}, 15},
121 {true, 4, {0}, 0},
122 {true, 4, {7}, 7},
123 {true, 4, {0xfffffff8}, 0xfffffff8},
124 {true, 4, {k32ones}, k32ones},
125 // 16 bits
126 {false, 16, {0}, 0},
127 {false, 16, {32767}, 32767},
128 {false, 16, {32768}, 32768},
129 {false, 16, {65000}, 65000},
130 {true, 16, {0}, 0},
131 {true, 16, {32767}, 32767},
132 {true, 16, {0xfffffff8}, 0xfffffff8},
133 {true, 16, {k32ones}, k32ones},
134 // 32 bits
135 {false, 32, {0}, 0},
136 {false, 32, {1000000}, 1000000},
137 {true, 32, {0xfffffff8}, 0xfffffff8},
138 {true, 32, {k32ones}, k32ones},
139 }));
140
141 INSTANTIATE_TEST_SUITE_P(AtMost64Bits, GetZeroExtendedValueTest,
142 ValuesIn(std::vector<GetZeroExtendedValueCase>{
143 // 48 bits
144 {false, 48, {0, 0}, 0},
145 {false, 48, {5, 0}, 5},
146 {false, 48, {0xfffffff8, k32ones}, uint64_t(-8)},
147 {false, 48, {k32ones, k32ones}, uint64_t(-1)},
148 {false, 48, {0xdcd65000, 1}, 8 * kUBillion},
149 {true, 48, {0xfffffff8, k32ones}, uint64_t(-8)},
150 {true, 48, {k32ones, k32ones}, uint64_t(-1)},
151 {true, 48, {0xdcd65000, 1}, 8 * kUBillion},
152
153 // 64 bits
154 {false, 64, {12, 0}, 12},
155 {false, 64, {0xdcd65000, 1}, 8 * kUBillion},
156 {false, 48, {0xfffffff8, k32ones}, uint64_t(-8)},
157 {false, 64, {k32ones, k32ones}, k64ones},
158 {true, 64, {12, 0}, 12},
159 {true, 64, {0xdcd65000, 1}, 8 * kUBillion},
160 {true, 48, {0xfffffff8, k32ones}, uint64_t(-8)},
161 {true, 64, {k32ones, k32ones}, k64ones},
162 }));
163
164 } // namespace
165 } // namespace analysis
166 } // namespace opt
167 } // namespace spvtools
168