OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2006-2008 The Android Open Source Project | |
3 * | |
4 * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 * you may not use this file except in compliance with the License. | |
6 * You may obtain a copy of the License at | |
7 * | |
8 * http://www.apache.org/licenses/LICENSE-2.0 | |
9 * | |
10 * Unless required by applicable law or agreed to in writing, software | |
11 * distributed under the License is distributed on an "AS IS" BASIS, | |
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 * See the License for the specific language governing permissions and | |
14 * limitations under the License. | |
15 */ | |
16 | |
17 #ifndef SkFloat_DEFINED | |
18 #define SkFloat_DEFINED | |
19 | |
20 #include "SkFixed.h" | |
21 | |
22 class SkFloat { | |
23 public: | |
24 SkFloat() {} | |
25 | |
26 void setZero() { fPacked = 0; } | |
27 // void setShift(int value, int shift) { fPacked = SetShift(value, shift); } | |
28 void setInt(int value) { fPacked = SetShift(value, 0); } | |
29 void setFixed(SkFixed value) { fPacked = SetShift(value, -16); } | |
30 void setFract(SkFract value) { fPacked = SetShift(value, -30); } | |
31 | |
32 // int getShift(int shift) const { return GetShift(fPacked, shift); } | |
33 int getInt() const { return GetShift(fPacked, 0); } | |
34 SkFixed getFixed() const { return GetShift(fPacked, -16); } | |
35 SkFract getFract() const { return GetShift(fPacked, -30); } | |
36 | |
37 void abs() { fPacked = Abs(fPacked); } | |
38 void negate() { fPacked = Neg(fPacked); } | |
39 | |
40 void shiftLeft(int bits) { fPacked = Shift(fPacked, bits); } | |
41 void setShiftLeft(const SkFloat& a, int bits) { fPacked = Shift(a.fPacked
, bits); } | |
42 | |
43 void shiftRight(int bits) { fPacked = Shift(fPacked, -bits); } | |
44 void setShiftRight(const SkFloat& a, int bits) { fPacked = Shift(a.fPacke
d, -bits); } | |
45 | |
46 void add(const SkFloat& a) { fPacked = Add(fPacked, a.fPacked); } | |
47 void setAdd(const SkFloat& a, const SkFloat& b) { fPacked = Add(a.fPacked
, b.fPacked); } | |
48 | |
49 void sub(const SkFloat& a) { fPacked = Add(fPacked, Neg(a.fPacked)); } | |
50 void setSub(const SkFloat& a, const SkFloat& b) { fPacked = Add(a.fPacked
, Neg(b.fPacked)); } | |
51 | |
52 void mul(const SkFloat& a) { fPacked = Mul(fPacked, a.fPacked); } | |
53 void setMul(const SkFloat& a, const SkFloat& b) { fPacked = Mul(a.fPacked
, b.fPacked); } | |
54 | |
55 void div(const SkFloat& a) { fPacked = Div(fPacked, a.fPacked); } | |
56 void setDiv(const SkFloat& a, const SkFloat& b) { fPacked = Div(a.fPacked
, b.fPacked); } | |
57 | |
58 void sqrt() { fPacked = Sqrt(fPacked); } | |
59 void setSqrt(const SkFloat& a) { fPacked = Sqrt(a.fPacked); } | |
60 void cubeRoot() { fPacked = CubeRoot(fPacked); } | |
61 void setCubeRoot(const SkFloat& a) { fPacked = CubeRoot(a.fPacked); } | |
62 | |
63 friend bool operator==(const SkFloat& a, const SkFloat& b) { return a.fPacke
d == b.fPacked; } | |
64 friend bool operator!=(const SkFloat& a, const SkFloat& b) { return a.fPacke
d != b.fPacked; } | |
65 friend bool operator<(const SkFloat& a, const SkFloat& b) { return Cmp(a.fPa
cked, b.fPacked) < 0; } | |
66 friend bool operator<=(const SkFloat& a, const SkFloat& b) { return Cmp(a.fP
acked, b.fPacked) <= 0; } | |
67 friend bool operator>(const SkFloat& a, const SkFloat& b) { return Cmp(a.fPa
cked, b.fPacked) > 0; } | |
68 friend bool operator>=(const SkFloat& a, const SkFloat& b) { return Cmp(a.fP
acked, b.fPacked) >= 0; } | |
69 | |
70 #ifdef SK_DEBUG | |
71 static void UnitTest(); | |
72 | |
73 void assertEquals(float f, int tolerance = 0) | |
74 { | |
75 union { | |
76 float fFloat; | |
77 int32_t fPacked; | |
78 } tmp; | |
79 | |
80 tmp.fFloat = f; | |
81 int d = tmp.fPacked - fPacked; | |
82 SkASSERT(SkAbs32(d) <= tolerance); | |
83 } | |
84 float getFloat() const | |
85 { | |
86 union { | |
87 float fFloat; | |
88 int32_t fPacked; | |
89 } tmp; | |
90 | |
91 tmp.fPacked = fPacked; | |
92 return tmp.fFloat; | |
93 } | |
94 #endif | |
95 | |
96 private: | |
97 int32_t fPacked; | |
98 | |
99 SkFloat(int32_t packed) : fPacked(fPacked) {} | |
100 | |
101 public: | |
102 static int GetShift(int32_t packed, int shift); | |
103 static int32_t SetShift(int value, int shift); | |
104 static int32_t Neg(int32_t); | |
105 static int32_t Abs(int32_t packed) { return (uint32_t)(packed << 1) >> 1; } | |
106 static int32_t Shift(int32_t, int bits); | |
107 static int32_t Add(int32_t, int32_t); | |
108 static int32_t Mul(int32_t, int32_t); | |
109 static int32_t MulInt(int32_t, int); | |
110 static int32_t Div(int32_t, int32_t); | |
111 static int32_t DivInt(int32_t, int); | |
112 static int32_t Invert(int32_t); | |
113 static int32_t Sqrt(int32_t); | |
114 static int32_t CubeRoot(int32_t); | |
115 static int Cmp(int32_t, int32_t); | |
116 }; | |
117 | |
118 #endif | |
OLD | NEW |