Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ endfunction()
add_benchmark(adjacent_difference src/adjacent_difference.cpp)
add_benchmark(adjacent_find src/adjacent_find.cpp)
add_benchmark(any_swap src/any_swap.cpp)
add_benchmark(array_compare src/array_compare.cpp CXX_STANDARD 20)
add_benchmark(bitset_from_string src/bitset_from_string.cpp)
add_benchmark(bitset_to_string src/bitset_to_string.cpp)
add_benchmark(charconv_floats src/charconv_floats.cpp)
Expand Down
89 changes: 89 additions & 0 deletions benchmarks/src/array_compare.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include <benchmark/benchmark.h>

#include <array>
#include <compare>
#include <cstddef>
#include <cstdint>
#include <cstring>

using namespace std;

enum class comparison {
spaceship_less,
memcmp_less,
spaceship_equal,
memcmp_equal,
};

enum class scenario {
equal,
different_first,
different_last,
};

template <size_t Size, comparison Comparison, scenario Scenario>
void bm(benchmark::State& state) {
array<uint8_t, Size> left{};
array<uint8_t, Size> right{};

for (size_t index = 0; index < Size; ++index) {
left[index] = static_cast<uint8_t>(index * 13 + 17);
right[index] = left[index];
}

if constexpr (Scenario == scenario::different_first) {
left.front() = 0x40;
right.front() = 0x80;
} else if constexpr (Scenario == scenario::different_last) {
left.back() = 0x40;
right.back() = 0x80;
}

benchmark::DoNotOptimize(left);
benchmark::DoNotOptimize(right);

for (auto _ : state) {
if constexpr (Comparison == comparison::spaceship_less) {
benchmark::DoNotOptimize((left <=> right) < 0);
} else if constexpr (Comparison == comparison::memcmp_less) {
benchmark::DoNotOptimize(memcmp(left.data(), right.data(), Size) < 0);
} else if constexpr (Comparison == comparison::spaceship_equal) {
benchmark::DoNotOptimize((left <=> right) == 0);
} else {
benchmark::DoNotOptimize(memcmp(left.data(), right.data(), Size) == 0);
}
}
}

#define BENCHMARK_SCENARIOS(Size, Comparison) \
BENCHMARK(bm<Size, comparison::Comparison, scenario::equal>); \
BENCHMARK(bm<Size, comparison::Comparison, scenario::different_first>); \
BENCHMARK(bm<Size, comparison::Comparison, scenario::different_last>)

#define BENCHMARK_COMPARISONS(Size) \
BENCHMARK_SCENARIOS(Size, spaceship_less); \
BENCHMARK_SCENARIOS(Size, memcmp_less); \
BENCHMARK_SCENARIOS(Size, spaceship_equal); \
BENCHMARK_SCENARIOS(Size, memcmp_equal)

BENCHMARK_COMPARISONS(1);
BENCHMARK_COMPARISONS(2);
BENCHMARK_COMPARISONS(3);
BENCHMARK_COMPARISONS(4);
BENCHMARK_COMPARISONS(7);
BENCHMARK_COMPARISONS(8);
BENCHMARK_COMPARISONS(15);
BENCHMARK_COMPARISONS(16);
BENCHMARK_COMPARISONS(31);
BENCHMARK_COMPARISONS(32);
BENCHMARK_COMPARISONS(63);
BENCHMARK_COMPARISONS(64);
BENCHMARK_COMPARISONS(127);
BENCHMARK_COMPARISONS(128);
BENCHMARK_COMPARISONS(255);
BENCHMARK_COMPARISONS(256);

BENCHMARK_MAIN();
9 changes: 9 additions & 0 deletions stl/inc/array
Original file line number Diff line number Diff line change
Expand Up @@ -769,9 +769,18 @@ _NODISCARD _CONSTEXPR20 bool operator==(const array<_Ty, _Size>& _Left, const ar
}

#if _HAS_CXX20
inline constexpr size_t _Array_compare_memcmp_max_count = 64;

_EXPORT_STD template <class _Ty, size_t _Size>
_NODISCARD constexpr _Synth_three_way_result<_Ty> operator<=>(
const array<_Ty, _Size>& _Left, const array<_Ty, _Size>& _Right) {
if constexpr (_Size != 0 && _Size <= _Array_compare_memcmp_max_count
&& _Lex_compare_memcmp_is_safe_elements<remove_const_t<_Ty>, remove_const_t<_Ty>>) {
if (!_STD is_constant_evaluated()) {
return _CSTD memcmp(_Left.data(), _Right.data(), _Size) <=> 0;
}
}

return _STD lexicographical_compare_three_way(
_Left.data(), _Left.data() + _Size, _Right.data(), _Right.data() + _Size, _Synth_three_way{});
}
Expand Down
12 changes: 10 additions & 2 deletions stl/inc/xutility
Original file line number Diff line number Diff line change
Expand Up @@ -6098,17 +6098,25 @@ namespace ranges {
} // namespace ranges
#endif // _HAS_CXX20

template <class _Elem1, class _Elem2>
constexpr bool _Lex_compare_memcmp_is_safe_elements =
sizeof(_Elem1) == 1 && sizeof(_Elem2) == 1
&& conjunction_v<_Is_character_or_bool<_Elem1>, _Is_character_or_bool<_Elem2>, is_unsigned<_Elem1>,
is_unsigned<_Elem2>>;

template <class _Elem1, class _Elem2>
constexpr bool _Lex_compare_memcmp_classify_elements =
#if _VECTORIZED_MISMATCH
is_integral_v<_Elem1> && is_integral_v<_Elem2> && sizeof(_Elem1) == sizeof(_Elem2)
&& is_unsigned_v<_Elem1> == is_unsigned_v<_Elem2>;
#else // ^^^ _VECTORIZED_MISMATCH / !_VECTORIZED_MISMATCH vvv
conjunction_v<_Is_character_or_bool<_Elem1>, _Is_character_or_bool<_Elem2>, is_unsigned<_Elem1>,
is_unsigned<_Elem2>>;
_Lex_compare_memcmp_is_safe_elements<_Elem1, _Elem2>;
#endif // ^^^ !_VECTORIZED_MISMATCH ^^^

#ifdef __cpp_lib_byte
template <>
inline constexpr bool _Lex_compare_memcmp_is_safe_elements<byte, byte> = true;

template <>
inline constexpr bool _Lex_compare_memcmp_classify_elements<byte, byte> = true;
#endif // defined(__cpp_lib_byte)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <array>
#include <cassert>
#include <climits>
#include <cstddef>
#include <cstdlib>
#include <iterator>
Expand Down Expand Up @@ -304,11 +305,86 @@ CONSTEXPR20 bool test_array_comparison() {
return true;
}

#if _HAS_CXX20
template <class Ty, size_t Size>
constexpr bool test_array_byte_comparison(const Ty low, const Ty high) {
array<Ty, Size> left{};
array<Ty, Size> right{};

assert(left <=> right == strong_ordering::equal);

right.front() = high;
assert(left <=> right == strong_ordering::less);
assert(right <=> left == strong_ordering::greater);

right.front() = low;
left.back() = high;
right.back() = low;
assert(left <=> right == strong_ordering::greater);
assert(right <=> left == strong_ordering::less);

return true;
}

void test_array_byte_comparison_exhaustive() {
for (int left_value = 0; left_value <= UCHAR_MAX; ++left_value) {
for (int right_value = 0; right_value <= UCHAR_MAX; ++right_value) {
const array left{static_cast<unsigned char>(left_value)};
const array right{static_cast<unsigned char>(right_value)};
const auto expected = left_value < right_value ? strong_ordering::less
: left_value > right_value ? strong_ordering::greater
: strong_ordering::equal;
assert((left <=> right) == expected);
}
}
}

template <size_t Size>
void test_array_byte_mismatch_positions() {
array<unsigned char, Size> left{};
array<unsigned char, Size> right{};

for (size_t position = 0; position < Size; ++position) {
right[position] = 1;
assert((left <=> right) == strong_ordering::less);
assert((right <=> left) == strong_ordering::greater);
right[position] = 0;
}
}
#endif // _HAS_CXX20

int main() {
test_array_get();
STATIC_ASSERT(test_array_get());
test_array_comparison();
#if _HAS_CXX20
static_assert(test_array_comparison());
assert((array<unsigned char, 0>{} <=> array<unsigned char, 0>{}) == strong_ordering::equal);
test_array_byte_comparison<unsigned char, 1>(0, 1);
test_array_byte_comparison<unsigned char, 64>(0, 1);
test_array_byte_comparison<unsigned char, 65>(0, 1);
test_array_byte_comparison<bool, 1>(false, true);
test_array_byte_comparison<byte, 1>(byte{0}, byte{1});
#if CHAR_MIN == 0
test_array_byte_comparison<char, 1>(char{0}, char{1});
#endif // CHAR_MIN == 0
test_array_byte_comparison_exhaustive();
test_array_byte_mismatch_positions<64>();
test_array_byte_mismatch_positions<65>();
#ifdef __cpp_char8_t
test_array_byte_comparison<char8_t, 1>(char8_t{0}, char8_t{1});
#endif // defined(__cpp_char8_t)
static_assert((array<unsigned char, 0>{} <=> array<unsigned char, 0>{}) == strong_ordering::equal);
static_assert(test_array_byte_comparison<unsigned char, 1>(0, 1));
static_assert(test_array_byte_comparison<unsigned char, 64>(0, 1));
static_assert(test_array_byte_comparison<unsigned char, 65>(0, 1));
static_assert(test_array_byte_comparison<bool, 1>(false, true));
static_assert(test_array_byte_comparison<byte, 1>(byte{0}, byte{1}));
#if CHAR_MIN == 0
static_assert(test_array_byte_comparison<char, 1>(char{0}, char{1}));
#endif // CHAR_MIN == 0
#ifdef __cpp_char8_t
static_assert(test_array_byte_comparison<char8_t, 1>(char8_t{0}, char8_t{1}));
#endif // defined(__cpp_char8_t)
#endif // _HAS_CXX20
}
Loading