Diffstat (limited to 'noncore/unsupported/qpdf/fixed.h') (more/less context) (show 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 | |||
@@ -122,2 +122,6 @@ private: | |||
122 | friend bool operator != <> ( int i, const fixed &f ); | 122 | friend bool operator != <> ( int i, const fixed &f ); |
123 | |||
124 | friend long int lrint ( const fixed &f ); | ||
125 | friend fixed sqrt ( const fixed &f ); | ||
126 | friend fixed fabs ( const fixed &f ); | ||
123 | #endif | 127 | #endif |
@@ -151,6 +155,5 @@ template <unsigned int SH> inline bool operator != ( int i, const fixed<SH> &f ) | |||
151 | 155 | ||
152 | 156 | template <unsigned int SH> inline long int lrint ( const fixed<SH> &f ) | |
153 | template <unsigned int SH> inline fixed<SH> sqrt ( const fixed<SH> &f ) | ||
154 | { | 157 | { |
155 | return fixed<SH> ( double( sqrt ( double( f )))); | 158 | return fixed<SH>::f2i (( f. m_f < 0 ) ? f. m_f - ( 1 << ( SH - 1 )) : f. m_f + ( 1 << ( SH - 1 ))); |
156 | } | 159 | } |
@@ -159,3 +162,20 @@ template <unsigned int SH> inline fixed<SH> fabs ( const fixed<SH> &f ) | |||
159 | { | 162 | { |
160 | return ( f < 0 ) ? -f : f; | 163 | return ( f. m_f < 0 ) ? fixed<SH> ( -f. m_f, true ) : f; |
164 | } | ||
165 | |||
166 | // roughly from QPE / qmath.h | ||
167 | template <unsigned int SH> inline fixed<SH> sqrt ( const fixed<SH> &f ) | ||
168 | { | ||
169 | if ( f. m_f <= 0 ) | ||
170 | return fixed<SH> ( 0, true ); | ||
171 | |||
172 | fixed<SH>::fix_t a0 = 0; | ||
173 | fixed<SH>::fix_t a1 = f. m_f; // take value as first approximation | ||
174 | |||
175 | do { | ||
176 | a0 = a1; | ||
177 | a1 = ( a0 + fixed<SH>::div ( f. m_f, a0 )) >> 1; | ||
178 | } while ( abs ( fixed<SH>::div ( a1 - a0, a1 )) > 1 ); | ||
179 | |||
180 | return fixed<SH> ( a1, true ); | ||
161 | } | 181 | } |