blob: 6e252306df072fee76024a186bab921aa6e17941 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com8a1c16f2008-12-17 15:59:43 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#ifndef SkMath_DEFINED
11#define SkMath_DEFINED
12
13#include "SkTypes.h"
14
reed@google.com57212f92013-12-30 14:40:38 +000015// 64bit -> 32bit utilities
16
17/**
18 * Return true iff the 64bit value can exactly be represented in signed 32bits
19 */
20static inline bool sk_64_isS32(int64_t value) {
21 return (int32_t)value == value;
22}
23
24/**
25 * Return the 64bit argument as signed 32bits, asserting in debug that the arg
26 * exactly fits in signed 32bits. In the release build, no checks are preformed
27 * and the return value if the arg does not fit is undefined.
28 */
29static inline int32_t sk_64_asS32(int64_t value) {
30 SkASSERT(sk_64_isS32(value));
31 return (int32_t)value;
32}
33
34// Handy util that can be passed two ints, and will automatically promote to
35// 64bits before the multiply, so the caller doesn't have to remember to cast
36// e.g. (int64_t)a * b;
37static inline int64_t sk_64_mul(int64_t a, int64_t b) {
38 return a * b;
39}
40
reed@google.com4b163ed2012-08-07 21:35:13 +000041///////////////////////////////////////////////////////////////////////////////
42
commit-bot@chromium.org0f1fef82014-04-19 22:12:35 +000043/**
44 * Computes numer1 * numer2 / denom in full 64 intermediate precision.
45 * It is an error for denom to be 0. There is no special handling if
46 * the result overflows 32bits.
47 */
48static inline int32_t SkMulDiv(int32_t numer1, int32_t numer2, int32_t denom) {
49 SkASSERT(denom);
skia.committer@gmail.comf7927dd2014-04-20 03:04:55 +000050
commit-bot@chromium.org0f1fef82014-04-19 22:12:35 +000051 int64_t tmp = sk_64_mul(numer1, numer2) / denom;
52 return sk_64_asS32(tmp);
53}
54
55/**
commit-bot@chromium.org0f1fef82014-04-19 22:12:35 +000056 * Return the integer square root of value, with a bias of bitBias
57 */
58int32_t SkSqrtBits(int32_t value, int bitBias);
59
60/** Return the integer square root of n, treated as a SkFixed (16.16)
61 */
62#define SkSqrt32(n) SkSqrtBits(n, 15)
63
reed@google.com4b163ed2012-08-07 21:35:13 +000064/**
65 * Returns (value < 0 ? 0 : value) efficiently (i.e. no compares or branches)
reed@android.com8a1c16f2008-12-17 15:59:43 +000066 */
reed@android.com8a1c16f2008-12-17 15:59:43 +000067static inline int SkClampPos(int value) {
68 return value & ~(value >> 31);
69}
70
71/** Given an integer and a positive (max) integer, return the value
reed@google.com4b163ed2012-08-07 21:35:13 +000072 * pinned against 0 and max, inclusive.
73 * @param value The value we want returned pinned between [0...max]
74 * @param max The positive max value
75 * @return 0 if value < 0, max if value > max, else value
76 */
reed@android.com8a1c16f2008-12-17 15:59:43 +000077static inline int SkClampMax(int value, int max) {
78 // ensure that max is positive
79 SkASSERT(max >= 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +000080 if (value < 0) {
81 value = 0;
82 }
83 if (value > max) {
84 value = max;
85 }
86 return value;
reed@android.com8a1c16f2008-12-17 15:59:43 +000087}
88
reed@google.com4b163ed2012-08-07 21:35:13 +000089/**
reed@google.com4b163ed2012-08-07 21:35:13 +000090 * Returns true if value is a power of 2. Does not explicitly check for
91 * value <= 0.
reed@android.comf2b98d62010-12-20 18:26:13 +000092 */
mtklein883c8ef2016-08-16 09:36:18 -070093template <typename T> constexpr inline bool SkIsPow2(T value) {
reed@android.comf2b98d62010-12-20 18:26:13 +000094 return (value & (value - 1)) == 0;
95}
96
reed@android.com8a1c16f2008-12-17 15:59:43 +000097///////////////////////////////////////////////////////////////////////////////
98
reed@google.com4b163ed2012-08-07 21:35:13 +000099/**
reed@google.com4b163ed2012-08-07 21:35:13 +0000100 * Return a*b/((1 << shift) - 1), rounding any fractional bits.
101 * Only valid if a and b are unsigned and <= 32767 and shift is > 0 and <= 8
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102 */
reed@google.comea774d22013-04-22 20:21:56 +0000103static inline unsigned SkMul16ShiftRound(U16CPU a, U16CPU b, int shift) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104 SkASSERT(a <= 32767);
105 SkASSERT(b <= 32767);
106 SkASSERT(shift > 0 && shift <= 8);
mtklein38484272015-08-07 08:48:12 -0700107 unsigned prod = a*b + (1 << (shift - 1));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108 return (prod + (prod >> shift)) >> shift;
109}
110
reed@google.com4b163ed2012-08-07 21:35:13 +0000111/**
reed@google.comea774d22013-04-22 20:21:56 +0000112 * Return a*b/255, rounding any fractional bits.
113 * Only valid if a and b are unsigned and <= 32767.
reed@android.coma0f5d152009-06-22 17:38:10 +0000114 */
reed@google.comea774d22013-04-22 20:21:56 +0000115static inline U8CPU SkMulDiv255Round(U16CPU a, U16CPU b) {
116 SkASSERT(a <= 32767);
117 SkASSERT(b <= 32767);
mtklein38484272015-08-07 08:48:12 -0700118 unsigned prod = a*b + 128;
reed@android.coma0f5d152009-06-22 17:38:10 +0000119 return (prod + (prod >> 8)) >> 8;
120}
121
commit-bot@chromium.org2c86fbb2013-09-26 19:22:54 +0000122/**
123 * Stores numer/denom and numer%denom into div and mod respectively.
124 */
125template <typename In, typename Out>
126inline void SkTDivMod(In numer, In denom, Out* div, Out* mod) {
mtklein3a2682a2014-06-03 12:07:31 -0700127#ifdef SK_CPU_ARM32
commit-bot@chromium.org2c86fbb2013-09-26 19:22:54 +0000128 // If we wrote this as in the else branch, GCC won't fuse the two into one
129 // divmod call, but rather a div call followed by a divmod. Silly! This
130 // version is just as fast as calling __aeabi_[u]idivmod manually, but with
131 // prettier code.
132 //
133 // This benches as around 2x faster than the code in the else branch.
134 const In d = numer/denom;
135 *div = static_cast<Out>(d);
136 *mod = static_cast<Out>(numer-d*denom);
137#else
138 // On x86 this will just be a single idiv.
139 *div = static_cast<Out>(numer/denom);
140 *mod = static_cast<Out>(numer%denom);
mtklein3a2682a2014-06-03 12:07:31 -0700141#endif
commit-bot@chromium.org2c86fbb2013-09-26 19:22:54 +0000142}
143
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144#endif