SofaBuffers C/C++ 0.12.0
API Documentation
Loading...
Searching...
No Matches
floats.hpp
Go to the documentation of this file.
1/*!
2 * @file floats.hpp
3 * @brief SofaBuffers C++ - bit-pattern equality for float arrays.
4 *
5 * Part of the **static-helper layer** (see @ref seq.hpp): code whose shape is
6 * the same for every schema, so it lives here once instead of being emitted
7 * into every generated header.
8 *
9 * A generated encoder omits a field iff its value equals its declared default
10 * (MESSAGE_SPEC §2), and floats round-trip bit for bit (CORELIB_PLAN §4.6).
11 * "Equals" for a float array is therefore a comparison of IEEE-754 bit
12 * patterns, never of values: `-0.0` differs from `+0.0`, and a NaN equals
13 * another NaN only when every bit, payload included, is identical. The
14 * container `operator==` (an elementwise IEEE `==`) gets both wrong and would
15 * drop a `-0.0` element that has to reach the wire.
16 *
17 * The helper is header-only, inline and stateless: a translation unit that
18 * does not call it pays 0 bytes, and the C library is untouched.
19 *
20 * Include `sofab/sofab.hpp`; it pulls this in. Including this header alone
21 * works too and means the same thing.
22 *
23 * SPDX-License-Identifier: MIT
24 */
25
26#ifndef SOFAB_FLOATS_HPP
27#define SOFAB_FLOATS_HPP
28
29/**
30 * @addtogroup cpp_api
31 * @{
32 */
33
34/* includes *******************************************************************/
35#include <cstddef>
36#include <cstring>
37#include <iterator>
38#include <type_traits>
39
40/* functions ******************************************************************/
41namespace sofab
42{
43 /*!
44 * @brief True iff both float arrays have the same length and the same bit
45 * pattern at every index.
46 *
47 * The length is compared first (early exit); the elements are then compared
48 * with one block `memcmp` over `n * sizeof(T)` bytes, so there is no IEEE
49 * `==` anywhere. Nothing is allocated or modified.
50 *
51 * @tparam T `float` (32-bit pattern) or `double` (64-bit pattern).
52 * @param a First array, or any pointer when @p na is 0.
53 * @param na Length of @p a in elements.
54 * @param b Second array, or any pointer when @p nb is 0.
55 * @param nb Length of @p b in elements.
56 */
57 template <typename T>
58 inline bool bitsEqual(const T *a, std::size_t na, const T *b, std::size_t nb) noexcept
59 {
60 static_assert(std::is_same_v<T, float> || std::is_same_v<T, double>,
61 "bitsEqual compares float or double arrays");
62 if (na != nb)
63 {
64 return false;
65 }
66 // memcmp with a null pointer is undefined even for a zero length, and an
67 // empty std::vector may hand one out.
68 return na == 0 || std::memcmp(a, b, na * sizeof(T)) == 0;
69 }
70
71 /*!
72 * @brief Container form of @ref bitsEqual: anything contiguous that
73 * `std::data` / `std::size` accept and that holds `float` or `double`.
74 *
75 * Takes a mutable field (`std::vector`, @ref InlineVector, `std::array`)
76 * and a constant default (a temporary of the same type, a `std::span`,
77 * a `std::initializer_list`) alike, and the two need not be the same
78 * container type — only the same element type.
79 */
80 template <typename A, typename B>
81 inline bool bitsEqual(const A &a, const B &b) noexcept
82 {
83 using T = std::remove_cv_t<std::remove_pointer_t<decltype(std::data(a))>>;
84 static_assert(std::is_same_v<T, std::remove_cv_t<std::remove_pointer_t<decltype(std::data(b))>>>,
85 "bitsEqual compares two arrays of the same element type");
86 return bitsEqual<T>(std::data(a), static_cast<std::size_t>(std::size(a)),
87 std::data(b), static_cast<std::size_t>(std::size(b)));
88 }
89}
90
91/** @} */ // end of addtogroup
92
93#endif // SOFAB_FLOATS_HPP
bool bitsEqual(const T *a, std::size_t na, const T *b, std::size_t nb) noexcept
True iff both float arrays have the same length and the same bit pattern at every index.
Definition floats.hpp:58