LCOV - code coverage report
Current view: top level - gfx/skia/skia/src/core - SkMathPriv.h (source / functions) Hit Total Coverage
Test: output.info Lines: 5 27 18.5 %
Date: 2017-07-14 16:53:18 Functions: 2 6 33.3 %
Legend: Lines: hit not hit

          Line data    Source code
       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             : 
       8             : #ifndef SkMathPriv_DEFINED
       9             : #define SkMathPriv_DEFINED
      10             : 
      11             : #include "SkMath.h"
      12             : 
      13             : #if defined(SK_BUILD_FOR_IOS) && (defined(SK_BUILD_FOR_ARM32) || defined(SK_BUILD_FOR_ARM64))
      14             : // iOS on ARM starts processes with the Flush-To-Zero (FTZ) and
      15             : // Denormals-Are-Zero (DAZ) bits in the fpscr register set.
      16             : // Algorithms that rely on denormalized numbers need alternative implementations.
      17             : // This can also be controlled in SSE with the MXCSR register,
      18             : // x87 with FSTCW/FLDCW, and mips with FCSR. This should be detected at runtime,
      19             : // or the library built one way or the other more generally (by the build).
      20             : #define SK_CPU_FLUSH_TO_ZERO
      21             : #endif
      22             : 
      23             : /** Returns -1 if n < 0, else returns 0
      24             :  */
      25             : #define SkExtractSign(n)    ((int32_t)(n) >> 31)
      26             : 
      27             : /** If sign == -1, returns -n, else sign must be 0, and returns n.
      28             :  Typically used in conjunction with SkExtractSign().
      29             :  */
      30             : static inline int32_t SkApplySign(int32_t n, int32_t sign) {
      31             :     SkASSERT(sign == 0 || sign == -1);
      32             :     return (n ^ sign) - sign;
      33             : }
      34             : 
      35             : /** Return x with the sign of y */
      36             : static inline int32_t SkCopySign32(int32_t x, int32_t y) {
      37             :     return SkApplySign(x, SkExtractSign(x ^ y));
      38             : }
      39             : 
      40             : /** Given a positive value and a positive max, return the value
      41             :  pinned against max.
      42             :  Note: only works as long as max - value doesn't wrap around
      43             :  @return max if value >= max, else value
      44             :  */
      45             : static inline unsigned SkClampUMax(unsigned value, unsigned max) {
      46             :     if (value > max) {
      47             :         value = max;
      48             :     }
      49             :     return value;
      50             : }
      51             : 
      52             : ///////////////////////////////////////////////////////////////////////////////
      53             : 
      54             : /** Return a*b/255, truncating away any fractional bits. Only valid if both
      55             :  a and b are 0..255
      56             :  */
      57             : static inline U8CPU SkMulDiv255Trunc(U8CPU a, U8CPU b) {
      58             :     SkASSERT((uint8_t)a == a);
      59             :     SkASSERT((uint8_t)b == b);
      60             :     unsigned prod = a*b + 1;
      61             :     return (prod + (prod >> 8)) >> 8;
      62             : }
      63             : 
      64             : /** Return (a*b)/255, taking the ceiling of any fractional bits. Only valid if
      65             :  both a and b are 0..255. The expected result equals (a * b + 254) / 255.
      66             :  */
      67             : static inline U8CPU SkMulDiv255Ceiling(U8CPU a, U8CPU b) {
      68             :     SkASSERT((uint8_t)a == a);
      69             :     SkASSERT((uint8_t)b == b);
      70             :     unsigned prod = a*b + 255;
      71             :     return (prod + (prod >> 8)) >> 8;
      72             : }
      73             : 
      74             : /** Just the rounding step in SkDiv255Round: round(value / 255)
      75             :  */
      76           0 : static inline unsigned SkDiv255Round(unsigned prod) {
      77           0 :     prod += 128;
      78           0 :     return (prod + (prod >> 8)) >> 8;
      79             : }
      80             : 
      81             : static inline float SkPinToUnitFloat(float x) {
      82             :     return SkTMin(SkTMax(x, 0.0f), 1.0f);
      83             : }
      84             : 
      85             : /**
      86             :  * Swap byte order of a 4-byte value, e.g. 0xaarrggbb -> 0xbbggrraa.
      87             :  */
      88             : #if defined(_MSC_VER)
      89             :     #include <intrin.h>
      90             :     static inline uint32_t SkBSwap32(uint32_t v) { return _byteswap_ulong(v); }
      91             : #else
      92             :     static inline uint32_t SkBSwap32(uint32_t v) { return __builtin_bswap32(v); }
      93             : #endif
      94             : 
      95             : //! Returns the number of leading zero bits (0...32)
      96             : int SkCLZ_portable(uint32_t);
      97             : 
      98             : #ifndef SkCLZ
      99             :     #if defined(SK_BUILD_FOR_WIN32)
     100             :         #include <intrin.h>
     101             : 
     102             :         static inline int SkCLZ(uint32_t mask) {
     103             :             if (mask) {
     104             :                 unsigned long index;
     105             :                 _BitScanReverse(&index, mask);
     106             :                 // Suppress this bogus /analyze warning. The check for non-zero
     107             :                 // guarantees that _BitScanReverse will succeed.
     108             : #pragma warning(suppress : 6102) // Using 'index' from failed function call
     109             :                 return index ^ 0x1F;
     110             :             } else {
     111             :                 return 32;
     112             :             }
     113             :         }
     114             :     #elif defined(SK_CPU_ARM32) || defined(__GNUC__) || defined(__clang__)
     115         627 :         static inline int SkCLZ(uint32_t mask) {
     116             :             // __builtin_clz(0) is undefined, so we have to detect that case.
     117         627 :             return mask ? __builtin_clz(mask) : 32;
     118             :         }
     119             :     #else
     120             :         #define SkCLZ(x)    SkCLZ_portable(x)
     121             :     #endif
     122             : #endif
     123             : 
     124             : /**
     125             :  *  Returns the smallest power-of-2 that is >= the specified value. If value
     126             :  *  is already a power of 2, then it is returned unchanged. It is undefined
     127             :  *  if value is <= 0.
     128             :  */
     129           0 : static inline int SkNextPow2(int value) {
     130           0 :     SkASSERT(value > 0);
     131           0 :     return 1 << (32 - SkCLZ(value - 1));
     132             : }
     133             : 
     134             : /**
     135             : *  Returns the largest power-of-2 that is <= the specified value. If value
     136             : *  is already a power of 2, then it is returned unchanged. It is undefined
     137             : *  if value is <= 0.
     138             : */
     139             : static inline int SkPrevPow2(int value) {
     140             :     SkASSERT(value > 0);
     141             :     return 1 << (32 - SkCLZ(value >> 1));
     142             : }
     143             : 
     144             : /**
     145             :  *  Returns the log2 of the specified value, were that value to be rounded up
     146             :  *  to the next power of 2. It is undefined to pass 0. Examples:
     147             :  *  SkNextLog2(1) -> 0
     148             :  *  SkNextLog2(2) -> 1
     149             :  *  SkNextLog2(3) -> 2
     150             :  *  SkNextLog2(4) -> 2
     151             :  *  SkNextLog2(5) -> 3
     152             :  */
     153         214 : static inline int SkNextLog2(uint32_t value) {
     154         214 :     SkASSERT(value != 0);
     155         214 :     return 32 - SkCLZ(value - 1);
     156             : }
     157             : 
     158             : /**
     159             : *  Returns the log2 of the specified value, were that value to be rounded down
     160             : *  to the previous power of 2. It is undefined to pass 0. Examples:
     161             : *  SkPrevLog2(1) -> 0
     162             : *  SkPrevLog2(2) -> 1
     163             : *  SkPrevLog2(3) -> 1
     164             : *  SkPrevLog2(4) -> 2
     165             : *  SkPrevLog2(5) -> 2
     166             : */
     167             : static inline int SkPrevLog2(uint32_t value) {
     168             :     SkASSERT(value != 0);
     169             :     return 32 - SkCLZ(value >> 1);
     170             : }
     171             : 
     172             : ///////////////////////////////////////////////////////////////////////////////
     173             : 
     174             : /**
     175             :  *  Return the next power of 2 >= n.
     176             :  */
     177             : static inline uint32_t GrNextPow2(uint32_t n) {
     178             :     return n ? (1 << (32 - SkCLZ(n - 1))) : 1;
     179             : }
     180             : 
     181             : /**
     182             :  * Returns the next power of 2 >= n or n if the next power of 2 can't be represented by size_t.
     183             :  */
     184           0 : static inline size_t GrNextSizePow2(size_t n) {
     185           0 :     constexpr int kNumSizeTBits = 8 * sizeof(size_t);
     186           0 :     constexpr size_t kHighBitSet = size_t(1) << (kNumSizeTBits - 1);
     187             : 
     188           0 :     if (!n) {
     189           0 :         return 1;
     190           0 :     } else if (n >= kHighBitSet) {
     191           0 :         return n;
     192             :     }
     193             : 
     194           0 :     n--;
     195           0 :     uint32_t shift = 1;
     196           0 :     while (shift < kNumSizeTBits) {
     197           0 :         n |= n >> shift;
     198           0 :         shift <<= 1;
     199             :     }
     200           0 :     return n + 1;
     201             : }
     202             : 
     203           0 : static inline int GrNextPow2(int n) {
     204           0 :     SkASSERT(n >= 0); // this impl only works for non-neg.
     205           0 :     return n ? (1 << (32 - SkCLZ(n - 1))) : 1;
     206             : }
     207             : #endif

Generated by: LCOV version 1.13