OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2006 The Android Open Source Project | 3 * Copyright 2006 The Android Open Source Project |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #ifndef SkMath_DEFINED | 10 #ifndef SkMath_DEFINED |
11 #define SkMath_DEFINED | 11 #define SkMath_DEFINED |
12 | 12 |
13 #include "SkTypes.h" | 13 #include "SkTypes.h" |
14 | 14 |
15 /** | |
16 * Computes numer1 * numer2 / denom in full 64 intermediate precision. | |
17 * It is an error for denom to be 0. There is no special handling if | |
18 * the result overflows 32bits. | |
19 */ | |
20 int32_t SkMulDiv(int32_t numer1, int32_t numer2, int32_t denom); | |
21 | |
22 /** | |
23 * Computes (numer1 << shift) / denom in full 64 intermediate precision. | |
24 * It is an error for denom to be 0. There is no special handling if | |
25 * the result overflows 32bits. | |
26 */ | |
27 int32_t SkDivBits(int32_t numer, int32_t denom, int shift); | |
28 | |
29 /** | |
30 * Return the integer square root of value, with a bias of bitBias | |
31 */ | |
32 int32_t SkSqrtBits(int32_t value, int bitBias); | |
33 | |
34 /** Return the integer square root of n, treated as a SkFixed (16.16) | |
35 */ | |
36 #define SkSqrt32(n) SkSqrtBits(n, 15) | |
37 | |
38 // 64bit -> 32bit utilities | 15 // 64bit -> 32bit utilities |
39 | 16 |
40 /** | 17 /** |
41 * Return true iff the 64bit value can exactly be represented in signed 32bits | 18 * Return true iff the 64bit value can exactly be represented in signed 32bits |
42 */ | 19 */ |
43 static inline bool sk_64_isS32(int64_t value) { | 20 static inline bool sk_64_isS32(int64_t value) { |
44 return (int32_t)value == value; | 21 return (int32_t)value == value; |
45 } | 22 } |
46 | 23 |
47 /** | 24 /** |
48 * Return the 64bit argument as signed 32bits, asserting in debug that the arg | 25 * Return the 64bit argument as signed 32bits, asserting in debug that the arg |
49 * exactly fits in signed 32bits. In the release build, no checks are preformed | 26 * exactly fits in signed 32bits. In the release build, no checks are preformed |
50 * and the return value if the arg does not fit is undefined. | 27 * and the return value if the arg does not fit is undefined. |
51 */ | 28 */ |
52 static inline int32_t sk_64_asS32(int64_t value) { | 29 static inline int32_t sk_64_asS32(int64_t value) { |
53 SkASSERT(sk_64_isS32(value)); | 30 SkASSERT(sk_64_isS32(value)); |
54 return (int32_t)value; | 31 return (int32_t)value; |
55 } | 32 } |
56 | 33 |
57 // Handy util that can be passed two ints, and will automatically promote to | 34 // Handy util that can be passed two ints, and will automatically promote to |
58 // 64bits before the multiply, so the caller doesn't have to remember to cast | 35 // 64bits before the multiply, so the caller doesn't have to remember to cast |
59 // e.g. (int64_t)a * b; | 36 // e.g. (int64_t)a * b; |
60 static inline int64_t sk_64_mul(int64_t a, int64_t b) { | 37 static inline int64_t sk_64_mul(int64_t a, int64_t b) { |
61 return a * b; | 38 return a * b; |
62 } | 39 } |
63 | 40 |
64 /////////////////////////////////////////////////////////////////////////////// | 41 /////////////////////////////////////////////////////////////////////////////// |
65 | 42 |
| 43 /** |
| 44 * Computes numer1 * numer2 / denom in full 64 intermediate precision. |
| 45 * It is an error for denom to be 0. There is no special handling if |
| 46 * the result overflows 32bits. |
| 47 */ |
| 48 static inline int32_t SkMulDiv(int32_t numer1, int32_t numer2, int32_t denom) { |
| 49 SkASSERT(denom); |
| 50 |
| 51 int64_t tmp = sk_64_mul(numer1, numer2) / denom; |
| 52 return sk_64_asS32(tmp); |
| 53 } |
| 54 |
| 55 /** |
| 56 * Computes (numer1 << shift) / denom in full 64 intermediate precision. |
| 57 * It is an error for denom to be 0. There is no special handling if |
| 58 * the result overflows 32bits. |
| 59 */ |
| 60 int32_t SkDivBits(int32_t numer, int32_t denom, int shift); |
| 61 |
| 62 /** |
| 63 * Return the integer square root of value, with a bias of bitBias |
| 64 */ |
| 65 int32_t SkSqrtBits(int32_t value, int bitBias); |
| 66 |
| 67 /** Return the integer square root of n, treated as a SkFixed (16.16) |
| 68 */ |
| 69 #define SkSqrt32(n) SkSqrtBits(n, 15) |
| 70 |
66 //! Returns the number of leading zero bits (0...32) | 71 //! Returns the number of leading zero bits (0...32) |
67 int SkCLZ_portable(uint32_t); | 72 int SkCLZ_portable(uint32_t); |
68 | 73 |
69 #ifndef SkCLZ | 74 #ifndef SkCLZ |
70 #if defined(_MSC_VER) && _MSC_VER >= 1400 | 75 #if defined(_MSC_VER) && _MSC_VER >= 1400 |
71 #include <intrin.h> | 76 #include <intrin.h> |
72 | 77 |
73 static inline int SkCLZ(uint32_t mask) { | 78 static inline int SkCLZ(uint32_t mask) { |
74 if (mask) { | 79 if (mask) { |
75 DWORD index; | 80 DWORD index; |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 *div = static_cast<Out>(d); | 220 *div = static_cast<Out>(d); |
216 *mod = static_cast<Out>(numer-d*denom); | 221 *mod = static_cast<Out>(numer-d*denom); |
217 #else | 222 #else |
218 // On x86 this will just be a single idiv. | 223 // On x86 this will just be a single idiv. |
219 *div = static_cast<Out>(numer/denom); | 224 *div = static_cast<Out>(numer/denom); |
220 *mod = static_cast<Out>(numer%denom); | 225 *mod = static_cast<Out>(numer%denom); |
221 #endif // SK_CPU_ARM | 226 #endif // SK_CPU_ARM |
222 } | 227 } |
223 | 228 |
224 #endif | 229 #endif |
OLD | NEW |