Chromium Code Reviews| 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 11 matching lines...) Expand all Loading... | |
| 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) |
|
mtklein
2016/03/23 02:16:41
This may no longer need its new indentation.
| |
| 33 #define SkFloatToFixed(x) ((SkFixed)((x) * SK_Fixed1)) | 33 |
| 34 /////////////////////////////////////////////////////////////////////////////// | |
| 35 // ASM alternatives for our portable versions. | |
| 36 | |
| 37 #if defined(SK_CPU_ARM32) | |
| 38 /* This guy does not handle NaN or other obscurities, but is faster than | |
| 39 than (int)(x*65536). When built on Android with -Os, needs forcing | |
| 40 to inline or we lose the speed benefit. | |
| 41 */ | |
| 42 SK_ALWAYS_INLINE SkFixed SkFloatToFixed_arm(float x) | |
| 43 { | |
| 44 int32_t y, z; | |
| 45 asm("movs %1, %3, lsl #1 \n" | |
| 46 "mov %2, #0x8E \n" | |
| 47 "sub %1, %2, %1, lsr #24 \n" | |
| 48 "mov %2, %3, lsl #8 \n" | |
| 49 "orr %2, %2, #0x80000000 \n" | |
| 50 "mov %1, %2, lsr %1 \n" | |
| 51 "it cs \n" | |
| 52 "rsbcs %1, %1, #0 \n" | |
| 53 : "=r"(x), "=&r"(y), "=&r"(z) | |
| 54 : "r"(x) | |
| 55 : "cc" | |
| 56 ); | |
| 57 return y; | |
| 58 } | |
| 59 inline SkFixed SkFixedMul_arm(SkFixed x, SkFixed y) | |
| 60 { | |
| 61 int32_t t; | |
| 62 asm("smull %0, %2, %1, %3 \n" | |
| 63 "mov %0, %0, lsr #16 \n" | |
| 64 "orr %0, %0, %2, lsl #16 \n" | |
| 65 : "=r"(x), "=&r"(y), "=r"(t) | |
| 66 : "r"(x), "1"(y) | |
| 67 : | |
| 68 ); | |
| 69 return x; | |
| 70 } | |
| 71 | |
| 72 #define SkFixedMul(x, y) SkFixedMul_arm(x, y) | |
| 73 #define SkFloatToFixed_Unsafe(x) SkFloatToFixed_arm(x) | |
| 74 #else | |
| 75 inline SkFixed SkFixedMul_longlong(SkFixed a, SkFixed b) { | |
| 76 return (SkFixed)((int64_t)a * b >> 16); | |
| 77 } | |
| 78 | |
| 79 #define SkFixedMul(x, y) SkFixedMul_longlong(x, y) | |
| 80 #define SkFloatToFixed_Unsafe(x) ((SkFixed)((x) * SK_Fixed1)) | |
| 81 #endif | |
| 82 | |
| 83 /////////////////////////////////////////////////////////////////////////////// | |
| 84 | |
| 85 static inline SkFixed SkFloatToFixed(float x) { | |
| 86 const SkFixed result = SkFloatToFixed_Unsafe(x); | |
| 87 SkASSERT(truncf(x * SK_Fixed1) == static_cast<float>(result)); | |
| 88 return result; | |
| 89 } | |
| 34 | 90 |
| 35 // Pins over/under flows to SK_FixedMax/SK_FixedMin (slower than just a cast). | 91 // Pins over/under flows to SK_FixedMax/SK_FixedMin (slower than just a cast). |
| 36 static inline SkFixed SkFloatPinToFixed(float x) { | 92 static inline SkFixed SkFloatPinToFixed(float x) { |
| 37 x *= SK_Fixed1; | 93 x *= SK_Fixed1; |
| 38 // Casting float to int outside the range of the target type (int32_t) is un defined behavior. | 94 // 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; | 95 if (x >= SK_FixedMax) return SK_FixedMax; |
| 40 if (x <= SK_FixedMin) return SK_FixedMin; | 96 if (x <= SK_FixedMin) return SK_FixedMin; |
| 41 const SkFixed result = static_cast<SkFixed>(x); | 97 const SkFixed result = static_cast<SkFixed>(x); |
| 42 SkASSERT(truncf(x) == static_cast<float>(result)); | 98 SkASSERT(truncf(x) == static_cast<float>(result)); |
| 43 return result; | 99 return result; |
| 44 } | 100 } |
| 45 | 101 |
| 46 #ifdef SK_DEBUG | 102 #define SkFixedToDouble(x) ((x) * 1.52587890625e-5) |
| 47 static inline SkFixed SkFloatToFixed_Check(float x) { | 103 #define SkDoubleToFixed_Unsafe(x) ((SkFixed)((x) * SK_Fixed1)) |
| 48 int64_t n64 = (int64_t)(x * SK_Fixed1); | |
| 49 SkFixed n32 = (SkFixed)n64; | |
| 50 SkASSERT(n64 == n32); | |
| 51 return n32; | |
| 52 } | |
| 53 #else | |
| 54 #define SkFloatToFixed_Check(x) SkFloatToFixed(x) | |
| 55 #endif | |
| 56 | 104 |
| 57 #define SkFixedToDouble(x) ((x) * 1.52587890625e-5) | 105 static inline SkFixed SkDoubleToFixed(double x) { |
| 58 #define SkDoubleToFixed(x) ((SkFixed)((x) * SK_Fixed1)) | 106 const SkFixed result = SkDoubleToFixed_Unsafe(x); |
| 107 SkASSERT(trunc(x * SK_Fixed1) == static_cast<double>(result)); | |
| 108 return result; | |
| 109 } | |
| 59 | 110 |
| 60 // Pins over/under flows to SK_FixedMax/SK_FixedMin (slower than just a cast). | 111 // Pins over/under flows to SK_FixedMax/SK_FixedMin (slower than just a cast). |
| 61 static inline SkFixed SkDoublePinToFixed(double x) { | 112 static inline SkFixed SkDoublePinToFixed(double x) { |
| 62 x *= SK_Fixed1; | 113 x *= SK_Fixed1; |
| 63 // Casting double to int outside the range of the target type (int32_t) is u ndefined behavior. | 114 // 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; | 115 if (x >= SK_FixedMax) return SK_FixedMax; |
| 65 if (x <= SK_FixedMin) return SK_FixedMin; | 116 if (x <= SK_FixedMin) return SK_FixedMin; |
| 66 const SkFixed result = static_cast<SkFixed>(x); | 117 const SkFixed result = static_cast<SkFixed>(x); |
| 67 SkASSERT(trunc(x) == static_cast<double>(result)); | 118 SkASSERT(trunc(x) == static_cast<double>(result)); |
| 68 return result; | 119 return result; |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 99 | 150 |
| 100 // Blink layout tests are baselined to Clang optimizing through undefined behavi or in SkDivBits. | 151 // Blink layout tests are baselined to Clang optimizing through undefined behavi or in SkDivBits. |
| 101 #if defined(SK_SUPPORT_LEGACY_DIVBITS_UB) | 152 #if defined(SK_SUPPORT_LEGACY_DIVBITS_UB) |
| 102 #define SkFixedDiv(numer, denom) SkDivBits(numer, denom, 16) | 153 #define SkFixedDiv(numer, denom) SkDivBits(numer, denom, 16) |
| 103 #else | 154 #else |
| 104 // The divide may exceed 32 bits. Clamp to a signed 32 bit result. | 155 // The divide may exceed 32 bits. Clamp to a signed 32 bit result. |
| 105 #define SkFixedDiv(numer, denom) \ | 156 #define SkFixedDiv(numer, denom) \ |
| 106 SkToS32(SkTPin<int64_t>((SkLeftShift((int64_t)numer, 16) / denom), SK_Mi nS32, SK_MaxS32)) | 157 SkToS32(SkTPin<int64_t>((SkLeftShift((int64_t)numer, 16) / denom), SK_Mi nS32, SK_MaxS32)) |
| 107 #endif | 158 #endif |
| 108 | 159 |
| 109 //////////////////////////////////////////////////////////////////////////////// ////////////////////// | |
| 110 // Now look for ASM overrides for our portable versions (should consider putting this in its own file) | |
| 111 | |
| 112 inline SkFixed SkFixedMul_longlong(SkFixed a, SkFixed b) { | |
| 113 return (SkFixed)((int64_t)a * b >> 16); | |
| 114 } | |
| 115 #define SkFixedMul(a,b) SkFixedMul_longlong(a,b) | |
| 116 | |
| 117 | |
| 118 #if defined(SK_CPU_ARM32) | |
| 119 /* This guy does not handle NaN or other obscurities, but is faster than | |
| 120 than (int)(x*65536). When built on Android with -Os, needs forcing | |
| 121 to inline or we lose the speed benefit. | |
| 122 */ | |
| 123 SK_ALWAYS_INLINE SkFixed SkFloatToFixed_arm(float x) | |
| 124 { | |
| 125 int32_t y, z; | |
| 126 asm("movs %1, %3, lsl #1 \n" | |
| 127 "mov %2, #0x8E \n" | |
| 128 "sub %1, %2, %1, lsr #24 \n" | |
| 129 "mov %2, %3, lsl #8 \n" | |
| 130 "orr %2, %2, #0x80000000 \n" | |
| 131 "mov %1, %2, lsr %1 \n" | |
| 132 "it cs \n" | |
| 133 "rsbcs %1, %1, #0 \n" | |
| 134 : "=r"(x), "=&r"(y), "=&r"(z) | |
| 135 : "r"(x) | |
| 136 : "cc" | |
| 137 ); | |
| 138 return y; | |
| 139 } | |
| 140 inline SkFixed SkFixedMul_arm(SkFixed x, SkFixed y) | |
| 141 { | |
| 142 int32_t t; | |
| 143 asm("smull %0, %2, %1, %3 \n" | |
| 144 "mov %0, %0, lsr #16 \n" | |
| 145 "orr %0, %0, %2, lsl #16 \n" | |
| 146 : "=r"(x), "=&r"(y), "=r"(t) | |
| 147 : "r"(x), "1"(y) | |
| 148 : | |
| 149 ); | |
| 150 return x; | |
| 151 } | |
| 152 #undef SkFixedMul | |
| 153 #define SkFixedMul(x, y) SkFixedMul_arm(x, y) | |
| 154 | |
| 155 #undef SkFloatToFixed | |
| 156 #define SkFloatToFixed(x) SkFloatToFixed_arm(x) | |
| 157 #endif | |
| 158 | |
| 159 /////////////////////////////////////////////////////////////////////////////// | 160 /////////////////////////////////////////////////////////////////////////////// |
| 160 | 161 |
| 161 typedef int64_t SkFixed3232; // 32.32 | 162 typedef int64_t SkFixed3232; // 32.32 |
| 162 | 163 |
| 163 #define SkIntToFixed3232(x) (SkLeftShift((SkFixed3232)(x), 32)) | 164 #define SK_Fixed3232_1 (static_cast<SkFixed3232>(1) << 32) |
| 164 #define SkFixed3232ToInt(x) ((int)((x) >> 32)) | 165 #define SkIntToFixed3232(x) (SkLeftShift((SkFixed3232)(x), 32)) |
| 165 #define SkFixedToFixed3232(x) (SkLeftShift((SkFixed3232)(x), 16)) | 166 #define SkFixed3232ToInt(x) ((int)((x) >> 32)) |
| 166 #define SkFixed3232ToFixed(x) ((SkFixed)((x) >> 16)) | 167 #define SkFixedToFixed3232(x) (SkLeftShift((SkFixed3232)(x), 16)) |
| 167 #define SkFloatToFixed3232(x) ((SkFixed3232)((x) * (65536.0f * 65536.0f))) | 168 #define SkFixed3232ToFixed(x) ((SkFixed)((x) >> 16)) |
| 169 #define SkFloatToFixed3232_Unsafe(x) (static_cast<SkFixed3232>((x) * SK_Fixed32 32_1)) | |
| 170 | |
| 171 static inline SkFixed3232 SkFloatToFixed3232(float x) { | |
| 172 const SkFixed3232 result = SkFloatToFixed3232_Unsafe(x); | |
| 173 SkASSERT(truncf(x * SK_Fixed3232_1) == static_cast<float>(result)); | |
| 174 return result; | |
| 175 } | |
| 168 | 176 |
| 169 #define SkScalarToFixed3232(x) SkFloatToFixed3232(x) | 177 #define SkScalarToFixed3232(x) SkFloatToFixed3232(x) |
| 170 | 178 |
| 171 /////////////////////////////////////////////////////////////////////////////// | 179 /////////////////////////////////////////////////////////////////////////////// |
| 172 | 180 |
| 173 // 64bits wide, with a 16bit bias. Useful when accumulating lots of 16.16 so | 181 // 64bits wide, with a 16bit bias. Useful when accumulating lots of 16.16 so |
| 174 // we don't overflow along the way | 182 // we don't overflow along the way |
| 175 typedef int64_t Sk48Dot16; | 183 typedef int64_t Sk48Dot16; |
| 176 | 184 |
| 177 #define Sk48Dot16FloorToInt(x) static_cast<int>((x) >> 16) | 185 #define Sk48Dot16FloorToInt(x) static_cast<int>((x) >> 16) |
| 178 | 186 |
| 179 static inline float Sk48Dot16ToScalar(Sk48Dot16 x) { | 187 static inline float Sk48Dot16ToScalar(Sk48Dot16 x) { |
| 180 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)) |
| 181 } | 189 } |
| 182 #define SkFloatTo48Dot16(x) (static_cast<Sk48Dot16>((x) * (1 << 16))) | 190 #define SkFloatTo48Dot16(x) (static_cast<Sk48Dot16>((x) * (1 << 16))) |
| 183 | 191 |
| 184 #define SkScalarTo48Dot16(x) SkFloatTo48Dot16(x) | 192 #define SkScalarTo48Dot16(x) SkFloatTo48Dot16(x) |
| 185 | 193 |
| 186 #endif | 194 #endif |
| OLD | NEW |