Repository navigation
feat(bits-equal): bit-pattern equality helper for float arrays (generator#636) - #152
Merged
Merged
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
sofab::bitsEqual(a, b)ininclude/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 singlememcmp. 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 frominclude/sofab/sofab.hpptest/test_bits_equal.cpp(new), registered intest/CMakeLists.txtREADME.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):
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,
bitsEqualagainstvector ==:==vs 39 for bitsEqual. Mismatch at the last element: 30 vs 42.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 withv.empty()or an explicitstd::span<const T>{}.-O3 -ffast-math(the compiler folds the signed-zero checks). Fast-math is not a supported configuration and no CI leg uses it.<ranges>.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.