| 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 |
| 11 #include "SkScalar.h" | 11 #include "SkScalar.h" |
| 12 #include "math.h" |
| 13 |
| 12 #include "SkTypes.h" | 14 #include "SkTypes.h" |
| 13 | 15 |
| 14 /** \file SkFixed.h | 16 /** \file SkFixed.h |
| 15 | 17 |
| 16 Types and macros for 16.16 fixed point | 18 Types and macros for 16.16 fixed point |
| 17 */ | 19 */ |
| 18 | 20 |
| 19 /** 32 bit signed integer used to represent fractions values with 16 bits to the
right of the decimal point | 21 /** 32 bit signed integer used to represent fractions values with 16 bits to the
right of the decimal point |
| 20 */ | 22 */ |
| 21 typedef int32_t SkFixed; | 23 typedef int32_t SkFixed; |
| 22 #define SK_Fixed1 (1 << 16) | 24 #define SK_Fixed1 (1 << 16) |
| 23 #define SK_FixedHalf (1 << 15) | 25 #define SK_FixedHalf (1 << 15) |
| 24 #define SK_FixedMax (0x7FFFFFFF) | 26 #define SK_FixedMax (0x7FFFFFFF) |
| 25 #define SK_FixedMin (-SK_FixedMax) | 27 #define SK_FixedMin (-SK_FixedMax) |
| 26 #define SK_FixedPI (0x3243F) | 28 #define SK_FixedPI (0x3243F) |
| 27 #define SK_FixedSqrt2 (92682) | 29 #define SK_FixedSqrt2 (92682) |
| 28 #define SK_FixedTanPIOver8 (0x6A0A) | 30 #define SK_FixedTanPIOver8 (0x6A0A) |
| 29 #define SK_FixedRoot2Over2 (0xB505) | 31 #define SK_FixedRoot2Over2 (0xB505) |
| 30 | 32 |
| 31 #define SkFixedToFloat(x) ((x) * 1.52587890625e-5f) | 33 #define SkFixedToFloat(x) ((x) * 1.52587890625e-5f) |
| 32 #if 1 | 34 #define SkFloatToFixed(x) ((SkFixed)((x) * SK_Fixed1)) |
| 33 #define SkFloatToFixed(x) ((SkFixed)((x) * SK_Fixed1)) | 35 |
| 34 #else | 36 // Pins over/under flows to SK_FixedMax/SK_FixedMin (slower than just a cast). |
| 35 // pins over/under flows to max/min int32 (slower than just a cast) | 37 static inline SkFixed SkFloatPinToFixed(float x) { |
| 36 static inline SkFixed SkFloatToFixed(float x) { | 38 x *= SK_Fixed1; |
| 37 int64_t n = x * SK_Fixed1; | 39 // Casting float to int outside the range of the target type (int32_t) is un
defined behavior. |
| 38 return (SkFixed)n; | 40 if (x >= SK_FixedMax) return SK_FixedMax; |
| 39 } | 41 if (x <= SK_FixedMin) return SK_FixedMin; |
| 40 #endif | 42 const SkFixed result = static_cast<SkFixed>(x); |
| 43 SkASSERT(truncf(x) == static_cast<float>(result)); |
| 44 return result; |
| 45 } |
| 41 | 46 |
| 42 #ifdef SK_DEBUG | 47 #ifdef SK_DEBUG |
| 43 static inline SkFixed SkFloatToFixed_Check(float x) { | 48 static inline SkFixed SkFloatToFixed_Check(float x) { |
| 44 int64_t n64 = (int64_t)(x * SK_Fixed1); | 49 int64_t n64 = (int64_t)(x * SK_Fixed1); |
| 45 SkFixed n32 = (SkFixed)n64; | 50 SkFixed n32 = (SkFixed)n64; |
| 46 SkASSERT(n64 == n32); | 51 SkASSERT(n64 == n32); |
| 47 return n32; | 52 return n32; |
| 48 } | 53 } |
| 49 #else | 54 #else |
| 50 #define SkFloatToFixed_Check(x) SkFloatToFixed(x) | 55 #define SkFloatToFixed_Check(x) SkFloatToFixed(x) |
| 51 #endif | 56 #endif |
| 52 | 57 |
| 53 #define SkFixedToDouble(x) ((x) * 1.52587890625e-5) | 58 #define SkFixedToDouble(x) ((x) * 1.52587890625e-5) |
| 54 #define SkDoubleToFixed(x) ((SkFixed)((x) * SK_Fixed1)) | 59 #define SkDoubleToFixed(x) ((SkFixed)((x) * SK_Fixed1)) |
| 55 | 60 |
| 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 |
| 56 /** Converts an integer to a SkFixed, asserting that the result does not overflo
w | 72 /** Converts an integer to a SkFixed, asserting that the result does not overflo
w |
| 57 a 32 bit signed integer | 73 a 32 bit signed integer |
| 58 */ | 74 */ |
| 59 #ifdef SK_DEBUG | 75 #ifdef SK_DEBUG |
| 60 inline SkFixed SkIntToFixed(int n) | 76 inline SkFixed SkIntToFixed(int n) |
| 61 { | 77 { |
| 62 SkASSERT(n >= -32768 && n <= 32767); | 78 SkASSERT(n >= -32768 && n <= 32767); |
| 63 // Left shifting a negative value has undefined behavior in C, so we cas
t to unsigned before | 79 // Left shifting a negative value has undefined behavior in C, so we cas
t to unsigned before |
| 64 // shifting. | 80 // shifting. |
| 65 return (unsigned)n << 16; | 81 return (unsigned)n << 16; |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 #undef SkFloatToFixed | 156 #undef SkFloatToFixed |
| 141 #define SkFloatToFixed(x) SkFloatToFixed_arm(x) | 157 #define SkFloatToFixed(x) SkFloatToFixed_arm(x) |
| 142 #endif | 158 #endif |
| 143 | 159 |
| 144 /////////////////////////////////////////////////////////////////////////////// | 160 /////////////////////////////////////////////////////////////////////////////// |
| 145 | 161 |
| 146 #if SK_SCALAR_IS_FLOAT | 162 #if SK_SCALAR_IS_FLOAT |
| 147 | 163 |
| 148 #define SkFixedToScalar(x) SkFixedToFloat(x) | 164 #define SkFixedToScalar(x) SkFixedToFloat(x) |
| 149 #define SkScalarToFixed(x) SkFloatToFixed(x) | 165 #define SkScalarToFixed(x) SkFloatToFixed(x) |
| 166 #define SkScalarPinToFixed(x) SkFloatPinToFixed(x) |
| 150 | 167 |
| 151 #else // SK_SCALAR_IS_DOUBLE | 168 #else // SK_SCALAR_IS_DOUBLE |
| 152 | 169 |
| 153 #define SkFixedToScalar(x) SkFixedToDouble(x) | 170 #define SkFixedToScalar(x) SkFixedToDouble(x) |
| 154 #define SkScalarToFixed(x) SkDoubleToFixed(x) | 171 #define SkScalarToFixed(x) SkDoubleToFixed(x) |
| 172 #define SkScalarPinToFixed(x) SkDoublePinToFixed(x) |
| 155 | 173 |
| 156 #endif | 174 #endif |
| 157 | 175 |
| 158 /////////////////////////////////////////////////////////////////////////////// | 176 /////////////////////////////////////////////////////////////////////////////// |
| 159 | 177 |
| 160 typedef int64_t SkFixed3232; // 32.32 | 178 typedef int64_t SkFixed3232; // 32.32 |
| 161 | 179 |
| 162 #define SkIntToFixed3232(x) (SkLeftShift((SkFixed3232)(x), 32)) | 180 #define SkIntToFixed3232(x) (SkLeftShift((SkFixed3232)(x), 32)) |
| 163 #define SkFixed3232ToInt(x) ((int)((x) >> 32)) | 181 #define SkFixed3232ToInt(x) ((int)((x) >> 32)) |
| 164 #define SkFixedToFixed3232(x) (SkLeftShift((SkFixed3232)(x), 16)) | 182 #define SkFixedToFixed3232(x) (SkLeftShift((SkFixed3232)(x), 16)) |
| 165 #define SkFixed3232ToFixed(x) ((SkFixed)((x) >> 16)) | 183 #define SkFixed3232ToFixed(x) ((SkFixed)((x) >> 16)) |
| 166 #define SkFloatToFixed3232(x) ((SkFixed3232)((x) * (65536.0f * 65536.0f))) | 184 #define SkFloatToFixed3232(x) ((SkFixed3232)((x) * (65536.0f * 65536.0f))) |
| 167 | 185 |
| 168 #define SkScalarToFixed3232(x) SkFloatToFixed3232(x) | 186 #define SkScalarToFixed3232(x) SkFloatToFixed3232(x) |
| 169 | 187 |
| 170 #endif | 188 #endif |
| OLD | NEW |