diff --git a/benchmarks/CMakeLists.txt b/benchmarks/CMakeLists.txt index dcbe625d816..3b590df78e9 100644 --- a/benchmarks/CMakeLists.txt +++ b/benchmarks/CMakeLists.txt @@ -137,6 +137,7 @@ add_benchmark(sample src/sample.cpp) add_benchmark(search src/search.cpp) add_benchmark(search_n src/search_n.cpp) add_benchmark(shuffle src/shuffle.cpp) +add_benchmark(signed128_mul src/signed128_mul.cpp) add_benchmark(std_copy src/std_copy.cpp) add_benchmark(sv_equal src/sv_equal.cpp) add_benchmark(swap_ranges src/swap_ranges.cpp) diff --git a/benchmarks/src/signed128_mul.cpp b/benchmarks/src/signed128_mul.cpp new file mode 100644 index 00000000000..01d3822c369 --- /dev/null +++ b/benchmarks/src/signed128_mul.cpp @@ -0,0 +1,75 @@ +// Copyright (c) Microsoft Corporation. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#include + +#include <__msvc_int128.hpp> +#include +#include +#include +#include +#include + +using namespace std; + +_NODISCARD constexpr _Signed128 old_mul128(_Signed128 _Left, _Signed128 _Right) { + bool _negative = false; + _Left._Strip_negative(_negative); + _Right._Strip_negative(_negative); + _Signed128 _Result{_Base128::_Multiply(_Left, _Right)}; + if (_negative) { + _Result = -_Result; + } + return _Result; +} + +vector> init_test_set() { + vector> vec(1'000'000); + mt19937_64 mt64{}; + for (auto& [val1, val2] : vec) { + val1._Word[0] = mt64(); + val1._Word[1] = mt64(); + val2._Word[0] = mt64(); + val2._Word[1] = mt64(); + } + return vec; +} + +void bm_signed128_mul(benchmark::State& state) { + const auto vec = init_test_set(); + + auto it = vec.begin(); + _Signed128 res = 0; + + for (auto _ : state) { + res = it->first * it->second; + + benchmark::DoNotOptimize(res); + ++it; + if (it == vec.end()) { + it = vec.begin(); + } + } +} + +void bm_signed128_oldmul(benchmark::State& state) { + const auto vec = init_test_set(); + + auto it = vec.begin(); + _Signed128 res = 0; + + for (auto _ : state) { + res = old_mul128(it->first, it->second); + + benchmark::DoNotOptimize(res); + ++it; + if (it == vec.end()) { + it = vec.begin(); + } + } +} + +BENCHMARK(bm_signed128_mul); +BENCHMARK(bm_signed128_oldmul); + +BENCHMARK_MAIN(); diff --git a/stl/inc/__msvc_int128.hpp b/stl/inc/__msvc_int128.hpp index e2f2f8f7f7b..afe5e4b4ac5 100644 --- a/stl/inc/__msvc_int128.hpp +++ b/stl/inc/__msvc_int128.hpp @@ -1370,14 +1370,7 @@ struct _Signed128 : _Base128 { } _NODISCARD friend constexpr _Signed128 operator*(_Signed128 _Left, _Signed128 _Right) noexcept { - bool _Negative = false; - _Left._Strip_negative(_Negative); - _Right._Strip_negative(_Negative); - _Signed128 _Result{_Base128::_Multiply(_Left, _Right)}; - if (_Negative) { - _Result = -_Result; - } - return _Result; + return _Signed128{_Base128::_Multiply(_Left, _Right)}; } _TEMPLATE_CLASS_INTEGRAL(_Ty)