Skip to content

feat(bits-equal): bit-pattern equality helper for float arrays (generator#636) - #152

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

Andste82 merged 1 commit 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, b) in include/sofab/bits_equal.hpp: equality of two float/double arrays by bit pattern. It compares the length first, then the IEEE-754 bits of every element (32 bits for float, 64 for double) with a single memcmp. It accepts vector, InlineVector, std::array, span, C arrays, or a braced default list on either side. Both sides must have the same element type.

Semantics: -0.0 != +0.0, a NaN equals itself when the payload is identical, and different NaN payloads (including sNaN) differ. An IEEE elementwise compare would treat [-0.0, 1.5] as equal to [0.0, 1.5] and drop the -0.0 element when an array field equal to its default is omitted.

The helper is schema-independent, so it lives in the corelib in its own header and generated code can call it instead of emitting a comparison per field.

Files

  • include/sofab/bits_equal.hpp (new), included from include/sofab/sofab.hpp
  • test/test_bits_equal.cpp (new), registered in test/CMakeLists.txt
  • README.md (documentation)

Tests and coverage

The new suite covers signed zero at first, middle and last index in both directions, NaN and sNaN payloads, infinities, subnormals, length mismatches, arrays over 64 elements, InlineVector, braced defaults, and a cross-check against a reference bit loop. Locally 136 checks pass.

Coverage (local gcov 15, Debug, SOFAB_ENABLE_COVERAGE=ON, merged by script because gcovr is not installed here):

  • Before (origin/main 44c58ef): sofab.hpp lines 1419/1443 (98.34%), branches 1509/2159 (69.89%).
  • After: bits_equal.hpp lines 12/12 (100%), branches 4/4 (100%). sofab.hpp unchanged. Total lines 1431/1455 (98.35%), branches 1513/2163 (69.95%).
  • The raw gcov branch count includes exception branches, so it differs from the CI gcovr number.

Footprint

Not measured: this is a maxspeed header-only corelib, and the header is a thin wrapper over std::memcmp.

Micro-measurement

Callgrind Ir, g++ -O3, std::vector, one call, bitsEqual against vector ==:

  • n=3 equal: 34 for == vs 39 for bitsEqual. Mismatch at the last element: 30 vs 42.
  • n=16 equal: 138 vs 37 (fp32) and 49 (fp64).
  • n=256 equal: 2058 vs 177 (fp32) and 313 (fp64).
  • n=4096 equal: 32778 vs 2217 (fp32) and 4393 (fp64).

Ir includes the glibc memcmp. Wall-clock timings were too noisy to report and are not claimed.

Negative control

With the memcmp replaced by an IEEE a[i] == b[i] loop, 22 of 136 checks in test_bits_equal fail (the -0.0 vs +0.0 cases at first, middle and last index, in both directions, for fp32 and fp64, plus the same-NaN, sNaN, braced-default and InlineVector cases). With the helper restored all 136 pass.

Notes

  • bitsEqual(v, {}) and mixed braced lists such as {0.0f, 1.5} do not deduce T. The generator should emit typed literals and handle an empty default with v.empty() or an explicit std::span<const T>{}.
  • test_bits_equal fails under -O3 -ffast-math (the compiler folds the signed-zero checks). Fast-math is not a supported configuration and no CI leg uses it.
  • sofab.hpp now also pulls in <ranges>.
  • Clang and the ppc64 big-endian leg could not be run locally; CI is the verifier.

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.

…enerator#636)

An array field is omitted when it equals its declared default, and floats
round-trip bit for bit. An IEEE elementwise compare calls [-0.0, 1.5] equal
to [0.0, 1.5] and drops the -0.0 element. sofab::bitsEqual compares length
first, then the IEEE-754 bit pattern of every element (32 bits for float,
64 for double) in one memcmp; it takes vector, InlineVector, array, span,
C arrays or a braced default on either side.

The helper is schema-independent, so it lives in the corelib (its own
header, bits_equal.hpp) and generated code calls it instead of emitting a
comparison per field. New suite test_bits_equal covers signed zero, NaN
payloads, infinities, subnormals, length mismatches, arrays over 64
elements and a cross-check against a reference bit loop; the header is at
100% line and branch coverage.
@Andste82
Andste82 merged commit 65755b0 into main Oct 4, 2026
9 checks passed
@Andste82
Andste82 deleted the feat/float-array-bits-equal-636 branch October 4, 2026 20:52
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