OLD | NEW |
---|---|
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 10 matching lines...) Expand all Loading... | |
21 #define SK_Fixed1 (1 << 16) | 21 #define SK_Fixed1 (1 << 16) |
22 #define SK_FixedHalf (1 << 15) | 22 #define SK_FixedHalf (1 << 15) |
23 #define SK_FixedMax (0x7FFFFFFF) | 23 #define SK_FixedMax (0x7FFFFFFF) |
24 #define SK_FixedMin (-SK_FixedMax) | 24 #define SK_FixedMin (-SK_FixedMax) |
25 #define SK_FixedPI (0x3243F) | 25 #define SK_FixedPI (0x3243F) |
26 #define SK_FixedSqrt2 (92682) | 26 #define SK_FixedSqrt2 (92682) |
27 #define SK_FixedTanPIOver8 (0x6A0A) | 27 #define SK_FixedTanPIOver8 (0x6A0A) |
28 #define SK_FixedRoot2Over2 (0xB505) | 28 #define SK_FixedRoot2Over2 (0xB505) |
29 | 29 |
30 #define SkFixedToFloat(x) ((x) * 1.52587890625e-5f) | 30 #define SkFixedToFloat(x) ((x) * 1.52587890625e-5f) |
31 #if 1 | 31 #define SkFloatToFixed(x) ((SkFixed)((x) * SK_Fixed1)) |
32 #define SkFloatToFixed(x) ((SkFixed)((x) * SK_Fixed1)) | 32 |
33 #else | 33 // Maximum input value for SkFloatToFixed. (-SK_FloatToFixedMax is the minimum.) |
34 // pins over/under flows to max/min int32 (slower than just a cast) | 34 // SkFixed has more precision than float near SK_FixedMax/SK_FixedMin. Values hi gher than |
mtklein
2016/03/11 15:01:29
Let's write "near SK_FixedMax and SK_FixedMin". B
dogben
2016/03/11 16:33:19
Removed.
| |
35 static inline SkFixed SkFloatToFixed(float x) { | 35 // SK_FloatToFixedMax will overflow SkFixed. |
36 int64_t n = x * SK_Fixed1; | 36 // This must be a macro because MSVC doesn't like constexpr float. |
37 return (SkFixed)n; | 37 #define SK_FloatToFixedMax SkFixedToFloat(SK_FixedMax - 0x7f) |
mtklein
2016/03/11 15:01:29
Walk me through this math / logic? I'm curious if
dogben
2016/03/11 16:33:19
Done, except it feels wrong to return values less
| |
38 } | 38 |
39 #endif | 39 // Pins over/under flows to SK_FixedMax/SK_FixedMin (slower than just a cast). |
40 static inline SkFixed SkFloatPinToFixed(float x) { | |
41 static_assert(SkFixedToFloat(SkFloatToFixed(SK_FloatToFixedMax)) == SK_Float ToFixedMax, | |
42 "SK_FloatToFixedMax is not the real max."); | |
43 static_assert(SkFixedToFloat(SkFloatToFixed(-SK_FloatToFixedMax)) == -SK_Flo atToFixedMax, | |
44 "-SK_FloatToFixedMax is not the real min."); | |
45 // Unlike SkDoublePinToFixed, we don't use SkTPin, because that would | |
46 // produce SkFloatToFixed(SK_FloatToFixedMax), which is less than SK_FixedMa x. | |
47 if (x > SK_FloatToFixedMax) return SK_FixedMax; | |
48 if (x < -SK_FloatToFixedMax) return SK_FixedMin; | |
49 return SkFloatToFixed(x); | |
50 } | |
40 | 51 |
41 #ifdef SK_DEBUG | 52 #ifdef SK_DEBUG |
42 static inline SkFixed SkFloatToFixed_Check(float x) { | 53 static inline SkFixed SkFloatToFixed_Check(float x) { |
43 int64_t n64 = (int64_t)(x * SK_Fixed1); | 54 int64_t n64 = (int64_t)(x * SK_Fixed1); |
44 SkFixed n32 = (SkFixed)n64; | 55 SkFixed n32 = (SkFixed)n64; |
45 SkASSERT(n64 == n32); | 56 SkASSERT(n64 == n32); |
46 return n32; | 57 return n32; |
47 } | 58 } |
48 #else | 59 #else |
49 #define SkFloatToFixed_Check(x) SkFloatToFixed(x) | 60 #define SkFloatToFixed_Check(x) SkFloatToFixed(x) |
50 #endif | 61 #endif |
51 | 62 |
52 #define SkFixedToDouble(x) ((x) * 1.52587890625e-5) | 63 #define SkFixedToDouble(x) ((x) * 1.52587890625e-5) |
53 #define SkDoubleToFixed(x) ((SkFixed)((x) * SK_Fixed1)) | 64 #define SkDoubleToFixed(x) ((SkFixed)((x) * SK_Fixed1)) |
54 | 65 |
66 // Maximum input value for SkDoubleToFixed. (-SK_DoubleToFixedMax is the minimum .) | |
67 // This must be a macro because MSVC doesn't like constexpr float. | |
68 #define SK_DoubleToFixedMax SkFixedToDouble(SK_FixedMax) | |
69 | |
70 // Pins over/under flows to SK_FixedMax/SK_FixedMin (slower than just a cast). | |
71 static inline SkFixed SkDoublePinToFixed(double x) { | |
72 static_assert(SkFixedToDouble(SkDoubleToFixed(SK_DoubleToFixedMax)) == SK_Do ubleToFixedMax, | |
73 "SK_DoubleToFixedMax is not the real max."); | |
74 static_assert(SkFixedToDouble(SkDoubleToFixed(-SK_DoubleToFixedMax)) == -SK_ DoubleToFixedMax, | |
75 "-SK_DoubleToFixedMax is not the real min."); | |
76 return static_cast<SkFixed>(SkTPin(x, -SK_DoubleToFixedMax, SK_DoubleToFixed Max) * SK_Fixed1); | |
77 } | |
78 | |
55 /** Converts an integer to a SkFixed, asserting that the result does not overflo w | 79 /** Converts an integer to a SkFixed, asserting that the result does not overflo w |
56 a 32 bit signed integer | 80 a 32 bit signed integer |
57 */ | 81 */ |
58 #ifdef SK_DEBUG | 82 #ifdef SK_DEBUG |
59 inline SkFixed SkIntToFixed(int n) | 83 inline SkFixed SkIntToFixed(int n) |
60 { | 84 { |
61 SkASSERT(n >= -32768 && n <= 32767); | 85 SkASSERT(n >= -32768 && n <= 32767); |
62 // Left shifting a negative value has undefined behavior in C, so we cas t to unsigned before | 86 // Left shifting a negative value has undefined behavior in C, so we cas t to unsigned before |
63 // shifting. | 87 // shifting. |
64 return (unsigned)n << 16; | 88 return (unsigned)n << 16; |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
161 #define Sk48Dot16FloorToInt(x) static_cast<int>((x) >> 16) | 185 #define Sk48Dot16FloorToInt(x) static_cast<int>((x) >> 16) |
162 | 186 |
163 static inline float Sk48Dot16ToScalar(Sk48Dot16 x) { | 187 static inline float Sk48Dot16ToScalar(Sk48Dot16 x) { |
164 return static_cast<float>(x * 1.5258789e-5); // x * (1.0f / (1 << 16)) | 188 return static_cast<float>(x * 1.5258789e-5); // x * (1.0f / (1 << 16)) |
165 } | 189 } |
166 #define SkFloatTo48Dot16(x) (static_cast<Sk48Dot16>((x) * (1 << 16))) | 190 #define SkFloatTo48Dot16(x) (static_cast<Sk48Dot16>((x) * (1 << 16))) |
167 | 191 |
168 #define SkScalarTo48Dot16(x) SkFloatTo48Dot16(x) | 192 #define SkScalarTo48Dot16(x) SkFloatTo48Dot16(x) |
169 | 193 |
170 #endif | 194 #endif |
OLD | NEW |