SkMath.h revision d77ac7792405814fac3fdf229dea348dede650f1
1
2/*
3 * Copyright 2006 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10#ifndef SkMath_DEFINED
11#define SkMath_DEFINED
12
13#include "SkTypes.h"
14
15// 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
41///////////////////////////////////////////////////////////////////////////////
42
43/**
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);
50
51    int64_t tmp = sk_64_mul(numer1, numer2) / denom;
52    return sk_64_asS32(tmp);
53}
54
55/**
56 *  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
64//! Returns the number of leading zero bits (0...32)
65int SkCLZ_portable(uint32_t);
66
67#ifndef SkCLZ
68    #if defined(_MSC_VER)
69        #include <intrin.h>
70
71        static inline int SkCLZ(uint32_t mask) {
72            if (mask) {
73                DWORD index;
74                _BitScanReverse(&index, mask);
75                // Suppress this bogus /analyze warning. The check for non-zero
76                // guarantees that _BitScanReverse will succeed.
77#pragma warning(suppress : 6102) // Using 'index' from failed function call
78                return index ^ 0x1F;
79            } else {
80                return 32;
81            }
82        }
83    #elif defined(SK_CPU_ARM32) || defined(__GNUC__) || defined(__clang__)
84        static inline int SkCLZ(uint32_t mask) {
85            // __builtin_clz(0) is undefined, so we have to detect that case.
86            return mask ? __builtin_clz(mask) : 32;
87        }
88    #else
89        #define SkCLZ(x)    SkCLZ_portable(x)
90    #endif
91#endif
92
93/**
94 *  Returns (value < 0 ? 0 : value) efficiently (i.e. no compares or branches)
95 */
96static inline int SkClampPos(int value) {
97    return value & ~(value >> 31);
98}
99
100/** Given an integer and a positive (max) integer, return the value
101 *  pinned against 0 and max, inclusive.
102 *  @param value    The value we want returned pinned between [0...max]
103 *  @param max      The positive max value
104 *  @return 0 if value < 0, max if value > max, else value
105 */
106static inline int SkClampMax(int value, int max) {
107    // ensure that max is positive
108    SkASSERT(max >= 0);
109    if (value < 0) {
110        value = 0;
111    }
112    if (value > max) {
113        value = max;
114    }
115    return value;
116}
117
118/**
119 *  Returns the smallest power-of-2 that is >= the specified value. If value
120 *  is already a power of 2, then it is returned unchanged. It is undefined
121 *  if value is <= 0.
122 */
123static inline int SkNextPow2(int value) {
124    SkASSERT(value > 0);
125    return 1 << (32 - SkCLZ(value - 1));
126}
127
128/**
129 *  Returns the log2 of the specified value, were that value to be rounded up
130 *  to the next power of 2. It is undefined to pass 0. Examples:
131 *  SkNextLog2(1) -> 0
132 *  SkNextLog2(2) -> 1
133 *  SkNextLog2(3) -> 2
134 *  SkNextLog2(4) -> 2
135 *  SkNextLog2(5) -> 3
136 */
137static inline int SkNextLog2(uint32_t value) {
138    SkASSERT(value != 0);
139    return 32 - SkCLZ(value - 1);
140}
141
142/**
143 *  Returns true if value is a power of 2. Does not explicitly check for
144 *  value <= 0.
145 */
146template <typename T> inline bool SkIsPow2(T value) {
147    return (value & (value - 1)) == 0;
148}
149
150///////////////////////////////////////////////////////////////////////////////
151
152/**
153 *  Return a*b/((1 << shift) - 1), rounding any fractional bits.
154 *  Only valid if a and b are unsigned and <= 32767 and shift is > 0 and <= 8
155 */
156static inline unsigned SkMul16ShiftRound(U16CPU a, U16CPU b, int shift) {
157    SkASSERT(a <= 32767);
158    SkASSERT(b <= 32767);
159    SkASSERT(shift > 0 && shift <= 8);
160    unsigned prod = a*b + (1 << (shift - 1));
161    return (prod + (prod >> shift)) >> shift;
162}
163
164/**
165 *  Return a*b/255, rounding any fractional bits.
166 *  Only valid if a and b are unsigned and <= 32767.
167 */
168static inline U8CPU SkMulDiv255Round(U16CPU a, U16CPU b) {
169    SkASSERT(a <= 32767);
170    SkASSERT(b <= 32767);
171    unsigned prod = a*b + 128;
172    return (prod + (prod >> 8)) >> 8;
173}
174
175/**
176 * Stores numer/denom and numer%denom into div and mod respectively.
177 */
178template <typename In, typename Out>
179inline void SkTDivMod(In numer, In denom, Out* div, Out* mod) {
180#ifdef SK_CPU_ARM32
181    // If we wrote this as in the else branch, GCC won't fuse the two into one
182    // divmod call, but rather a div call followed by a divmod.  Silly!  This
183    // version is just as fast as calling __aeabi_[u]idivmod manually, but with
184    // prettier code.
185    //
186    // This benches as around 2x faster than the code in the else branch.
187    const In d = numer/denom;
188    *div = static_cast<Out>(d);
189    *mod = static_cast<Out>(numer-d*denom);
190#else
191    // On x86 this will just be a single idiv.
192    *div = static_cast<Out>(numer/denom);
193    *mod = static_cast<Out>(numer%denom);
194#endif
195}
196
197#endif
198