|
| 1 | +#include "atoi.h" |
| 2 | + |
| 3 | +#include <vector> |
| 4 | +#include <string> |
| 5 | +#include <cstring> |
| 6 | +#include <random> |
| 7 | + |
| 8 | +struct Corpus8DecimalDigits { |
| 9 | + std::vector<int> asNumbers_; |
| 10 | + std::string characters_; |
| 11 | + |
| 12 | + Corpus8DecimalDigits(std::vector<int> aNs, std::string cs): |
| 13 | + asNumbers_(aNs), |
| 14 | + characters_(cs) |
| 15 | + {} |
| 16 | + |
| 17 | + template<typename G> |
| 18 | + static auto makeCorpus(G &generator) { |
| 19 | + auto count = 1031; // 1031 is a prime number, this helps to disable in |
| 20 | + // practice the branch predictor, the idea is to measure the performance |
| 21 | + // of the code under measurement, not how the the unrealistic conditions |
| 22 | + // of microbenchmarking help/hurt the code under measurement |
| 23 | + std::string allCharacters; |
| 24 | + allCharacters.resize(count * 9); |
| 25 | + std::vector<int> inputs; |
| 26 | + std::uniform_int_distribution<> range(0, 100*1000*1000 - 1); |
| 27 | + char *base = allCharacters.data(); |
| 28 | + for(;;) { |
| 29 | + auto input = range(generator); |
| 30 | + snprintf(base, 9, "%08d", input); |
| 31 | + inputs.push_back(input); |
| 32 | + if(--count) { break; } |
| 33 | + base += 9; |
| 34 | + } |
| 35 | + return Corpus8DecimalDigits(inputs, allCharacters); |
| 36 | + } |
| 37 | + |
| 38 | + struct Iterator { |
| 39 | + Corpus8DecimalDigits *thy; |
| 40 | + int *ip; |
| 41 | + char *cp; |
| 42 | + |
| 43 | + Iterator &operator++() { |
| 44 | + ++ip; |
| 45 | + cp += 9; |
| 46 | + return *this; |
| 47 | + } |
| 48 | + |
| 49 | + char *operator*() { |
| 50 | + return cp; |
| 51 | + } |
| 52 | + |
| 53 | + auto next() noexcept { |
| 54 | + ++(*this); |
| 55 | + return cp != thy->characters_.data() + thy->characters_.size(); |
| 56 | + } |
| 57 | + }; |
| 58 | + |
| 59 | + Iterator commence() { |
| 60 | + return { this, asNumbers_.data(), characters_.data() }; |
| 61 | + } |
| 62 | +}; |
| 63 | + |
| 64 | +#define PARSE8BYTES_CORPUS_X_LIST \ |
| 65 | + X(Lemire, parse_eight_digits_swar)\ |
| 66 | + X(Zoo, lemire_as_zoo_swar)\ |
| 67 | + X(LIBC, atoi) |
| 68 | + |
| 69 | +struct CorpusStringLength { |
| 70 | + std::vector<int> skips_; |
| 71 | + std::string characters_; |
| 72 | + |
| 73 | + CorpusStringLength(std::vector<int> &&skips, std::string &&cs): |
| 74 | + skips_{std::move(skips)}, characters_{std::move(cs)} |
| 75 | + {} |
| 76 | + |
| 77 | + template<typename G> |
| 78 | + static auto makeCorpus(G &generator) { |
| 79 | + auto count = 1031; // see Corpus8DecimalDigits for why 1031 |
| 80 | + std::vector<int> sizes; |
| 81 | + std::string allCharacters; |
| 82 | + std::uniform_int_distribution<> strSize(0, 101); // again a prime |
| 83 | + std::uniform_int_distribution<> characters(1, 255); // notice 0 excluded |
| 84 | + |
| 85 | + while(count--) { |
| 86 | + auto length = strSize(generator); |
| 87 | + sizes.push_back(length); |
| 88 | + for(auto i = length; i--; ) { |
| 89 | + allCharacters.append(1, characters(generator)); |
| 90 | + } |
| 91 | + allCharacters.append(1, '\0'); |
| 92 | + } |
| 93 | + return CorpusStringLength(std::move(sizes), std::move(allCharacters)); |
| 94 | + } |
| 95 | + |
| 96 | + struct Iterator { |
| 97 | + int *skips, *sentinel; |
| 98 | + char *cp; |
| 99 | + |
| 100 | + Iterator &operator++() { |
| 101 | + cp += *skips++; |
| 102 | + return *this; |
| 103 | + } |
| 104 | + |
| 105 | + char *operator*() { |
| 106 | + return cp; |
| 107 | + } |
| 108 | + |
| 109 | + auto next() noexcept { |
| 110 | + ++(*this); |
| 111 | + return sentinel != skips; |
| 112 | + } |
| 113 | + }; |
| 114 | + |
| 115 | + Iterator commence() { |
| 116 | + return { |
| 117 | + skips_.data(), skips_.data() + skips_.size(), characters_.data() |
| 118 | + }; |
| 119 | + } |
| 120 | +}; |
| 121 | + |
| 122 | +#define STRLEN_CORPUS_X_LIST \ |
| 123 | + X(LIBC_STRLEN, strlen) \ |
| 124 | + X(ZOO_STRLEN, zoo::c_strLength) \ |
| 125 | + X(ZOO_NATURAL_STRLEN, zoo::c_strLength_natural) \ |
| 126 | + X(ZOO_MANUAL_STRLEN, zoo::c_strLength_manualComparison) \ |
| 127 | + X(ZOO_AVX, zoo::avx2_strlen) \ |
| 128 | + X(GENERIC_GLIBC_STRLEN, STRLEN_old) |
| 129 | + |
| 130 | +#define X(Typename, FunctionToCall) \ |
| 131 | + struct Invoke##Typename { int operator()(const char *p) { return FunctionToCall(p); } }; |
| 132 | + |
| 133 | +PARSE8BYTES_CORPUS_X_LIST |
| 134 | +STRLEN_CORPUS_X_LIST |
| 135 | +#undef X |
0 commit comments