SofaBuffers C++ 0.12.0
Pure-C++20 SofaBuffers — API Documentation
Loading...
Searching...
No Matches
bits_equal.hpp
Go to the documentation of this file.
1/*!
2 * @file bits_equal.hpp
3 * @brief Bit-pattern equality for float arrays (`sofab::bitsEqual`).
4 *
5 * A generated encoder omits a field iff its value equals its declared default
6 * (MESSAGE_SPEC §2), and CORELIB_PLAN §4.6 makes floats round-trip bit for bit.
7 * An IEEE `==` over the elements contradicts both: `-0.0 == +0.0`, so an array
8 * `[-0.0, 1.5]` would be taken for the default `[0.0, 1.5]` and its first element
9 * dropped, and `NaN != NaN` would write a default array that holds a NaN.
10 *
11 * `bitsEqual` is the comparison the omission test needs: equal length and, at
12 * every index, an identical IEEE-754 bit pattern (32 bits for `float`, 64 for
13 * `double`). `+0.0` and `-0.0` differ; two NaNs are equal exactly when their bit
14 * patterns, payload included, are identical. No IEEE `==` is involved.
15 *
16 * Schema-independent by construction: the element type is a template argument and
17 * the bound is the argument's own length, so generated code calls it instead of
18 * emitting a loop per field.
19 *
20 * SPDX-License-Identifier: MIT
21 */
22
23#ifndef SOFAB_BITS_EQUAL_HPP
24#define SOFAB_BITS_EQUAL_HPP
25
26#include <concepts>
27#include <cstddef>
28#include <cstring>
29#include <initializer_list>
30#include <ranges>
31#include <span>
32#include <type_traits>
33
34namespace sofab
35{
36 namespace detail
37 {
38 /*! @brief `float` or `double`, the two element types of an fp array. */
39 template <typename T>
40 concept FloatElement = std::same_as<T, float> || std::same_as<T, double>;
41
42 /*! @brief Length check, then one block compare of the object representation. */
43 template <FloatElement T>
44 inline bool bitsEqualSpan(std::span<const T> a, std::span<const T> b) noexcept
45 {
46 if (a.size() != b.size()) { return false; }
47 /* memcmp with a null pointer is undefined even for a zero count, and an
48 * empty std::vector or a default-constructed span has no storage. */
49 if (a.empty()) { return true; }
50 return std::memcmp(a.data(), b.data(), a.size() * sizeof(T)) == 0;
51 }
52 } // namespace detail
53
54 /*!
55 * @brief True iff @p a and @p b have the same length and every element pair has
56 * the same IEEE-754 bit pattern.
57 *
58 * Accepts anything contiguous and sized whose element type is `float` or
59 * `double`, on both sides: `std::vector`, `sofab::InlineVector`, `std::array`,
60 * `std::span`, a C array, or a braced list (`bitsEqual(v, {0.0f, 1.5f})`
61 * binds a `std::initializer_list<float>`). Both sides must have the same
62 * element type. The length is compared first; nothing is allocated or
63 * modified.
64 *
65 * @return `false` on a length mismatch or any differing bit pattern.
66 */
67 template <std::ranges::contiguous_range A, std::ranges::contiguous_range B>
68 requires std::ranges::sized_range<A> && std::ranges::sized_range<B>
69 && detail::FloatElement<std::remove_cv_t<std::ranges::range_value_t<A>>>
70 && std::same_as<std::remove_cv_t<std::ranges::range_value_t<A>>,
71 std::remove_cv_t<std::ranges::range_value_t<B>>>
72 inline bool bitsEqual(const A &a, const B &b) noexcept
73 {
74 using T = std::remove_cv_t<std::ranges::range_value_t<A>>;
75 return detail::bitsEqualSpan<T>(
76 std::span<const T>(std::ranges::data(a), std::ranges::size(a)),
77 std::span<const T>(std::ranges::data(b), std::ranges::size(b)));
78 }
79
80 /*! @brief Braced-list default: `bitsEqual(field, {0.0f, 1.5f})`. */
81 template <detail::FloatElement T, std::ranges::contiguous_range A>
82 requires std::ranges::sized_range<A>
83 && std::same_as<std::remove_cv_t<std::ranges::range_value_t<A>>, T>
84 inline bool bitsEqual(const A &a, std::initializer_list<T> b) noexcept
85 {
86 return detail::bitsEqualSpan<T>(
87 std::span<const T>(std::ranges::data(a), std::ranges::size(a)),
88 std::span<const T>(b.begin(), b.size()));
89 }
90} // namespace sofab
91
92#endif // SOFAB_BITS_EQUAL_HPP
float or double, the two element types of an fp array.
bool bitsEqualSpan(std::span< const T > a, std::span< const T > b) noexcept
Length check, then one block compare of the object representation.
bool bitsEqual(const A &a, const B &b) noexcept
True iff a and b have the same length and every element pair has the same IEEE-754 bit pattern.