// SPDX-License-Identifier: GPL-2.0-or-later /* * Inkscape::Util::FixedPoint - fixed point type * * Authors: * Jasper van de Gronde * * Copyright (C) 2006 Jasper van de Gronde * * Released under GNU GPL v2+, read the file 'COPYING' for more information. */ #ifndef SEEN_INKSCAPE_UTIL_FIXED_POINT_H #define SEEN_INKSCAPE_UTIL_FIXED_POINT_H #include #include #include namespace Inkscape { namespace Util { template class FixedPoint { public: FixedPoint() = default; FixedPoint(const FixedPoint& value) : v(value.v) {} FixedPoint(char value) : v(static_cast(value)<(value)<(value)<(value)<(value)<(value)<(floor(value*(1<>half_size, bh = val.v>>half_size; v = static_cast(al*bl)>>precision; if ( half_size >= precision ) { v += ((al*bh)+(ah*bl)+((ah*bh)<>(precision-half_size); v += (ah*bh)<<(2*half_size-precision); } return *this; } FixedPoint& operator*=(char val) { v *= val; return *this; } FixedPoint& operator*=(unsigned char val) { v *= val; return *this; } FixedPoint& operator*=(short val) { v *= val; return *this; } FixedPoint& operator*=(unsigned short val) { v *= val; return *this; } FixedPoint& operator*=(int val) { v *= val; return *this; } FixedPoint& operator*=(unsigned int val) { v *= val; return *this; } FixedPoint operator+(FixedPoint val) const { FixedPoint r(*this); return r+=val; } FixedPoint operator-(FixedPoint val) const { FixedPoint r(*this); return r-=val; } FixedPoint operator*(FixedPoint val) const { FixedPoint r(*this); return r*=val; } FixedPoint operator*(char val) const { FixedPoint r(*this); return r*=val; } FixedPoint operator*(unsigned char val) const { FixedPoint r(*this); return r*=val; } FixedPoint operator*(short val) const { FixedPoint r(*this); return r*=val; } FixedPoint operator*(unsigned short val) const { FixedPoint r(*this); return r*=val; } FixedPoint operator*(int val) const { FixedPoint r(*this); return r*=val; } FixedPoint operator*(unsigned int val) const { FixedPoint r(*this); return r*=val; } float operator*(float val) const { return static_cast(*this)*val; } double operator*(double val) const { return static_cast(*this)*val; } operator char() const { return v>>precision; } operator unsigned char() const { return v>>precision; } operator short() const { return v>>precision; } operator unsigned short() const { return v>>precision; } operator int() const { return v>>precision; } operator unsigned int() const { return v>>precision; } operator float() const { return ldexpf(v,-precision); } operator double() const { return ldexp(v,-precision); } private: T v; }; template FixedPoint operator *(char a, FixedPoint b) { return b*=a; } template FixedPoint operator *(unsigned char a, FixedPoint b) { return b*=a; } template FixedPoint operator *(short a, FixedPoint b) { return b*=a; } template FixedPoint operator *(unsigned short a, FixedPoint b) { return b*=a; } template FixedPoint operator *(int a, FixedPoint b) { return b*=a; } template FixedPoint operator *(unsigned int a, FixedPoint b) { return b*=a; } template float operator *(float a, FixedPoint b) { return b*a; } template double operator *(double a, FixedPoint b) { return b*a; } } } #endif /* Local Variables: mode:c++ c-file-style:"stroustrup" c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) indent-tabs-mode:nil fill-column:99 End: */ // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :