gem5  v21.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
intmath.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/intmath.hh"
32 
33 TEST(IntmathTest, isPowerOf2)
34 {
35  EXPECT_TRUE(isPowerOf2(1));
36  EXPECT_TRUE(isPowerOf2(32));
37  EXPECT_TRUE(isPowerOf2(65536));
38  EXPECT_TRUE(isPowerOf2(131072));
39  EXPECT_TRUE(isPowerOf2(262144));
40  EXPECT_FALSE(isPowerOf2(0));
41  EXPECT_FALSE(isPowerOf2(36));
42  EXPECT_FALSE(isPowerOf2(2521));
43  EXPECT_FALSE(isPowerOf2(1679616));
44 }
45 
46 TEST(IntmathTest, power)
47 {
48  EXPECT_EQ(65536, power(2, 16));
49  EXPECT_EQ(9765625, power(5, 10));
50  EXPECT_EQ(43046721, power(power(3, 4), 4));
51 }
52 
53 TEST(IntmathTest, floorLog2)
54 {
55  EXPECT_EQ(0, floorLog2(1));
56  EXPECT_EQ(4, floorLog2(16));
57  EXPECT_EQ(4, floorLog2(31));
58  EXPECT_EQ(5, floorLog2(36));
59  EXPECT_EQ(8, floorLog2(436));
60  EXPECT_EQ(16, floorLog2(65537));
61  EXPECT_EQ(20, floorLog2(1783592));
62  EXPECT_EQ(41, floorLog2(2821109907456));
63 
64  // Test unsigned integers of various sizes.
65  EXPECT_EQ(0, floorLog2((uint8_t)1));
66  EXPECT_EQ(0, floorLog2((uint16_t)1));
67  EXPECT_EQ(0, floorLog2((uint32_t)1));
68  EXPECT_EQ(0, floorLog2((uint64_t)1));
69 
70  // Test signed integers of various sizes.
71  EXPECT_EQ(0, floorLog2((int8_t)1));
72  EXPECT_EQ(0, floorLog2((int16_t)1));
73  EXPECT_EQ(0, floorLog2((int32_t)1));
74  EXPECT_EQ(0, floorLog2((int64_t)1));
75 }
76 
77 /* The IntmathDeathTest floorLog2 test is dependent on an assert being
78  * triggered. We therefore only run this test for .debug and .opt (where
79  * `TRACING_ON == 1`).
80  */
81 #if TRACING_ON
82 TEST(IntmathDeathTest, floorLog2)
83 {
84  // Verify a non-positive input triggers an assert.
85  EXPECT_DEATH_IF_SUPPORTED(floorLog2(0), "x > 0");
86 }
87 #endif
88 
89 TEST(IntmathTest, ceilLog2)
90 {
91  EXPECT_EQ(0, ceilLog2(1));
92  EXPECT_EQ(4, ceilLog2(16));
93  EXPECT_EQ(5, ceilLog2(31));
94  EXPECT_EQ(6, ceilLog2(36));
95  EXPECT_EQ(9, ceilLog2(436));
96  EXPECT_EQ(17, ceilLog2(65537));
97  EXPECT_EQ(21, ceilLog2(1783592));
98  EXPECT_EQ(42, ceilLog2(2821109907456));
99 }
100 
101 
102 TEST(IntmathTest, divCeil)
103 {
104  EXPECT_EQ(5, divCeil(55, 13));
105  EXPECT_EQ(90, divCeil(7922, 89));
106  EXPECT_EQ(4, divCeil(4800, 1442));
107  EXPECT_EQ(4, divCeil(75, 24));
108  EXPECT_EQ(46, divCeil(451, 10));
109 }
110 
111 TEST(IntmathTest, roundUp)
112 {
113  EXPECT_EQ(4104, roundUp(4101, 4));
114  EXPECT_EQ(4112, roundUp(4105, 8));
115  EXPECT_EQ(4112, roundUp(4101, 16));
116  EXPECT_EQ(8192, roundUp(7991, 256));
117 }
118 
119 TEST(IntmathTest, roundDown)
120 {
121  EXPECT_EQ(4100, roundDown(4101, 4));
122  EXPECT_EQ(4104, roundDown(4105, 8));
123  EXPECT_EQ(4096, roundDown(4101, 16));
124  EXPECT_EQ(7936, roundDown(7991, 256));
125 }
roundDown
T roundDown(const T &val, const U &align)
This function is used to align addresses in memory.
Definition: intmath.hh:150
ceilLog2
int ceilLog2(const T &n)
Definition: intmath.hh:88
divCeil
T divCeil(const T &a, const U &b)
Definition: intmath.hh:114
TEST
TEST(IntmathTest, isPowerOf2)
Definition: intmath.test.cc:33
floorLog2
std::enable_if_t< std::is_integral< T >::value, int > floorLog2(T x)
Definition: intmath.hh:63
roundUp
T roundUp(const T &val, const U &align)
This function is used to align addresses in memory.
Definition: intmath.hh:131
intmath.hh
power
uint64_t power(uint32_t n, uint32_t e)
Definition: intmath.hh:43
isPowerOf2
bool isPowerOf2(const T &n)
Definition: intmath.hh:102

Generated on Tue Mar 23 2021 19:41:24 for gem5 by doxygen 1.8.17