Skip to content

feat(cpp): bit-pattern equality helper for float arrays (generator#636) - #193

Merged
Andste82 merged 2 commits into
mainfrom
feat/float-array-bits-equal-636
Oct 4, 2026
Merged

Andste82 merged 2 commits into
mainfrom
feat/float-array-bits-equal-636

Conversation

@Andste82

@Andste82 Andste82 commented Oct 4, 2026

Copy link
Copy Markdown
Member

What

sofab::bitsEqual, a bit-pattern equality helper for float arrays, in its own header src/include/sofab/floats.hpp (pulled in by sofab/sofab.hpp), next to seq.hpp in the static-helper layer.

Semantics:

  • Overloads: pointer+length (bitsEqual<T>(a, na, b, nb), T = float or double) and a container form for anything with data()/size() (vector, InlineVector, std::array, span, initializer_list, C arrays).
  • True iff the lengths match and every element has the same IEEE-754 bit pattern. The lengths are compared first, then one block memcmp over n * sizeof(T) bytes. There is no IEEE ==, so -0.0 differs from +0.0, and a NaN equals another NaN only when all bits (payload included) match.
  • noexcept, header-only, inline, no allocation, no mutation.
  • A braced list cannot be passed straight to the container form (the template cannot deduce it); pass a typed constant such as std::initializer_list<T>, a constexpr array or a span.

This is the schema-independent helper that a generated encoder needs for "omit iff equal to default", so it lives in the corelib instead of being emitted per field.

Files

  • src/include/sofab/floats.hpp (new)
  • src/include/sofab/sofab.hpp (include)
  • test/cpp/test_floats.cpp (new), test/cpp/CMakeLists.txt
  • README row

Coverage

  • Before: the CI coverage job runs gcovr over src/*.c and src/*.h only, so C++ headers are outside the report and the published figure is unaffected.
  • After: local gcov (Debug, coverage on, strict UTF-8 on) shows floats.hpp at 100% of 45 lines and 100% of 12 branches. gcovr is not installed locally.

Footprint

Header-only: no .text, .data or .bss unless the helper is used. The C library is untouched.

Micro-measurement

None taken.

Negative control

Replacing the memcmp with an IEEE == loop made sofabpptest fail at test_floats.cpp:185 (the -0.0 cases). Restoring the helper made the suite pass again.

Local verification

Run: host gcc 15 with ASan and UBSan, the repo ctest suite, arm-none-eabi-g++ 14 syntax and codegen, -Wall -Wextra -Wpedantic -Werror -Wconversion -Wsign-conversion -Wshadow (no diagnostics from the new files). Not run locally: clang, doxygen, the cross/qemu/LTO CI legs. CI is the verifier for those.

Refs sofa-buffers/generator#636

Do not merge yet: the generator part of sofa-buffers/generator#636 will call this helper and the PRs are merged together when that part starts.

A generated encoder omits a field iff its value equals its default, and
floats round-trip bit for bit, so a float array must be compared by IEEE-754
bit pattern. The container operator== compares by value and treats -0.0 as
equal to +0.0, which drops a -0.0 element that has to reach the wire.

Add sofab::bitsEqual in its own header (sofab/floats.hpp, pulled in by
sofab.hpp): length check, then one memcmp. Header-only and stateless, so it
costs nothing when unused and adds no code to the C library. Accepts a
pointer+length pair or any contiguous container on either side.

Corelib half of generator#636.
@Andste82
Andste82 merged commit b031f3b into main Oct 4, 2026
26 checks passed
@Andste82
Andste82 deleted the feat/float-array-bits-equal-636 branch October 4, 2026 20:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant