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

Generated on Mon Oct 27 2025 04:12:59 for gem5 by doxygen 1.14.0