Diffstat (limited to 'noncore/unsupported/qpdf/fixed.h') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/unsupported/qpdf/fixed.h | 28 |
1 files changed, 24 insertions, 4 deletions
diff --git a/noncore/unsupported/qpdf/fixed.h b/noncore/unsupported/qpdf/fixed.h index 111b95e..c912954 100644 --- a/noncore/unsupported/qpdf/fixed.h +++ b/noncore/unsupported/qpdf/fixed.h @@ -111,24 +111,28 @@ private: friend bool operator > <> ( double d, const fixed &f ); friend bool operator <= <> ( double d, const fixed &f ); friend bool operator >= <> ( double d, const fixed &f ); friend bool operator == <> ( double d, const fixed &f ); friend bool operator != <> ( double d, const fixed &f ); friend bool operator < <> ( int i, const fixed &f ); friend bool operator > <> ( int i, const fixed &f ); friend bool operator <= <> ( int i, const fixed &f ); friend bool operator >= <> ( int i, const fixed &f ); friend bool operator == <> ( int i, const fixed &f ); friend bool operator != <> ( int i, const fixed &f ); + + friend long int lrint ( const fixed &f ); + friend fixed sqrt ( const fixed &f ); + friend fixed fabs ( const fixed &f ); #endif }; template <unsigned int SH> inline fixed<SH> operator + ( int i, const fixed<SH> &f ) { return fixed<SH> ( fixed<SH>::i2f( i ) + f. m_f, true ); } template <unsigned int SH> inline fixed<SH> operator - ( int i, const fixed<SH> &f ) { return fixed<SH> ( fixed<SH>::i2f( i ) - f. m_f, true ); } template <unsigned int SH> inline fixed<SH> operator * ( int i, const fixed<SH> &f ) { return fixed<SH> ( i * f. m_f, true ); } template <unsigned int SH> inline fixed<SH> operator / ( int i, const fixed<SH> &f ) { return fixed<SH> ( fixed<SH>::div ( fixed<SH>::i2f( i ), f. m_f ), true ); } //template <unsigned int SH> inline fixed<SH> operator / ( int i, const fixed<SH> &f ) { return fixed<SH> ( fixed<SH>::i2f ( i / fixed<SH>::f2i ( f. m_f )), true ); } template <unsigned int SH> inline fixed<SH> operator + ( double d, const fixed<SH> &f ) { return fixed<SH> ( fixed<SH>::d2f( d ) + f. m_f, true ); } template <unsigned int SH> inline fixed<SH> operator - ( double d, const fixed<SH> &f ) { return fixed<SH> ( fixed<SH>::d2f( d ) - f. m_f, true ); } @@ -140,30 +144,46 @@ template <unsigned int SH> inline bool operator > ( double d, const fixed<SH> & template <unsigned int SH> inline bool operator <= ( double d, const fixed<SH> &f ) { return fixed<SH>::d2f( d ) <= f. m_f; } template <unsigned int SH> inline bool operator >= ( double d, const fixed<SH> &f ) { return fixed<SH>::d2f( d ) >= f. m_f; } template <unsigned int SH> inline bool operator == ( double d, const fixed<SH> &f ) { return fixed<SH>::d2f( d ) == f. m_f; } template <unsigned int SH> inline bool operator != ( double d, const fixed<SH> &f ) { return fixed<SH>::d2f( d ) != f. m_f; } template <unsigned int SH> inline bool operator < ( int i, const fixed<SH> &f ) { return fixed<SH>::i2f( i ) < f. m_f; } template <unsigned int SH> inline bool operator > ( int i, const fixed<SH> &f ) { return fixed<SH>::i2f( i ) > f. m_f; } template <unsigned int SH> inline bool operator <= ( int i, const fixed<SH> &f ) { return fixed<SH>::i2f( i ) <= f. m_f; } template <unsigned int SH> inline bool operator >= ( int i, const fixed<SH> &f ) { return fixed<SH>::i2f( i ) >= f. m_f; } template <unsigned int SH> inline bool operator == ( int i, const fixed<SH> &f ) { return fixed<SH>::i2f( i ) == f. m_f; } template <unsigned int SH> inline bool operator != ( int i, const fixed<SH> &f ) { return fixed<SH>::i2f( i ) != f. m_f; } - -template <unsigned int SH> inline fixed<SH> sqrt ( const fixed<SH> &f ) +template <unsigned int SH> inline long int lrint ( const fixed<SH> &f ) { - return fixed<SH> ( double( sqrt ( double( f )))); + return fixed<SH>::f2i (( f. m_f < 0 ) ? f. m_f - ( 1 << ( SH - 1 )) : f. m_f + ( 1 << ( SH - 1 ))); } template <unsigned int SH> inline fixed<SH> fabs ( const fixed<SH> &f ) { - return ( f < 0 ) ? -f : f; + return ( f. m_f < 0 ) ? fixed<SH> ( -f. m_f, true ) : f; +} + +// roughly from QPE / qmath.h +template <unsigned int SH> inline fixed<SH> sqrt ( const fixed<SH> &f ) +{ + if ( f. m_f <= 0 ) + return fixed<SH> ( 0, true ); + + fixed<SH>::fix_t a0 = 0; + fixed<SH>::fix_t a1 = f. m_f; // take value as first approximation + + do { + a0 = a1; + a1 = ( a0 + fixed<SH>::div ( f. m_f, a0 )) >> 1; + } while ( abs ( fixed<SH>::div ( a1 - a0, a1 )) > 1 ); + + return fixed<SH> ( a1, true ); } template <unsigned int SH> inline ostream &operator << ( ostream &o, const fixed<SH> &f ) { o << double( f ); return o; } #endif |