Although type punning is defined for union in C, it is UB in C++.
This patch introduces a bit_cast function to convert between types in a safe way.
This is necessary to get llvm-libc compile with GCC.
This patch is extracted from D119002.
Paths
| Differential D119145
[libc] Replace type punning with bit_cast ClosedPublic Authored by gchatelet on Feb 7 2022, 7:43 AM.
Details
Summary Although type punning is defined for union in C, it is UB in C++. This is necessary to get llvm-libc compile with GCC.
Diff Detail
Event Timeline
gchatelet marked 3 inline comments as done. Comment Actions
Comment Actions FYI I also had the following constness warning /redacted/git/llvm-project/libc/src/fenv/fesetexceptflag.cpp: In function ‘int __llvm_libc::__fesetexceptflag_impl__(const fexcept_t*, int)’: /redacted/git/llvm-project/libc/src/fenv/fesetexceptflag.cpp:23:24: warning: type qualifiers ignored on cast result type [-Wignored-qualifiers] 23 | int excepts_to_set = static_cast<const int>(*flagp) & excepts; | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ This revision is now accepted and ready to land.Feb 8 2022, 7:29 AM Comment Actions I am not an expert, but normally the word "illegal" in code/commit logs is not preferred as it can imply non-technical errors. So, may be replace it with "incorrect". gchatelet retitled this revision from [libc] Fix illegal type punning to [libc] Replace type punning with bit_cast.Feb 8 2022, 8:56 AM Comment Actions
Thx, I reworded it. This revision was landed with ongoing or failed builds.Feb 8 2022, 12:46 PM Closed by commit rG7e7ecef98080: [libc] Replace type punning with bit_cast (authored by gchatelet). · Explain Why This revision was automatically updated to reflect the committed changes.
Revision Contents
Diff 406743 libc/src/__support/CPP/Bit.h
libc/src/__support/CPP/CMakeLists.txt
libc/src/__support/FPUtil/FPBits.h
libc/src/__support/FPUtil/Hypot.h
libc/src/__support/FPUtil/ManipulationFunctions.h
libc/src/__support/FPUtil/generic/sqrt.h
libc/src/__support/FPUtil/x86_64/LongDoubleBits.h
libc/src/__support/FPUtil/x86_64/NextAfterLongDouble.h
libc/src/__support/FPUtil/x86_64/sqrt.h
libc/src/fenv/fesetexceptflag.cpp
libc/src/math/generic/log10f.cpp
libc/src/math/generic/log2f.cpp
libc/src/math/generic/logf.cpp
libc/src/math/generic/math_utils.h
libc/src/string/memory_utils/CMakeLists.txt
libc/src/string/memory_utils/elements_x86.h
libc/test/src/math/NextAfterTest.h
libc/test/src/math/SqrtTest.h
utils/bazel/llvm-project-overlay/libc/BUILD.bazel
|
Extra space around =, and extra closing parenthesis to be removed.