Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / benchmarks / GenerateInput.h
blob5710b4e1418ba3014dac07b6b3ec45e82e2b6e10
1 #ifndef BENCHMARK_GENERATE_INPUT_H
2 #define BENCHMARK_GENERATE_INPUT_H
4 #include <algorithm>
5 #include <climits>
6 #include <cstddef>
7 #include <random>
8 #include <string>
9 #include <vector>
11 static const char Letters[] = {
12 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
13 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
14 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
15 static const std::size_t LettersSize = sizeof(Letters);
17 inline std::default_random_engine& getRandomEngine() {
18 static std::default_random_engine RandEngine(std::random_device{}());
19 return RandEngine;
22 inline char getRandomChar() {
23 std::uniform_int_distribution<> LettersDist(0, LettersSize - 1);
24 return Letters[LettersDist(getRandomEngine())];
27 template <class IntT>
28 inline IntT getRandomInteger(IntT Min, IntT Max) {
29 std::uniform_int_distribution<unsigned long long> dist(Min, Max);
30 return static_cast<IntT>(dist(getRandomEngine()));
33 inline std::string getRandomString(std::size_t Len) {
34 std::string str(Len, 0);
35 std::generate_n(str.begin(), Len, &getRandomChar);
36 return str;
39 template <class IntT>
40 inline std::vector<IntT> getDuplicateIntegerInputs(size_t N) {
41 std::vector<IntT> inputs(N, static_cast<IntT>(-1));
42 return inputs;
45 template <class IntT>
46 inline std::vector<IntT> getSortedIntegerInputs(size_t N) {
47 std::vector<IntT> inputs;
48 for (size_t i = 0; i < N; i += 1)
49 inputs.push_back(i);
50 return inputs;
53 template <class IntT>
54 std::vector<IntT> getSortedLargeIntegerInputs(size_t N) {
55 std::vector<IntT> inputs;
56 for (size_t i = 0; i < N; ++i) {
57 inputs.push_back(i + N);
59 return inputs;
62 template <class IntT>
63 std::vector<IntT> getSortedTopBitsIntegerInputs(size_t N) {
64 std::vector<IntT> inputs = getSortedIntegerInputs<IntT>(N);
65 for (auto& E : inputs)
66 E <<= ((sizeof(IntT) / 2) * CHAR_BIT);
67 return inputs;
70 template <class IntT>
71 inline std::vector<IntT> getReverseSortedIntegerInputs(size_t N) {
72 std::vector<IntT> inputs;
73 std::size_t i = N;
74 while (i > 0) {
75 --i;
76 inputs.push_back(i);
78 return inputs;
81 template <class IntT>
82 std::vector<IntT> getPipeOrganIntegerInputs(size_t N) {
83 std::vector<IntT> v;
84 v.reserve(N);
85 for (size_t i = 0; i < N / 2; ++i)
86 v.push_back(i);
87 for (size_t i = N / 2; i < N; ++i)
88 v.push_back(N - i);
89 return v;
92 template <class IntT>
93 std::vector<IntT> getRandomIntegerInputs(size_t N) {
94 std::vector<IntT> inputs;
95 for (size_t i = 0; i < N; ++i) {
96 inputs.push_back(getRandomInteger<IntT>(0, std::numeric_limits<IntT>::max()));
98 return inputs;
101 inline std::vector<std::string> getDuplicateStringInputs(size_t N) {
102 std::vector<std::string> inputs(N, getRandomString(1024));
103 return inputs;
106 inline std::vector<std::string> getRandomStringInputs(size_t N) {
107 std::vector<std::string> inputs;
108 for (size_t i = 0; i < N; ++i) {
109 inputs.push_back(getRandomString(1024));
111 return inputs;
114 inline std::vector<std::string> getPrefixedRandomStringInputs(size_t N) {
115 std::vector<std::string> inputs;
116 constexpr int kSuffixLength = 32;
117 const std::string prefix = getRandomString(1024 - kSuffixLength);
118 for (size_t i = 0; i < N; ++i) {
119 inputs.push_back(prefix + getRandomString(kSuffixLength));
121 return inputs;
124 inline std::vector<std::string> getSortedStringInputs(size_t N) {
125 std::vector<std::string> inputs = getRandomStringInputs(N);
126 std::sort(inputs.begin(), inputs.end());
127 return inputs;
130 inline std::vector<std::string> getReverseSortedStringInputs(size_t N) {
131 std::vector<std::string> inputs = getSortedStringInputs(N);
132 std::reverse(inputs.begin(), inputs.end());
133 return inputs;
136 inline std::vector<const char*> getRandomCStringInputs(size_t N) {
137 static std::vector<std::string> inputs = getRandomStringInputs(N);
138 std::vector<const char*> cinputs;
139 for (auto const& str : inputs)
140 cinputs.push_back(str.c_str());
141 return cinputs;
144 #endif // BENCHMARK_GENERATE_INPUT_H