diff options
Diffstat (limited to 'skia/corecg/SkFloatBits.h')
-rw-r--r-- | skia/corecg/SkFloatBits.h | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/skia/corecg/SkFloatBits.h b/skia/corecg/SkFloatBits.h new file mode 100644 index 0000000..012770a --- /dev/null +++ b/skia/corecg/SkFloatBits.h @@ -0,0 +1,43 @@ +#ifndef SkFloatBits_DEFINED +#define SkFloatBits_DEFINED + +#include "SkTypes.h" + +/** Convert a sign-bit int (i.e. float interpreted as int) into a 2s compliement + int. This also converts -0 (0x80000000) to 0. Doing this to a float allows + it to be compared using normal C operators (<, <=, etc.) +*/ +static inline int32_t SkSignBitTo2sCompliment(int32_t x) { + if (x < 0) { + x &= 0x7FFFFFFF; + x = -x; + } + return x; +} + +#ifdef SK_CAN_USE_FLOAT + +union SkFloatIntUnion { + float fFloat; + int32_t fSignBitInt; +}; + +/** Return the float as a 2s compliment int. Just just be used to compare floats + to each other or against positive float-bit-constants (like 0) +*/ +static int32_t SkFloatAsInt(float x) { + SkFloatIntUnion data; + data.fFloat = x; + return SkSignBitTo2sCompliment(data.fSignBitInt); +} + +#endif + +#ifdef SK_SCALAR_IS_FLOAT + #define SkScalarAsInt(x) SkFloatAsInt(x) +#else + #define SkScalarAsInt(x) (x) +#endif + +#endif + |