1/*
2 * @APPLE_LICENSE_HEADER_START@
3 *
4 * Copyright (c) 2011 Apple Computer, Inc.  All Rights Reserved.
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24#ifndef _IOFIXEDPOINT64_H
25#define _IOFIXEDPOINT64_H
26
27#include <IOKit/IOTypes.h>
28#include <IOKit/graphics/IOGraphicsTypes.h>
29#include <IOKit/hidsystem/IOHIDTypes.h>
30#include "IOFixed64.h"
31
32//===========================================================================
33class IOFixedPoint64
34{
35private:
36    IOFixed64 x;
37    IOFixed64 y;
38
39public:
40    IOFixedPoint64() {
41        // zeroed by IOFixed64
42    }
43
44    IOFixedPoint64(IOFixedPoint32 p) {
45        x.fromFixed24x8(p.x);
46        y.fromFixed24x8(p.y);
47    }
48
49    operator const IOFixedPoint32() {
50        IOFixedPoint32 r;
51        r.x = x.asFixed24x8();
52        r.y = y.asFixed24x8();
53        return r;
54    }
55
56    operator const IOGPoint() {
57        IOGPoint r;
58        r.x = x.as32();
59        r.y = y.as32();
60        return r;
61    }
62
63    operator const bool() {
64        return (x || y);
65    }
66
67    IOFixedPoint64& fromIntFloor(SInt64 x_in, SInt64 y_in) {
68        x.fromIntFloor(x_in);
69        y.fromIntFloor(y_in);
70        return *this;
71    }
72
73    IOFixedPoint64& fromIntCeiling(SInt64 x_in, SInt64 y_in) {
74        x.fromIntCeiling(x_in);
75        y.fromIntCeiling(y_in);
76        return *this;
77    }
78
79    IOFixedPoint64& fromFixed(IOFixed x_in, IOFixed y_in) {
80        x.fromFixed(x_in);
81        y.fromFixed(y_in);
82        return *this;
83    }
84
85    IOFixedPoint64& fromFixed64(IOFixed64 x_in, IOFixed64 y_in) {
86        x = x_in;
87        y = y_in;
88        return *this;
89    }
90
91    IOFixedPoint64& fromFixed24x8(int32_t x_in, int32_t y_in) {
92        x.fromFixed24x8(x_in);
93        y.fromFixed24x8(y_in);
94        return *this;
95    }
96
97    bool inRect(volatile IOGBounds &rect) {
98        return (x >= (SInt64)rect.minx) && (x < (SInt64)rect.maxx) && (y >= (SInt64)rect.miny) && (y < (SInt64)rect.maxy);
99    }
100
101    void clipToRect(volatile IOGBounds &rect) {
102        IOFixed64 minx;
103        minx.fromIntFloor(rect.minx);
104        IOFixed64 maxx;
105        maxx.fromIntCeiling(rect.maxx - 1);
106
107        if (x < minx)
108            x = minx;
109        else if (x > maxx)
110            x = maxx;
111
112        IOFixed64 miny;
113        miny.fromIntFloor(rect.miny);
114        IOFixed64 maxy;
115        maxy.fromIntCeiling(rect.maxy - 1);
116
117        if (y < miny)
118            y = miny;
119        else if (y > maxy)
120            y = maxy;
121    }
122
123    IOFixed64& xValue() {
124        return x;
125    }
126
127    IOFixed64& yValue() {
128        return y;
129    }
130
131#define ARITHMETIC_OPERATOR(OP) \
132IOFixedPoint64& operator OP(IOFixedPoint64 p) { \
133    x OP p.x; \
134    y OP p.y; \
135    return *this; \
136} \
137IOFixedPoint64& operator OP(IOFixed64 s) { \
138    x OP s; \
139    y OP s; \
140    return *this; \
141} \
142IOFixedPoint64& operator OP(SInt64 s) { \
143    IOFixed64 s_fixed; \
144    return (*this OP s_fixed.fromIntFloor(s)); \
145}
146
147    ARITHMETIC_OPERATOR(+=)
148    ARITHMETIC_OPERATOR(-=)
149    ARITHMETIC_OPERATOR(*=)
150    ARITHMETIC_OPERATOR(/=)
151
152#undef ARITHMETIC_OPERATOR
153
154#define BOOL_OPERATOR(OP) \
155bool operator OP (const IOFixedPoint64 p) const { return (x OP p.x) || (y OP p.y); }
156
157    BOOL_OPERATOR(>)
158    BOOL_OPERATOR(>=)
159    BOOL_OPERATOR(<)
160    BOOL_OPERATOR(<=)
161    BOOL_OPERATOR(==)
162    BOOL_OPERATOR(!=)
163
164#undef BOOL_OPERATOR
165
166};
167
168//===========================================================================
169IOFixedPoint64 operator* (const IOFixedPoint64 a, const IOFixedPoint64 b);
170IOFixedPoint64 operator* (const IOFixedPoint64 a, const IOFixed64 b);
171IOFixedPoint64 operator* (const IOFixedPoint64 a, const SInt64 b);
172IOFixedPoint64 operator/ (const IOFixedPoint64 a, const IOFixedPoint64 b);
173IOFixedPoint64 operator/ (const IOFixedPoint64 a, const IOFixed64 b);
174IOFixedPoint64 operator/ (const IOFixedPoint64 a, const SInt64 b);
175IOFixedPoint64 operator+ (const IOFixedPoint64 a, const IOFixedPoint64 b);
176IOFixedPoint64 operator- (const IOFixedPoint64 a, const IOFixedPoint64 b);
177
178//===========================================================================
179#endif // _IOFIXEDPOINT64_H
180