Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ObjC] Don't use unions and instead use memcpy for the type swaps. #6672

Merged
merged 1 commit into from
Sep 20, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[ObjC] Don't use unions and instead use memcpy for the type swaps.
The code in question hasn't change in a long time so the cause of
firebase/firebase-ios-sdk#3851 still appears to be
an Xcode 11 clang change/bug; but this does appear to be slightly better
code for the work being done.
  • Loading branch information
thomasvl committed Sep 19, 2019
commit 84782b8ad3ccf520e17da4702af5b91904a03fbe
28 changes: 16 additions & 12 deletions objectivec/GPBUtilities_PackagePrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,27 +71,31 @@ GPB_INLINE void GPBDebugCheckRuntimeVersion() {
// Conversion functions for de/serializing floating point types.

GPB_INLINE int64_t GPBConvertDoubleToInt64(double v) {
union { double f; int64_t i; } u;
u.f = v;
return u.i;
GPBInternalCompileAssert(sizeof(double) == sizeof(int64_t), double_not_64_bits);
int64_t result;
memcpy(&result, &v, sizeof(result));
return result;
}

GPB_INLINE int32_t GPBConvertFloatToInt32(float v) {
union { float f; int32_t i; } u;
u.f = v;
return u.i;
GPBInternalCompileAssert(sizeof(float) == sizeof(int32_t), float_not_32_bits);
int32_t result;
memcpy(&result, &v, sizeof(result));
return result;
}

GPB_INLINE double GPBConvertInt64ToDouble(int64_t v) {
union { double f; int64_t i; } u;
u.i = v;
return u.f;
GPBInternalCompileAssert(sizeof(double) == sizeof(int64_t), double_not_64_bits);
double result;
memcpy(&result, &v, sizeof(result));
return result;
}

GPB_INLINE float GPBConvertInt32ToFloat(int32_t v) {
union { float f; int32_t i; } u;
u.i = v;
return u.f;
GPBInternalCompileAssert(sizeof(float) == sizeof(int32_t), float_not_32_bits);
float result;
memcpy(&result, &v, sizeof(result));
return result;
}

GPB_INLINE int32_t GPBLogicalRightShift32(int32_t value, int32_t spaces) {
Expand Down