13#ifndef SEEN_INKSCAPE_UTIL_FIXED_POINT_H
14#define SEEN_INKSCAPE_UTIL_FIXED_POINT_H
24template <
typename T,
unsigned int precision>
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) {}
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);
47 v += ((al*bh)+(ah*bl))>>(precision-half_size);
48 v += (ah*bh)<<(2*half_size-precision);
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); }
FixedPoint & operator-=(FixedPoint val)
FixedPoint & operator+=(FixedPoint val)
FixedPoint operator-(FixedPoint val) const
FixedPoint operator*(int val) const
FixedPoint & operator*=(unsigned int val)
FixedPoint operator*(unsigned short val) const
FixedPoint(unsigned char value)
FixedPoint(const FixedPoint &value)
FixedPoint operator*(FixedPoint val) const
double operator*(double val) const
FixedPoint & operator*=(int val)
FixedPoint & operator*=(unsigned short val)
FixedPoint & operator*=(char val)
FixedPoint operator+(FixedPoint val) const
FixedPoint operator*(char val) const
FixedPoint(unsigned int value)
float operator*(float val) const
FixedPoint & operator*=(short val)
FixedPoint & operator*=(FixedPoint val)
FixedPoint operator*(unsigned int val) const
FixedPoint operator*(unsigned char val) const
FixedPoint & operator*=(unsigned char val)
FixedPoint operator*(short val) const
FixedPoint(unsigned short value)
auto floor(Geom::Rect const &rect)
Miscellaneous supporting code.
FixedPoint< T, precision > operator*(char a, FixedPoint< T, precision > b)
Helper class to stream background task notifications as a series of messages.