gem5 v24.0.0.0
Loading...
Searching...
No Matches
amo.test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 The Regents of the University of California
3 * All rights reserved
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer;
11 * redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution;
14 * neither the name of the copyright holders nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include <gtest/gtest.h>
32
33#include <string>
34#include <array>
35
36#include "base/amo.hh"
37
38using namespace gem5;
39
40void
41multiply2Op(int *b, int a)
42{
43 *b *= a;
44}
45
46void
47multiply3Op(int *b, int a, int c)
48{
49 *b *= a * c;
50}
51
52void
53addSubColumns(int *b, const std::array<int, 2>& a, const std::array<int, 2>& c)
54{
55 *b += a[0] + c[0];
56 *b -= a[1] + c[1];
57}
58
60{
61 // test with ints and strings
62 int test_int_smaller = 5;
63 int test_int_bigger = 15;
64 std::string test_string_smaller = "apple";
65 std::string test_string_bigger = "cat";
66
67 std::unique_ptr<TypedAtomicOpFunctor<int>> amo_op_int =
68 std::make_unique<AtomicOpMin<int>>(10);
69 std::unique_ptr<TypedAtomicOpFunctor<std::string>> amo_op_string =
70 std::make_unique<AtomicOpMin<std::string>>("base");
71 amo_op_int->execute(&test_int_smaller);
72 amo_op_int->execute(&test_int_bigger);
73 amo_op_string->execute(&test_string_smaller);
74 amo_op_string->execute(&test_string_bigger);
75
76 EXPECT_EQ(test_int_smaller, 5);
77 EXPECT_EQ(test_int_bigger, 10);
78 EXPECT_EQ(test_string_smaller, "apple");
79 EXPECT_EQ(test_string_bigger, "base");
80}
81
83{
84 int test_int_smaller = 5;
85 int test_int_bigger = 15;
86 std::string test_string_smaller = "apple";
87 std::string test_string_bigger = "cat";
88
89 std::unique_ptr<TypedAtomicOpFunctor<int>> amo_op_int =
90 std::make_unique<AtomicOpMax<int>>(10);
91 std::unique_ptr<TypedAtomicOpFunctor<std::string>> amo_op_string =
92 std::make_unique<AtomicOpMax<std::string>>("base");
93 amo_op_int->execute(&test_int_smaller);
94 amo_op_int->execute(&test_int_bigger);
95 amo_op_string->execute(&test_string_smaller);
96 amo_op_string->execute(&test_string_bigger);
97
98 EXPECT_EQ(test_int_smaller, 10);
99 EXPECT_EQ(test_int_bigger, 15);
100 EXPECT_EQ(test_string_smaller, "base");
101 EXPECT_EQ(test_string_bigger, "cat");
102}
103
105{
106 int test_int = 10;
107 char test_char = 'c';
108
109 std::unique_ptr<TypedAtomicOpFunctor<int>> amo_op_int =
110 std::make_unique<AtomicOpDec<int>>();
111 std::unique_ptr<TypedAtomicOpFunctor<char>> amo_op_char =
112 std::make_unique<AtomicOpDec<char>>();
113 amo_op_int->execute(&test_int);
114 amo_op_char->execute(&test_char);
115
116 EXPECT_EQ(test_int, 9);
117 EXPECT_EQ(test_char, 'b');
118}
119
121{
122 int test_int = 10;
123 char test_char = 'c';
124
125 std::unique_ptr<TypedAtomicOpFunctor<int>> amo_op_int =
126 std::make_unique<AtomicOpInc<int>>();
127 std::unique_ptr<TypedAtomicOpFunctor<char>> amo_op_char =
128 std::make_unique<AtomicOpInc<char>>();
129 amo_op_int->execute(&test_int);
130 amo_op_char->execute(&test_char);
131
132 EXPECT_EQ(test_int, 11);
133 EXPECT_EQ(test_char, 'd');
134}
135
137{
138 int test_int = 10;
139 char test_char = 'c';
140
141 std::unique_ptr<TypedAtomicOpFunctor<int>> amo_op_int =
142 std::make_unique<AtomicOpSub<int>>(2);
143 std::unique_ptr<TypedAtomicOpFunctor<char>> amo_op_char =
144 std::make_unique<AtomicOpSub<char>>('a');
145 amo_op_int->execute(&test_int);
146 amo_op_char->execute(&test_char);
147
148 EXPECT_EQ(test_int, 8);
149 EXPECT_EQ(test_char, 2);
150}
151
153{
154 int test_int = 10;
155 char test_char = 'c';
156
157 std::unique_ptr<TypedAtomicOpFunctor<int>> amo_op_int =
158 std::make_unique<AtomicOpAdd<int>>(2);
159 std::unique_ptr<TypedAtomicOpFunctor<char>> amo_op_char =
160 std::make_unique<AtomicOpAdd<char>>(2);
161 amo_op_int->execute(&test_int);
162 amo_op_char->execute(&test_char);
163
164 EXPECT_EQ(test_int, 12);
165 EXPECT_EQ(test_char, 'e');
166}
167
169{
170 int test_int = 10;
171 char test_char = 'c';
172
173 std::unique_ptr<TypedAtomicOpFunctor<int>> amo_op_int =
174 std::make_unique<AtomicOpExch<int>>(2);
175 std::unique_ptr<TypedAtomicOpFunctor<char>> amo_op_char =
176 std::make_unique<AtomicOpExch<char>>('a');
177 amo_op_int->execute(&test_int);
178 amo_op_char->execute(&test_char);
179
180 EXPECT_EQ(test_int, 2);
181 EXPECT_EQ(test_char, 'a');
182}
183
185{
186 int test_int = 10;
187 char test_char = 'c';
188
189 std::unique_ptr<TypedAtomicOpFunctor<int>> amo_op_int =
190 std::make_unique<AtomicOpXor<int>>(2);
191 std::unique_ptr<TypedAtomicOpFunctor<char>> amo_op_char =
192 std::make_unique<AtomicOpXor<char>>('a');
193 amo_op_int->execute(&test_int);
194 amo_op_char->execute(&test_char);
195
196 EXPECT_EQ(test_int, 8); // 1010 ^ 0010 = 1000
197 EXPECT_EQ(test_char, 2); // 99 ^ 97 = 2
198}
199
201{
202 int test_int = 8;
203 bool test_bool = true;
204
205 std::unique_ptr<TypedAtomicOpFunctor<int>> amo_op_int =
206 std::make_unique<AtomicOpOr<int>>(2);
207 std::unique_ptr<TypedAtomicOpFunctor<bool>> amo_op_bool =
208 std::make_unique<AtomicOpOr<bool>>(false);
209 amo_op_int->execute(&test_int);
210 amo_op_bool->execute(&test_bool);
211
212 EXPECT_EQ(test_int, 10);
213 EXPECT_EQ(test_bool, true);
214}
215
217{
218 int test_int = 10;
219 char test_char = 'c';
220
221 std::unique_ptr<TypedAtomicOpFunctor<int>> amo_op_int =
222 std::make_unique<AtomicOpAnd<int>>(6);
223 std::unique_ptr<TypedAtomicOpFunctor<char>> amo_op_char =
224 std::make_unique<AtomicOpAnd<char>>('a');
225 amo_op_int->execute(&test_int);
226 amo_op_char->execute(&test_char);
227
228 EXPECT_EQ(test_int, 2);
229 EXPECT_EQ(test_char, 'a');
230}
231
233{
234 int test_int = 9;
235
236 std::unique_ptr<TypedAtomicOpFunctor<int>> amo_op_int =
237 std::make_unique<AtomicGeneric2Op<int>>(9, multiply2Op);
238 amo_op_int->execute(&test_int);
239
240 EXPECT_EQ(test_int, 81);
241}
242
244{
245 int test_int = 2;
246
247 std::unique_ptr<TypedAtomicOpFunctor<int>> amo_op_int =
248 std::make_unique<AtomicGeneric3Op<int>>(4, 3, multiply3Op);
249 amo_op_int->execute(&test_int);
250
251 EXPECT_EQ(test_int, 24);
252}
253
255{
256 int test_int = 5;
257
258 std::array<int, 2> a = {6, 3};
259 std::array<int, 2> c = {10, 8};
260 std::unique_ptr<TypedAtomicOpFunctor<int>> amo_op_int =
261 std::make_unique<AtomicGenericPair3Op<int>>(a, c, addSubColumns);
262 amo_op_int->execute(&test_int);
263
264 EXPECT_EQ(test_int, 10);
265}
void multiply2Op(int *b, int a)
Definition amo.test.cc:41
void multiply3Op(int *b, int a, int c)
Definition amo.test.cc:47
void addSubColumns(int *b, const std::array< int, 2 > &a, const std::array< int, 2 > &c)
Definition amo.test.cc:53
TEST(AmoTest, AtomicOpMin)
Definition amo.test.cc:59
Bitfield< 7 > b
Bitfield< 29 > c
Definition misc_types.hh:53
Bitfield< 8 > a
Definition misc_types.hh:66
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36

Generated on Tue Jun 18 2024 16:24:00 for gem5 by doxygen 1.11.0