Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / numerics / rand / rand.util / rand.util.canonical / generate_canonical.pass.cpp
blobc21cdd39d13a2019ffcc0b333ba00cf9939c19e0
1 //===----------------------------------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
9 // <random>
11 // template<class RealType, size_t bits, class URNG>
12 // RealType generate_canonical(URNG& g);
14 #include <random>
16 #include <cassert>
17 #include <limits>
19 #include "test_macros.h"
20 #include "truncate_fp.h"
22 int main(int, char**)
25 typedef std::minstd_rand0 E;
26 typedef float F;
27 E r;
28 F f = std::generate_canonical<F, 0>(r);
29 assert(f == truncate_fp((16807 - E::min()) / (static_cast<F>(E::max() - E::min()) + F(1))));
32 typedef std::minstd_rand0 E;
33 typedef float F;
34 E r;
35 F f = std::generate_canonical<F, 1>(r);
36 assert(f == truncate_fp((16807 - E::min()) / (static_cast<F>(E::max() - E::min()) + F(1))));
39 typedef std::minstd_rand0 E;
40 typedef float F;
41 E r;
42 F f = std::generate_canonical<F, std::numeric_limits<F>::digits - 1>(r);
43 assert(f == truncate_fp((16807 - E::min()) / (static_cast<F>(E::max() - E::min()) + F(1))));
46 typedef std::minstd_rand0 E;
47 typedef float F;
48 E r;
49 F f = std::generate_canonical<F, std::numeric_limits<F>::digits>(r);
50 assert(f == truncate_fp((16807 - E::min()) / (static_cast<F>(E::max() - E::min()) + F(1))));
53 typedef std::minstd_rand0 E;
54 typedef float F;
55 E r;
56 F f = std::generate_canonical<F, std::numeric_limits<F>::digits + 1>(r);
57 assert(f == truncate_fp((16807 - E::min()) / (static_cast<F>(E::max() - E::min()) + F(1))));
61 typedef std::minstd_rand0 E;
62 typedef double F;
63 E r;
64 F f = std::generate_canonical<F, 0>(r);
65 assert(f == truncate_fp((16807 - E::min()) / (static_cast<F>(E::max() - E::min()) + F(1))));
68 typedef std::minstd_rand0 E;
69 typedef double F;
70 E r;
71 F f = std::generate_canonical<F, 1>(r);
72 assert(f == truncate_fp((16807 - E::min()) / (static_cast<F>(E::max() - E::min()) + F(1))));
75 typedef std::minstd_rand0 E;
76 typedef double F;
77 E r;
78 F f = std::generate_canonical<F, std::numeric_limits<F>::digits - 1>(r);
79 assert(f == truncate_fp(
80 (16807 - E::min() +
81 (282475249 - E::min()) * (static_cast<F>(E::max() - E::min()) + F(1))) /
82 ((static_cast<F>(E::max() - E::min()) + F(1)) * (static_cast<F>(E::max() - E::min()) + F(1)))));
85 typedef std::minstd_rand0 E;
86 typedef double F;
87 E r;
88 F f = std::generate_canonical<F, std::numeric_limits<F>::digits>(r);
89 assert(f == truncate_fp(
90 (16807 - E::min() +
91 (282475249 - E::min()) * (static_cast<F>(E::max() - E::min()) + F(1))) /
92 ((static_cast<F>(E::max() - E::min()) + F(1)) * (static_cast<F>(E::max() - E::min()) + F(1)))));
95 typedef std::minstd_rand0 E;
96 typedef double F;
97 E r;
98 F f = std::generate_canonical<F, std::numeric_limits<F>::digits + 1>(r);
99 assert(f == truncate_fp(
100 (16807 - E::min() +
101 (282475249 - E::min()) * (static_cast<F>(E::max() - E::min()) + F(1))) /
102 ((static_cast<F>(E::max() - E::min()) + F(1)) * (static_cast<F>(E::max() - E::min()) + F(1)))));
105 return 0;