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

Generated on Wed Sep 30 2020 14:02:07 for gem5 by doxygen 1.8.17