[PATCH] D25344: Add a fast path to alignTo.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 6 13:04:14 PDT 2016


ruiu created this revision.
ruiu added reviewers: rafael, davide.
ruiu added a subscriber: llvm-commits.

If an alignment is a power of two, we can avoid integer division,
and it is very likely to happen.


https://reviews.llvm.org/D25344

Files:
  include/llvm/Support/MathExtras.h


Index: include/llvm/Support/MathExtras.h
===================================================================
--- include/llvm/Support/MathExtras.h
+++ include/llvm/Support/MathExtras.h
@@ -670,6 +670,10 @@
 /// \endcode
 inline uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew = 0) {
   assert(Align != 0u && "Align can't be 0.");
+  if (LLVM_LIKELY(isPowerOf2_64(Align))) {
+    Skew &= Align - 1;
+    return ((Value + Align - 1 - Skew) & -Align) + Skew;
+  }
   Skew %= Align;
   return (Value + Align - 1 - Skew) / Align * Align + Skew;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D25344.73841.patch
Type: text/x-patch
Size: 558 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161006/a28d78d1/attachment.bin>


More information about the llvm-commits mailing list