1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
11 // template <class UIntType, UIntType a, UIntType c, UIntType m>
12 // class linear_congruential_engine;
14 // result_type operator()();
19 #include "test_macros.h"
23 typedef unsigned long long T
;
25 // m might overflow, but the overflow is OK so it shouldn't use Schrage's algorithm
26 typedef std::linear_congruential_engine
<T
, 25214903917ull, 1, (1ull << 48)> E1
;
28 // make sure the right algorithm was used
29 assert(e1() == 25214903918ull);
30 assert(e1() == 205774354444503ull);
31 assert(e1() == 158051849450892ull);
32 // make sure result is in bounds
33 assert(e1() < (1ull << 48));
34 assert(e1() < (1ull << 48));
35 assert(e1() < (1ull << 48));
36 assert(e1() < (1ull << 48));
37 assert(e1() < (1ull << 48));
39 // m might overflow. The overflow is not OK and result will be in bounds
40 // so we should use Schrage's algorithm
41 typedef std::linear_congruential_engine
<T
, (1ull << 32), 0, (1ull << 63) + 1ull> E2
;
43 // make sure Schrage's algorithm is used (it would be 0s after the first otherwise)
44 assert(e2() == (1ull << 32));
45 assert(e2() == (1ull << 63) - 1ull);
46 assert(e2() == (1ull << 63) - 0x1ffffffffull
);
47 // make sure result is in bounds
48 assert(e2() < (1ull << 63) + 1);
49 assert(e2() < (1ull << 63) + 1);
50 assert(e2() < (1ull << 63) + 1);
51 assert(e2() < (1ull << 63) + 1);
52 assert(e2() < (1ull << 63) + 1);
54 // m might overflow. The overflow is not OK and result will be in bounds
55 // so we should use Schrage's algorithm. m is even
56 typedef std::linear_congruential_engine
<T
, 0x18000001ull
, 0x12347ull
, (3ull << 56)> E3
;
58 // make sure Schrage's algorithm is used
59 assert(e3() == 0x18012348ull
);
60 assert(e3() == 0x2401b4ed802468full
);
61 assert(e3() == 0x18051ec400369d6ull
);
62 // make sure result is in bounds
63 assert(e3() < (3ull << 56));
64 assert(e3() < (3ull << 56));
65 assert(e3() < (3ull << 56));
66 assert(e3() < (3ull << 56));
67 assert(e3() < (3ull << 56));
70 // m might overflow. The overflow is not OK, result will be in bounds,
71 // and Schrage's algorithm is incompatible here. Need to use 64 bit arithmetic.
72 typedef std::linear_congruential_engine
<unsigned, 0x10009u
, 0u, 0x7fffffffu
> E4
;
74 // make sure enough precision is used
75 assert(e4() == 0x10009u
);
76 assert(e4() == 0x120053u
);
77 assert(e4() == 0xf5030fu
);
78 // make sure result is in bounds
79 assert(e4() < 0x7fffffffu
);
80 assert(e4() < 0x7fffffffu
);
81 assert(e4() < 0x7fffffffu
);
82 assert(e4() < 0x7fffffffu
);
83 assert(e4() < 0x7fffffffu
);
85 #ifndef TEST_HAS_NO_INT128
86 // m might overflow. The overflow is not OK, result will be in bounds,
87 // and Schrage's algorithm is incompatible here. Need to use 128 bit arithmetic.
88 typedef std::linear_congruential_engine
<T
, 0x100000001ull
, 0ull, (1ull << 61) - 1ull> E5
;
90 // make sure enough precision is used
91 assert(e5() == 0x100000001ull
);
92 assert(e5() == 0x200000009ull
);
93 assert(e5() == 0xb00000019ull
);
94 // make sure result is in bounds
95 assert(e5() < (1ull << 61) - 1ull);
96 assert(e5() < (1ull << 61) - 1ull);
97 assert(e5() < (1ull << 61) - 1ull);
98 assert(e5() < (1ull << 61) - 1ull);
99 assert(e5() < (1ull << 61) - 1ull);
102 // m will not overflow so we should not use Schrage's algorithm
103 typedef std::linear_congruential_engine
<T
, 1ull, 1, (1ull << 48)> E6
;
105 // make sure the correct algorithm was used
106 assert(e6() == 2ull);
107 assert(e6() == 3ull);
108 assert(e6() == 4ull);
109 // make sure result is in bounds
110 assert(e6() < (1ull << 48));
111 assert(e6() < (1ull << 48));
112 assert(e6() < (1ull << 48));
113 assert(e6() < (1ull << 48));
114 assert(e6() < (1ull << 48));