• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:
SkFloat()24     SkFloat() {}
25 
setZero()26     void    setZero() { fPacked = 0; }
27 //  void    setShift(int value, int shift) { fPacked = SetShift(value, shift); }
setInt(int value)28     void    setInt(int value) { fPacked = SetShift(value, 0); }
setFixed(SkFixed value)29     void    setFixed(SkFixed value) { fPacked = SetShift(value, -16); }
setFract(SkFract value)30     void    setFract(SkFract value) { fPacked = SetShift(value, -30); }
31 
32 //  int     getShift(int shift) const { return GetShift(fPacked, shift); }
getInt()33     int     getInt() const { return GetShift(fPacked, 0); }
getFixed()34     SkFixed getFixed() const { return GetShift(fPacked, -16); }
getFract()35     SkFract getFract() const { return GetShift(fPacked, -30); }
36 
abs()37     void    abs() { fPacked = Abs(fPacked); }
negate()38     void    negate() { fPacked = Neg(fPacked); }
39 
shiftLeft(int bits)40     void    shiftLeft(int bits) { fPacked = Shift(fPacked, bits); }
setShiftLeft(const SkFloat & a,int bits)41     void    setShiftLeft(const SkFloat& a, int bits) { fPacked = Shift(a.fPacked, bits); }
42 
shiftRight(int bits)43     void    shiftRight(int bits) { fPacked = Shift(fPacked, -bits); }
setShiftRight(const SkFloat & a,int bits)44     void    setShiftRight(const SkFloat& a, int bits) { fPacked = Shift(a.fPacked, -bits); }
45 
add(const SkFloat & a)46     void    add(const SkFloat& a) { fPacked = Add(fPacked, a.fPacked); }
setAdd(const SkFloat & a,const SkFloat & b)47     void    setAdd(const SkFloat& a, const SkFloat& b) { fPacked = Add(a.fPacked, b.fPacked); }
48 
sub(const SkFloat & a)49     void    sub(const SkFloat& a) { fPacked = Add(fPacked, Neg(a.fPacked)); }
setSub(const SkFloat & a,const SkFloat & b)50     void    setSub(const SkFloat& a, const SkFloat& b) { fPacked = Add(a.fPacked, Neg(b.fPacked)); }
51 
mul(const SkFloat & a)52     void    mul(const SkFloat& a) { fPacked = Mul(fPacked, a.fPacked); }
setMul(const SkFloat & a,const SkFloat & b)53     void    setMul(const SkFloat& a, const SkFloat& b) { fPacked = Mul(a.fPacked, b.fPacked); }
54 
div(const SkFloat & a)55     void    div(const SkFloat& a) { fPacked = Div(fPacked, a.fPacked); }
setDiv(const SkFloat & a,const SkFloat & b)56     void    setDiv(const SkFloat& a, const SkFloat& b) { fPacked = Div(a.fPacked, b.fPacked); }
57 
sqrt()58     void    sqrt() { fPacked = Sqrt(fPacked); }
setSqrt(const SkFloat & a)59     void    setSqrt(const SkFloat& a) { fPacked = Sqrt(a.fPacked); }
cubeRoot()60     void    cubeRoot() { fPacked = CubeRoot(fPacked); }
setCubeRoot(const SkFloat & a)61     void    setCubeRoot(const SkFloat& a) { fPacked = CubeRoot(a.fPacked); }
62 
63     friend bool operator==(const SkFloat& a, const SkFloat& b) { return a.fPacked == b.fPacked; }
64     friend bool operator!=(const SkFloat& a, const SkFloat& b) { return a.fPacked != b.fPacked; }
65     friend bool operator<(const SkFloat& a, const SkFloat& b) { return Cmp(a.fPacked, b.fPacked) < 0; }
66     friend bool operator<=(const SkFloat& a, const SkFloat& b) { return Cmp(a.fPacked, b.fPacked) <= 0; }
67     friend bool operator>(const SkFloat& a, const SkFloat& b) { return Cmp(a.fPacked, b.fPacked) > 0; }
68     friend bool operator>=(const SkFloat& a, const SkFloat& b) { return Cmp(a.fPacked, 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     }
getFloat()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 
SkFloat(int32_t packed)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);
Abs(int32_t packed)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
119