SkMathPriv.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright 2012 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #ifndef SkMathPriv_DEFINED
  8. #define SkMathPriv_DEFINED
  9. #include "include/core/SkMath.h"
  10. /**
  11. * Return the integer square root of value, with a bias of bitBias
  12. */
  13. int32_t SkSqrtBits(int32_t value, int bitBias);
  14. /** Return the integer square root of n, treated as a SkFixed (16.16)
  15. */
  16. static inline int32_t SkSqrt32(int32_t n) { return SkSqrtBits(n, 15); }
  17. /**
  18. * Returns (value < 0 ? 0 : value) efficiently (i.e. no compares or branches)
  19. */
  20. static inline int SkClampPos(int value) {
  21. return value & ~(value >> 31);
  22. }
  23. /**
  24. * Stores numer/denom and numer%denom into div and mod respectively.
  25. */
  26. template <typename In, typename Out>
  27. inline void SkTDivMod(In numer, In denom, Out* div, Out* mod) {
  28. #ifdef SK_CPU_ARM32
  29. // If we wrote this as in the else branch, GCC won't fuse the two into one
  30. // divmod call, but rather a div call followed by a divmod. Silly! This
  31. // version is just as fast as calling __aeabi_[u]idivmod manually, but with
  32. // prettier code.
  33. //
  34. // This benches as around 2x faster than the code in the else branch.
  35. const In d = numer/denom;
  36. *div = static_cast<Out>(d);
  37. *mod = static_cast<Out>(numer-d*denom);
  38. #else
  39. // On x86 this will just be a single idiv.
  40. *div = static_cast<Out>(numer/denom);
  41. *mod = static_cast<Out>(numer%denom);
  42. #endif
  43. }
  44. /** Returns -1 if n < 0, else returns 0
  45. */
  46. #define SkExtractSign(n) ((int32_t)(n) >> 31)
  47. /** If sign == -1, returns -n, else sign must be 0, and returns n.
  48. Typically used in conjunction with SkExtractSign().
  49. */
  50. static inline int32_t SkApplySign(int32_t n, int32_t sign) {
  51. SkASSERT(sign == 0 || sign == -1);
  52. return (n ^ sign) - sign;
  53. }
  54. /** Return x with the sign of y */
  55. static inline int32_t SkCopySign32(int32_t x, int32_t y) {
  56. return SkApplySign(x, SkExtractSign(x ^ y));
  57. }
  58. /** Given a positive value and a positive max, return the value
  59. pinned against max.
  60. Note: only works as long as max - value doesn't wrap around
  61. @return max if value >= max, else value
  62. */
  63. static inline unsigned SkClampUMax(unsigned value, unsigned max) {
  64. if (value > max) {
  65. value = max;
  66. }
  67. return value;
  68. }
  69. // If a signed int holds min_int (e.g. 0x80000000) it is undefined what happens when
  70. // we negate it (even though we *know* we're 2's complement and we'll get the same
  71. // value back). So we create this helper function that casts to size_t (unsigned) first,
  72. // to avoid the complaint.
  73. static inline size_t sk_negate_to_size_t(int32_t value) {
  74. #if defined(_MSC_VER)
  75. #pragma warning(push)
  76. #pragma warning(disable : 4146) // Thanks MSVC, we know what we're negating an unsigned
  77. #endif
  78. return -static_cast<size_t>(value);
  79. #if defined(_MSC_VER)
  80. #pragma warning(pop)
  81. #endif
  82. }
  83. ///////////////////////////////////////////////////////////////////////////////
  84. /** Return a*b/255, truncating away any fractional bits. Only valid if both
  85. a and b are 0..255
  86. */
  87. static inline U8CPU SkMulDiv255Trunc(U8CPU a, U8CPU b) {
  88. SkASSERT((uint8_t)a == a);
  89. SkASSERT((uint8_t)b == b);
  90. unsigned prod = a*b + 1;
  91. return (prod + (prod >> 8)) >> 8;
  92. }
  93. /** Return (a*b)/255, taking the ceiling of any fractional bits. Only valid if
  94. both a and b are 0..255. The expected result equals (a * b + 254) / 255.
  95. */
  96. static inline U8CPU SkMulDiv255Ceiling(U8CPU a, U8CPU b) {
  97. SkASSERT((uint8_t)a == a);
  98. SkASSERT((uint8_t)b == b);
  99. unsigned prod = a*b + 255;
  100. return (prod + (prod >> 8)) >> 8;
  101. }
  102. /** Just the rounding step in SkDiv255Round: round(value / 255)
  103. */
  104. static inline unsigned SkDiv255Round(unsigned prod) {
  105. prod += 128;
  106. return (prod + (prod >> 8)) >> 8;
  107. }
  108. static inline float SkPinToUnitFloat(float x) {
  109. return SkTMin(SkTMax(x, 0.0f), 1.0f);
  110. }
  111. /**
  112. * Swap byte order of a 4-byte value, e.g. 0xaarrggbb -> 0xbbggrraa.
  113. */
  114. #if defined(_MSC_VER)
  115. #include <stdlib.h>
  116. static inline uint32_t SkBSwap32(uint32_t v) { return _byteswap_ulong(v); }
  117. #else
  118. static inline uint32_t SkBSwap32(uint32_t v) { return __builtin_bswap32(v); }
  119. #endif
  120. //! Returns the number of leading zero bits (0...32)
  121. int SkCLZ_portable(uint32_t);
  122. #ifndef SkCLZ
  123. #if defined(SK_BUILD_FOR_WIN)
  124. #include <intrin.h>
  125. static inline int SkCLZ(uint32_t mask) {
  126. if (mask) {
  127. unsigned long index;
  128. _BitScanReverse(&index, mask);
  129. // Suppress this bogus /analyze warning. The check for non-zero
  130. // guarantees that _BitScanReverse will succeed.
  131. #pragma warning(suppress : 6102) // Using 'index' from failed function call
  132. return index ^ 0x1F;
  133. } else {
  134. return 32;
  135. }
  136. }
  137. #elif defined(SK_CPU_ARM32) || defined(__GNUC__) || defined(__clang__)
  138. static inline int SkCLZ(uint32_t mask) {
  139. // __builtin_clz(0) is undefined, so we have to detect that case.
  140. return mask ? __builtin_clz(mask) : 32;
  141. }
  142. #else
  143. #define SkCLZ(x) SkCLZ_portable(x)
  144. #endif
  145. #endif
  146. /**
  147. * Returns the smallest power-of-2 that is >= the specified value. If value
  148. * is already a power of 2, then it is returned unchanged. It is undefined
  149. * if value is <= 0.
  150. */
  151. static inline int SkNextPow2(int value) {
  152. SkASSERT(value > 0);
  153. return 1 << (32 - SkCLZ(value - 1));
  154. }
  155. /**
  156. * Returns the largest power-of-2 that is <= the specified value. If value
  157. * is already a power of 2, then it is returned unchanged. It is undefined
  158. * if value is <= 0.
  159. */
  160. static inline int SkPrevPow2(int value) {
  161. SkASSERT(value > 0);
  162. return 1 << (32 - SkCLZ(value >> 1));
  163. }
  164. /**
  165. * Returns the log2 of the specified value, were that value to be rounded up
  166. * to the next power of 2. It is undefined to pass 0. Examples:
  167. * SkNextLog2(1) -> 0
  168. * SkNextLog2(2) -> 1
  169. * SkNextLog2(3) -> 2
  170. * SkNextLog2(4) -> 2
  171. * SkNextLog2(5) -> 3
  172. */
  173. static inline int SkNextLog2(uint32_t value) {
  174. SkASSERT(value != 0);
  175. return 32 - SkCLZ(value - 1);
  176. }
  177. /**
  178. * Returns the log2 of the specified value, were that value to be rounded down
  179. * to the previous power of 2. It is undefined to pass 0. Examples:
  180. * SkPrevLog2(1) -> 0
  181. * SkPrevLog2(2) -> 1
  182. * SkPrevLog2(3) -> 1
  183. * SkPrevLog2(4) -> 2
  184. * SkPrevLog2(5) -> 2
  185. */
  186. static inline int SkPrevLog2(uint32_t value) {
  187. SkASSERT(value != 0);
  188. return 32 - SkCLZ(value >> 1);
  189. }
  190. ///////////////////////////////////////////////////////////////////////////////
  191. /**
  192. * Return the smallest power-of-2 >= n.
  193. */
  194. static inline uint32_t GrNextPow2(uint32_t n) {
  195. return n ? (1 << (32 - SkCLZ(n - 1))) : 1;
  196. }
  197. /**
  198. * Returns the next power of 2 >= n or n if the next power of 2 can't be represented by size_t.
  199. */
  200. static inline size_t GrNextSizePow2(size_t n) {
  201. constexpr int kNumSizeTBits = 8 * sizeof(size_t);
  202. constexpr size_t kHighBitSet = size_t(1) << (kNumSizeTBits - 1);
  203. if (!n) {
  204. return 1;
  205. } else if (n >= kHighBitSet) {
  206. return n;
  207. }
  208. n--;
  209. uint32_t shift = 1;
  210. while (shift < kNumSizeTBits) {
  211. n |= n >> shift;
  212. shift <<= 1;
  213. }
  214. return n + 1;
  215. }
  216. // conservative check. will return false for very large values that "could" fit
  217. template <typename T> static inline bool SkFitsInFixed(T x) {
  218. return SkTAbs(x) <= 32767.0f;
  219. }
  220. #endif