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 | |
Hal Canary | 3b34723 | 2018-09-06 11:51:43 -0400 | [diff] [blame^] | 12 | #include "SkMath.h" |
Mike Klein | e9f78b4 | 2016-11-22 08:57:45 -0500 | [diff] [blame] | 13 | #include "SkSafe_math.h" |
Hal Canary | 3b34723 | 2018-09-06 11:51:43 -0400 | [diff] [blame^] | 14 | #include "SkTypes.h" |
| 15 | |
Mike Reed | 8addae8 | 2017-08-04 15:12:14 -0400 | [diff] [blame] | 16 | #include <float.h> |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | |
| 18 | /** Convert a sign-bit int (i.e. float interpreted as int) into a 2s compliement |
| 19 | int. This also converts -0 (0x80000000) to 0. Doing this to a float allows |
| 20 | it to be compared using normal C operators (<, <=, etc.) |
| 21 | */ |
| 22 | static inline int32_t SkSignBitTo2sCompliment(int32_t x) { |
| 23 | if (x < 0) { |
| 24 | x &= 0x7FFFFFFF; |
| 25 | x = -x; |
| 26 | } |
| 27 | return x; |
| 28 | } |
| 29 | |
| 30 | /** Convert a 2s compliment int to a sign-bit (i.e. int interpreted as float). |
| 31 | This undoes the result of SkSignBitTo2sCompliment(). |
| 32 | */ |
| 33 | static inline int32_t Sk2sComplimentToSignBit(int32_t x) { |
| 34 | int sign = x >> 31; |
| 35 | // make x positive |
| 36 | x = (x ^ sign) - sign; |
| 37 | // set the sign bit as needed |
caryclark | 952538e | 2016-02-26 05:01:42 -0800 | [diff] [blame] | 38 | x |= SkLeftShift(sign, 31); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 39 | return x; |
| 40 | } |
| 41 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 42 | union SkFloatIntUnion { |
| 43 | float fFloat; |
| 44 | int32_t fSignBitInt; |
| 45 | }; |
| 46 | |
| 47 | // Helper to see a float as its bit pattern (w/o aliasing warnings) |
| 48 | static inline int32_t SkFloat2Bits(float x) { |
| 49 | SkFloatIntUnion data; |
| 50 | data.fFloat = x; |
| 51 | return data.fSignBitInt; |
| 52 | } |
| 53 | |
| 54 | // Helper to see a bit pattern as a float (w/o aliasing warnings) |
| 55 | static inline float SkBits2Float(int32_t floatAsBits) { |
| 56 | SkFloatIntUnion data; |
| 57 | data.fSignBitInt = floatAsBits; |
| 58 | return data.fFloat; |
| 59 | } |
| 60 | |
Mike Reed | 9236b02 | 2018-05-14 13:37:16 -0400 | [diff] [blame] | 61 | constexpr int32_t gFloatBits_exponent_mask = 0x7F800000; |
| 62 | constexpr int32_t gFloatBits_matissa_mask = 0x007FFFFF; |
| 63 | |
| 64 | static inline bool SkFloatBits_IsFinite(int32_t bits) { |
| 65 | return (bits & gFloatBits_exponent_mask) != gFloatBits_exponent_mask; |
| 66 | } |
| 67 | |
| 68 | static inline bool SkFloatBits_IsInf(int32_t bits) { |
| 69 | return ((bits & gFloatBits_exponent_mask) == gFloatBits_exponent_mask) && |
| 70 | (bits & gFloatBits_matissa_mask) == 0; |
| 71 | } |
| 72 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 73 | /** Return the float as a 2s compliment int. Just to be used to compare floats |
| 74 | to each other or against positive float-bit-constants (like 0). This does |
| 75 | not return the int equivalent of the float, just something cheaper for |
| 76 | compares-only. |
| 77 | */ |
| 78 | static inline int32_t SkFloatAs2sCompliment(float x) { |
| 79 | return SkSignBitTo2sCompliment(SkFloat2Bits(x)); |
| 80 | } |
| 81 | |
| 82 | /** Return the 2s compliment int as a float. This undos the result of |
| 83 | SkFloatAs2sCompliment |
| 84 | */ |
| 85 | static inline float Sk2sComplimentAsFloat(int32_t x) { |
| 86 | return SkBits2Float(Sk2sComplimentToSignBit(x)); |
| 87 | } |
| 88 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 89 | // Scalar wrappers for float-bit routines |
| 90 | |
reed@google.com | 8f4d230 | 2013-12-17 16:44:46 +0000 | [diff] [blame] | 91 | #define SkScalarAs2sCompliment(x) SkFloatAs2sCompliment(x) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 92 | |
| 93 | #endif |