Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
fixed_point.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Inkscape::Util::FixedPoint - fixed point type
4 *
5 * Authors:
6 * Jasper van de Gronde <th.v.d.gronde@hccnet.net>
7 *
8 * Copyright (C) 2006 Jasper van de Gronde
9 *
10 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
11 */
12
13#ifndef SEEN_INKSCAPE_UTIL_FIXED_POINT_H
14#define SEEN_INKSCAPE_UTIL_FIXED_POINT_H
15
16#include <cmath>
17#include <algorithm>
18#include <limits>
19
20namespace Inkscape {
21
22namespace Util {
23
24template <typename T, unsigned int precision>
26public:
27 FixedPoint() = default;
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)))) {}
36
37 FixedPoint& operator+=(FixedPoint val) { v += val.v; return *this; }
38 FixedPoint& operator-=(FixedPoint val) { v -= val.v; return *this; }
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 }
52
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; }
59
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; }
63
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; }
70
71 float operator*(float val) const { return static_cast<float>(*this)*val; }
72 double operator*(double val) const { return static_cast<double>(*this)*val; }
73
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; }
80
81 operator float() const { return ldexpf(v,-precision); }
82 operator double() const { return ldexp(v,-precision); }
83private:
84 T v;
85};
86
87template<typename T, unsigned int precision> FixedPoint<T,precision> operator *(char a, FixedPoint<T,precision> b) { return b*=a; }
88template<typename T, unsigned int precision> FixedPoint<T,precision> operator *(unsigned char a, FixedPoint<T,precision> b) { return b*=a; }
89template<typename T, unsigned int precision> FixedPoint<T,precision> operator *(short a, FixedPoint<T,precision> b) { return b*=a; }
90template<typename T, unsigned int precision> FixedPoint<T,precision> operator *(unsigned short a, FixedPoint<T,precision> b) { return b*=a; }
91template<typename T, unsigned int precision> FixedPoint<T,precision> operator *(int a, FixedPoint<T,precision> b) { return b*=a; }
92template<typename T, unsigned int precision> FixedPoint<T,precision> operator *(unsigned int a, FixedPoint<T,precision> b) { return b*=a; }
93
94template<typename T, unsigned int precision> float operator *(float a, FixedPoint<T,precision> b) { return b*a; }
95template<typename T, unsigned int precision> double operator *(double a, FixedPoint<T,precision> b) { return b*a; }
96
97}
98
99}
100
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:fileencoding=utf-8:textwidth=99 :
FixedPoint & operator-=(FixedPoint val)
Definition fixed_point.h:38
FixedPoint & operator+=(FixedPoint val)
Definition fixed_point.h:37
FixedPoint operator-(FixedPoint val) const
Definition fixed_point.h:61
FixedPoint operator*(int val) const
Definition fixed_point.h:68
FixedPoint & operator*=(unsigned int val)
Definition fixed_point.h:58
FixedPoint operator*(unsigned short val) const
Definition fixed_point.h:67
FixedPoint(unsigned char value)
Definition fixed_point.h:30
FixedPoint(const FixedPoint &value)
Definition fixed_point.h:28
FixedPoint operator*(FixedPoint val) const
Definition fixed_point.h:62
double operator*(double val) const
Definition fixed_point.h:72
FixedPoint & operator*=(int val)
Definition fixed_point.h:57
FixedPoint & operator*=(unsigned short val)
Definition fixed_point.h:56
FixedPoint & operator*=(char val)
Definition fixed_point.h:53
FixedPoint operator+(FixedPoint val) const
Definition fixed_point.h:60
FixedPoint operator*(char val) const
Definition fixed_point.h:64
FixedPoint(unsigned int value)
Definition fixed_point.h:34
float operator*(float val) const
Definition fixed_point.h:71
FixedPoint & operator*=(short val)
Definition fixed_point.h:55
FixedPoint & operator*=(FixedPoint val)
Definition fixed_point.h:39
FixedPoint operator*(unsigned int val) const
Definition fixed_point.h:69
FixedPoint operator*(unsigned char val) const
Definition fixed_point.h:65
FixedPoint & operator*=(unsigned char val)
Definition fixed_point.h:54
FixedPoint operator*(short val) const
Definition fixed_point.h:66
FixedPoint(unsigned short value)
Definition fixed_point.h:32
auto floor(Geom::Rect const &rect)
Definition geom.h:131
Miscellaneous supporting code.
Definition document.h:93
FixedPoint< T, precision > operator*(char a, FixedPoint< T, precision > b)
Definition fixed_point.h:87
Helper class to stream background task notifications as a series of messages.