Skip to content

Instantly share code, notes, and snippets.

@simonwhitaker
Last active January 1, 2016 22:58
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonwhitaker/8213159 to your computer and use it in GitHub Desktop.
Save simonwhitaker/8213159 to your computer and use it in GitHub Desktop.
Architecture-independent floor() function
/*
* 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