aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/SkFloat.h
blob: fe41c2723e9e3b665b99e9632d13506a806338ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109

/*
 * Copyright 2008 The Android Open Source Project
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */


#ifndef SkFloat_DEFINED
#define SkFloat_DEFINED

#include "SkFixed.h"

class SkFloat {
public:
    SkFloat() {}

    void    setZero() { fPacked = 0; }
//  void    setShift(int value, int shift) { fPacked = SetShift(value, shift); }
    void    setInt(int value) { fPacked = SetShift(value, 0); }
    void    setFixed(SkFixed value) { fPacked = SetShift(value, -16); }
    void    setFract(SkFract value) { fPacked = SetShift(value, -30); }

//  int     getShift(int shift) const { return GetShift(fPacked, shift); }
    int     getInt() const { return GetShift(fPacked, 0); }
    SkFixed getFixed() const { return GetShift(fPacked, -16); }
    SkFract getFract() const { return GetShift(fPacked, -30); }

    void    abs() { fPacked = Abs(fPacked); }
    void    negate() { fPacked = Neg(fPacked); }

    void    shiftLeft(int bits) { fPacked = Shift(fPacked, bits); }
    void    setShiftLeft(const SkFloat& a, int bits) { fPacked = Shift(a.fPacked, bits); }

    void    shiftRight(int bits) { fPacked = Shift(fPacked, -bits); }
    void    setShiftRight(const SkFloat& a, int bits) { fPacked = Shift(a.fPacked, -bits); }

    void    add(const SkFloat& a) { fPacked = Add(fPacked, a.fPacked); }
    void    setAdd(const SkFloat& a, const SkFloat& b) { fPacked = Add(a.fPacked, b.fPacked); }

    void    sub(const SkFloat& a) { fPacked = Add(fPacked, Neg(a.fPacked)); }
    void    setSub(const SkFloat& a, const SkFloat& b) { fPacked = Add(a.fPacked, Neg(b.fPacked)); }

    void    mul(const SkFloat& a) { fPacked = Mul(fPacked, a.fPacked); }
    void    setMul(const SkFloat& a, const SkFloat& b) { fPacked = Mul(a.fPacked, b.fPacked); }

    void    div(const SkFloat& a) { fPacked = Div(fPacked, a.fPacked); }
    void    setDiv(const SkFloat& a, const SkFloat& b) { fPacked = Div(a.fPacked, b.fPacked); }

    void    sqrt() { fPacked = Sqrt(fPacked); }
    void    setSqrt(const SkFloat& a) { fPacked = Sqrt(a.fPacked); }
    void    cubeRoot() { fPacked = CubeRoot(fPacked); }
    void    setCubeRoot(const SkFloat& a) { fPacked = CubeRoot(a.fPacked); }

    friend bool operator==(const SkFloat& a, const SkFloat& b) { return a.fPacked == b.fPacked; }
    friend bool operator!=(const SkFloat& a, const SkFloat& b) { return a.fPacked != b.fPacked; }
    friend bool operator<(const SkFloat& a, const SkFloat& b) { return Cmp(a.fPacked, b.fPacked) < 0; }
    friend bool operator<=(const SkFloat& a, const SkFloat& b) { return Cmp(a.fPacked, b.fPacked) <= 0; }
    friend bool operator>(const SkFloat& a, const SkFloat& b) { return Cmp(a.fPacked, b.fPacked) > 0; }
    friend bool operator>=(const SkFloat& a, const SkFloat& b) { return Cmp(a.fPacked, b.fPacked) >= 0; }

#ifdef SK_DEBUG
    static void UnitTest();

    void assertEquals(float f, int tolerance = 0)
    {
        union {
            float   fFloat;
            int32_t fPacked;
        } tmp;
        
        tmp.fFloat = f;
        int d = tmp.fPacked - fPacked;
        SkASSERT(SkAbs32(d) <= tolerance);
    }
    float getFloat() const
    {
        union {
            float   fFloat;
            int32_t fPacked;
        } tmp;
        
        tmp.fPacked = fPacked;
        return tmp.fFloat;
    }
#endif

private:
    int32_t fPacked;

public:
    static int GetShift(int32_t packed, int shift);
    static int32_t SetShift(int value, int shift);
    static int32_t Neg(int32_t);
    static int32_t Abs(int32_t packed) { return (uint32_t)(packed << 1) >> 1; }
    static int32_t Shift(int32_t, int bits);
    static int32_t Add(int32_t, int32_t);
    static int32_t Mul(int32_t, int32_t);
    static int32_t MulInt(int32_t, int);
    static int32_t Div(int32_t, int32_t);
    static int32_t DivInt(int32_t, int);
    static int32_t Invert(int32_t);
    static int32_t Sqrt(int32_t);
    static int32_t CubeRoot(int32_t);
    static int Cmp(int32_t, int32_t);
};

#endif