reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2008 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 8 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | #ifndef SkFloatBits_DEFINED |
| 10 | #define SkFloatBits_DEFINED |
| 11 | |
| 12 | #include "SkTypes.h" |
Mike Klein | e9f78b4 | 2016-11-22 08:57:45 -0500 | [diff] [blame] | 13 | #include "SkSafe_math.h" |
Mike Reed | 8addae8 | 2017-08-04 15:12:14 -0400 | [diff] [blame] | 14 | #include <float.h> |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | |
| 16 | /** Convert a sign-bit int (i.e. float interpreted as int) into a 2s compliement |
| 17 | int. This also converts -0 (0x80000000) to 0. Doing this to a float allows |
| 18 | it to be compared using normal C operators (<, <=, etc.) |
| 19 | */ |
| 20 | static inline int32_t SkSignBitTo2sCompliment(int32_t x) { |
| 21 | if (x < 0) { |
| 22 | x &= 0x7FFFFFFF; |
| 23 | x = -x; |
| 24 | } |
| 25 | return x; |
| 26 | } |
| 27 | |
| 28 | /** Convert a 2s compliment int to a sign-bit (i.e. int interpreted as float). |
| 29 | This undoes the result of SkSignBitTo2sCompliment(). |
| 30 | */ |
| 31 | static inline int32_t Sk2sComplimentToSignBit(int32_t x) { |
| 32 | int sign = x >> 31; |
| 33 | // make x positive |
| 34 | x = (x ^ sign) - sign; |
| 35 | // set the sign bit as needed |
caryclark | 952538e | 2016-02-26 05:01:42 -0800 | [diff] [blame] | 36 | x |= SkLeftShift(sign, 31); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 37 | return x; |
| 38 | } |
| 39 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 40 | union SkFloatIntUnion { |
| 41 | float fFloat; |
| 42 | int32_t fSignBitInt; |
| 43 | }; |
| 44 | |
| 45 | // Helper to see a float as its bit pattern (w/o aliasing warnings) |
| 46 | static inline int32_t SkFloat2Bits(float x) { |
| 47 | SkFloatIntUnion data; |
| 48 | data.fFloat = x; |
| 49 | return data.fSignBitInt; |
| 50 | } |
| 51 | |
| 52 | // Helper to see a bit pattern as a float (w/o aliasing warnings) |
| 53 | static inline float SkBits2Float(int32_t floatAsBits) { |
| 54 | SkFloatIntUnion data; |
| 55 | data.fSignBitInt = floatAsBits; |
| 56 | return data.fFloat; |
| 57 | } |
| 58 | |
| 59 | /** Return the float as a 2s compliment int. Just to be used to compare floats |
| 60 | to each other or against positive float-bit-constants (like 0). This does |
| 61 | not return the int equivalent of the float, just something cheaper for |
| 62 | compares-only. |
| 63 | */ |
| 64 | static inline int32_t SkFloatAs2sCompliment(float x) { |
| 65 | return SkSignBitTo2sCompliment(SkFloat2Bits(x)); |
| 66 | } |
| 67 | |
| 68 | /** Return the 2s compliment int as a float. This undos the result of |
| 69 | SkFloatAs2sCompliment |
| 70 | */ |
| 71 | static inline float Sk2sComplimentAsFloat(int32_t x) { |
| 72 | return SkBits2Float(Sk2sComplimentToSignBit(x)); |
| 73 | } |
| 74 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | // Scalar wrappers for float-bit routines |
| 76 | |
reed@google.com | 8f4d230 | 2013-12-17 16:44:46 +0000 | [diff] [blame] | 77 | #define SkScalarAs2sCompliment(x) SkFloatAs2sCompliment(x) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 78 | |
| 79 | #endif |