Skia
2DGraphicsLibrary
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SkMath.h
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 
20 static inline bool sk_64_isS32(int64_t value) {
21  return (int32_t)value == value;
22 }
23 
29 static 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;
37 static inline int64_t sk_64_mul(int64_t a, int64_t b) {
38  return a * b;
39 }
40 
42 
48 static 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 
58 int32_t SkSqrtBits(int32_t value, int bitBias);
59 
62 #define SkSqrt32(n) SkSqrtBits(n, 15)
63 
67 static inline int SkClampPos(int value) {
68  return value & ~(value >> 31);
69 }
70 
77 static inline int SkClampMax(int value, int max) {
78  // ensure that max is positive
79  SkASSERT(max >= 0);
80  if (value < 0) {
81  value = 0;
82  }
83  if (value > max) {
84  value = max;
85  }
86  return value;
87 }
88 
93 template <typename T> constexpr inline bool SkIsPow2(T value) {
94  return (value & (value - 1)) == 0;
95 }
96 
98 
103 static inline unsigned SkMul16ShiftRound(U16CPU a, U16CPU b, int shift) {
104  SkASSERT(a <= 32767);
105  SkASSERT(b <= 32767);
106  SkASSERT(shift > 0 && shift <= 8);
107  unsigned prod = a*b + (1 << (shift - 1));
108  return (prod + (prod >> shift)) >> shift;
109 }
110 
115 static inline U8CPU SkMulDiv255Round(U16CPU a, U16CPU b) {
116  SkASSERT(a <= 32767);
117  SkASSERT(b <= 32767);
118  unsigned prod = a*b + 128;
119  return (prod + (prod >> 8)) >> 8;
120 }
121 
125 template <typename In, typename Out>
126 inline void SkTDivMod(In numer, In denom, Out* div, Out* mod) {
127 #ifdef SK_CPU_ARM32
128  // 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);
141 #endif
142 }
143 
144 #endif
unsigned U16CPU
Fast type for unsigned 16 bits.
Definition: SkTypes.h:203
unsigned U8CPU
Fast type for unsigned 8 bits.
Definition: SkTypes.h:191