• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2008 The Android Open Source Project
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SkFloatBits_DEFINED
9 #define SkFloatBits_DEFINED
10 
11 #include "include/private/base/SkMath.h"
12 
13 #include <cstdint>
14 
15 /** Convert a sign-bit int (i.e. float interpreted as int) into a 2s compliement
16     int. This also converts -0 (0x80000000) to 0. Doing this to a float allows
17     it to be compared using normal C operators (<, <=, etc.)
18 */
SkSignBitTo2sCompliment(int32_t x)19 static inline int32_t SkSignBitTo2sCompliment(int32_t x) {
20     if (x < 0) {
21         x &= 0x7FFFFFFF;
22         x = -x;
23     }
24     return x;
25 }
26 
27 /** Convert a 2s compliment int to a sign-bit (i.e. int interpreted as float).
28     This undoes the result of SkSignBitTo2sCompliment().
29  */
Sk2sComplimentToSignBit(int32_t x)30 static inline int32_t Sk2sComplimentToSignBit(int32_t x) {
31     int sign = x >> 31;
32     // make x positive
33     x = (x ^ sign) - sign;
34     // set the sign bit as needed
35     x |= SkLeftShift(sign, 31);
36     return x;
37 }
38 
39 union SkFloatIntUnion {
40     float   fFloat;
41     int32_t fSignBitInt;
42 };
43 
44 // Helper to see a float as its bit pattern (w/o aliasing warnings)
SkFloat2Bits(float x)45 static inline int32_t SkFloat2Bits(float x) {
46     SkFloatIntUnion data;
47     data.fFloat = x;
48     return data.fSignBitInt;
49 }
50 
51 // Helper to see a bit pattern as a float (w/o aliasing warnings)
SkBits2Float(int32_t floatAsBits)52 static inline float SkBits2Float(int32_t floatAsBits) {
53     SkFloatIntUnion data;
54     data.fSignBitInt = floatAsBits;
55     return data.fFloat;
56 }
57 
58 constexpr int32_t gFloatBits_exponent_mask = 0x7F800000;
59 constexpr int32_t gFloatBits_matissa_mask  = 0x007FFFFF;
60 
SkFloatBits_IsFinite(int32_t bits)61 static inline bool SkFloatBits_IsFinite(int32_t bits) {
62     return (bits & gFloatBits_exponent_mask) != gFloatBits_exponent_mask;
63 }
64 
SkFloatBits_IsInf(int32_t bits)65 static inline bool SkFloatBits_IsInf(int32_t bits) {
66     return ((bits & gFloatBits_exponent_mask) == gFloatBits_exponent_mask) &&
67             (bits & gFloatBits_matissa_mask) == 0;
68 }
69 
70 /** Return the float as a 2s compliment int. Just to be used to compare floats
71     to each other or against positive float-bit-constants (like 0). This does
72     not return the int equivalent of the float, just something cheaper for
73     compares-only.
74  */
SkFloatAs2sCompliment(float x)75 static inline int32_t SkFloatAs2sCompliment(float x) {
76     return SkSignBitTo2sCompliment(SkFloat2Bits(x));
77 }
78 
79 /** Return the 2s compliment int as a float. This undos the result of
80     SkFloatAs2sCompliment
81  */
Sk2sComplimentAsFloat(int32_t x)82 static inline float Sk2sComplimentAsFloat(int32_t x) {
83     return SkBits2Float(Sk2sComplimentToSignBit(x));
84 }
85 
86 //  Scalar wrappers for float-bit routines
87 
88 #define SkScalarAs2sCompliment(x)    SkFloatAs2sCompliment(x)
89 
90 #endif
91