gem5 v24.0.0.0
Loading...
Searching...
No Matches
condcodes.test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 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 "base/condcodes.hh"
32
33using namespace gem5;
34
35/*
36 * Add 0x80 + 0x80 to get 0x100. findCarry should report a carry flag after
37 * this operation.
38 */
39TEST(CondCodes, FindCarryWithNoCarryIn8Bit)
40{
41 EXPECT_TRUE(findCarry(8, 0x100, 0x80, 0x80));
42}
43
44/*
45 * Add 0xf0 + 0x0f to get 0xff. findCarry should not report a carry flag after
46 * this operation.
47 */
48TEST(CondCodes, FindNoCarryWithNoCarryIn8Bit)
49{
50 EXPECT_FALSE(findCarry(8, 0xff, 0xf0, 0x0f));
51}
52
53/*
54 * Add 0x7f + 0x80 + 0x01 to get 0x100. findCarry should report a carry flag
55 * after this operation.
56 */
57TEST(CondCodes, FindCarryWithCarryIn8Bit)
58{
59 EXPECT_TRUE(findCarry(8, 0x100, 0x80, 0x7f));
60}
61
62/*
63 * Add 0x80 + 0x7e + 0x01 to get 0xff. findCarry should not report a carry
64 * flag after this operation.
65 */
66TEST(CondCodes, FindNoCarryWithCarryIn8Bit)
67{
68 EXPECT_FALSE(findCarry(8, 0xff, 0x80, 0x7e));
69}
70
71/*
72 * Add 0x80000000 + 0x80000000 to get 0x100000000. findCarry should report a
73 * carry flag after this operation.
74 */
75TEST(CondCodes, FindCarryWithNoCarryIn32Bit)
76{
77 EXPECT_TRUE(findCarry(32, 0x100000000, 0x80000000, 0x80000000));
78}
79
80/*
81 * Add 0xffff0000 + 0x0000ffff to get 0xffffffff. findCarry should not report a
82 * carry flag after this operation.
83 */
84TEST(CondCodes, FindNoCarryWithNoCarryIn32Bit)
85{
86 EXPECT_FALSE(findCarry(32, 0xffffffff, 0xffff0000, 0x0000ffff));
87}
88
89TEST(CondCodes, FindCarryWithCarryIn32Bit)
90{
91 /*
92 * Add 0x80000000 + 0x7fffffff + 0x00000001 to get 0x100000000,
93 * resulting in a carry
94 */
95 EXPECT_TRUE(findCarry(32, 0x100000000, 0x80000000, 0x7fffffff));
96 // Add 0x80000000 + 0x7ffffffe + 0x00000001 to get 0xffffffff,
97 // resulting in no carry
98 EXPECT_FALSE(findCarry(32, 0xffffffff, 0x80000000, 0x7ffffffe));
99 // Add 0xffffffff + 0x00000000 + 0x00000001 to get 0x100000000,
100 // resulting in a carry
101 EXPECT_TRUE(findCarry(32, 0x100000000, 0xffffffff, 0x00000000));
102}
103
104TEST(CondCodes, FindCarryWithNoCarryIn64Bit)
105{
106 // Add 0x8000000000000000 + 0x8000000000000000 to get 0x10000000000000000,
107 // (unrepresentable with uint64_t), resulting in a carry
108 EXPECT_TRUE(findCarry(64, 0x0000000000000000,
109 0x8000000000000000,
110 0x8000000000000000));
111 /*
112 * Add 0x0000000000000000 + 0x0000000000000000 to get 0x0000000000000000
113 * resulting in no carry
114 * We get the same sum as above case due to unrepresentability, but we
115 * should still expect no carry
116 */
117 EXPECT_FALSE(findCarry(64, 0x0000000000000000,
118 0x0000000000000000,
119 0x0000000000000000));
120 /*
121 * Add 0x8000000000000000 + 0x7fffffffffffffff to get 0xffffffffffffffff,
122 * resulting in no carry
123 */
124 EXPECT_FALSE(findCarry(64, 0xffffffffffffffff,
125 0x8000000000000000,
126 0x7fffffffffffffff));
127 /*
128 * Add 0xffffffff00000000 + 0x00000000ffffffff to get 0xffffffffffffffff,
129 * resulting in no carry
130 */
131 EXPECT_FALSE(findCarry(64, 0xffffffffffffffff,
132 0xffffffff00000000,
133 0x00000000ffffffff));
134}
135
136TEST(CondCodes, FindCarryWithCarryIn64Bit)
137{
138 /* Add 0x8000000000000000 + 0x8000000000000000 + 0x0000000000000001
139 * to get 0x1 000000000000001 (unrepresentable with uint64_t),
140 * resulting in a carry
141 */
142 EXPECT_TRUE(findCarry(64, 0x0000000000000000,
143 0x8000000000000000,
144 0x7fffffffffffffff));
145 /*
146 * Add 0x0000000000000000 + 0x0000000000000000 + 0x0000000000000001
147 * resulting in no carry
148 * We get the same sum as the above case due to unrepresentability, but we
149 * should still expect no carry
150 */
151 EXPECT_FALSE(findCarry(64, 0x0000000000000001,
152 0x0000000000000000,
153 0x0000000000000000));
154 /*
155 * Add 0x8000000000000000 + 0x7fffffffffffffff + 0x0000000000000001
156 * to get 0x1 0000000000000000 (unrepresentable with uint64_t),
157 * resulting in a carry
158 */
159 EXPECT_TRUE(findCarry(64, 0x0000000000000000,
160 0x8000000000000000,
161 0x7fffffffffffffff));
162 /*
163 * Add 0xffffffff00000000 + 0x000000000000000 + 0x0000000000000001
164 * to get 0x1 0000000000000000 (unrepresentable with uint64_t),
165 * resulting in a carry
166 */
167 EXPECT_TRUE(findCarry(64, 0x0000000000000000,
168 0xffffffffffffffff,
169 0x0000000000000001));
170}
171
172TEST(CondCodes, FindOverflow8Bit)
173{
174 /*
175 * Addition of 127 + 1 = 128 or -128 as signed two's complement.
176 * Overflow occurs in this case
177 */
178 EXPECT_TRUE(findOverflow(8, 0x80, 0x7f, 0x01));
179 /*
180 * Addition of 64 + 63 = 127, or 127 as signed two's complement.
181 * No overflow occurs
182 */
183 EXPECT_FALSE(findOverflow(8, 0x7f, 0x40, 0x3f));
184}
185
186TEST(CondCodes, FindOverflow32Bit)
187{
188 /*
189 * Addition of 2,147,483,647 + 1 = 2,147,483,648, or -2,147,483,648 as
190 * signed two's complement. Overflow occurs in this case
191 */
192 EXPECT_TRUE(findOverflow(32, 0x80000000, 0x7fffffff, 0x00000001));
193 /*
194 * Addition of 1,073,741,824 + 1,073,741,823 = 2,147,483,647, or
195 * 2,147,483,647 as signed two's complement. No overflow occurs
196 */
197 EXPECT_FALSE(findOverflow(32, 0x7fffffff, 0x40000000, 0x3fffffff));
198}
199
200TEST(CondCodes, FindOverflow64Bit)
201{
202 /*
203 * Addition of 0x7fffffffffffffff + 0x0000000000000001 =
204 * 0x8000000000000000, or -9,223,372,036,854,775,808 as signed two's
205 * complement. Overflow occurs in this case
206 */
207 EXPECT_TRUE(findOverflow(64, 0x8000000000000000,
208 0x7fffffffffffffff,
209 0x0000000000000001));
210 /* Addition of 0x4000000000000000 + 0x3fffffffffffffff =
211 * 0x7fffffffffffffff, or 9,223,372,036,854,775,807 as signed two's
212 * complement. No overflow occurs
213 */
214 EXPECT_FALSE(findOverflow(64, 0x7fffffffffffffff,
215 0x4000000000000000,
216 0x3fffffffffffffff));
217}
218
219TEST(CondCodes, OddParity)
220{
221 EXPECT_EQ(1, findParity(8, 1));
222}
223
224TEST(CondCodes, EvenParity)
225{
226 EXPECT_EQ(0, findParity(8, 3));
227}
228
229TEST(CondCodes, OddParityOverflow)
230{
231 EXPECT_EQ(1, findParity(8, 0x102));
232}
233
234TEST(CondCodes, EvenParityOverflow)
235{
236 EXPECT_EQ(0, findParity(4,0x43));
237}
238
239TEST(CondCodes, IsNegative)
240{
241 EXPECT_EQ(1, findNegative(8, 128));
242}
243
244TEST(CondCodes, IsNotNegative)
245{
246 EXPECT_EQ(0, findNegative(8, 127));
247}
248
249TEST(CondCodes, IsZero)
250{
251 EXPECT_EQ(1, findZero(8, 0));
252}
253
254TEST(CondCodes, IsNotZero)
255{
256 EXPECT_EQ(0, findZero(8, 1));
257}
258
259TEST(CondCodes, IsZeroOverflow)
260{
261 EXPECT_EQ(1, findZero(8,0x100));
262}
TEST(CondCodes, FindCarryWithNoCarryIn8Bit)
static bool findNegative(int width, uint64_t dest)
Calculate the negative flag.
Definition condcodes.hh:141
static bool findOverflow(int width, uint64_t dest, uint64_t src1, uint64_t src2)
Calculate the overflow flag from an addition.
Definition condcodes.hh:98
static bool findZero(int width, uint64_t dest)
Calculate the zero flag.
Definition condcodes.hh:152
static bool findParity(int width, uint64_t dest)
Calculate the parity of a value.
Definition condcodes.hh:123
static bool findCarry(int width, uint64_t dest, uint64_t src1, uint64_t src2)
Calculate the carry flag from an addition.
Definition condcodes.hh:84
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