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
9 #ifndef SkFloatBits_DEFINED
10 #define SkFloatBits_DEFINED
11
12 #include "SkTypes.h"
13 #include "SkSafe_math.h"
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 /** Return the float as a 2s compliment int. Just to be used to compare floats
59 to each other or against positive float-bit-constants (like 0). This does
60 not return the int equivalent of the float, just something cheaper for
61 compares-only.
62 */
SkFloatAs2sCompliment(float x)63 static inline int32_t SkFloatAs2sCompliment(float x) {
64 return SkSignBitTo2sCompliment(SkFloat2Bits(x));
65 }
66
67 /** Return the 2s compliment int as a float. This undos the result of
68 SkFloatAs2sCompliment
69 */
Sk2sComplimentAsFloat(int32_t x)70 static inline float Sk2sComplimentAsFloat(int32_t x) {
71 return SkBits2Float(Sk2sComplimentToSignBit(x));
72 }
73
pin_double_to_int(double x)74 static inline int32_t pin_double_to_int(double x) {
75 return (int32_t)SkTPin<double>(x, SK_MinS32, SK_MaxS32);
76 }
77
78 /** Return the floor of the float as an int.
79 If the value is out of range, or NaN, return +/- SK_MaxS32
80 */
SkFloatToIntFloor(float x)81 static inline int32_t SkFloatToIntFloor(float x) {
82 return pin_double_to_int(floor(x));
83 }
84
85 /** Return the float rounded to an int.
86 If the value is out of range, or NaN, return +/- SK_MaxS32
87 */
SkFloatToIntRound(float x)88 static inline int32_t SkFloatToIntRound(float x) {
89 return pin_double_to_int(floor((double)x + 0.5));
90 }
91
92 /** Return the ceiling of the float as an int.
93 If the value is out of range, or NaN, return +/- SK_MaxS32
94 */
SkFloatToIntCeil(float x)95 static inline int32_t SkFloatToIntCeil(float x) {
96 return pin_double_to_int(ceil(x));
97 }
98
99 // Scalar wrappers for float-bit routines
100
101 #define SkScalarAs2sCompliment(x) SkFloatAs2sCompliment(x)
102 #define Sk2sComplimentAsScalar(x) Sk2sComplimentAsFloat(x)
103
104 #endif
105