Skia
2D Graphics Library
SkMath.h
Go to the documentation of this file.
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;
18 static inline int64_t sk_64_mul(int64_t a, int64_t b) {
19  return a * b;
20 }
21 
23 
28 template <typename T> constexpr inline bool SkIsPow2(T value) {
29  return (value & (value - 1)) == 0;
30 }
31 
33 
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 
50 static inline U8CPU SkMulDiv255Round(U16CPU a, U16CPU b) {
51  return SkMul16ShiftRound(a,b,8);
52 }
53 
54 #endif
SkTypes.h
SkMul16ShiftRound
static unsigned SkMul16ShiftRound(U16CPU a, U16CPU b, int shift)
Return a*b/((1 << shift) - 1), rounding any fractional bits.
Definition: SkMath.h:38
SkASSERT
#define SkASSERT(cond)
Definition: SkTypes.h:468
SkIsPow2
constexpr bool SkIsPow2(T value)
Returns true if value is a power of 2.
Definition: SkMath.h:28
sk_64_mul
static int64_t sk_64_mul(int64_t a, int64_t b)
Definition: SkMath.h:18
U16CPU
unsigned U16CPU
Fast type for unsigned 16 bits.
Definition: SkTypes.h:501
SkMulDiv255Round
static U8CPU SkMulDiv255Round(U16CPU a, U16CPU b)
Return a*b/255, rounding any fractional bits.
Definition: SkMath.h:50
U8CPU
unsigned U8CPU
Fast type for unsigned 8 bits.
Definition: SkTypes.h:496