gfx/skia/trunk/src/core/SkMathPriv.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/src/core/SkMathPriv.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,71 @@
     1.4 +/*
     1.5 + * Copyright 2012 Google Inc.
     1.6 + *
     1.7 + * Use of this source code is governed by a BSD-style license that can be
     1.8 + * found in the LICENSE file.
     1.9 + */
    1.10 +
    1.11 +#ifndef SkMathPriv_DEFINED
    1.12 +#define SkMathPriv_DEFINED
    1.13 +
    1.14 +#include "SkMath.h"
    1.15 +
    1.16 +/** Returns -1 if n < 0, else returns 0
    1.17 + */
    1.18 +#define SkExtractSign(n)    ((int32_t)(n) >> 31)
    1.19 +
    1.20 +/** If sign == -1, returns -n, else sign must be 0, and returns n.
    1.21 + Typically used in conjunction with SkExtractSign().
    1.22 + */
    1.23 +static inline int32_t SkApplySign(int32_t n, int32_t sign) {
    1.24 +    SkASSERT(sign == 0 || sign == -1);
    1.25 +    return (n ^ sign) - sign;
    1.26 +}
    1.27 +
    1.28 +/** Return x with the sign of y */
    1.29 +static inline int32_t SkCopySign32(int32_t x, int32_t y) {
    1.30 +    return SkApplySign(x, SkExtractSign(x ^ y));
    1.31 +}
    1.32 +
    1.33 +/** Given a positive value and a positive max, return the value
    1.34 + pinned against max.
    1.35 + Note: only works as long as max - value doesn't wrap around
    1.36 + @return max if value >= max, else value
    1.37 + */
    1.38 +static inline unsigned SkClampUMax(unsigned value, unsigned max) {
    1.39 +    if (value > max) {
    1.40 +        value = max;
    1.41 +    }
    1.42 +    return value;
    1.43 +}
    1.44 +
    1.45 +///////////////////////////////////////////////////////////////////////////////
    1.46 +
    1.47 +/** Return a*b/255, truncating away any fractional bits. Only valid if both
    1.48 + a and b are 0..255
    1.49 + */
    1.50 +static inline U8CPU SkMulDiv255Trunc(U8CPU a, U8CPU b) {
    1.51 +    SkASSERT((uint8_t)a == a);
    1.52 +    SkASSERT((uint8_t)b == b);
    1.53 +    unsigned prod = SkMulS16(a, b) + 1;
    1.54 +    return (prod + (prod >> 8)) >> 8;
    1.55 +}
    1.56 +
    1.57 +/** Return (a*b)/255, taking the ceiling of any fractional bits. Only valid if
    1.58 + both a and b are 0..255. The expected result equals (a * b + 254) / 255.
    1.59 + */
    1.60 +static inline U8CPU SkMulDiv255Ceiling(U8CPU a, U8CPU b) {
    1.61 +    SkASSERT((uint8_t)a == a);
    1.62 +    SkASSERT((uint8_t)b == b);
    1.63 +    unsigned prod = SkMulS16(a, b) + 255;
    1.64 +    return (prod + (prod >> 8)) >> 8;
    1.65 +}
    1.66 +
    1.67 +/** Just the rounding step in SkDiv255Round: round(value / 255)
    1.68 + */
    1.69 +static inline unsigned SkDiv255Round(unsigned prod) {
    1.70 +    prod += 128;
    1.71 +    return (prod + (prod >> 8)) >> 8;
    1.72 +}
    1.73 +
    1.74 +#endif

mercurial