OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
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 SkMathPriv_DEFINED | 8 #ifndef SkMathPriv_DEFINED |
9 #define SkMathPriv_DEFINED | 9 #define SkMathPriv_DEFINED |
10 | 10 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 } | 50 } |
51 | 51 |
52 /////////////////////////////////////////////////////////////////////////////// | 52 /////////////////////////////////////////////////////////////////////////////// |
53 | 53 |
54 /** Return a*b/255, truncating away any fractional bits. Only valid if both | 54 /** Return a*b/255, truncating away any fractional bits. Only valid if both |
55 a and b are 0..255 | 55 a and b are 0..255 |
56 */ | 56 */ |
57 static inline U8CPU SkMulDiv255Trunc(U8CPU a, U8CPU b) { | 57 static inline U8CPU SkMulDiv255Trunc(U8CPU a, U8CPU b) { |
58 SkASSERT((uint8_t)a == a); | 58 SkASSERT((uint8_t)a == a); |
59 SkASSERT((uint8_t)b == b); | 59 SkASSERT((uint8_t)b == b); |
60 unsigned prod = SkMulS16(a, b) + 1; | 60 unsigned prod = a*b + 1; |
61 return (prod + (prod >> 8)) >> 8; | 61 return (prod + (prod >> 8)) >> 8; |
62 } | 62 } |
63 | 63 |
64 /** Return (a*b)/255, taking the ceiling of any fractional bits. Only valid if | 64 /** Return (a*b)/255, taking the ceiling of any fractional bits. Only valid if |
65 both a and b are 0..255. The expected result equals (a * b + 254) / 255. | 65 both a and b are 0..255. The expected result equals (a * b + 254) / 255. |
66 */ | 66 */ |
67 static inline U8CPU SkMulDiv255Ceiling(U8CPU a, U8CPU b) { | 67 static inline U8CPU SkMulDiv255Ceiling(U8CPU a, U8CPU b) { |
68 SkASSERT((uint8_t)a == a); | 68 SkASSERT((uint8_t)a == a); |
69 SkASSERT((uint8_t)b == b); | 69 SkASSERT((uint8_t)b == b); |
70 unsigned prod = SkMulS16(a, b) + 255; | 70 unsigned prod = a*b + 255; |
71 return (prod + (prod >> 8)) >> 8; | 71 return (prod + (prod >> 8)) >> 8; |
72 } | 72 } |
73 | 73 |
74 /** Just the rounding step in SkDiv255Round: round(value / 255) | 74 /** Just the rounding step in SkDiv255Round: round(value / 255) |
75 */ | 75 */ |
76 static inline unsigned SkDiv255Round(unsigned prod) { | 76 static inline unsigned SkDiv255Round(unsigned prod) { |
77 prod += 128; | 77 prod += 128; |
78 return (prod + (prod >> 8)) >> 8; | 78 return (prod + (prod >> 8)) >> 8; |
79 } | 79 } |
80 | 80 |
81 #endif | 81 #endif |
OLD | NEW |