Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright (C) 2006 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef SkMath_DEFINED
     18 #define SkMath_DEFINED
     19 
     20 #include "SkTypes.h"
     21 
     22 //! Returns the number of leading zero bits (0...32)
     23 int SkCLZ_portable(uint32_t);
     24 
     25 /** Computes the 64bit product of a * b, and then shifts the answer down by
     26     shift bits, returning the low 32bits. shift must be [0..63]
     27     e.g. to perform a fixedmul, call SkMulShift(a, b, 16)
     28 */
     29 int32_t SkMulShift(int32_t a, int32_t b, unsigned shift);
     30 
     31 /** Computes numer1 * numer2 / denom in full 64 intermediate precision.
     32     It is an error for denom to be 0. There is no special handling if
     33     the result overflows 32bits.
     34 */
     35 int32_t SkMulDiv(int32_t numer1, int32_t numer2, int32_t denom);
     36 
     37 /** Computes (numer1 << shift) / denom in full 64 intermediate precision.
     38     It is an error for denom to be 0. There is no special handling if
     39     the result overflows 32bits.
     40 */
     41 int32_t SkDivBits(int32_t numer, int32_t denom, int shift);
     42 
     43 /** Return the integer square root of value, with a bias of bitBias
     44 */
     45 int32_t SkSqrtBits(int32_t value, int bitBias);
     46 
     47 /** Return the integer square root of n, treated as a SkFixed (16.16)
     48 */
     49 #define SkSqrt32(n)         SkSqrtBits(n, 15)
     50 
     51 /** Return the integer cube root of value, with a bias of bitBias
     52  */
     53 int32_t SkCubeRootBits(int32_t value, int bitBias);
     54 
     55 /** Returns -1 if n < 0, else returns 0
     56 */
     57 #define SkExtractSign(n)    ((int32_t)(n) >> 31)
     58 
     59 /** If sign == -1, returns -n, else sign must be 0, and returns n.
     60     Typically used in conjunction with SkExtractSign().
     61 */
     62 static inline int32_t SkApplySign(int32_t n, int32_t sign) {
     63     SkASSERT(sign == 0 || sign == -1);
     64     return (n ^ sign) - sign;
     65 }
     66 
     67 /** Return x with the sign of y */
     68 static inline int32_t SkCopySign32(int32_t x, int32_t y) {
     69     return SkApplySign(x, SkExtractSign(x ^ y));
     70 }
     71 
     72 /** Returns (value < 0 ? 0 : value) efficiently (i.e. no compares or branches)
     73 */
     74 static inline int SkClampPos(int value) {
     75     return value & ~(value >> 31);
     76 }
     77 
     78 /** Given an integer and a positive (max) integer, return the value
     79     pinned against 0 and max, inclusive.
     80     @param value    The value we want returned pinned between [0...max]
     81     @param max      The positive max value
     82     @return 0 if value < 0, max if value > max, else value
     83 */
     84 static inline int SkClampMax(int value, int max) {
     85     // ensure that max is positive
     86     SkASSERT(max >= 0);
     87     if (value < 0) {
     88         value = 0;
     89     }
     90     if (value > max) {
     91         value = max;
     92     }
     93     return value;
     94 }
     95 
     96 /** Given a positive value and a positive max, return the value
     97     pinned against max.
     98     Note: only works as long as max - value doesn't wrap around
     99     @return max if value >= max, else value
    100 */
    101 static inline unsigned SkClampUMax(unsigned value, unsigned max) {
    102 #ifdef SK_CPU_HAS_CONDITIONAL_INSTR
    103     if (value > max) {
    104         value = max;
    105     }
    106     return value;
    107 #else
    108     int diff = max - value;
    109     // clear diff if diff is positive
    110     diff &= diff >> 31;
    111 
    112     return value + diff;
    113 #endif
    114 }
    115 
    116 ///////////////////////////////////////////////////////////////////////////////
    117 
    118 #if defined(__arm__)
    119     #define SkCLZ(x)    __builtin_clz(x)
    120 #endif
    121 
    122 #ifndef SkCLZ
    123     #define SkCLZ(x)    SkCLZ_portable(x)
    124 #endif
    125 
    126 ///////////////////////////////////////////////////////////////////////////////
    127 
    128 /** Returns the smallest power-of-2 that is >= the specified value. If value
    129     is already a power of 2, then it is returned unchanged. It is undefined
    130     if value is <= 0.
    131 */
    132 static inline int SkNextPow2(int value) {
    133     SkASSERT(value > 0);
    134     return 1 << (32 - SkCLZ(value - 1));
    135 }
    136 
    137 /** Returns the log2 of the specified value, were that value to be rounded up
    138     to the next power of 2. It is undefined to pass 0. Examples:
    139          SkNextLog2(1) -> 0
    140          SkNextLog2(2) -> 1
    141          SkNextLog2(3) -> 2
    142          SkNextLog2(4) -> 2
    143          SkNextLog2(5) -> 3
    144 */
    145 static inline int SkNextLog2(uint32_t value) {
    146     SkASSERT(value != 0);
    147     return 32 - SkCLZ(value - 1);
    148 }
    149 
    150 /** Returns true if value is a power of 2. Does not explicitly check for
    151     value <= 0.
    152  */
    153 static inline bool SkIsPow2(int value) {
    154     return (value & (value - 1)) == 0;
    155 }
    156 
    157 ///////////////////////////////////////////////////////////////////////////////
    158 
    159 /** SkMulS16(a, b) multiplies a * b, but requires that a and b are both int16_t.
    160     With this requirement, we can generate faster instructions on some
    161     architectures.
    162 */
    163 #if defined(__arm__) \
    164   && !defined(__thumb__) \
    165   && !defined(__ARM_ARCH_4T__) \
    166   && !defined(__ARM_ARCH_5T__)
    167     static inline int32_t SkMulS16(S16CPU x, S16CPU y) {
    168         SkASSERT((int16_t)x == x);
    169         SkASSERT((int16_t)y == y);
    170         int32_t product;
    171         asm("smulbb %0, %1, %2 \n"
    172             : "=r"(product)
    173             : "r"(x), "r"(y)
    174             );
    175         return product;
    176     }
    177 #else
    178     #ifdef SK_DEBUG
    179         static inline int32_t SkMulS16(S16CPU x, S16CPU y) {
    180             SkASSERT((int16_t)x == x);
    181             SkASSERT((int16_t)y == y);
    182             return x * y;
    183         }
    184     #else
    185         #define SkMulS16(x, y)  ((x) * (y))
    186     #endif
    187 #endif
    188 
    189 /** Return a*b/255, truncating away any fractional bits. Only valid if both
    190     a and b are 0..255
    191 */
    192 static inline U8CPU SkMulDiv255Trunc(U8CPU a, U8CPU b) {
    193     SkASSERT((uint8_t)a == a);
    194     SkASSERT((uint8_t)b == b);
    195     unsigned prod = SkMulS16(a, b) + 1;
    196     return (prod + (prod >> 8)) >> 8;
    197 }
    198 
    199 /** Return a*b/255, rounding any fractional bits. Only valid if both
    200     a and b are 0..255
    201  */
    202 static inline U8CPU SkMulDiv255Round(U8CPU a, U8CPU b) {
    203     SkASSERT((uint8_t)a == a);
    204     SkASSERT((uint8_t)b == b);
    205     unsigned prod = SkMulS16(a, b) + 128;
    206     return (prod + (prod >> 8)) >> 8;
    207 }
    208 
    209 /** Return (a*b)/255, taking the ceiling of any fractional bits. Only valid if
    210     both a and b are 0..255. The expected result equals (a * b + 254) / 255.
    211  */
    212 static inline U8CPU SkMulDiv255Ceiling(U8CPU a, U8CPU b) {
    213     SkASSERT((uint8_t)a == a);
    214     SkASSERT((uint8_t)b == b);
    215     unsigned prod = SkMulS16(a, b) + 255;
    216     return (prod + (prod >> 8)) >> 8;
    217 }
    218 
    219 /** Return a*b/((1 << shift) - 1), rounding any fractional bits.
    220     Only valid if a and b are unsigned and <= 32767 and shift is > 0 and <= 8
    221 */
    222 static inline unsigned SkMul16ShiftRound(unsigned a, unsigned b, int shift) {
    223     SkASSERT(a <= 32767);
    224     SkASSERT(b <= 32767);
    225     SkASSERT(shift > 0 && shift <= 8);
    226     unsigned prod = SkMulS16(a, b) + (1 << (shift - 1));
    227     return (prod + (prod >> shift)) >> shift;
    228 }
    229 
    230 /** Just the rounding step in SkDiv255Round: round(value / 255)
    231  */
    232 static inline unsigned SkDiv255Round(unsigned prod) {
    233     prod += 128;
    234     return (prod + (prod >> 8)) >> 8;
    235 }
    236 
    237 #endif
    238 
    239