Last active
January 1, 2016 22:58
-
-
Save simonwhitaker/8213159 to your computer and use it in GitHub Desktop.
Architecture-independent floor() function
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
/* | |
* Calling math.h functions like `floor` and `floorf` on CGFloat variables | |
* becomes problematic when compiling the same code for both 32-bit and | |
* 64-bit platforms, since CGFloat is double on 64-bit, float on 32-bit. | |
* Hence, if you compile with -Wconversion set, `floorf` will give a warning | |
* on 64-bit, while `floor` gives a warning on 32-bit. | |
* | |
* Here's a suggested implementation of an architecture-independent `floor` | |
* function, written using plain old function overloading which is enabled | |
* using the `__attribute__((overloadable))` Clang language extension. | |
* | |
* See http://clang.llvm.org/docs/LanguageExtensions.html#function-overloading-in-c | |
*/ | |
float __attribute__((overloadable)) swfloor(float f) { return floorf(f); } | |
double __attribute__((overloadable)) swfloor(double d) { return floor(d); } | |
@implementation MyThingy | |
- (void)aMethod:(CGFloat)arg { | |
#pragma clang diagnostic push | |
#pragma clang diagnostic warning "-Wconversion" | |
CGFloat flooredArg = swfloor(arg); // No warnings! | |
#pragma clang diagnostic pop | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment