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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/src/core/SkFloat.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,107 @@
     1.4 +
     1.5 +/*
     1.6 + * Copyright 2008 The Android Open Source Project
     1.7 + *
     1.8 + * Use of this source code is governed by a BSD-style license that can be
     1.9 + * found in the LICENSE file.
    1.10 + */
    1.11 +
    1.12 +
    1.13 +#ifndef SkFloat_DEFINED
    1.14 +#define SkFloat_DEFINED
    1.15 +
    1.16 +#include "SkFixed.h"
    1.17 +
    1.18 +class SkFloat {
    1.19 +public:
    1.20 +    SkFloat() {}
    1.21 +
    1.22 +    void    setZero() { fPacked = 0; }
    1.23 +//  void    setShift(int value, int shift) { fPacked = SetShift(value, shift); }
    1.24 +    void    setInt(int value) { fPacked = SetShift(value, 0); }
    1.25 +    void    setFixed(SkFixed value) { fPacked = SetShift(value, -16); }
    1.26 +
    1.27 +//  int     getShift(int shift) const { return GetShift(fPacked, shift); }
    1.28 +    int     getInt() const { return GetShift(fPacked, 0); }
    1.29 +    SkFixed getFixed() const { return GetShift(fPacked, -16); }
    1.30 +
    1.31 +    void    abs() { fPacked = Abs(fPacked); }
    1.32 +    void    negate() { fPacked = Neg(fPacked); }
    1.33 +
    1.34 +    void    shiftLeft(int bits) { fPacked = Shift(fPacked, bits); }
    1.35 +    void    setShiftLeft(const SkFloat& a, int bits) { fPacked = Shift(a.fPacked, bits); }
    1.36 +
    1.37 +    void    shiftRight(int bits) { fPacked = Shift(fPacked, -bits); }
    1.38 +    void    setShiftRight(const SkFloat& a, int bits) { fPacked = Shift(a.fPacked, -bits); }
    1.39 +
    1.40 +    void    add(const SkFloat& a) { fPacked = Add(fPacked, a.fPacked); }
    1.41 +    void    setAdd(const SkFloat& a, const SkFloat& b) { fPacked = Add(a.fPacked, b.fPacked); }
    1.42 +
    1.43 +    void    sub(const SkFloat& a) { fPacked = Add(fPacked, Neg(a.fPacked)); }
    1.44 +    void    setSub(const SkFloat& a, const SkFloat& b) { fPacked = Add(a.fPacked, Neg(b.fPacked)); }
    1.45 +
    1.46 +    void    mul(const SkFloat& a) { fPacked = Mul(fPacked, a.fPacked); }
    1.47 +    void    setMul(const SkFloat& a, const SkFloat& b) { fPacked = Mul(a.fPacked, b.fPacked); }
    1.48 +
    1.49 +    void    div(const SkFloat& a) { fPacked = Div(fPacked, a.fPacked); }
    1.50 +    void    setDiv(const SkFloat& a, const SkFloat& b) { fPacked = Div(a.fPacked, b.fPacked); }
    1.51 +
    1.52 +    void    sqrt() { fPacked = Sqrt(fPacked); }
    1.53 +    void    setSqrt(const SkFloat& a) { fPacked = Sqrt(a.fPacked); }
    1.54 +    void    cubeRoot() { fPacked = CubeRoot(fPacked); }
    1.55 +    void    setCubeRoot(const SkFloat& a) { fPacked = CubeRoot(a.fPacked); }
    1.56 +
    1.57 +    friend bool operator==(const SkFloat& a, const SkFloat& b) { return a.fPacked == b.fPacked; }
    1.58 +    friend bool operator!=(const SkFloat& a, const SkFloat& b) { return a.fPacked != b.fPacked; }
    1.59 +    friend bool operator<(const SkFloat& a, const SkFloat& b) { return Cmp(a.fPacked, b.fPacked) < 0; }
    1.60 +    friend bool operator<=(const SkFloat& a, const SkFloat& b) { return Cmp(a.fPacked, b.fPacked) <= 0; }
    1.61 +    friend bool operator>(const SkFloat& a, const SkFloat& b) { return Cmp(a.fPacked, b.fPacked) > 0; }
    1.62 +    friend bool operator>=(const SkFloat& a, const SkFloat& b) { return Cmp(a.fPacked, b.fPacked) >= 0; }
    1.63 +
    1.64 +#ifdef SK_DEBUG
    1.65 +    static void UnitTest();
    1.66 +
    1.67 +    void assertEquals(float f, int tolerance = 0)
    1.68 +    {
    1.69 +        union {
    1.70 +            float   fFloat;
    1.71 +            int32_t fPacked;
    1.72 +        } tmp;
    1.73 +
    1.74 +        tmp.fFloat = f;
    1.75 +        int d = tmp.fPacked - fPacked;
    1.76 +        SkASSERT(SkAbs32(d) <= tolerance);
    1.77 +    }
    1.78 +    float getFloat() const
    1.79 +    {
    1.80 +        union {
    1.81 +            float   fFloat;
    1.82 +            int32_t fPacked;
    1.83 +        } tmp;
    1.84 +
    1.85 +        tmp.fPacked = fPacked;
    1.86 +        return tmp.fFloat;
    1.87 +    }
    1.88 +#endif
    1.89 +
    1.90 +private:
    1.91 +    int32_t fPacked;
    1.92 +
    1.93 +public:
    1.94 +    static int GetShift(int32_t packed, int shift);
    1.95 +    static int32_t SetShift(int value, int shift);
    1.96 +    static int32_t Neg(int32_t);
    1.97 +    static int32_t Abs(int32_t packed) { return (uint32_t)(packed << 1) >> 1; }
    1.98 +    static int32_t Shift(int32_t, int bits);
    1.99 +    static int32_t Add(int32_t, int32_t);
   1.100 +    static int32_t Mul(int32_t, int32_t);
   1.101 +    static int32_t MulInt(int32_t, int);
   1.102 +    static int32_t Div(int32_t, int32_t);
   1.103 +    static int32_t DivInt(int32_t, int);
   1.104 +    static int32_t Invert(int32_t);
   1.105 +    static int32_t Sqrt(int32_t);
   1.106 +    static int32_t CubeRoot(int32_t);
   1.107 +    static int Cmp(int32_t, int32_t);
   1.108 +};
   1.109 +
   1.110 +#endif

mercurial