Home | History | Annotate | Download | only in core
      1 
      2 /*
      3  * Copyright 2008 The Android Open Source Project
      4  *
      5  * Use of this source code is governed by a BSD-style license that can be
      6  * found in the LICENSE file.
      7  */
      8 
      9 
     10 #ifndef SkFloat_DEFINED
     11 #define SkFloat_DEFINED
     12 
     13 #include "SkFixed.h"
     14 
     15 class SkFloat {
     16 public:
     17     SkFloat() {}
     18 
     19     void    setZero() { fPacked = 0; }
     20 //  void    setShift(int value, int shift) { fPacked = SetShift(value, shift); }
     21     void    setInt(int value) { fPacked = SetShift(value, 0); }
     22     void    setFixed(SkFixed value) { fPacked = SetShift(value, -16); }
     23 
     24 //  int     getShift(int shift) const { return GetShift(fPacked, shift); }
     25     int     getInt() const { return GetShift(fPacked, 0); }
     26     SkFixed getFixed() const { return GetShift(fPacked, -16); }
     27 
     28     void    abs() { fPacked = Abs(fPacked); }
     29     void    negate() { fPacked = Neg(fPacked); }
     30 
     31     void    shiftLeft(int bits) { fPacked = Shift(fPacked, bits); }
     32     void    setShiftLeft(const SkFloat& a, int bits) { fPacked = Shift(a.fPacked, bits); }
     33 
     34     void    shiftRight(int bits) { fPacked = Shift(fPacked, -bits); }
     35     void    setShiftRight(const SkFloat& a, int bits) { fPacked = Shift(a.fPacked, -bits); }
     36 
     37     void    add(const SkFloat& a) { fPacked = Add(fPacked, a.fPacked); }
     38     void    setAdd(const SkFloat& a, const SkFloat& b) { fPacked = Add(a.fPacked, b.fPacked); }
     39 
     40     void    sub(const SkFloat& a) { fPacked = Add(fPacked, Neg(a.fPacked)); }
     41     void    setSub(const SkFloat& a, const SkFloat& b) { fPacked = Add(a.fPacked, Neg(b.fPacked)); }
     42 
     43     void    mul(const SkFloat& a) { fPacked = Mul(fPacked, a.fPacked); }
     44     void    setMul(const SkFloat& a, const SkFloat& b) { fPacked = Mul(a.fPacked, b.fPacked); }
     45 
     46     void    div(const SkFloat& a) { fPacked = Div(fPacked, a.fPacked); }
     47     void    setDiv(const SkFloat& a, const SkFloat& b) { fPacked = Div(a.fPacked, b.fPacked); }
     48 
     49     void    sqrt() { fPacked = Sqrt(fPacked); }
     50     void    setSqrt(const SkFloat& a) { fPacked = Sqrt(a.fPacked); }
     51     void    cubeRoot() { fPacked = CubeRoot(fPacked); }
     52     void    setCubeRoot(const SkFloat& a) { fPacked = CubeRoot(a.fPacked); }
     53 
     54     friend bool operator==(const SkFloat& a, const SkFloat& b) { return a.fPacked == b.fPacked; }
     55     friend bool operator!=(const SkFloat& a, const SkFloat& b) { return a.fPacked != b.fPacked; }
     56     friend bool operator<(const SkFloat& a, const SkFloat& b) { return Cmp(a.fPacked, b.fPacked) < 0; }
     57     friend bool operator<=(const SkFloat& a, const SkFloat& b) { return Cmp(a.fPacked, b.fPacked) <= 0; }
     58     friend bool operator>(const SkFloat& a, const SkFloat& b) { return Cmp(a.fPacked, b.fPacked) > 0; }
     59     friend bool operator>=(const SkFloat& a, const SkFloat& b) { return Cmp(a.fPacked, b.fPacked) >= 0; }
     60 
     61 #ifdef SK_DEBUG
     62     static void UnitTest();
     63 
     64     void assertEquals(float f, int tolerance = 0)
     65     {
     66         union {
     67             float   fFloat;
     68             int32_t fPacked;
     69         } tmp;
     70 
     71         tmp.fFloat = f;
     72         int d = tmp.fPacked - fPacked;
     73         SkASSERT(SkAbs32(d) <= tolerance);
     74     }
     75     float getFloat() const
     76     {
     77         union {
     78             float   fFloat;
     79             int32_t fPacked;
     80         } tmp;
     81 
     82         tmp.fPacked = fPacked;
     83         return tmp.fFloat;
     84     }
     85 #endif
     86 
     87 private:
     88     int32_t fPacked;
     89 
     90 public:
     91     static int GetShift(int32_t packed, int shift);
     92     static int32_t SetShift(int value, int shift);
     93     static int32_t Neg(int32_t);
     94     static int32_t Abs(int32_t packed) { return (uint32_t)(packed << 1) >> 1; }
     95     static int32_t Shift(int32_t, int bits);
     96     static int32_t Add(int32_t, int32_t);
     97     static int32_t Mul(int32_t, int32_t);
     98     static int32_t MulInt(int32_t, int);
     99     static int32_t Div(int32_t, int32_t);
    100     static int32_t DivInt(int32_t, int);
    101     static int32_t Invert(int32_t);
    102     static int32_t Sqrt(int32_t);
    103     static int32_t CubeRoot(int32_t);
    104     static int Cmp(int32_t, int32_t);
    105 };
    106 
    107 #endif
    108