michael@0: michael@0: /* michael@0: * Copyright 2008 The Android Open Source Project michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: michael@0: #ifndef SkFloat_DEFINED michael@0: #define SkFloat_DEFINED michael@0: michael@0: #include "SkFixed.h" michael@0: michael@0: class SkFloat { michael@0: public: michael@0: SkFloat() {} michael@0: michael@0: void setZero() { fPacked = 0; } michael@0: // void setShift(int value, int shift) { fPacked = SetShift(value, shift); } michael@0: void setInt(int value) { fPacked = SetShift(value, 0); } michael@0: void setFixed(SkFixed value) { fPacked = SetShift(value, -16); } michael@0: michael@0: // int getShift(int shift) const { return GetShift(fPacked, shift); } michael@0: int getInt() const { return GetShift(fPacked, 0); } michael@0: SkFixed getFixed() const { return GetShift(fPacked, -16); } michael@0: michael@0: void abs() { fPacked = Abs(fPacked); } michael@0: void negate() { fPacked = Neg(fPacked); } michael@0: michael@0: void shiftLeft(int bits) { fPacked = Shift(fPacked, bits); } michael@0: void setShiftLeft(const SkFloat& a, int bits) { fPacked = Shift(a.fPacked, bits); } michael@0: michael@0: void shiftRight(int bits) { fPacked = Shift(fPacked, -bits); } michael@0: void setShiftRight(const SkFloat& a, int bits) { fPacked = Shift(a.fPacked, -bits); } michael@0: michael@0: void add(const SkFloat& a) { fPacked = Add(fPacked, a.fPacked); } michael@0: void setAdd(const SkFloat& a, const SkFloat& b) { fPacked = Add(a.fPacked, b.fPacked); } michael@0: michael@0: void sub(const SkFloat& a) { fPacked = Add(fPacked, Neg(a.fPacked)); } michael@0: void setSub(const SkFloat& a, const SkFloat& b) { fPacked = Add(a.fPacked, Neg(b.fPacked)); } michael@0: michael@0: void mul(const SkFloat& a) { fPacked = Mul(fPacked, a.fPacked); } michael@0: void setMul(const SkFloat& a, const SkFloat& b) { fPacked = Mul(a.fPacked, b.fPacked); } michael@0: michael@0: void div(const SkFloat& a) { fPacked = Div(fPacked, a.fPacked); } michael@0: void setDiv(const SkFloat& a, const SkFloat& b) { fPacked = Div(a.fPacked, b.fPacked); } michael@0: michael@0: void sqrt() { fPacked = Sqrt(fPacked); } michael@0: void setSqrt(const SkFloat& a) { fPacked = Sqrt(a.fPacked); } michael@0: void cubeRoot() { fPacked = CubeRoot(fPacked); } michael@0: void setCubeRoot(const SkFloat& a) { fPacked = CubeRoot(a.fPacked); } michael@0: michael@0: friend bool operator==(const SkFloat& a, const SkFloat& b) { return a.fPacked == b.fPacked; } michael@0: friend bool operator!=(const SkFloat& a, const SkFloat& b) { return a.fPacked != b.fPacked; } michael@0: friend bool operator<(const SkFloat& a, const SkFloat& b) { return Cmp(a.fPacked, b.fPacked) < 0; } michael@0: friend bool operator<=(const SkFloat& a, const SkFloat& b) { return Cmp(a.fPacked, b.fPacked) <= 0; } michael@0: friend bool operator>(const SkFloat& a, const SkFloat& b) { return Cmp(a.fPacked, b.fPacked) > 0; } michael@0: friend bool operator>=(const SkFloat& a, const SkFloat& b) { return Cmp(a.fPacked, b.fPacked) >= 0; } michael@0: michael@0: #ifdef SK_DEBUG michael@0: static void UnitTest(); michael@0: michael@0: void assertEquals(float f, int tolerance = 0) michael@0: { michael@0: union { michael@0: float fFloat; michael@0: int32_t fPacked; michael@0: } tmp; michael@0: michael@0: tmp.fFloat = f; michael@0: int d = tmp.fPacked - fPacked; michael@0: SkASSERT(SkAbs32(d) <= tolerance); michael@0: } michael@0: float getFloat() const michael@0: { michael@0: union { michael@0: float fFloat; michael@0: int32_t fPacked; michael@0: } tmp; michael@0: michael@0: tmp.fPacked = fPacked; michael@0: return tmp.fFloat; michael@0: } michael@0: #endif michael@0: michael@0: private: michael@0: int32_t fPacked; michael@0: michael@0: public: michael@0: static int GetShift(int32_t packed, int shift); michael@0: static int32_t SetShift(int value, int shift); michael@0: static int32_t Neg(int32_t); michael@0: static int32_t Abs(int32_t packed) { return (uint32_t)(packed << 1) >> 1; } michael@0: static int32_t Shift(int32_t, int bits); michael@0: static int32_t Add(int32_t, int32_t); michael@0: static int32_t Mul(int32_t, int32_t); michael@0: static int32_t MulInt(int32_t, int); michael@0: static int32_t Div(int32_t, int32_t); michael@0: static int32_t DivInt(int32_t, int); michael@0: static int32_t Invert(int32_t); michael@0: static int32_t Sqrt(int32_t); michael@0: static int32_t CubeRoot(int32_t); michael@0: static int Cmp(int32_t, int32_t); michael@0: }; michael@0: michael@0: #endif