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

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


Or to make alignTo accept only power of twos and fix code that passes
non-power-of-twos.

On Thu, Oct 6, 2016 at 1:35 PM, Rafael Avila de Espindola <
rafael.espindola at gmail.com> wrote:

> My understanding is that modern CPUs do this already.
>
> What could help is to have a dedicated alignToP2.
>
>
> Cheers,
> Rafael
>
>
> On October 6, 2016 4:04:14 PM EDT, Rui Ueyama <ruiu at google.com> wrote:
>>
>> 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;
>>  }
>>
>>
>>
> --
> Sent from my Android device with K-9 Mail. Please excuse my brevity.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161006/8ac3223c/attachment.html>


More information about the llvm-commits mailing list