brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.7 KiB · 1ed179a Raw
140 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++03, c++11, c++14, c++1710 11#include <utility>12#include "../CartesianBenchmarks.h"13#include "benchmark/benchmark.h"14 15namespace {16 17enum ValueType : size_t {18  SChar,19  UChar,20  Short,21  UShort,22  Int,23  UInt,24  Long,25  ULong,26  LongLong,27  ULongLong,28#ifndef TEST_HAS_NO_INT12829  Int128,30  UInt128,31#endif32};33 34struct AllValueTypes : EnumValuesAsTuple<AllValueTypes, ValueType, 6> {35  static constexpr const char* Names[] = {36      "schar",37      "uchar",38      "short",39      "ushort",40      "int",41      "uint",42      "long",43      "ulong",44      "longlong",45      "ulonglong",46#ifndef TEST_HAS_NO_INT12847      "int128",48      "uint128"49#endif50  };51};52 53using TestType =54    std::tuple< signed char,55                unsigned char,56                short,57                unsigned short,58                int,59                unsigned int,60                long,61                unsigned long,62                long long,63                unsigned long long64#ifndef TEST_HAS_NO_INT12865                ,66                __int128_t,67                __uint128_t68#endif69                >;70 71template <typename TType, typename UType>72struct CmpEqual {73  static void run(benchmark::State& state) {74    using T = std::tuple_element_t<TType::value, TestType>;75    using U = std::tuple_element_t<UType::value, TestType>;76 77    T x1 = T{127}, x2 = T{111};78    U y1 = U{123}, y2 = U{1};79    for (auto _ : state) {80      benchmark::DoNotOptimize(x1);81      benchmark::DoNotOptimize(x2);82      benchmark::DoNotOptimize(y1);83      benchmark::DoNotOptimize(y2);84      benchmark::DoNotOptimize(std::cmp_equal(x1, y1));85      benchmark::DoNotOptimize(std::cmp_equal(y1, x1));86      benchmark::DoNotOptimize(std::cmp_equal(x1, x1));87      benchmark::DoNotOptimize(std::cmp_equal(y1, y1));88 89      benchmark::DoNotOptimize(std::cmp_equal(x2, y2));90      benchmark::DoNotOptimize(std::cmp_equal(y2, x2));91      benchmark::DoNotOptimize(std::cmp_equal(x2, x2));92      benchmark::DoNotOptimize(std::cmp_equal(y2, y2));93    }94  }95 96  static std::string name() { return "BM_CmpEqual" + TType::name() + UType::name(); }97};98 99template <typename TType, typename UType>100struct CmpLess {101  static void run(benchmark::State& state) {102    using T = std::tuple_element_t<TType::value, TestType>;103    using U = std::tuple_element_t<UType::value, TestType>;104 105    T x1 = T{127}, x2 = T{111};106    U y1 = U{123}, y2 = U{1};107    for (auto _ : state) {108      benchmark::DoNotOptimize(x1);109      benchmark::DoNotOptimize(x2);110      benchmark::DoNotOptimize(y1);111      benchmark::DoNotOptimize(y2);112      benchmark::DoNotOptimize(std::cmp_less(x1, y1));113      benchmark::DoNotOptimize(std::cmp_less(y1, x1));114      benchmark::DoNotOptimize(std::cmp_less(x1, x1));115      benchmark::DoNotOptimize(std::cmp_less(y1, y1));116 117      benchmark::DoNotOptimize(std::cmp_less(x2, y2));118      benchmark::DoNotOptimize(std::cmp_less(y2, x2));119      benchmark::DoNotOptimize(std::cmp_less(x2, x2));120      benchmark::DoNotOptimize(std::cmp_less(y2, y2));121    }122  }123 124  static std::string name() { return "BM_CmpLess" + TType::name() + UType::name(); }125};126 127} // namespace128 129int main(int argc, char** argv) {130  benchmark::Initialize(&argc, argv);131  if (benchmark::ReportUnrecognizedArguments(argc, argv))132    return 1;133 134  makeCartesianProductBenchmark<CmpEqual, AllValueTypes, AllValueTypes>();135  makeCartesianProductBenchmark<CmpLess, AllValueTypes, AllValueTypes>();136  benchmark::RunSpecifiedBenchmarks();137 138  return 0;139}140