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: SkFloat()17 SkFloat() {} 18 setZero()19 void setZero() { fPacked = 0; } 20 // void setShift(int value, int shift) { fPacked = SetShift(value, shift); } setInt(int value)21 void setInt(int value) { fPacked = SetShift(value, 0); } setFixed(SkFixed value)22 void setFixed(SkFixed value) { fPacked = SetShift(value, -16); } setFract(SkFract value)23 void setFract(SkFract value) { fPacked = SetShift(value, -30); } 24 25 // int getShift(int shift) const { return GetShift(fPacked, shift); } getInt()26 int getInt() const { return GetShift(fPacked, 0); } getFixed()27 SkFixed getFixed() const { return GetShift(fPacked, -16); } getFract()28 SkFract getFract() const { return GetShift(fPacked, -30); } 29 abs()30 void abs() { fPacked = Abs(fPacked); } negate()31 void negate() { fPacked = Neg(fPacked); } 32 shiftLeft(int bits)33 void shiftLeft(int bits) { fPacked = Shift(fPacked, bits); } setShiftLeft(const SkFloat & a,int bits)34 void setShiftLeft(const SkFloat& a, int bits) { fPacked = Shift(a.fPacked, bits); } 35 shiftRight(int bits)36 void shiftRight(int bits) { fPacked = Shift(fPacked, -bits); } setShiftRight(const SkFloat & a,int bits)37 void setShiftRight(const SkFloat& a, int bits) { fPacked = Shift(a.fPacked, -bits); } 38 add(const SkFloat & a)39 void add(const SkFloat& a) { fPacked = Add(fPacked, a.fPacked); } setAdd(const SkFloat & a,const SkFloat & b)40 void setAdd(const SkFloat& a, const SkFloat& b) { fPacked = Add(a.fPacked, b.fPacked); } 41 sub(const SkFloat & a)42 void sub(const SkFloat& a) { fPacked = Add(fPacked, Neg(a.fPacked)); } setSub(const SkFloat & a,const SkFloat & b)43 void setSub(const SkFloat& a, const SkFloat& b) { fPacked = Add(a.fPacked, Neg(b.fPacked)); } 44 mul(const SkFloat & a)45 void mul(const SkFloat& a) { fPacked = Mul(fPacked, a.fPacked); } setMul(const SkFloat & a,const SkFloat & b)46 void setMul(const SkFloat& a, const SkFloat& b) { fPacked = Mul(a.fPacked, b.fPacked); } 47 div(const SkFloat & a)48 void div(const SkFloat& a) { fPacked = Div(fPacked, a.fPacked); } setDiv(const SkFloat & a,const SkFloat & b)49 void setDiv(const SkFloat& a, const SkFloat& b) { fPacked = Div(a.fPacked, b.fPacked); } 50 sqrt()51 void sqrt() { fPacked = Sqrt(fPacked); } setSqrt(const SkFloat & a)52 void setSqrt(const SkFloat& a) { fPacked = Sqrt(a.fPacked); } cubeRoot()53 void cubeRoot() { fPacked = CubeRoot(fPacked); } setCubeRoot(const SkFloat & a)54 void setCubeRoot(const SkFloat& a) { fPacked = CubeRoot(a.fPacked); } 55 56 friend bool operator==(const SkFloat& a, const SkFloat& b) { return a.fPacked == b.fPacked; } 57 friend bool operator!=(const SkFloat& a, const SkFloat& b) { return a.fPacked != b.fPacked; } 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 friend bool operator>(const SkFloat& a, const SkFloat& b) { return Cmp(a.fPacked, b.fPacked) > 0; } 61 friend bool operator>=(const SkFloat& a, const SkFloat& b) { return Cmp(a.fPacked, b.fPacked) >= 0; } 62 63 #ifdef SK_DEBUG 64 static void UnitTest(); 65 66 void assertEquals(float f, int tolerance = 0) 67 { 68 union { 69 float fFloat; 70 int32_t fPacked; 71 } tmp; 72 73 tmp.fFloat = f; 74 int d = tmp.fPacked - fPacked; 75 SkASSERT(SkAbs32(d) <= tolerance); 76 } getFloat()77 float getFloat() const 78 { 79 union { 80 float fFloat; 81 int32_t fPacked; 82 } tmp; 83 84 tmp.fPacked = fPacked; 85 return tmp.fFloat; 86 } 87 #endif 88 89 private: 90 int32_t fPacked; 91 92 public: 93 static int GetShift(int32_t packed, int shift); 94 static int32_t SetShift(int value, int shift); 95 static int32_t Neg(int32_t); Abs(int32_t packed)96 static int32_t Abs(int32_t packed) { return (uint32_t)(packed << 1) >> 1; } 97 static int32_t Shift(int32_t, int bits); 98 static int32_t Add(int32_t, int32_t); 99 static int32_t Mul(int32_t, int32_t); 100 static int32_t MulInt(int32_t, int); 101 static int32_t Div(int32_t, int32_t); 102 static int32_t DivInt(int32_t, int); 103 static int32_t Invert(int32_t); 104 static int32_t Sqrt(int32_t); 105 static int32_t CubeRoot(int32_t); 106 static int Cmp(int32_t, int32_t); 107 }; 108 109 #endif 110