[libcxx-commits] [libcxx] linear_congruential_engine: Fixes for __lce_alg_picker (PR #81080)
Jonathan Wakely via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Feb 8 03:17:10 PST 2024
================
@@ -23,47 +23,62 @@ int main(int, char**)
typedef unsigned long long T;
// m might overflow, but the overflow is OK so it shouldn't use schrage's algorithm
- typedef std::linear_congruential_engine<T, 25214903917ull, 1, (1ull<<48)> E1;
+ typedef std::linear_congruential_engine<T, 25214903917ull, 1, (1ull << 48)> E1;
E1 e1;
// make sure the right algorithm was used
assert(e1() == 25214903918);
assert(e1() == 205774354444503);
assert(e1() == 158051849450892);
// make sure result is in bounds
- assert(e1() < (1ull<<48));
- assert(e1() < (1ull<<48));
- assert(e1() < (1ull<<48));
- assert(e1() < (1ull<<48));
- assert(e1() < (1ull<<48));
+ assert(e1() < (1ull << 48));
+ assert(e1() < (1ull << 48));
+ assert(e1() < (1ull << 48));
+ assert(e1() < (1ull << 48));
+ assert(e1() < (1ull << 48));
// m might overflow. The overflow is not OK and result will be in bounds
// so we should use shrage's algorithm
- typedef std::linear_congruential_engine<T, (1ull<<2), 0, (1ull<<63) + 1> E2;
+ typedef std::linear_congruential_engine<T, (1ull << 2), 0, (1ull << 63) + 1> E2;
E2 e2;
// make sure shrage's algorithm is used (it would be 0s otherwise)
assert(e2() == 4);
assert(e2() == 16);
assert(e2() == 64);
// make sure result is in bounds
- assert(e2() < (1ull<<48) + 1);
- assert(e2() < (1ull<<48) + 1);
- assert(e2() < (1ull<<48) + 1);
- assert(e2() < (1ull<<48) + 1);
- assert(e2() < (1ull<<48) + 1);
+ assert(e2() < (1ull << 48) + 1);
+ assert(e2() < (1ull << 48) + 1);
+ assert(e2() < (1ull << 48) + 1);
+ assert(e2() < (1ull << 48) + 1);
+ assert(e2() < (1ull << 48) + 1);
- // m will not overflow so we should not use shrage's algorithm
- typedef std::linear_congruential_engine<T, 1ull, 1, (1ull<<48)> E3;
+ // m might overflow. The overflow is not OK and result will be in bounds
+ // so we should use shrage's algorithm. m is even
+ typedef std::linear_congruential_engine<T, 0x18000001ull, 0x12347ull, (3ull << 56)> E3;
E3 e3;
+ // make sure shrage's algorithm is used (it would be 0s otherwise)
----------------
jwakely wrote:
```suggestion
// so we should use Schrage's algorithm. m is even
typedef std::linear_congruential_engine<T, 0x18000001ull, 0x12347ull, (3ull << 56)> E3;
E3 e3;
// make sure Schrage's algorithm is used (it would be 0s otherwise)
```
https://github.com/llvm/llvm-project/pull/81080
More information about the libcxx-commits
mailing list