| 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 15 matching lines...) Expand all Loading... |
| 26 #define SK_FixedMax (0x7FFFFFFF) | 26 #define SK_FixedMax (0x7FFFFFFF) |
| 27 #define SK_FixedMin (-SK_FixedMax) | 27 #define SK_FixedMin (-SK_FixedMax) |
| 28 #define SK_FixedPI (0x3243F) | 28 #define SK_FixedPI (0x3243F) |
| 29 #define SK_FixedSqrt2 (92682) | 29 #define SK_FixedSqrt2 (92682) |
| 30 #define SK_FixedTanPIOver8 (0x6A0A) | 30 #define SK_FixedTanPIOver8 (0x6A0A) |
| 31 #define SK_FixedRoot2Over2 (0xB505) | 31 #define SK_FixedRoot2Over2 (0xB505) |
| 32 | 32 |
| 33 #define SkFixedToFloat(x) ((x) * 1.52587890625e-5f) | 33 #define SkFixedToFloat(x) ((x) * 1.52587890625e-5f) |
| 34 #define SkFloatToFixed(x) ((SkFixed)((x) * SK_Fixed1)) | 34 #define SkFloatToFixed(x) ((SkFixed)((x) * SK_Fixed1)) |
| 35 | 35 |
| 36 // Pins over/under flows to SK_FixedMax/SK_FixedMin (slower than just a cast). | |
| 37 static inline SkFixed SkFloatPinToFixed(float x) { | |
| 38 x *= SK_Fixed1; | |
| 39 // Casting float to int outside the range of the target type (int32_t) is un
defined behavior. | |
| 40 if (x >= SK_FixedMax) return SK_FixedMax; | |
| 41 if (x <= SK_FixedMin) return SK_FixedMin; | |
| 42 const SkFixed result = static_cast<SkFixed>(x); | |
| 43 SkASSERT(truncf(x) == static_cast<float>(result)); | |
| 44 return result; | |
| 45 } | |
| 46 | |
| 47 #ifdef SK_DEBUG | 36 #ifdef SK_DEBUG |
| 48 static inline SkFixed SkFloatToFixed_Check(float x) { | 37 static inline SkFixed SkFloatToFixed_Check(float x) { |
| 49 int64_t n64 = (int64_t)(x * SK_Fixed1); | 38 int64_t n64 = (int64_t)(x * SK_Fixed1); |
| 50 SkFixed n32 = (SkFixed)n64; | 39 SkFixed n32 = (SkFixed)n64; |
| 51 SkASSERT(n64 == n32); | 40 SkASSERT(n64 == n32); |
| 52 return n32; | 41 return n32; |
| 53 } | 42 } |
| 54 #else | 43 #else |
| 55 #define SkFloatToFixed_Check(x) SkFloatToFixed(x) | 44 #define SkFloatToFixed_Check(x) SkFloatToFixed(x) |
| 56 #endif | 45 #endif |
| 57 | 46 |
| 58 #define SkFixedToDouble(x) ((x) * 1.52587890625e-5) | 47 #define SkFixedToDouble(x) ((x) * 1.52587890625e-5) |
| 59 #define SkDoubleToFixed(x) ((SkFixed)((x) * SK_Fixed1)) | 48 #define SkDoubleToFixed(x) ((SkFixed)((x) * SK_Fixed1)) |
| 60 | 49 |
| 61 // Pins over/under flows to SK_FixedMax/SK_FixedMin (slower than just a cast). | |
| 62 static inline SkFixed SkDoublePinToFixed(double x) { | |
| 63 x *= SK_Fixed1; | |
| 64 // Casting double to int outside the range of the target type (int32_t) is u
ndefined behavior. | |
| 65 if (x >= SK_FixedMax) return SK_FixedMax; | |
| 66 if (x <= SK_FixedMin) return SK_FixedMin; | |
| 67 const SkFixed result = static_cast<SkFixed>(x); | |
| 68 SkASSERT(trunc(x) == static_cast<double>(result)); | |
| 69 return result; | |
| 70 } | |
| 71 | |
| 72 /** Converts an integer to a SkFixed, asserting that the result does not overflo
w | 50 /** Converts an integer to a SkFixed, asserting that the result does not overflo
w |
| 73 a 32 bit signed integer | 51 a 32 bit signed integer |
| 74 */ | 52 */ |
| 75 #ifdef SK_DEBUG | 53 #ifdef SK_DEBUG |
| 76 inline SkFixed SkIntToFixed(int n) | 54 inline SkFixed SkIntToFixed(int n) |
| 77 { | 55 { |
| 78 SkASSERT(n >= -32768 && n <= 32767); | 56 SkASSERT(n >= -32768 && n <= 32767); |
| 79 // Left shifting a negative value has undefined behavior in C, so we cas
t to unsigned before | 57 // Left shifting a negative value has undefined behavior in C, so we cas
t to unsigned before |
| 80 // shifting. | 58 // shifting. |
| 81 return (unsigned)n << 16; | 59 return (unsigned)n << 16; |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 #undef SkFloatToFixed | 129 #undef SkFloatToFixed |
| 152 #define SkFloatToFixed(x) SkFloatToFixed_arm(x) | 130 #define SkFloatToFixed(x) SkFloatToFixed_arm(x) |
| 153 #endif | 131 #endif |
| 154 | 132 |
| 155 /////////////////////////////////////////////////////////////////////////////// | 133 /////////////////////////////////////////////////////////////////////////////// |
| 156 | 134 |
| 157 #if SK_SCALAR_IS_FLOAT | 135 #if SK_SCALAR_IS_FLOAT |
| 158 | 136 |
| 159 #define SkFixedToScalar(x) SkFixedToFloat(x) | 137 #define SkFixedToScalar(x) SkFixedToFloat(x) |
| 160 #define SkScalarToFixed(x) SkFloatToFixed(x) | 138 #define SkScalarToFixed(x) SkFloatToFixed(x) |
| 161 #define SkScalarPinToFixed(x) SkFloatPinToFixed(x) | |
| 162 | 139 |
| 163 #else // SK_SCALAR_IS_DOUBLE | 140 #else // SK_SCALAR_IS_DOUBLE |
| 164 | 141 |
| 165 #define SkFixedToScalar(x) SkFixedToDouble(x) | 142 #define SkFixedToScalar(x) SkFixedToDouble(x) |
| 166 #define SkScalarToFixed(x) SkDoubleToFixed(x) | 143 #define SkScalarToFixed(x) SkDoubleToFixed(x) |
| 167 #define SkScalarPinToFixed(x) SkDoublePinToFixed(x) | |
| 168 | 144 |
| 169 #endif | 145 #endif |
| 170 | 146 |
| 171 /////////////////////////////////////////////////////////////////////////////// | 147 /////////////////////////////////////////////////////////////////////////////// |
| 172 | 148 |
| 173 typedef int64_t SkFixed3232; // 32.32 | 149 typedef int64_t SkFixed3232; // 32.32 |
| 174 | 150 |
| 175 #define SkIntToFixed3232(x) (SkLeftShift((SkFixed3232)(x), 32)) | 151 #define SkIntToFixed3232(x) (SkLeftShift((SkFixed3232)(x), 32)) |
| 176 #define SkFixed3232ToInt(x) ((int)((x) >> 32)) | 152 #define SkFixed3232ToInt(x) ((int)((x) >> 32)) |
| 177 #define SkFixedToFixed3232(x) (SkLeftShift((SkFixed3232)(x), 16)) | 153 #define SkFixedToFixed3232(x) (SkLeftShift((SkFixed3232)(x), 16)) |
| 178 #define SkFixed3232ToFixed(x) ((SkFixed)((x) >> 16)) | 154 #define SkFixed3232ToFixed(x) ((SkFixed)((x) >> 16)) |
| 179 #define SkFloatToFixed3232(x) ((SkFixed3232)((x) * (65536.0f * 65536.0f))) | 155 #define SkFloatToFixed3232(x) ((SkFixed3232)((x) * (65536.0f * 65536.0f))) |
| 180 | 156 |
| 181 #define SkScalarToFixed3232(x) SkFloatToFixed3232(x) | 157 #define SkScalarToFixed3232(x) SkFloatToFixed3232(x) |
| 182 | 158 |
| 183 #endif | 159 #endif |
| OLD | NEW |