www.digitalmars.com         C & C++   DMDScript  

D - #define in D

reply "Andrew Edwards" <edwardsac spamfreeusa.com> writes:
How would one accomplish the following in D?

<snip from metronome twister rng>
#define MATRIX_A 0x9908b0dfUL   /* constant vector a */
#define UMASK 0x80000000UL /* most significant w-r bits */
#define LMASK 0x7fffffffUL /* least significant r bits */
#define MIXBITS(u,v) ( ((u) & UMASK) | ((v) & LMASK) )
#define TWIST(u,v) ((MIXBITS(u,v) >> 1) ^ ((v)&1UL ? MATRIX_A : 0UL))
</snip from metronome twister rng>

I read the "Converting C to D" and "The C Preproscesor vs D" pages but did
not find a clear-cut example for the above cases.

thanks in advance,
Andrew
Jul 04 2003
parent reply "Walter" <walter digitalmars.com> writes:
"Andrew Edwards" <edwardsac spamfreeusa.com> wrote in message
news:be4i4q$2vdt$1 digitaldaemon.com...
 How would one accomplish the following in D?

 <snip from metronome twister rng>
 #define MATRIX_A 0x9908b0dfUL   /* constant vector a */
 #define UMASK 0x80000000UL /* most significant w-r bits */
 #define LMASK 0x7fffffffUL /* least significant r bits */
 #define MIXBITS(u,v) ( ((u) & UMASK) | ((v) & LMASK) )
 #define TWIST(u,v) ((MIXBITS(u,v) >> 1) ^ ((v)&1UL ? MATRIX_A : 0UL))
 </snip from metronome twister rng>

 I read the "Converting C to D" and "The C Preproscesor vs D" pages but did
 not find a clear-cut example for the above cases.

const uint MATRIX_A = 0x9908b0df; /* constant vector a */ const uint UMASK = 0x80000000; /* most significant w-r bits */ const uint LMASK = 0x7fffffff; /* least significant r bits */ const uint MIXBITS(uint u, uint v) { return (u & UMASK) | (v & LMASK); } const uint TWIST(uint u,uint v) { return (MIXBITS(u,v) >> 1) ^ (v&1 ? MATRIX_A : 0); }
Jul 04 2003
next sibling parent reply "Andrew Edwards" <edwardsac spamfreeusa.com> writes:
"Walter" <walter digitalmars.com> wrote...

 const uint MIXBITS(uint u, uint v) { return (u & UMASK) | (v & LMASK); }
 const uint TWIST(uint u,uint v) { return (MIXBITS(u,v) >> 1) ^ (v&1 ?
 MATRIX_A : 0); }

Thanks Walter, However, the above lines generate the following error: mt.d(54): function MAXBITS functions cannot be const or auto would it be safe to just drop the const or is this a bug?
Jul 04 2003
parent reply "Walter" <walter digitalmars.com> writes:
"Andrew Edwards" <edwardsac spamfreeusa.com> wrote in message
news:be4m35$25v$1 digitaldaemon.com...
 "Walter" <walter digitalmars.com> wrote...

 const uint MIXBITS(uint u, uint v) { return (u & UMASK) | (v & LMASK); }
 const uint TWIST(uint u,uint v) { return (MIXBITS(u,v) >> 1) ^ (v&1 ?
 MATRIX_A : 0); }

Thanks Walter, However, the above lines generate the following error: mt.d(54): function MAXBITS functions cannot be const or auto would it be safe to just drop the const or is this a bug?

My mistake. Drop the const.
Jul 04 2003
parent "Sean L. Palmer" <palmer.sean verizon.net> writes:
Heh.

Sean

"Walter" <walter digitalmars.com> wrote in message
news:be504p$b7s$1 digitaldaemon.com...
 However, the above lines generate the following error:

     mt.d(54): function MAXBITS functions cannot be const or auto

 would it be safe to just drop the const or is this a bug?

My mistake. Drop the const.

Jul 05 2003
prev sibling parent "Sean L. Palmer" <palmer.sean verizon.net> writes:
Are those functions that return a const value or const functions?

Does this type of construct tell the compiler that the function cannot have
side-effects?

Sean

"Walter" <walter digitalmars.com> wrote in message
news:be4kle$ji$1 digitaldaemon.com...
 const uint MIXBITS(uint u, uint v) { return (u & UMASK) | (v & LMASK); }
 const uint TWIST(uint u,uint v) { return (MIXBITS(u,v) >> 1) ^ (v&1 ?
 MATRIX_A : 0); }

Jul 05 2003