Nikolas Klauser 8fc2538f33
Reapply "[libc++] Optimize num_put integral functions" (#131613) (#133572)
This reverts commit d1156fcb56891fb1a426c3e8331a51d47f98a1b8.

This patch fixes the reported incorrect formatting changes and adds
tests for them. The performance should be unaffected, since there are no
significant changes required to fix the bugs.

Specifically, a `>` was changed to a `>=` to also add a `+` in the zero
case, and we're checking for zero now before printing the octal and
hexadecimal prefixes.

Closes #131710
2025-04-11 15:35:58 +02:00

40 lines
1.1 KiB
C++

//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03
#include <ios>
#include <locale>
#include <benchmark/benchmark.h>
struct num_put : std::num_put<char, std::string::iterator> {};
template <class T>
void BM_num_put(benchmark::State& state) {
auto val = T(123);
std::ios ios(nullptr);
num_put np;
for (auto _ : state) {
benchmark::DoNotOptimize(val);
std::string str;
benchmark::DoNotOptimize(np.put(str.begin(), ios, ' ', val));
}
}
BENCHMARK(BM_num_put<bool>);
BENCHMARK(BM_num_put<long>);
BENCHMARK(BM_num_put<long long>);
BENCHMARK(BM_num_put<unsigned long>);
BENCHMARK(BM_num_put<unsigned long long>);
BENCHMARK(BM_num_put<double>);
BENCHMARK(BM_num_put<long double>);
BENCHMARK(BM_num_put<const void*>);
BENCHMARK_MAIN();