Changeset 2386:81b47fc5c444 in lemon-0.x for lemon/random.h
- Timestamp:
- 03/02/07 19:04:28 (17 years ago)
- Branch:
- default
- Phase:
- public
- Convert:
- svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@3217
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lemon/random.h
r2380 r2386 122 122 static const int shift = 156; 123 123 124 static const Word mul = (Word)0x5851F42Du << 32 | (Word)0x4C957F2Du;125 static const Word arrayInit = (Word)0x00000000u << 32 |(Word)0x012BD6AAu;126 static const Word arrayMul1 = (Word)0x369DEA0Fu << 32 |(Word)0x31A53F85u;127 static const Word arrayMul2 = (Word)0x27BB2EE6u << 32 |(Word)0x87B0B0FDu;128 129 static const Word mask = (Word)0xB5026F5Au << 32 | (Word)0xA96619E9u;130 static const Word loMask = ( (Word)1u<< 31) - 1;124 static const Word mul = Word(0x5851F42Du) << 32 | Word(0x4C957F2Du); 125 static const Word arrayInit = Word(0x00000000u) << 32 |Word(0x012BD6AAu); 126 static const Word arrayMul1 = Word(0x369DEA0Fu) << 32 |Word(0x31A53F85u); 127 static const Word arrayMul2 = Word(0x27BB2EE6u) << 32 |Word(0x87B0B0FDu); 128 129 static const Word mask = Word(0xB5026F5Au) << 32 | Word(0xA96619E9u); 130 static const Word loMask = (Word(1u) << 31) - 1; 131 131 static const Word hiMask = ~loMask; 132 132 133 133 static Word tempering(Word rnd) { 134 rnd ^= (rnd >> 29) & ( (Word)0x55555555u << 32 | (Word)0x55555555u);135 rnd ^= (rnd << 17) & ( (Word)0x71D67FFFu << 32 | (Word)0xEDA60000u);136 rnd ^= (rnd << 37) & ( (Word)0xFFF7EEE0u << 32 | (Word)0x00000000u);134 rnd ^= (rnd >> 29) & (Word(0x55555555u) << 32 | Word(0x55555555u)); 135 rnd ^= (rnd << 17) & (Word(0x71D67FFFu) << 32 | Word(0xEDA60000u)); 136 rnd ^= (rnd << 37) & (Word(0xFFF7EEE0u) << 32 | Word(0x00000000u)); 137 137 rnd ^= (rnd >> 43); 138 138 return rnd; … … 217 217 } 218 218 219 state[length - 1] = (Word)1<< (bits - 1);219 state[length - 1] = Word(1) << (bits - 1); 220 220 } 221 221 … … 274 274 static Result mask(const Result& result) { 275 275 return Masker<Result, (shift + 1) / 2>:: 276 mask( (Result)(result | (result >> shift)));276 mask(static_cast<Result>(result | (result >> shift))); 277 277 } 278 278 }; … … 281 281 struct Masker<Result, 1> { 282 282 static Result mask(const Result& result) { 283 return (Result)(result | (result >> 1));283 return static_cast<Result>(result | (result >> 1)); 284 284 } 285 285 }; … … 292 292 293 293 static Result convert(RandomCore<Word>& rnd) { 294 return (Result)(rnd() >> (bits - rest)) << shift;294 return static_cast<Result>(rnd() >> (bits - rest)) << shift; 295 295 } 296 296 … … 302 302 303 303 static Result convert(RandomCore<Word>& rnd) { 304 return ( (Result)rnd() << shift) |304 return (static_cast<Result>(rnd()) << shift) | 305 305 IntConversion<Result, Word, rest - bits, shift + bits>::convert(rnd); 306 306 } … … 313 313 struct Mapping { 314 314 static Result map(RandomCore<Word>& rnd, const Result& bound) { 315 Word max = (Word)(bound - 1);315 Word max = Word(bound - 1); 316 316 Result mask = Masker<Result>::mask(bound - 1); 317 317 Result num; … … 326 326 struct Mapping<Result, Word, false> { 327 327 static Result map(RandomCore<Word>& rnd, const Result& bound) { 328 Word max = (Word)(bound - 1);328 Word max = Word(bound - 1); 329 329 Word mask = Masker<Word, (std::numeric_limits<Result>::digits + 1) / 2> 330 330 ::mask(max); … … 342 342 Result res = ShiftMultiplier<Result, exp / 2>::multiplier(); 343 343 res *= res; 344 if ((exp & 1) == 1) res *= (Result)2.0;344 if ((exp & 1) == 1) res *= static_cast<Result>(2.0); 345 345 return res; 346 346 } … … 352 352 Result res = ShiftMultiplier<Result, exp / 2>::multiplier(); 353 353 res *= res; 354 if ((exp & 1) == 1) res *= (Result)0.5;354 if ((exp & 1) == 1) res *= static_cast<Result>(0.5); 355 355 return res; 356 356 } … … 360 360 struct ShiftMultiplier<Result, 0, true> { 361 361 static const Result multiplier() { 362 return (Result)1.0;362 return static_cast<Result>(1.0); 363 363 } 364 364 }; … … 367 367 struct ShiftMultiplier<Result, -20, true> { 368 368 static const Result multiplier() { 369 return (Result)(1.0/1048576.0);369 return static_cast<Result>(1.0/1048576.0); 370 370 } 371 371 }; … … 374 374 struct ShiftMultiplier<Result, -32, true> { 375 375 static const Result multiplier() { 376 return (Result)(1.0/424967296.0);376 return static_cast<Result>(1.0/424967296.0); 377 377 } 378 378 }; … … 381 381 struct ShiftMultiplier<Result, -53, true> { 382 382 static const Result multiplier() { 383 return (Result)(1.0/9007199254740992.0);383 return static_cast<Result>(1.0/9007199254740992.0); 384 384 } 385 385 }; … … 388 388 struct ShiftMultiplier<Result, -64, true> { 389 389 static const Result multiplier() { 390 return (Result)(1.0/18446744073709551616.0);390 return static_cast<Result>(1.0/18446744073709551616.0); 391 391 } 392 392 }; … … 407 407 static Result convert(RandomCore<Word>& rnd) { 408 408 return Shifting<Result, - shift - rest>:: 409 shift( (Result)(rnd() >> (bits - rest)));409 shift(static_cast<Result>(rnd() >> (bits - rest))); 410 410 } 411 411 }; … … 416 416 417 417 static Result convert(RandomCore<Word>& rnd) { 418 return Shifting<Result, - shift - bits>::shift((Result)rnd()) + 419 RealConversion<Result, Word, rest-bits, shift + bits>::convert(rnd); 418 return Shifting<Result, - shift - bits>:: 419 shift(static_cast<Result>(rnd())) + 420 RealConversion<Result, Word, rest-bits, shift + bits>:: 421 convert(rnd); 420 422 } 421 423 }; … … 428 430 std::vector<Word> ws; 429 431 for (Iterator it = begin; it != end; ++it) { 430 ws.push_back( (Word)*it);432 ws.push_back(Word(*it)); 431 433 } 432 434 rnd.initState(ws.begin(), ws.end());
Note: See TracChangeset
for help on using the changeset viewer.