Consider the following function
CGSize CGSizeIntegral(CGSize size)
{
return CGSizeMake(ceilf(size.width), ceilf(size.height));
}
CGSize
actually consists of two CGFloat
s, and CGFloat
's definition changes depending on the architecture:
typedef float CGFloat;// 32-bit
typedef double CGFloat;// 64-bit
So, the above code is wrong on 64-bit systems, and needs to be updated with something like
CGSize CGSizeIntegral(CGSize size)
{
#if 64_bit
return CGSizeMake(ceil(size.width), ceil(size.height));
#else
return CGSizeMake(ceilf(size.width), ceilf(size.height));
#endif
}
There is surely a compiler macro/constant for this (for Mac we can use INTEL_X86
for example) but I haven't been able to find this in the 64-bit transition guide.
How can I determine what architecture is being built for?
Apple has included a new Settings pane in iOS 10.3 to easily check for installed 32-bit apps, and to see if there are updates available. To check, open Settings. Tap on General. Tap on About, and select Applications.
Same as above, click on Properties from the menu. Under Properties, click on the Compatibility tab. Click the box that says “Run this program in compatibility mode for:” and select the Windows version you want to use. Then, click Apply and try to run your application.
To determine if you are compiling for 64-bit, use __LP64__
:
#if __LP64__
return CGSizeMake(ceil(size.width), ceil(size.height));
#else
return CGSizeMake(ceilf(size.width), ceilf(size.height));
#endif
__LP64__
stands for "longs and pointers are 64-bit" and is architecture-neutral.
According to your transition guide it applies for iOS as well:
The compiler defines the
__LP64__
macro when compiling for the 64-bit runtime.
However, the preferred way to handle your use case is to use CGFLOAT_IS_DOUBLE
. There is no guarantee that __LP64__
will always mean the CGFloat
is a double, but it would be guaranteed with CGFLOAT_IS_DOUBLE
.
#if CGFLOAT_IS_DOUBLE
return CGSizeMake(ceil(size.width), ceil(size.height));
#else
return CGSizeMake(ceilf(size.width), ceilf(size.height));
#endif
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With