8#ifndef SkSafeMath_DEFINED
9#define SkSafeMath_DEFINED
26 bool ok()
const {
return fOK; }
27 explicit operator bool()
const {
return fOK; }
29 size_t mul(
size_t x,
size_t y) {
30 return sizeof(size_t) ==
sizeof(uint64_t) ? mul64(
x,
y) : mul32(
x,
y);
33 size_t add(
size_t x,
size_t y) {
55 SkASSERT(alignment && !(alignment & (alignment - 1)));
56 return add(
x, alignment - 1) & ~(alignment - 1);
60 if (!SkTFitsIn<T>(
value)) {
63 return static_cast<T>(
value);
67 static size_t Add(
size_t x,
size_t y);
68 static size_t Mul(
size_t x,
size_t y);
75 uint32_t mul32(uint32_t
x, uint32_t
y) {
84 uint64_t mul64(uint64_t
x, uint64_t
y) {
89 auto hi = [](uint64_t
x) {
return x >> 32; };
90 auto lo = [](uint64_t
x) {
return x & 0xFFFFFFFF; };
92 uint64_t lx_ly = lo(
x) * lo(
y);
93 uint64_t hx_ly = hi(
x) * lo(
y);
94 uint64_t lx_hy = lo(
x) * hi(
y);
95 uint64_t hx_hy = hi(
x) * hi(
y);
98 result = this->
add(result, (lx_hy << 32));
99 fOK &= (hx_hy + (hx_ly >> 32) + (lx_hy >> 32)) == 0;
101 #if defined(SK_DEBUG) && defined(__clang__) && defined(__x86_64__)
102 auto double_check = (
unsigned __int128)
x *
y;
104 SkASSERT(!fOK || (double_check >> 64 == 0));
static size_t Add(size_t x, size_t y)
size_t add(size_t x, size_t y)
size_t alignUp(size_t x, size_t alignment)
static size_t Align4(size_t x)
static size_t Mul(size_t x, size_t y)
size_t mul(size_t x, size_t y)
static float max(float r, float g, float b)
static float min(float r, float g, float b)