Best way in D2 to rotate a ubyte[4] array

Andrew Wiley debio264 at gmail.com
Wed Mar 9 23:38:07 PST 2011


On Wed, Mar 9, 2011 at 7:25 PM, U2 fan <iam at u2fan.com> wrote:
> == Quote from bearophile (bearophileHUGS at lycos.com)'s article
>> Tom:
>> > What is the most efficient way of implement a rotation of ubyte[4] array?
>> >
>> > By rotation I mean: rotateRight([1, 2, 3, 4]) -> [4, 1, 2, 3]
>> Two versions, I have done no benchmarks so far:
>> import std.c.stdio: printf;
>> union Four {
>>     ubyte[4] a;
>>     uint u;
>> }
>> void showFour(Four f) {
>>     printf("f.u: %u\n", f.u);
>>     printf("f.a: [%d, %d, %d, %d]\n",
>>            cast(int)f.a[0], cast(int)f.a[1],
>>            cast(int)f.a[2], cast(int)f.a[3]);
>> }
>> void main() {
>>     Four f;
>>     f.a[] = [1, 2, 3, 4];
>>     showFour(f);
>>     f.u = (f.u << 8) | (f.u >> 24);
>>     showFour(f);
>>     printf("\n");
>>     // alternative
>>     f.a[] = [1, 2, 3, 4];
>>     uint u2 = f.u;
>>     showFour(f);
>>     printf("u2: %u\n", u2);
>>     asm {
>>         rol  u2, 8;
>>     }
>>     f.u = u2;
>>     showFour(f);
>> }
>> Bye,
>> bearophile
>
> I am offend!
>

Once I figured it out, I lol'd quite a bit.


More information about the Digitalmars-d-learn mailing list