summaryrefslogtreecommitdiff
path: root/noncore/unsupported/qpdf/fixed.h
blob: 44aaa5e7fbe356b82b8ff604a8073c8dfe827194 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#ifndef __FIXED_H__
#define __FIXED_H__

#include <stdlib.h>
#include <iostream>

#define _GCC_TEMPLATE_BUG_ 1

template <unsigned int SH> class fixed {
public:
	inline fixed ( int i = 0 )      : m_f ( i2f( i ) ) { }
	inline fixed ( double d )       : m_f ( d2f( d )) { }
	inline fixed ( const fixed &f ) : m_f ( f. m_f ) { }
	
	inline operator bool ( ) const   { return m_f != 0; }	
	inline operator double ( ) const { return f2d( m_f ); }
	inline operator float ( ) const  { return (float) f2d( m_f ); }
	inline operator int ( ) const    { return (int) f2i( m_f ); }

	inline fixed &operator = ( const fixed &f ) { m_f = f. m_f; return *this; }
	inline fixed &operator = ( double d )       { m_f = d2f( d ); return *this; }
	inline fixed &operator = ( float f )        { m_f = d2f( f ); return *this; }
	inline fixed &operator = ( int i )          { m_f = i2f( i ); return *this; }

	inline fixed &operator += ( const fixed &f ) { m_f += f. m_f; return *this; }
	inline fixed &operator -= ( const fixed &f ) { m_f -= f. m_f; return *this; }
	inline fixed &operator *= ( const fixed &f ) { m_f = mul ( m_f, f. m_f ); return *this; }
	inline fixed &operator /= ( const fixed &f ) { m_f = div ( m_f, f. m_f ); return *this; }

	inline fixed &operator += ( int i ) { m_f += i2f( i ); return *this; }
	inline fixed &operator -= ( int i ) { m_f -= i2f( i ); return *this; }
	inline fixed &operator *= ( int i ) { m_f *= i; return *this; }
	inline fixed &operator /= ( int i ) { m_f /= i; return *this; }

	inline fixed &operator += ( double d ) { m_f += d2f( d ); return *this; }
	inline fixed &operator -= ( double d ) { m_f -= d2f( d ); return *this; }
	inline fixed &operator *= ( double d ) { m_f = mul ( m_f, d2f( d )); return *this; }
	inline fixed &operator /= ( double d ) { m_f = div ( m_f, d2f( d )); return *this; }

	inline fixed operator - ( ) const { return fixed ( -m_f, true ); }

	inline fixed operator + ( const fixed &f ) const { return fixed ( m_f + f. m_f, true ); }
	inline fixed operator - ( const fixed &f ) const { return fixed ( m_f - f. m_f, true ); }
	inline fixed operator * ( const fixed &f ) const { return fixed ( mul ( m_f, f. m_f ), true ); }
	inline fixed operator / ( const fixed &f ) const { return fixed ( div ( m_f, f. m_f ), true ); }

	inline fixed operator + ( double d ) const { return fixed ( m_f + d2f( d ), true ); }
	inline fixed operator - ( double d ) const { return fixed ( m_f - d2f( d ), true ); }
	inline fixed operator * ( double d ) const { return fixed ( mul ( m_f, d2f( d )), true ); }
	inline fixed operator / ( double d ) const { return fixed ( div ( m_f, d2f( d )), true ); }

	inline fixed operator + ( int i ) const { return fixed ( m_f + i2f( i ), true ); }
	inline fixed operator - ( int i ) const { return fixed ( m_f - i2f( i ), true ); }
	inline fixed operator * ( int i ) const { return fixed ( m_f * i, true ); }
	inline fixed operator / ( int i ) const { return fixed ( m_f / i, true ); }

	inline bool operator <  ( const fixed &f ) const { return m_f < f. m_f; }
	inline bool operator >  ( const fixed &f ) const { return m_f > f. m_f; }
	inline bool operator <= ( const fixed &f ) const { return m_f <= f. m_f; }
	inline bool operator >= ( const fixed &f ) const { return m_f >= f. m_f; }
	inline bool operator == ( const fixed &f ) const { return m_f == f. m_f; }
	inline bool operator != ( const fixed &f ) const { return m_f != f. m_f; }

	inline bool operator <  ( double d ) const { return m_f < d2f( d ); }
	inline bool operator >  ( double d ) const { return m_f > d2f( d ); }
	inline bool operator <= ( double d ) const { return m_f <= d2f( d ); }
	inline bool operator >= ( double d ) const { return m_f >= d2f( d ); }
	inline bool operator == ( double d ) const { return m_f == d2f( d ); }
	inline bool operator != ( double d ) const { return m_f != d2f( d ); }

	inline bool operator <  ( int i ) const { return m_f < i2f( i ); }
	inline bool operator >  ( int i ) const { return m_f > i2f( i ); }
	inline bool operator <= ( int i ) const { return m_f <= i2f( i ); }
	inline bool operator >= ( int i ) const { return m_f >= i2f( i ); }
	inline bool operator == ( int i ) const { return m_f == i2f( i ); }
	inline bool operator != ( int i ) const { return m_f != i2f( i ); }

#if _GCC_TEMPLATE_BUG_
public: 
#else
private:
#endif
	typedef int fix_t;

	inline static double f2d ( fix_t f )   { return ((double) f ) / ((double) ( 1 << SH )); }
	inline static fix_t  d2f ( double d )  { return (fix_t) ( d * ((double) ( 1 << SH ))); }
	
	inline static int    f2i ( fix_t f )   { return (int) ( f >> SH ); }
	inline static fix_t  i2f ( int i )     { return (fix_t) ( i << SH ); }	
	
	inline static fix_t mul ( fix_t m1, fix_t m2 )	{ return (fix_t) ((((long long int) m1 ) * m2 ) >> SH ); }	
	inline static fix_t div ( fix_t d1, fix_t d2 )	{ return (fix_t) ((((long long int) d1 ) << SH ) / d2 ); }
	
	fixed ( fix_t f, bool /*dummy*/ ) : m_f ( f ) { }

	//data
	fix_t m_f;

// friends:		
#if !_GCC_TEMPLATE_BUG_
	friend fixed operator + <> ( int i, const fixed &f );
	friend fixed operator - <> ( int i, const fixed &f );
	friend fixed operator * <> ( int i, const fixed &f );
	friend fixed operator / <> ( int i, const fixed &f );

	friend fixed operator + <> ( double d, const fixed &f );
	friend fixed operator - <> ( double d, const fixed &f );
	friend fixed operator * <> ( double d, const fixed &f );
	friend fixed &operator / <> ( double d, const fixed<SH> &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 == <> ( 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 ); }
template <unsigned int SH> inline fixed<SH> operator * ( double d, const fixed<SH> &f ) { return fixed<SH> ( fixed<SH>::mul ( 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>::mul ( fixed<SH>::d2f( d ), f. m_f ), true ); }

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 == ( 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 long int lrint ( const fixed<SH> &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. 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 );
		
	typename fixed<SH>::fix_t a0 = 0;
	typename 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 );
}

#if 0 // no std::ostream needed in OPIE
template <unsigned int SH> inline std::ostream &operator << ( std::ostream &o, const fixed<SH> &f )
{
	o << double( f );
	return o;
}
#endif

#endif