Convert this C macro kroundup32 to D mixin?

biocyberman via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Apr 8 04:01:34 PDT 2017


On Saturday, 8 April 2017 at 10:09:47 UTC, Mike Parker wrote:
> T kroundup32(T)(T x) {
>     pragma(inline, true);
>     --(x);
>     (x)|=(x)>>1;
>     (x)|=(x)>>2;
>     (x)|=(x)>>4;
>     (x)|=(x)>>8;
>     (x)|=(x)>>16;
>     return ++(x);
> }


I also came up with this:

import std.stdio;
pragma( inline, true ):
static int kroundup32( int x){
--(x);
writeln("X: ",x);
  (x)|=(x)>>1;
writeln("X: ",x);
  (x)|=(x)>>2;
writeln("X: ",x);
(x)|=(x)>>4;
writeln("X: ",x);
  (x)|=(x)>>8;
writeln("X: ",x);
  (x)|=(x)>>16;
writeln("X: ",x);
  ++(x);
writeln("X: ",x);

  return x;
}

int main(){
   int num = 31;
   num = kroundup32(num);
   writeln("Num:", num);
   return 0;
}

Is this way of using pragma the same as your way? I am still new 
to this so I want to understand more.

And is it a good idea to do manipulate 'num' directly so I can 
omit 'return' and avoid re-assigning statement? That's what C 
version does.


More information about the Digitalmars-d-learn mailing list