[PATCH] D76274: [Alignment] Add alignTo with skew parameter

Guillaume Chatelet via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 17 03:44:07 PDT 2020


gchatelet created this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
gchatelet edited the summary of this revision.
gchatelet added a reviewer: courbet.
gchatelet edited the summary of this revision.
courbet accepted this revision.
courbet added inline comments.
This revision is now accepted and ready to land.


================
Comment at: llvm/include/llvm/Support/Alignment.h:192
+inline uint64_t alignTo(uint64_t Size, Align A, uint64_t Skew) {
+  const uint64_t value = A.value();
+  Skew %= value;
----------------
`Value`


================
Comment at: llvm/unittests/Support/AlignmentTest.cpp:129
+TEST(AlignmentTest, AlignToWithSkew) {
+  EXPECT_EQ(alignTo(5, Align(8), 7), 7U);
+  EXPECT_EQ(alignTo(17, Align(8), 1), 17U);
----------------
Add a test for `0` ?


This is derived from MathExtra's `alignTo`


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D76274

Files:
  llvm/include/llvm/Support/Alignment.h
  llvm/unittests/Support/AlignmentTest.cpp


Index: llvm/unittests/Support/AlignmentTest.cpp
===================================================================
--- llvm/unittests/Support/AlignmentTest.cpp
+++ llvm/unittests/Support/AlignmentTest.cpp
@@ -125,6 +125,12 @@
   }
 }
 
+TEST(AlignmentTest, AlignToWithSkew) {
+  EXPECT_EQ(alignTo(5, Align(8), 7), 7U);
+  EXPECT_EQ(alignTo(17, Align(8), 1), 17U);
+  EXPECT_EQ(alignTo(~0LL, Align(8), 3), 3U);
+}
+
 TEST(AlignmentTest, Log2) {
   for (uint64_t Value : getValidAlignments()) {
     EXPECT_EQ(Log2(Align(Value)), Log2_64(Value));
Index: llvm/include/llvm/Support/Alignment.h
===================================================================
--- llvm/include/llvm/Support/Alignment.h
+++ llvm/include/llvm/Support/Alignment.h
@@ -174,7 +174,24 @@
 
   // Most compilers can generate this code but the pattern may be missed when
   // multiple functions gets inlined.
-  return (Size + value - 1) & ~(value - 1);
+  return (Size + value - 1) & ~(value - 1U);
+}
+
+/// If non-zero \p Skew is specified, the return value will be a minimal integer
+/// that is greater than or equal to \p Size and equal to \p A * N + \p Skew for
+/// some integer N. If \p Skew is larger than \p A, its value is adjusted to '\p
+/// Skew mod \p A'.
+///
+/// Examples:
+/// \code
+///   alignTo(5, Align(8), 7) = 7
+///   alignTo(17, Align(8), 1) = 17
+///   alignTo(~0LL, Align(8), 3) = 3
+/// \endcode
+inline uint64_t alignTo(uint64_t Size, Align A, uint64_t Skew) {
+  const uint64_t value = A.value();
+  Skew %= value;
+  return ((Size + value - 1 - Skew) & ~(value - 1U)) + Skew;
 }
 
 /// Returns a multiple of A needed to store `Size` bytes.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D76274.250714.patch
Type: text/x-patch
Size: 1649 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200317/8af97c4a/attachment.bin>


More information about the llvm-commits mailing list