05a73dab515cadf2c32c5935d0bbdba3ed375425
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 "util/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 :