Type punning though a union is not UB in C. It is UB in C++. Quick inverse square root is type punning through a cast, which is UB in both C and C++ since it violates strict aliasing.
To elaborate, C does not guarantee that type punning through a union will work, but it also doesn't affirmatively mark it as UB. It is implementation defined/unspecified with most compilers supporting it on most platforms. The technical term is "IB with possible trap" iirc
View all comments
Show parent comment
3
u/realestLink Feb 12 '26
Type punning though a union is not UB in C. It is UB in C++. Quick inverse square root is type punning through a cast, which is UB in both C and C++ since it violates strict aliasing.