Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #ifndef _MCVMATH_HXX
30 : : #define _MCVMATH_HXX
31 : :
32 : : #include <tools/solar.h>
33 : :
34 : : class FixCpx;
35 : :
36 : : // No of fractal bits
37 : : // allowed range 0..14, must be even
38 : : #define FIX_POST 14
39 : :
40 : : // scale for ...Big() -Functions
41 : : #if (FIX_POST>=4)
42 : : #define FIX_P2 4
43 : : #define FIX_P3 (FIX_POST-FIX_P2)
44 : : #else
45 : : #define FIX_P2 0
46 : : #define FIX_P3 FIX_POST
47 : : #endif
48 : :
49 : : #if (FIX_POST>=1)
50 : : #define FIX_ADD (1<<(FIX_POST-1))
51 : : #else
52 : : #define FIX_ADD 0
53 : : #endif
54 : :
55 : : #if (FIX_P2>=1)
56 : : #define FIX_A2 (1<<(FIX_P2-1))
57 : : #else
58 : : #define FIX_A2 0
59 : : #endif
60 : :
61 : : #if (FIX_P3>=1)
62 : : #define FIX_A3 (1<<(FIX_P3-1))
63 : : #else
64 : : #define FIX_A3 0
65 : : #endif
66 : :
67 : : // -------
68 : : // - Fix -
69 : : // -------
70 : :
71 : : class Fix
72 : : {
73 : : private:
74 : : friend class FixCpx;
75 : :
76 : : public:
77 : : long x;
78 : :
79 : : public:
80 : 0 : Fix() { x=0; }
81 : : Fix( int i ) { x=(long(i)<<FIX_POST); }
82 : : Fix( short l ) { x=(long(l)<<FIX_POST); }
83 : : Fix( sal_uInt16 l ) { x=(long(l)<<FIX_POST); }
84 : 0 : Fix( long l ) { x=(l<<FIX_POST); }
85 : : Fix( long Z, long N ) { x=(Z<<FIX_POST)/N; }
86 : :
87 : : void SetInternVal( long nVal ) { x=nVal; }
88 : : long GetInternVal() const { return x; }
89 : :
90 : : void operator+= ( const Fix& a ) { x+=a.x; }
91 : : void operator-= ( const Fix& a ) { x-=a.x; }
92 : : void operator*= ( const Fix& a ) { x=(x*a.x+FIX_ADD)>>FIX_POST; }
93 : : void operator/= ( const Fix& a ) { x=(x<<FIX_POST)/a.x; }
94 : : friend Fix operator- ( const Fix& a );
95 : :
96 : : void MultBig( const Fix& a )
97 : : { x=((((a.x+FIX_A2)>>FIX_P2)*x+FIX_A3)>>FIX_P3); }
98 : : void DivBig( const Fix& a )
99 : : { x=((x<<FIX_P3)/a.x)<<FIX_P2; }
100 : :
101 : : friend sal_Bool operator> ( const Fix& a, const Fix& b ) { return a.x > b.x; }
102 : : friend sal_Bool operator< ( const Fix& a, const Fix& b ) { return a.x < b.x; }
103 : :
104 : : operator long() const { return (x+FIX_ADD) >> FIX_POST; }
105 : : operator double() const { return double(x)/(1<<FIX_POST); }
106 : :
107 : : friend Fix operator+ ( const Fix& a, const Fix& b );
108 : : friend Fix operator- ( const Fix& a, const Fix& b );
109 : : friend Fix operator* ( const Fix& a, const Fix& b );
110 : : friend Fix operator/ ( const Fix& a, const Fix& b );
111 : :
112 : : friend FixCpx operator-( const FixCpx& a );
113 : : };
114 : :
115 : : // ----------
116 : : // - FixCpx -
117 : : // ----------
118 : :
119 : : class FixCpx
120 : : {
121 : : public:
122 : : Fix r;
123 : : Fix i;
124 : :
125 : : public:
126 : 0 : FixCpx() : r(), i() {}
127 : 0 : FixCpx( Fix a ) : r( a ), i() {}
128 : : FixCpx( Fix a, Fix b ) : r( a ), i( b ) {}
129 : :
130 : : Fix& GetReal() { return r; }
131 : : Fix& GetImag() { return i; }
132 : :
133 : : void operator*= ( const FixCpx& ra );
134 : : void MultBig( const FixCpx& ra, const FixCpx& rb );
135 : :
136 : : friend FixCpx operator+ ( const FixCpx& a, const FixCpx& b );
137 : : friend FixCpx operator- ( const FixCpx& a, const FixCpx& b );
138 : : friend FixCpx operator* ( const FixCpx& a, const FixCpx& b );
139 : : friend FixCpx operator/ ( const FixCpx& a, const FixCpx& b );
140 : : friend FixCpx operator- ( const FixCpx& a );
141 : : };
142 : :
143 : : inline Fix operator- ( const Fix& a )
144 : : {
145 : : Fix f;
146 : : f.x = -a.x;
147 : : return f;
148 : : }
149 : :
150 : 0 : inline Fix operator+ ( const Fix& a, const Fix& b )
151 : : {
152 : 0 : long l = a.x+b.x;
153 : 0 : return *((Fix*)&l);
154 : : }
155 : :
156 : 0 : inline Fix operator- ( const Fix& a, const Fix& b )
157 : : {
158 : 0 : long l = a.x-b.x;
159 : 0 : return *((Fix*)&l);
160 : : }
161 : :
162 : 0 : inline Fix operator* ( const Fix& a, const Fix& b )
163 : : {
164 : 0 : long l=(a.x*b.x+FIX_ADD)>>FIX_POST;
165 : 0 : return *((Fix*)&l);
166 : : }
167 : :
168 : : inline Fix operator/ ( const Fix& a, const Fix& b )
169 : : {
170 : : long l=(a.x<<FIX_POST)/b.x;
171 : : return *((Fix*)&l);
172 : : }
173 : :
174 : : inline FixCpx operator- ( const FixCpx& a )
175 : : {
176 : : FixCpx fc;
177 : :
178 : : fc.r.x = -a.r.x;
179 : : fc.i.x = -a.i.x;
180 : : return fc;
181 : : }
182 : :
183 : : inline FixCpx operator+ ( const FixCpx& a, const FixCpx& b )
184 : : {
185 : : return FixCpx( a.r+b.r, a.i+b.i );
186 : : }
187 : :
188 : : inline FixCpx operator- ( const FixCpx& a, const FixCpx& b )
189 : : {
190 : : return FixCpx( a.r-b.r, a.i-b.i );
191 : : }
192 : :
193 : 0 : inline void FixCpx::operator*= ( const FixCpx& ra )
194 : : {
195 : 0 : Fix rr = ra.r*r-ra.i*i;
196 : 0 : i = ra.r*i+ra.i*r;
197 : 0 : r = rr;
198 : 0 : }
199 : :
200 : : inline FixCpx operator* ( const FixCpx& a, const FixCpx& b )
201 : : {
202 : : return FixCpx( a.r*b.r-a.i*b.i, a.r*b.i+a.i*b.r );
203 : : }
204 : :
205 : : inline FixCpx operator/ ( const FixCpx& a, const FixCpx& b )
206 : : {
207 : : return FixCpx( (a.r*b.r+a.i*b.i)/(b.r*b.r+b.i*b.i),
208 : : (b.r*a.r-a.r*b.i)/(b.r*b.r+b.i*b.i) );
209 : : }
210 : :
211 : : // -----------------------------------------------------------------------
212 : :
213 : : Fix ImpMultBig2( const Fix& a, const Fix& b );
214 : :
215 : : sal_uInt16 ImpSqrt( sal_uLong nRadi );
216 : : FixCpx ImpExPI( sal_uInt16 nPhi );
217 : :
218 : : #endif // _MCVMATH_HXX
219 : :
220 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|