• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2006 The Android Open Source Project
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SkMath_DEFINED
9 #define SkMath_DEFINED
10 
11 #include "include/core/SkTypes.h"
12 
13 // 64bit -> 32bit utilities
14 
15 // Handy util that can be passed two ints, and will automatically promote to
16 // 64bits before the multiply, so the caller doesn't have to remember to cast
17 // e.g. (int64_t)a * b;
sk_64_mul(int64_t a,int64_t b)18 static inline int64_t sk_64_mul(int64_t a, int64_t b) {
19     return a * b;
20 }
21 
22 ///////////////////////////////////////////////////////////////////////////////
23 
24 /**
25  *  Returns true if value is a power of 2. Does not explicitly check for
26  *  value <= 0.
27  */
SkIsPow2(T value)28 template <typename T> constexpr inline bool SkIsPow2(T value) {
29     return (value & (value - 1)) == 0;
30 }
31 
32 ///////////////////////////////////////////////////////////////////////////////
33 
34 /**
35  *  Return a*b/((1 << shift) - 1), rounding any fractional bits.
36  *  Only valid if a and b are unsigned and <= 32767 and shift is > 0 and <= 8
37  */
SkMul16ShiftRound(U16CPU a,U16CPU b,int shift)38 static inline unsigned SkMul16ShiftRound(U16CPU a, U16CPU b, int shift) {
39     SkASSERT(a <= 32767);
40     SkASSERT(b <= 32767);
41     SkASSERT(shift > 0 && shift <= 8);
42     unsigned prod = a*b + (1 << (shift - 1));
43     return (prod + (prod >> shift)) >> shift;
44 }
45 
46 /**
47  *  Return a*b/255, rounding any fractional bits.
48  *  Only valid if a and b are unsigned and <= 32767.
49  */
SkMulDiv255Round(U16CPU a,U16CPU b)50 static inline U8CPU SkMulDiv255Round(U16CPU a, U16CPU b) {
51     return SkMul16ShiftRound(a,b,8);
52 }
53 
54 #endif
55