C quick calculation of next multiple of 4?

前端 未结 7 1894
无人共我
无人共我 2020-12-15 05:04

What\'s a fast way to round up an unsigned int to a multiple of 4?

A multiple of 4 has the two least significant bits 0, right? So I could

7条回答
  •  心在旅途
    2020-12-15 05:42

    I assume that what you are trying to achieve is the alignment of the input number, i.e. if the original number is already a multiple of 4, then it doesn't need to be changed. However, this is not clear from your question. Maybe you want next multiple even when the original number is already a multiple? Please, clarify.

    In order to align an arbitrary non-negative number i on an arbitrary boundary n you just need to do

    i = i / n * n;
    

    But this will align it towards the negative infinity. In order to align it to the positive infinity, add n - 1 before peforming the alignment

    i = (i + n - 1) / n * n;
    

    This is already good enough for all intents and purposes. In your case it would be

    i = (i + 3) / 4 * 4;
    

    However, if you would prefer to to squeeze a few CPU clocks out of this, you might use the fact that the i / 4 * 4 can be replaced with a bit-twiddling i & ~0x3, giving you

    i = (i + 3) & ~0x3;
    

    although it wouldn't surprise me if modern compilers could figure out the latter by themselves.

提交回复
热议问题