Add MR::bit_cast, usable without std::bit_cast - #6599
Merged
Conversation
std::bit_cast is missing from the Xcode 14.2 SDK's libc++, which the MeshInspectorCode macOS x64 build compiles against, and MRIsNaN.h already guards against the same gap. The builtin needs no library support and is available in every compiler that builds MeshLib.
isNanFast no longer degrades to std::isnan, and binary STL vertex reading no longer degrades to std::memcpy, on a standard library without std::bit_cast: the builtin needs none. MRMeshLoad.cpp never included <cstring> for that memcpy anyway.
It forwards to std::bit_cast where the standard library has it, and to the __builtin_bit_cast intrinsic otherwise, so the choice lives in one header instead of at every call site. The three call sites now use it.
HolyBlackCat
approved these changes
Aug 18, 2026
adalisk-emikhaylov
approved these changes
Aug 18, 2026
oitel
approved these changes
Aug 18, 2026
One-line comment on bit_cast; unqualified calls where the argument type has no associated namespace; drop <bit> from MRMeshLoad.cpp, which no longer uses anything from it.
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.
exp2iinMRFastInt.cppusesstd::bit_cast, which is not available in every libc++ MeshLib is built against. It breaks the MeshInspectorCode macOS x64 build, where brew clang 22 is pointed at the Xcode 14.2 SDK's libc++ (-nostdinc++ -isystem .../MacOSX.sdk/usr/include/c++/v1):<bit>is included; that libc++ simply predates thebit_castwrapper.MRIsNaN.handMRMeshLoad.cppalready knew about the gap, and each carried its own#if __cpp_lib_bit_cast >= 201806Lwith a different fallback.New
MRBitCast.hholds that choice once:std::bit_castforwards to__builtin_bit_castanyway, so the fallback is the same operation without the library wrapper, and it is available in clang 9+, GCC 11+ and MSVC 19.26+ (VS 2019 16.6) — all far below what-std=c++23already requires. The header includes<version>before testing the feature macro, so the test is not at the mercy of what some other header happened to pull in.All three call sites now use
MR::bit_cast, and the#ifis gone from everywhere else. Both fallbacks go with it:isNanFastno longer degrades tostd::isnan, and binary STL vertex reading no longer degrades tostd::memcpy— a bit-cast is what both wanted on every platform.Checked on Godbolt, with both branches of the
#ifforced, that the whole set — theconstexprNaN bit pattern, thestd::array<char, 12>->Vector3fcast andexp2i— compiles clean under-Wall -Wextra -Werror -pedantic-errorson GCC 12/14/15, clang 16/20 and MSVC latest. The generated code is byte-identical between the two branches, so nothing about this is a pessimisation on the standard path.Incidental:
MRMeshLoad.cppnever included<cstring>for thestd::memcpyin its fallback branch, so that branch depended on a transitive include to compile at all.MRIsNaN.hno longer needs<bit>or<cmath>directly.MRFastInt.cppkeeps<bit>forstd::countl_zero, which that old libc++ does provide.MeshLib's own CI cannot reproduce the original failure:
build-test-macos.ymlcompiles onmacos-15-intelandmacos-14, both with a recent SDK.macos-12-intelappears there only as an install/test target. The failing compile is in the downstream MeshInspectorCode build.