Attention is currently required from: Daniel Carvalho, Giacomo Travaglini.
Hello kokoro, Daniel Carvalho, Giacomo Travaglini,

I'd like you to do a code review. Please visit

    https://gem5-review.googlesource.com/c/public/gem5/+/43234

to review the following change.


Change subject: base: Add log2i to calculate log2 for integers
......................................................................

base: Add log2i to calculate log2 for integers

This is meant to evaluate the log2 for power of 2 integers

Change-Id: Iaa110cce4d36c578a201c8a45e9e2e3a369ffb30
Signed-off-by: Giacomo Travaglini <[email protected]>
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/41494
Maintainer: Gabe Black <[email protected]>
Tested-by: kokoro <[email protected]>
Reviewed-by: Daniel Carvalho <[email protected]>
---
M src/base/intmath.hh
M src/base/intmath.test.cc
2 files changed, 83 insertions(+), 0 deletions(-)



diff --git a/src/base/intmath.hh b/src/base/intmath.hh
index acf7681..ba20177 100644
--- a/src/base/intmath.hh
+++ b/src/base/intmath.hh
@@ -1,4 +1,16 @@
 /*
+ * Copyright (c) 2021 ARM Limited
+ * All rights reserved
+ *
+ * The license below extends only to copyright in the software and shall
+ * not be construed as granting a license to any other intellectual
+ * property including but not limited to intellectual property relating
+ * to a hardware implementation of the functionality of the software
+ * licensed hereunder.  You may use the software subject to the license
+ * terms below provided that you ensure that this notice is replicated
+ * unmodified and in its entirety in all distributions of the software,
+ * modified or unmodified, in source code or in binary form.
+ *
  * Copyright (c) 2001, 2003-2005 The Regents of The University of Michigan
  * All rights reserved.
  *
@@ -33,6 +45,7 @@
 #include <cstdint>
 #include <type_traits>

+#include "base/bitfield.hh"
 #include "base/logging.hh"
 #include "base/types.hh"

@@ -154,4 +167,19 @@
     return val & ~mask;
 }

+/**
+ * Calculate the log2 of a power of 2 integer
+ *
+ * @param An input value
+ * @return The base 2 log of value
+ *
+ * @ingroup api_base_utils
+ */
+inline int
+log2i(int value)
+{
+    assert(isPowerOf2(value) && value > 0);
+    return ctz32(value);
+}
+
 #endif // __BASE_INTMATH_HH__
diff --git a/src/base/intmath.test.cc b/src/base/intmath.test.cc
index e985a1b..e953a7e 100644
--- a/src/base/intmath.test.cc
+++ b/src/base/intmath.test.cc
@@ -1,4 +1,15 @@
 /*
+ * Copyright (c) 2021 ARM Limited
+ *
+ * The license below extends only to copyright in the software and shall
+ * not be construed as granting a license to any other intellectual
+ * property including but not limited to intellectual property relating
+ * to a hardware implementation of the functionality of the software
+ * licensed hereunder.  You may use the software subject to the license
+ * terms below provided that you ensure that this notice is replicated
+ * unmodified and in its entirety in all distributions of the software,
+ * modified or unmodified, in source code or in binary form.
+ *
  * Copyright (c) 2019 The Regents of the University of California
  * All rights reserved
  *
@@ -123,3 +134,47 @@
     EXPECT_EQ(4096, roundDown(4101, 16));
     EXPECT_EQ(7936, roundDown(7991, 256));
 }
+
+/** This is testing if log2i actually works.
+ * at every iteration value is multiplied by 2 (left shift) and expected
+ * is incremented by one. This until value reaches becomes negative (by
+ * left shifting) which is when expected points to the MSB
+ */
+TEST(IntmathTest, Log2i)
+{
+    int expected = 0;
+    for (int value = 1; value > 0; expected++, value <<= 1) {
+        EXPECT_EQ(expected, log2i(value));
+    }
+
+    // Just as a sanity check for expected to point to the MSB
+    EXPECT_EQ(expected, sizeof(int) * 8 - 1);
+}
+
+/** This is testing the assertions: what if invalid arguments are
+ * provided to log2i:
+ *
+ * 1) value = 0
+ * 2) value < 0
+ * 3) value is not a power of 2
+ */
+TEST(IntmathDeathTest, Log2iDeath)
+{
+    // 1) value = 0
+    EXPECT_DEATH({
+        const int value = 0;
+        log2i(value);
+    }, "value > 0.*failed");
+
+    // 2) value < 0
+    EXPECT_DEATH({
+        const int value = -1;
+        log2i(value);
+    }, "value > 0.*failed");
+
+    // 3) value is not a power of 2
+    EXPECT_DEATH({
+        const int value = 5;
+        log2i(value);
+    }, "isPowerOf2");
+}

--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/43234
To unsubscribe, or for help writing mail filters, visit https://gem5-review.googlesource.com/settings

Gerrit-Project: public/gem5
Gerrit-Branch: release-staging-v21-0
Gerrit-Change-Id: Iaa110cce4d36c578a201c8a45e9e2e3a369ffb30
Gerrit-Change-Number: 43234
Gerrit-PatchSet: 1
Gerrit-Owner: Peter Yuen <[email protected]>
Gerrit-Reviewer: Daniel Carvalho <[email protected]>
Gerrit-Reviewer: Giacomo Travaglini <[email protected]>
Gerrit-Reviewer: kokoro <[email protected]>
Gerrit-Attention: Daniel Carvalho <[email protected]>
Gerrit-Attention: Giacomo Travaglini <[email protected]>
Gerrit-MessageType: newchange
_______________________________________________
gem5-dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s

Reply via email to