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

Side by Side Diff: include/core/SkFixed.h

Issue 1824733002: For *ToFixed, in debug mode, assert that the value is in range. (Closed) Base URL: https://skia.googlesource.com/skia@scalar-pin-to-fixed
Patch Set: Created 4 years, 9 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 | « no previous file | src/core/SkScan.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2006 The Android Open Source Project 2 * Copyright 2006 The Android Open Source Project
3 * 3 *
4 * 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
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #ifndef SkFixed_DEFINED 8 #ifndef SkFixed_DEFINED
9 #define SkFixed_DEFINED 9 #define SkFixed_DEFINED
10 10
(...skipping 11 matching lines...) Expand all
22 typedef int32_t SkFixed; 22 typedef int32_t SkFixed;
23 #define SK_Fixed1 (1 << 16) 23 #define SK_Fixed1 (1 << 16)
24 #define SK_FixedHalf (1 << 15) 24 #define SK_FixedHalf (1 << 15)
25 #define SK_FixedMax (0x7FFFFFFF) 25 #define SK_FixedMax (0x7FFFFFFF)
26 #define SK_FixedMin (-SK_FixedMax) 26 #define SK_FixedMin (-SK_FixedMax)
27 #define SK_FixedPI (0x3243F) 27 #define SK_FixedPI (0x3243F)
28 #define SK_FixedSqrt2 (92682) 28 #define SK_FixedSqrt2 (92682)
29 #define SK_FixedTanPIOver8 (0x6A0A) 29 #define SK_FixedTanPIOver8 (0x6A0A)
30 #define SK_FixedRoot2Over2 (0xB505) 30 #define SK_FixedRoot2Over2 (0xB505)
31 31
32 #define SkFixedToFloat(x) ((x) * 1.52587890625e-5f) 32 #define SkFixedToFloat(x) ((x) * 1.52587890625e-5f)
33 #define SkFloatToFixed(x) ((SkFixed)((x) * SK_Fixed1)) 33 #define SkFloatToFixed_Unsafe(x) ((SkFixed)((x) * SK_Fixed1))
34
35 #ifdef SK_DEBUG
36 static inline SkFixed SkFloatToFixed(float x) {
mtklein 2016/03/22 12:38:02 It should be fine to use this definition in Debug
37 const SkFixed result = SkFloatToFixed_Unsafe(x);
38 SkASSERT(truncf(x * SK_Fixed1) == static_cast<float>(result));
39 return result;
40 }
41 #else
42 #define SkFloatToFixed(x) SkFloatToFixed_Unsafe(x)
43 #endif
34 44
35 // Pins over/under flows to SK_FixedMax/SK_FixedMin (slower than just a cast). 45 // Pins over/under flows to SK_FixedMax/SK_FixedMin (slower than just a cast).
36 static inline SkFixed SkFloatPinToFixed(float x) { 46 static inline SkFixed SkFloatPinToFixed(float x) {
37 x *= SK_Fixed1; 47 x *= SK_Fixed1;
38 // Casting float to int outside the range of the target type (int32_t) is un defined behavior. 48 // Casting float to int outside the range of the target type (int32_t) is un defined behavior.
39 if (x >= SK_FixedMax) return SK_FixedMax; 49 if (x >= SK_FixedMax) return SK_FixedMax;
40 if (x <= SK_FixedMin) return SK_FixedMin; 50 if (x <= SK_FixedMin) return SK_FixedMin;
41 const SkFixed result = static_cast<SkFixed>(x); 51 const SkFixed result = static_cast<SkFixed>(x);
42 SkASSERT(truncf(x) == static_cast<float>(result)); 52 SkASSERT(truncf(x) == static_cast<float>(result));
43 return result; 53 return result;
44 } 54 }
45 55
56 #define SkFixedToDouble(x) ((x) * 1.52587890625e-5)
57 #define SkDoubleToFixed_Unsafe(x) ((SkFixed)((x) * SK_Fixed1))
58
46 #ifdef SK_DEBUG 59 #ifdef SK_DEBUG
mtklein 2016/03/22 12:38:02 Ditto.
47 static inline SkFixed SkFloatToFixed_Check(float x) { 60 static inline SkFixed SkDoubleToFixed(double x) {
48 int64_t n64 = (int64_t)(x * SK_Fixed1); 61 const SkFixed result = SkDoubleToFixed_Unsafe(x);
49 SkFixed n32 = (SkFixed)n64; 62 SkASSERT(trunc(x * SK_Fixed1) == static_cast<double>(result));
50 SkASSERT(n64 == n32); 63 return result;
51 return n32;
52 } 64 }
53 #else 65 #else
54 #define SkFloatToFixed_Check(x) SkFloatToFixed(x) 66 #define SkDoubleToFixed(x) SkDoubleToFixed_Unsafe(x)
55 #endif 67 #endif
56 68
57 #define SkFixedToDouble(x) ((x) * 1.52587890625e-5)
58 #define SkDoubleToFixed(x) ((SkFixed)((x) * SK_Fixed1))
59
60 // Pins over/under flows to SK_FixedMax/SK_FixedMin (slower than just a cast). 69 // Pins over/under flows to SK_FixedMax/SK_FixedMin (slower than just a cast).
61 static inline SkFixed SkDoublePinToFixed(double x) { 70 static inline SkFixed SkDoublePinToFixed(double x) {
62 x *= SK_Fixed1; 71 x *= SK_Fixed1;
63 // Casting double to int outside the range of the target type (int32_t) is u ndefined behavior. 72 // Casting double to int outside the range of the target type (int32_t) is u ndefined behavior.
64 if (x >= SK_FixedMax) return SK_FixedMax; 73 if (x >= SK_FixedMax) return SK_FixedMax;
65 if (x <= SK_FixedMin) return SK_FixedMin; 74 if (x <= SK_FixedMin) return SK_FixedMin;
66 const SkFixed result = static_cast<SkFixed>(x); 75 const SkFixed result = static_cast<SkFixed>(x);
67 SkASSERT(trunc(x) == static_cast<double>(result)); 76 SkASSERT(trunc(x) == static_cast<double>(result));
68 return result; 77 return result;
69 } 78 }
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 "orr %0, %0, %2, lsl #16 \n" 154 "orr %0, %0, %2, lsl #16 \n"
146 : "=r"(x), "=&r"(y), "=r"(t) 155 : "=r"(x), "=&r"(y), "=r"(t)
147 : "r"(x), "1"(y) 156 : "r"(x), "1"(y)
148 : 157 :
149 ); 158 );
150 return x; 159 return x;
151 } 160 }
152 #undef SkFixedMul 161 #undef SkFixedMul
153 #define SkFixedMul(x, y) SkFixedMul_arm(x, y) 162 #define SkFixedMul(x, y) SkFixedMul_arm(x, y)
154 163
155 #undef SkFloatToFixed 164 #undef SkFloatToFixed_Unsafe
156 #define SkFloatToFixed(x) SkFloatToFixed_arm(x) 165 #define SkFloatToFixed_Unsafe(x) SkFloatToFixed_arm(x)
mtklein 2016/03/22 12:38:02 Can you double check that SkFloatToFixed still cal
157 #endif 166 #endif
158 167
159 /////////////////////////////////////////////////////////////////////////////// 168 ///////////////////////////////////////////////////////////////////////////////
160 169
161 typedef int64_t SkFixed3232; // 32.32 170 typedef int64_t SkFixed3232; // 32.32
162 171
163 #define SkIntToFixed3232(x) (SkLeftShift((SkFixed3232)(x), 32)) 172 #define SK_Fixed3232_1 (static_cast<SkFixed3232>(1) << 32)
164 #define SkFixed3232ToInt(x) ((int)((x) >> 32)) 173 #define SkIntToFixed3232(x) (SkLeftShift((SkFixed3232)(x), 32))
165 #define SkFixedToFixed3232(x) (SkLeftShift((SkFixed3232)(x), 16)) 174 #define SkFixed3232ToInt(x) ((int)((x) >> 32))
166 #define SkFixed3232ToFixed(x) ((SkFixed)((x) >> 16)) 175 #define SkFixedToFixed3232(x) (SkLeftShift((SkFixed3232)(x), 16))
167 #define SkFloatToFixed3232(x) ((SkFixed3232)((x) * (65536.0f * 65536.0f))) 176 #define SkFixed3232ToFixed(x) ((SkFixed)((x) >> 16))
177 #define SkFloatToFixed3232_Unsafe(x) (static_cast<SkFixed3232>((x) * SK_Fixed32 32_1))
178
179 #ifdef SK_DEBUG
mtklein 2016/03/22 12:38:03 ditto
180 static inline SkFixed3232 SkFloatToFixed3232(float x) {
181 const SkFixed3232 result = SkFloatToFixed3232_Unsafe(x);
182 SkASSERT(truncf(x * SK_Fixed3232_1) == static_cast<float>(result));
183 return result;
184 }
185 #else
186 #define SkFloatToFixed3232(x) SkFloatToFixed3232_Unsafe(x)
187 #endif
168 188
169 #define SkScalarToFixed3232(x) SkFloatToFixed3232(x) 189 #define SkScalarToFixed3232(x) SkFloatToFixed3232(x)
170 190
171 /////////////////////////////////////////////////////////////////////////////// 191 ///////////////////////////////////////////////////////////////////////////////
172 192
173 // 64bits wide, with a 16bit bias. Useful when accumulating lots of 16.16 so 193 // 64bits wide, with a 16bit bias. Useful when accumulating lots of 16.16 so
174 // we don't overflow along the way 194 // we don't overflow along the way
175 typedef int64_t Sk48Dot16; 195 typedef int64_t Sk48Dot16;
176 196
177 #define Sk48Dot16FloorToInt(x) static_cast<int>((x) >> 16) 197 #define Sk48Dot16FloorToInt(x) static_cast<int>((x) >> 16)
178 198
179 static inline float Sk48Dot16ToScalar(Sk48Dot16 x) { 199 static inline float Sk48Dot16ToScalar(Sk48Dot16 x) {
180 return static_cast<float>(x * 1.5258789e-5); // x * (1.0f / (1 << 16)) 200 return static_cast<float>(x * 1.5258789e-5); // x * (1.0f / (1 << 16))
181 } 201 }
182 #define SkFloatTo48Dot16(x) (static_cast<Sk48Dot16>((x) * (1 << 16))) 202 #define SkFloatTo48Dot16(x) (static_cast<Sk48Dot16>((x) * (1 << 16)))
183 203
184 #define SkScalarTo48Dot16(x) SkFloatTo48Dot16(x) 204 #define SkScalarTo48Dot16(x) SkFloatTo48Dot16(x)
185 205
186 #endif 206 #endif
OLDNEW
« no previous file with comments | « no previous file | src/core/SkScan.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698