Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(443)

Side by Side Diff: include/private/SkFloatBits.h

Issue 2012333003: Clean up SkFloatBits (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: member Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « gyp/core.gypi ('k') | src/core/SkFloatBits.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1
2 /* 1 /*
3 * Copyright 2008 The Android Open Source Project 2 * Copyright 2008 The Android Open Source Project
4 * 3 *
5 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 5 * found in the LICENSE file.
7 */ 6 */
8 7
9 8
10 #ifndef SkFloatBits_DEFINED 9 #ifndef SkFloatBits_DEFINED
11 #define SkFloatBits_DEFINED 10 #define SkFloatBits_DEFINED
12 11
13 #include "SkTypes.h" 12 #include "SkTypes.h"
13 #include <math.h>
14 14
15 /** Convert a sign-bit int (i.e. float interpreted as int) into a 2s compliement 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 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.) 17 it to be compared using normal C operators (<, <=, etc.)
18 */ 18 */
19 static inline int32_t SkSignBitTo2sCompliment(int32_t x) { 19 static inline int32_t SkSignBitTo2sCompliment(int32_t x) {
20 if (x < 0) { 20 if (x < 0) {
21 x &= 0x7FFFFFFF; 21 x &= 0x7FFFFFFF;
22 x = -x; 22 x = -x;
23 } 23 }
24 return x; 24 return x;
25 } 25 }
26 26
27 /** Convert a 2s compliment int to a sign-bit (i.e. int interpreted as float). 27 /** Convert a 2s compliment int to a sign-bit (i.e. int interpreted as float).
28 This undoes the result of SkSignBitTo2sCompliment(). 28 This undoes the result of SkSignBitTo2sCompliment().
29 */ 29 */
30 static inline int32_t Sk2sComplimentToSignBit(int32_t x) { 30 static inline int32_t Sk2sComplimentToSignBit(int32_t x) {
31 int sign = x >> 31; 31 int sign = x >> 31;
32 // make x positive 32 // make x positive
33 x = (x ^ sign) - sign; 33 x = (x ^ sign) - sign;
34 // set the sign bit as needed 34 // set the sign bit as needed
35 x |= SkLeftShift(sign, 31); 35 x |= SkLeftShift(sign, 31);
36 return x; 36 return x;
37 } 37 }
38 38
39 /** Given the bit representation of a float, return its value cast to an int.
40 If the value is out of range, or NaN, return return +/- SK_MaxS32
41 */
42 int32_t SkFloatBits_toIntCast(int32_t floatBits);
43
44 /** Given the bit representation of a float, return its floor as an int.
45 If the value is out of range, or NaN, return return +/- SK_MaxS32
46 */
47 SK_API int32_t SkFloatBits_toIntFloor(int32_t floatBits);
48
49 /** Given the bit representation of a float, return it rounded to an int.
50 If the value is out of range, or NaN, return return +/- SK_MaxS32
51 */
52 SK_API int32_t SkFloatBits_toIntRound(int32_t floatBits);
53
54 /** Given the bit representation of a float, return its ceiling as an int.
55 If the value is out of range, or NaN, return return +/- SK_MaxS32
56 */
57 SK_API int32_t SkFloatBits_toIntCeil(int32_t floatBits);
58
59
60 union SkFloatIntUnion { 39 union SkFloatIntUnion {
61 float fFloat; 40 float fFloat;
62 int32_t fSignBitInt; 41 int32_t fSignBitInt;
63 }; 42 };
64 43
65 // Helper to see a float as its bit pattern (w/o aliasing warnings) 44 // Helper to see a float as its bit pattern (w/o aliasing warnings)
66 static inline int32_t SkFloat2Bits(float x) { 45 static inline int32_t SkFloat2Bits(float x) {
67 SkFloatIntUnion data; 46 SkFloatIntUnion data;
68 data.fFloat = x; 47 data.fFloat = x;
69 return data.fSignBitInt; 48 return data.fSignBitInt;
(...skipping 15 matching lines...) Expand all
85 return SkSignBitTo2sCompliment(SkFloat2Bits(x)); 64 return SkSignBitTo2sCompliment(SkFloat2Bits(x));
86 } 65 }
87 66
88 /** Return the 2s compliment int as a float. This undos the result of 67 /** Return the 2s compliment int as a float. This undos the result of
89 SkFloatAs2sCompliment 68 SkFloatAs2sCompliment
90 */ 69 */
91 static inline float Sk2sComplimentAsFloat(int32_t x) { 70 static inline float Sk2sComplimentAsFloat(int32_t x) {
92 return SkBits2Float(Sk2sComplimentToSignBit(x)); 71 return SkBits2Float(Sk2sComplimentToSignBit(x));
93 } 72 }
94 73
95 /** Return x cast to a float (i.e. (float)x) 74 static inline int32_t pin_double_to_int(double x) {
96 */ 75 return (int32_t)SkTPin<double>(x, SK_MinS32, SK_MaxS32);
97 float SkIntToFloatCast(int x);
98
99 /** Return the float cast to an int.
100 If the value is out of range, or NaN, return +/- SK_MaxS32
101 */
102 static inline int32_t SkFloatToIntCast(float x) {
103 return SkFloatBits_toIntCast(SkFloat2Bits(x));
104 } 76 }
105 77
106 /** Return the floor of the float as an int. 78 /** Return the floor of the float as an int.
107 If the value is out of range, or NaN, return +/- SK_MaxS32 79 If the value is out of range, or NaN, return +/- SK_MaxS32
108 */ 80 */
109 static inline int32_t SkFloatToIntFloor(float x) { 81 static inline int32_t SkFloatToIntFloor(float x) {
110 return SkFloatBits_toIntFloor(SkFloat2Bits(x)); 82 return pin_double_to_int(floor(x));
111 } 83 }
112 84
113 /** Return the float rounded to an int. 85 /** Return the float rounded to an int.
114 If the value is out of range, or NaN, return +/- SK_MaxS32 86 If the value is out of range, or NaN, return +/- SK_MaxS32
115 */ 87 */
116 static inline int32_t SkFloatToIntRound(float x) { 88 static inline int32_t SkFloatToIntRound(float x) {
117 return SkFloatBits_toIntRound(SkFloat2Bits(x)); 89 return pin_double_to_int(floor((double)x + 0.5));
118 } 90 }
119 91
120 /** Return the ceiling of the float as an int. 92 /** Return the ceiling of the float as an int.
121 If the value is out of range, or NaN, return +/- SK_MaxS32 93 If the value is out of range, or NaN, return +/- SK_MaxS32
122 */ 94 */
123 static inline int32_t SkFloatToIntCeil(float x) { 95 static inline int32_t SkFloatToIntCeil(float x) {
124 return SkFloatBits_toIntCeil(SkFloat2Bits(x)); 96 return pin_double_to_int(ceil(x));
125 } 97 }
126 98
127 // Scalar wrappers for float-bit routines 99 // Scalar wrappers for float-bit routines
128 100
129 #define SkScalarAs2sCompliment(x) SkFloatAs2sCompliment(x) 101 #define SkScalarAs2sCompliment(x) SkFloatAs2sCompliment(x)
130 #define Sk2sComplimentAsScalar(x) Sk2sComplimentAsFloat(x) 102 #define Sk2sComplimentAsScalar(x) Sk2sComplimentAsFloat(x)
131 103
132 #endif 104 #endif
OLDNEW
« no previous file with comments | « gyp/core.gypi ('k') | src/core/SkFloatBits.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698