1 /*
2 * Inkscape::Util::FixedPoint - fixed point type
3 *
4 * Authors:
5 * Jasper van de Gronde <th.v.d.gronde@hccnet.net>
6 *
7 * Copyright (C) 2006 Jasper van de Gronde
8 *
9 * Released under GNU GPL, read the file 'COPYING' for more information
10 */
12 #ifndef SEEN_INKSCAPE_UTIL_FIXED_POINT_H
13 #define SEEN_INKSCAPE_UTIL_FIXED_POINT_H
15 #include "traits/reference.h"
16 #include <math.h>
17 #include <algorithm>
18 #include <limits>
20 namespace Inkscape {
22 namespace Util {
24 template <typename T, unsigned int precision>
25 class FixedPoint {
26 public:
27 FixedPoint() {}
28 FixedPoint(const FixedPoint& value) : v(value.v) {}
29 FixedPoint(char value) : v(static_cast<T>(value)<<precision) {}
30 FixedPoint(unsigned char value) : v(static_cast<T>(value)<<precision) {}
31 FixedPoint(short value) : v(static_cast<T>(value)<<precision) {}
32 FixedPoint(unsigned short value) : v(static_cast<T>(value)<<precision) {}
33 FixedPoint(int value) : v(static_cast<T>(value)<<precision) {}
34 FixedPoint(unsigned int value) : v(static_cast<T>(value)<<precision) {}
35 FixedPoint(double value) : v(static_cast<T>(floor(value*(1<<precision)))) {}
37 FixedPoint& operator+=(FixedPoint val) { v += val.v; return *this; }
38 FixedPoint& operator-=(FixedPoint val) { v -= val.v; return *this; }
39 FixedPoint& operator*=(FixedPoint val) {
40 const unsigned int half_size = 8*sizeof(T)/2;
41 const T al = v&((1<<half_size)-1), bl = val.v&((1<<half_size)-1);
42 const T ah = v>>half_size, bh = val.v>>half_size;
43 v = static_cast<unsigned int>(al*bl)>>precision;
44 if ( half_size >= precision ) {
45 v += ((al*bh)+(ah*bl)+((ah*bh)<<half_size))<<(half_size-precision);
46 } else {
47 v += ((al*bh)+(ah*bl))>>(precision-half_size);
48 v += (ah*bh)<<(2*half_size-precision);
49 }
50 return *this;
51 }
53 FixedPoint& operator*=(char val) { v *= val; return *this; }
54 FixedPoint& operator*=(unsigned char val) { v *= val; return *this; }
55 FixedPoint& operator*=(short val) { v *= val; return *this; }
56 FixedPoint& operator*=(unsigned short val) { v *= val; return *this; }
57 FixedPoint& operator*=(int val) { v *= val; return *this; }
58 FixedPoint& operator*=(unsigned int val) { v *= val; return *this; }
60 FixedPoint operator+(FixedPoint val) const { FixedPoint r(*this); return r+=val; }
61 FixedPoint operator-(FixedPoint val) const { FixedPoint r(*this); return r-=val; }
62 FixedPoint operator*(FixedPoint val) const { FixedPoint r(*this); return r*=val; }
64 FixedPoint operator*(char val) const { FixedPoint r(*this); return r*=val; }
65 FixedPoint operator*(unsigned char val) const { FixedPoint r(*this); return r*=val; }
66 FixedPoint operator*(short val) const { FixedPoint r(*this); return r*=val; }
67 FixedPoint operator*(unsigned short val) const { FixedPoint r(*this); return r*=val; }
68 FixedPoint operator*(int val) const { FixedPoint r(*this); return r*=val; }
69 FixedPoint operator*(unsigned int val) const { FixedPoint r(*this); return r*=val; }
71 float operator*(float val) const { return static_cast<float>(*this)*val; }
72 double operator*(double val) const { return static_cast<double>(*this)*val; }
74 operator char() const { return v>>precision; }
75 operator unsigned char() const { return v>>precision; }
76 operator short() const { return v>>precision; }
77 operator unsigned short() const { return v>>precision; }
78 operator int() const { return v>>precision; }
79 operator unsigned int() const { return v>>precision; }
81 operator float() const { return ldexpf(v,-precision); }
82 operator double() const { return ldexp(v,-precision); }
83 private:
84 T v;
85 };
87 template<typename T, unsigned int precision> FixedPoint<T,precision> operator *(char a, FixedPoint<T,precision> b) { return b*=a; }
88 template<typename T, unsigned int precision> FixedPoint<T,precision> operator *(unsigned char a, FixedPoint<T,precision> b) { return b*=a; }
89 template<typename T, unsigned int precision> FixedPoint<T,precision> operator *(short a, FixedPoint<T,precision> b) { return b*=a; }
90 template<typename T, unsigned int precision> FixedPoint<T,precision> operator *(unsigned short a, FixedPoint<T,precision> b) { return b*=a; }
91 template<typename T, unsigned int precision> FixedPoint<T,precision> operator *(int a, FixedPoint<T,precision> b) { return b*=a; }
92 template<typename T, unsigned int precision> FixedPoint<T,precision> operator *(unsigned int a, FixedPoint<T,precision> b) { return b*=a; }
94 template<typename T, unsigned int precision> float operator *(float a, FixedPoint<T,precision> b) { return b*a; }
95 template<typename T, unsigned int precision> double operator *(double a, FixedPoint<T,precision> b) { return b*a; }
97 }
99 }
101 #endif
102 /*
103 Local Variables:
104 mode:c++
105 c-file-style:"stroustrup"
106 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
107 indent-tabs-mode:nil
108 fill-column:99
109 End:
110 */
111 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
112 /*
113 * Inkscape::Util::FixedPoint - fixed point type
114 *
115 * Authors:
116 * Jasper van de Gronde <th.v.d.gronde@hccnet.net>
117 *
118 * Copyright (C) 2006 Jasper van de Gronde
119 *
120 * Released under GNU GPL, read the file 'COPYING' for more information
121 */
123 #ifndef SEEN_INKSCAPE_UTIL_FIXED_POINT_H
124 #define SEEN_INKSCAPE_UTIL_FIXED_POINT_H
126 #include "traits/reference.h"
127 #include <math.h>
128 #include <algorithm>
129 #include <limits>
131 namespace Inkscape {
133 namespace Util {
135 template <typename T, unsigned int precision>
136 class FixedPoint {
137 public:
138 FixedPoint() {}
139 FixedPoint(const FixedPoint& value) : v(value.v) {}
140 FixedPoint(char value) : v(static_cast<T>(value)<<precision) {}
141 FixedPoint(unsigned char value) : v(static_cast<T>(value)<<precision) {}
142 FixedPoint(short value) : v(static_cast<T>(value)<<precision) {}
143 FixedPoint(unsigned short value) : v(static_cast<T>(value)<<precision) {}
144 FixedPoint(int value) : v(static_cast<T>(value)<<precision) {}
145 FixedPoint(unsigned int value) : v(static_cast<T>(value)<<precision) {}
146 FixedPoint(double value) : v(static_cast<T>(floor(value*(1<<precision)))) {}
148 FixedPoint& operator+=(FixedPoint val) { v += val.v; return *this; }
149 FixedPoint& operator-=(FixedPoint val) { v -= val.v; return *this; }
150 FixedPoint& operator*=(FixedPoint val) {
151 const unsigned int half_size = 8*sizeof(T)/2;
152 const T al = v&((1<<half_size)-1), bl = val.v&((1<<half_size)-1);
153 const T ah = v>>half_size, bh = val.v>>half_size;
154 v = static_cast<unsigned int>(al*bl)>>precision;
155 if ( half_size >= precision ) {
156 v += ((al*bh)+(ah*bl)+((ah*bh)<<half_size))<<(half_size-precision);
157 } else {
158 v += ((al*bh)+(ah*bl))>>(precision-half_size);
159 v += (ah*bh)<<(2*half_size-precision);
160 }
161 return *this;
162 }
164 FixedPoint& operator*=(char val) { v *= val; return *this; }
165 FixedPoint& operator*=(unsigned char val) { v *= val; return *this; }
166 FixedPoint& operator*=(short val) { v *= val; return *this; }
167 FixedPoint& operator*=(unsigned short val) { v *= val; return *this; }
168 FixedPoint& operator*=(int val) { v *= val; return *this; }
169 FixedPoint& operator*=(unsigned int val) { v *= val; return *this; }
171 FixedPoint operator+(FixedPoint val) const { FixedPoint r(*this); return r+=val; }
172 FixedPoint operator-(FixedPoint val) const { FixedPoint r(*this); return r-=val; }
173 FixedPoint operator*(FixedPoint val) const { FixedPoint r(*this); return r*=val; }
175 FixedPoint operator*(char val) const { FixedPoint r(*this); return r*=val; }
176 FixedPoint operator*(unsigned char val) const { FixedPoint r(*this); return r*=val; }
177 FixedPoint operator*(short val) const { FixedPoint r(*this); return r*=val; }
178 FixedPoint operator*(unsigned short val) const { FixedPoint r(*this); return r*=val; }
179 FixedPoint operator*(int val) const { FixedPoint r(*this); return r*=val; }
180 FixedPoint operator*(unsigned int val) const { FixedPoint r(*this); return r*=val; }
182 float operator*(float val) const { return static_cast<float>(*this)*val; }
183 double operator*(double val) const { return static_cast<double>(*this)*val; }
185 operator char() const { return v>>precision; }
186 operator unsigned char() const { return v>>precision; }
187 operator short() const { return v>>precision; }
188 operator unsigned short() const { return v>>precision; }
189 operator int() const { return v>>precision; }
190 operator unsigned int() const { return v>>precision; }
192 operator float() const { return ldexpf(v,-precision); }
193 operator double() const { return ldexp(v,-precision); }
194 private:
195 T v;
196 };
198 template<typename T, unsigned int precision> FixedPoint<T,precision> operator *(char a, FixedPoint<T,precision> b) { return b*=a; }
199 template<typename T, unsigned int precision> FixedPoint<T,precision> operator *(unsigned char a, FixedPoint<T,precision> b) { return b*=a; }
200 template<typename T, unsigned int precision> FixedPoint<T,precision> operator *(short a, FixedPoint<T,precision> b) { return b*=a; }
201 template<typename T, unsigned int precision> FixedPoint<T,precision> operator *(unsigned short a, FixedPoint<T,precision> b) { return b*=a; }
202 template<typename T, unsigned int precision> FixedPoint<T,precision> operator *(int a, FixedPoint<T,precision> b) { return b*=a; }
203 template<typename T, unsigned int precision> FixedPoint<T,precision> operator *(unsigned int a, FixedPoint<T,precision> b) { return b*=a; }
205 template<typename T, unsigned int precision> float operator *(float a, FixedPoint<T,precision> b) { return b*a; }
206 template<typename T, unsigned int precision> double operator *(double a, FixedPoint<T,precision> b) { return b*a; }
208 }
210 }
212 #endif
213 /*
214 Local Variables:
215 mode:c++
216 c-file-style:"stroustrup"
217 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
218 indent-tabs-mode:nil
219 fill-column:99
220 End:
221 */
222 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
223 /*
224 * Inkscape::Util::FixedPoint - fixed point type
225 *
226 * Authors:
227 * Jasper van de Gronde <th.v.d.gronde@hccnet.net>
228 *
229 * Copyright (C) 2006 Jasper van de Gronde
230 *
231 * Released under GNU GPL, read the file 'COPYING' for more information
232 */
234 #ifndef SEEN_INKSCAPE_UTIL_FIXED_POINT_H
235 #define SEEN_INKSCAPE_UTIL_FIXED_POINT_H
237 #include "traits/reference.h"
238 #include <math.h>
239 #include <algorithm>
240 #include <limits>
242 namespace Inkscape {
244 namespace Util {
246 template <typename T, unsigned int precision>
247 class FixedPoint {
248 public:
249 FixedPoint() {}
250 FixedPoint(const FixedPoint& value) : v(value.v) {}
251 FixedPoint(char value) : v(static_cast<T>(value)<<precision) {}
252 FixedPoint(unsigned char value) : v(static_cast<T>(value)<<precision) {}
253 FixedPoint(short value) : v(static_cast<T>(value)<<precision) {}
254 FixedPoint(unsigned short value) : v(static_cast<T>(value)<<precision) {}
255 FixedPoint(int value) : v(static_cast<T>(value)<<precision) {}
256 FixedPoint(unsigned int value) : v(static_cast<T>(value)<<precision) {}
257 FixedPoint(double value) : v(static_cast<T>(floor(value*(1<<precision)))) {}
259 FixedPoint& operator+=(FixedPoint val) { v += val.v; return *this; }
260 FixedPoint& operator-=(FixedPoint val) { v -= val.v; return *this; }
261 FixedPoint& operator*=(FixedPoint val) {
262 const unsigned int half_size = 8*sizeof(T)/2;
263 const T al = v&((1<<half_size)-1), bl = val.v&((1<<half_size)-1);
264 const T ah = v>>half_size, bh = val.v>>half_size;
265 v = static_cast<unsigned int>(al*bl)>>precision;
266 if ( half_size >= precision ) {
267 v += ((al*bh)+(ah*bl)+((ah*bh)<<half_size))<<(half_size-precision);
268 } else {
269 v += ((al*bh)+(ah*bl))>>(precision-half_size);
270 v += (ah*bh)<<(2*half_size-precision);
271 }
272 return *this;
273 }
275 FixedPoint& operator*=(char val) { v *= val; return *this; }
276 FixedPoint& operator*=(unsigned char val) { v *= val; return *this; }
277 FixedPoint& operator*=(short val) { v *= val; return *this; }
278 FixedPoint& operator*=(unsigned short val) { v *= val; return *this; }
279 FixedPoint& operator*=(int val) { v *= val; return *this; }
280 FixedPoint& operator*=(unsigned int val) { v *= val; return *this; }
282 FixedPoint operator+(FixedPoint val) const { FixedPoint r(*this); return r+=val; }
283 FixedPoint operator-(FixedPoint val) const { FixedPoint r(*this); return r-=val; }
284 FixedPoint operator*(FixedPoint val) const { FixedPoint r(*this); return r*=val; }
286 FixedPoint operator*(char val) const { FixedPoint r(*this); return r*=val; }
287 FixedPoint operator*(unsigned char val) const { FixedPoint r(*this); return r*=val; }
288 FixedPoint operator*(short val) const { FixedPoint r(*this); return r*=val; }
289 FixedPoint operator*(unsigned short val) const { FixedPoint r(*this); return r*=val; }
290 FixedPoint operator*(int val) const { FixedPoint r(*this); return r*=val; }
291 FixedPoint operator*(unsigned int val) const { FixedPoint r(*this); return r*=val; }
293 float operator*(float val) const { return static_cast<float>(*this)*val; }
294 double operator*(double val) const { return static_cast<double>(*this)*val; }
296 operator char() const { return v>>precision; }
297 operator unsigned char() const { return v>>precision; }
298 operator short() const { return v>>precision; }
299 operator unsigned short() const { return v>>precision; }
300 operator int() const { return v>>precision; }
301 operator unsigned int() const { return v>>precision; }
303 operator float() const { return ldexpf(v,-precision); }
304 operator double() const { return ldexp(v,-precision); }
305 private:
306 T v;
307 };
309 template<typename T, unsigned int precision> FixedPoint<T,precision> operator *(char a, FixedPoint<T,precision> b) { return b*=a; }
310 template<typename T, unsigned int precision> FixedPoint<T,precision> operator *(unsigned char a, FixedPoint<T,precision> b) { return b*=a; }
311 template<typename T, unsigned int precision> FixedPoint<T,precision> operator *(short a, FixedPoint<T,precision> b) { return b*=a; }
312 template<typename T, unsigned int precision> FixedPoint<T,precision> operator *(unsigned short a, FixedPoint<T,precision> b) { return b*=a; }
313 template<typename T, unsigned int precision> FixedPoint<T,precision> operator *(int a, FixedPoint<T,precision> b) { return b*=a; }
314 template<typename T, unsigned int precision> FixedPoint<T,precision> operator *(unsigned int a, FixedPoint<T,precision> b) { return b*=a; }
316 template<typename T, unsigned int precision> float operator *(float a, FixedPoint<T,precision> b) { return b*a; }
317 template<typename T, unsigned int precision> double operator *(double a, FixedPoint<T,precision> b) { return b*a; }
319 }
321 }
323 #endif
324 /*
325 Local Variables:
326 mode:c++
327 c-file-style:"stroustrup"
328 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
329 indent-tabs-mode:nil
330 fill-column:99
331 End:
332 */
333 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :