Convert this C macro kroundup32 to D mixin?

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


On Saturday, 8 April 2017 at 11:24:02 UTC, Nicholas Wilson wrote:

> The ':' means that it applies to everything that follows it, so 
> while it doesn't matters in this example if you had
> pragma( inline, true ):
> int kroundup32( int x) { ... }
>
> auto someVeryLargeFunction( Args args)
> {
>     // ...
> }
>
> and then you used someVeryLargeFunction in a bunch of places 
> then that would cause a lot of binary bloat.

That's big difference! Thank you for pointing this out for me.


> if you want the the function to affect the variable use a 'ref' 
> as in
>
>  void kroundup32(T)(ref T x) {
>      pragma(inline, true);
>      --(x);
>      (x)|=(x)>>1;
>      (x)|=(x)>>2;
>      (x)|=(x)>>4;
>      (x)|=(x)>>8;
>      (x)|=(x)>>16;
>      return ++(x);
>  }
>
>  int main(){
>    int num = 31;
>    writeln("Before: ",num); // 31
>    kroundup32(num);
>    writeln("After: ", num);   //32
>    return 0;
>  }
>
> is it a good idea? I would not think it is necessary.
>
> As an aside the C version has parentheses around the "x" 
> because it is a macro and it is substituted as text not 
> symbolically, they are not needed in D.

This thing now is clear and settled while I try to navigate my 
mind around many new things. Really appreciate your help, Nicolas.




More information about the Digitalmars-d-learn mailing list