#144 msvc unsigned/signed problem in random.h

release_3.10.x
closed
5
2012-09-15
2007-01-15
Tomas Andersson
No

Msvc fails to compile IT++ 3.10.8 "out of the box" because of an unsigned/signed warning being treated as an error with the project settings provided.

The problem is due to the mixing of signed and unsigned integers in the function twist. By proper typecasting, the problem disappears.

I changed line 202 in random.h

from

{ return m ^ (mixBits(s0,s1)>>1) ^ (-loBit(s1) & 0x9908b0dfU); }

to

{ return m ^ (mixBits(s0,s1)>>1) ^ (static_cast<unsigned int="">(-static_cast<int>(loBit(s1))) & 0x9908b0dfU); }

Maybe there is a better way to do the same thing, but this solution works fine.

Discussion

  • Logged In: YES
    user_id=1004597
    Originator: NO

    Thanks for your report Tomas. I have already contacted the author of this code (this is a third party code included in IT++) to comment on this problem.

    /ediap

     
  • Logged In: YES
    user_id=1004597
    Originator: NO

    This bug is now fixed in the repository sources. Therefore I am closing this bug report.

    For your convenience, here is the fixed twist() function code:

    unsigned int twist( const unsigned int& m, const unsigned int& s0,
            const unsigned int& s1 ) const
    { return m ^ (mixBits(s0,s1)>>1) ^ (loBit(s1) ? 0x9908b0dfU : 0U); }
    

    This implementation is almost identical to the macro implementation from original C sources by Takuji Nishimura and Makoto Matsumoto:

    #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))

    /ediap