2008-12-17 15:59:43 +00:00
|
|
|
/*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Copyright 2006 The Android Open Source Project
|
2008-12-17 15:59:43 +00:00
|
|
|
*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkMath_DEFINED
|
|
|
|
#define SkMath_DEFINED
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkTypes.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2013-12-30 14:40:38 +00:00
|
|
|
// 64bit -> 32bit utilities
|
|
|
|
|
|
|
|
// Handy util that can be passed two ints, and will automatically promote to
|
|
|
|
// 64bits before the multiply, so the caller doesn't have to remember to cast
|
|
|
|
// e.g. (int64_t)a * b;
|
|
|
|
static inline int64_t sk_64_mul(int64_t a, int64_t b) {
|
|
|
|
return a * b;
|
|
|
|
}
|
|
|
|
|
2012-08-07 21:35:13 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2012-08-07 21:35:13 +00:00
|
|
|
/**
|
|
|
|
* Returns true if value is a power of 2. Does not explicitly check for
|
|
|
|
* value <= 0.
|
2010-12-20 18:26:13 +00:00
|
|
|
*/
|
2016-08-16 16:36:18 +00:00
|
|
|
template <typename T> constexpr inline bool SkIsPow2(T value) {
|
2010-12-20 18:26:13 +00:00
|
|
|
return (value & (value - 1)) == 0;
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-08-07 21:35:13 +00:00
|
|
|
/**
|
|
|
|
* Return a*b/((1 << shift) - 1), rounding any fractional bits.
|
|
|
|
* Only valid if a and b are unsigned and <= 32767 and shift is > 0 and <= 8
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
2013-04-22 20:21:56 +00:00
|
|
|
static inline unsigned SkMul16ShiftRound(U16CPU a, U16CPU b, int shift) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkASSERT(a <= 32767);
|
|
|
|
SkASSERT(b <= 32767);
|
|
|
|
SkASSERT(shift > 0 && shift <= 8);
|
2015-08-07 15:48:12 +00:00
|
|
|
unsigned prod = a*b + (1 << (shift - 1));
|
2008-12-17 15:59:43 +00:00
|
|
|
return (prod + (prod >> shift)) >> shift;
|
|
|
|
}
|
|
|
|
|
2012-08-07 21:35:13 +00:00
|
|
|
/**
|
2013-04-22 20:21:56 +00:00
|
|
|
* Return a*b/255, rounding any fractional bits.
|
|
|
|
* Only valid if a and b are unsigned and <= 32767.
|
2009-06-22 17:38:10 +00:00
|
|
|
*/
|
2013-04-22 20:21:56 +00:00
|
|
|
static inline U8CPU SkMulDiv255Round(U16CPU a, U16CPU b) {
|
|
|
|
SkASSERT(a <= 32767);
|
|
|
|
SkASSERT(b <= 32767);
|
2015-08-07 15:48:12 +00:00
|
|
|
unsigned prod = a*b + 128;
|
2009-06-22 17:38:10 +00:00
|
|
|
return (prod + (prod >> 8)) >> 8;
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#endif
|