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

Generated on Fri Feb 28 2020 16:26:58 for gem5 by doxygen 1.8.13