Repository navigation
feat(cpp): bit-pattern equality helper for float arrays (generator#636) - #193
Merged
Merged
Conversation
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.
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 bit-pattern equality helper for float arrays, in its own headersrc/include/sofab/floats.hpp(pulled in bysofab/sofab.hpp), next toseq.hppin the static-helper layer.Semantics:
bitsEqual<T>(a, na, b, nb),T=floatordouble) and a container form for anything withdata()/size()(vector, InlineVector, std::array, span, initializer_list, C arrays).memcmpovern * sizeof(T)bytes. There is no IEEE==, so-0.0differs from+0.0, and a NaN equals another NaN only when all bits (payload included) match.noexcept, header-only, inline, no allocation, no mutation.std::initializer_list<T>, aconstexprarray 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.txtCoverage
src/*.candsrc/*.honly, so C++ headers are outside the report and the published figure is unaffected.floats.hppat 100% of 45 lines and 100% of 12 branches. gcovr is not installed locally.Footprint
Header-only: no
.text,.dataor.bssunless the helper is used. The C library is untouched.Micro-measurement
None taken.
Negative control
Replacing the
memcmpwith an IEEE==loop madesofabpptestfail attest_floats.cpp:185(the-0.0cases). 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.