00001 // $Id: fixed.cpp 1282 2006-06-09 09:46:49Z alex $ 00002 /* @@tag:xara-cn@@ DO NOT MODIFY THIS LINE 00003 ================================XARAHEADERSTART=========================== 00004 00005 Xara LX, a vector drawing and manipulation program. 00006 Copyright (C) 1993-2006 Xara Group Ltd. 00007 Copyright on certain contributions may be held in joint with their 00008 respective authors. See AUTHORS file for details. 00009 00010 LICENSE TO USE AND MODIFY SOFTWARE 00011 ---------------------------------- 00012 00013 This file is part of Xara LX. 00014 00015 Xara LX is free software; you can redistribute it and/or modify it 00016 under the terms of the GNU General Public License version 2 as published 00017 by the Free Software Foundation. 00018 00019 Xara LX and its component source files are distributed in the hope 00020 that it will be useful, but WITHOUT ANY WARRANTY; without even the 00021 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00022 See the GNU General Public License for more details. 00023 00024 You should have received a copy of the GNU General Public License along 00025 with Xara LX (see the file GPL in the root directory of the 00026 distribution); if not, write to the Free Software Foundation, Inc., 51 00027 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00028 00029 00030 ADDITIONAL RIGHTS 00031 ----------------- 00032 00033 Conditional upon your continuing compliance with the GNU General Public 00034 License described above, Xara Group Ltd grants to you certain additional 00035 rights. 00036 00037 The additional rights are to use, modify, and distribute the software 00038 together with the wxWidgets library, the wxXtra library, and the "CDraw" 00039 library and any other such library that any version of Xara LX relased 00040 by Xara Group Ltd requires in order to compile and execute, including 00041 the static linking of that library to XaraLX. In the case of the 00042 "CDraw" library, you may satisfy obligation under the GNU General Public 00043 License to provide source code by providing a binary copy of the library 00044 concerned and a copy of the license accompanying it. 00045 00046 Nothing in this section restricts any of the rights you have under 00047 the GNU General Public License. 00048 00049 00050 SCOPE OF LICENSE 00051 ---------------- 00052 00053 This license applies to this program (XaraLX) and its constituent source 00054 files only, and does not necessarily apply to other Xara products which may 00055 in part share the same code base, and are subject to their own licensing 00056 terms. 00057 00058 This license does not apply to files in the wxXtra directory, which 00059 are built into a separate library, and are subject to the wxWindows 00060 license contained within that directory in the file "WXXTRA-LICENSE". 00061 00062 This license does not apply to the binary libraries (if any) within 00063 the "libs" directory, which are subject to a separate license contained 00064 within that directory in the file "LIBS-LICENSE". 00065 00066 00067 ARRANGEMENTS FOR CONTRIBUTION OF MODIFICATIONS 00068 ---------------------------------------------- 00069 00070 Subject to the terms of the GNU Public License (see above), you are 00071 free to do whatever you like with your modifications. However, you may 00072 (at your option) wish contribute them to Xara's source tree. You can 00073 find details of how to do this at: 00074 http://www.xaraxtreme.org/developers/ 00075 00076 Prior to contributing your modifications, you will need to complete our 00077 contributor agreement. This can be found at: 00078 http://www.xaraxtreme.org/developers/contribute/ 00079 00080 Please note that Xara will not accept modifications which modify any of 00081 the text between the start and end of this header (marked 00082 XARAHEADERSTART and XARAHEADEREND). 00083 00084 00085 MARKS 00086 ----- 00087 00088 Xara, Xara LX, Xara X, Xara X/Xtreme, Xara Xtreme, the Xtreme and Xara 00089 designs are registered or unregistered trademarks, design-marks, and/or 00090 service marks of Xara Group Ltd. All rights in these marks are reserved. 00091 00092 00093 Xara Group Ltd, Gaddesden Place, Hemel Hempstead, HP2 6EX, UK. 00094 http://www.xara.com/ 00095 00096 =================================XARAHEADEREND============================ 00097 */ 00098 // 00099 // Fixed-point primitive routines 00100 // 00101 00102 00103 #include "camtypes.h" 00104 //#include "fixed.h" - in camtypes.h [AUTOMATICALLY REMOVED] 00105 00106 00107 /******************************************************************************************** 00108 00109 > FIXED16 Fixed16Mul(FIXED16 arg1, FIXED16 arg2) 00110 00111 Author: Andy_Pennell (Xara Group Ltd) <camelotdev@xara.com> 00112 Created: 10/5/93 00113 Inputs: two fixed16s 00114 Outputs: None 00115 Returns: FIXED16 result (arg1 * arg2) 00116 Scope: Only to be used in the FIXED16 class. 00117 Purpose: signed multiplication 00118 Errors: None (no overflow checks). Rounds 00119 00120 ********************************************************************************************/ 00121 00122 fixed16 Fixed16Mul(fixed16 a,fixed16 b) 00123 { 00124 fixed16 result ; 00125 // result.all = INT32(Int32x32To64(a.all,b.all)+0x8000>>16) ; 00126 result.all = INT32(((xlong)a.all*b.all+0x8000) >> 16) ; 00127 return result ; 00128 } 00129 00130 00131 /******************************************************************************************** 00132 00133 > FIXED16 Fixed16Div(FIXED16 arg1, FIXED16 arg2) 00134 00135 Author: Andy_Pennell (Xara Group Ltd) <camelotdev@xara.com> 00136 Created: 10/5/93 00137 Inputs: two fixed16s 00138 Outputs: None 00139 Returns: FIXED16 result (arg1 / arg2) 00140 Scope: Only to be used in the FIXED16 class. 00141 Purpose: signed division 00142 Errors: None (no overflow checks). No rounding either (should it?) 00143 Overflow will cause a DIV0 exception, as will divide by zero 00144 00145 ********************************************************************************************/ 00146 00147 fixed16 Fixed16Div(fixed16 a,fixed16 b) 00148 { 00149 fixed16 result ; 00150 // result.all = MulDiv(a.all,1<<16,b.all) ; 00151 result.all = INT32(((xlong)a.all<<16)/b.all) ; 00152 return result ; 00153 } 00154 00155 00156 00157 00158 00159 /******************************************************************************************** 00160 00161 > NOTREALLYAFIXED16 InternalDiv32by32( INT32 arg1, INT32 arg2 ) 00162 00163 Author: Andy_Pennell (Xara Group Ltd) <camelotdev@xara.com> 00164 Created: 11/5/93 00165 Inputs: two signed 32-bit numbers 00166 Outputs: None 00167 Returns: FIXED16 result of arg1/arg2 00168 Scope: Private to the CCMATHS library. 00169 Purpose: Obtain a fixed-point representation of the ratio between two signed 00170 numbers. 00171 Errors: None. Div0 and overflow will create a div0 exception. 00172 00173 Awooga! Awooga! Do not call this function directly - it returns a INT32 and is meant to be 00174 used only in the C++ wrapper (i.e. it returns the value << 16) 00175 00176 This is done by multiplying arg1 by 65536, then doing a 64-bit/32-bit division 00177 00178 Curiously enough, this function is 100% the same as Fixed16Div. When you think about it, 00179 its not really that curious as both args are 65536 times bigger than the FIXED16 versions, 00180 and x/y is identical to x*65536/y*65536. 00181 00182 ********************************************************************************************/ 00183 00184 fixed16 InternalDiv32by32( INT32 a, INT32 b ) 00185 { 00186 fixed16 result ; 00187 // result.all = MulDiv(a.all,1<<16,b.all) ; 00188 result.all = INT32( ( (xlong)a << 16 ) / b ); 00189 return result ; 00190 } 00191 00192 00193 00194 /******************************************************************************************** 00195 00196 > double Fixed16ToDouble( FIXED16 arg ) 00197 00198 Author: Andy_Pennell (Xara Group Ltd) <camelotdev@xara.com> 00199 Created: 13/5/93 00200 Inputs: FIXED16 argument 00201 Outputs: None 00202 Returns: double-precision value 00203 Scope: Only to be used in the FIXED16 class. 00204 Purpose: General type conversion 00205 Errors: None 00206 00207 This code relies on the compiler efficiently replacing the division by a multiplication 00208 of the reciprical. 00209 00210 ********************************************************************************************/ 00211 00212 double Fixed16ToDouble( fixed16 a ) 00213 { 00214 return (double)a.all/(1<<16) ; 00215 } 00216 00217 00218 /******************************************************************************************** 00219 00220 > fixed16 DoubleToFixed16( double arg ) 00221 00222 Author: Andy_Pennell (Xara Group Ltd) <camelotdev@xara.com> 00223 Created: 14/5/93 00224 Inputs: double-precision argument 00225 Outputs: None 00226 Returns: FIXED16 value 00227 Purpose: General type conversion 00228 Errors: None (overflow will cause exception) 00229 00230 This is done by taking the arg and multiplying it by 65536, then converting to integer. 00231 00232 ********************************************************************************************/ 00233 00234 fixed16 DoubleToFixed16( double arg ) 00235 { 00236 fixed16 result ; 00237 #if 0//defined(_M_IX86) 00238 // 00239 // This avoids the slow fix operation. Add a large number so that 00240 // the double always has the same exponent and the mantissa becomes, 00241 // in affect, a fixed point value with the value we want in the 00242 // bottom 32 bits. 00243 // 00244 // Note that we are relying on the compiler to convert this to 00245 // efficient code. 00246 // 00247 // TODO: Test! 00248 // 00249 static const double fFix = (XLONG)3<<(DBL_MANT_DIG-16-2); 00250 double F = arg+fFix; 00251 result.all = (INT32&)F ; 00252 #else 00253 result.all = INT32(arg*(1<<16)) ; 00254 #endif 00255 return result ; 00256 } 00257 00258 00259 /******************************************************************************************** 00260 00261 > FIXED16 Fixed16Sin( FIXED16 arg ) 00262 00263 Author: Andy_Pennell (Xara Group Ltd) <camelotdev@xara.com> 00264 Created: 13/5/93 00265 Inputs: FIXED16 argument in degrees 00266 Outputs: None 00267 Returns: FIXED16 result 00268 Scope: Only to be used in the FIXED16 class. 00269 Purpose: General trigonometry 00270 Errors: None 00271 00272 ********************************************************************************************/ 00273 00274 /******************************************************************************************** 00275 00276 > FIXED16 Fixed16Cos( FIXED16 arg ) 00277 00278 Author: Andy_Pennell (Xara Group Ltd) <camelotdev@xara.com> 00279 Created: 13/5/93 00280 Inputs: FIXED16 argument in degrees 00281 Outputs: None 00282 Returns: FIXED16 result 00283 Scope: Only to be used in the FIXED16 class. 00284 Purpose: General trigonometry 00285 Errors: None 00286 00287 ********************************************************************************************/ 00288 // 00289 // If n is the input arg in degrees, then from a INT32 it is arg/65536. 00290 // In radians, this is arg/65536*pi/180 which is arg*pi/11796480 (=approx 2.66e-7) 00291 // The result then needs multiplying by 65536 before returning as a INT32. 00292 // 00293 fixed16 Fixed16Sin( fixed16 arg ) 00294 { 00295 return DoubleToFixed16( sin( double(2.6631610900792382460383465095346e-7) * arg.MakeDouble() ) ); 00296 } 00297 00298 fixed16 Fixed16Cos( fixed16 arg ) 00299 { 00300 return DoubleToFixed16( cos( double(2.6631610900792382460383465095346e-7) * arg.MakeDouble() ) ); 00301 } 00302 00303 00304 00305 /******************************************************************************************** 00306 00307 > INT32 MatrixCalc( FIXED16 fix1, INT32 l1, FIXED16 fix2, INT32 l2) 00308 00309 Author: Andy_Pennell (Xara Group Ltd) <camelotdev@xara.com> 00310 Created: 14/5/93 00311 Inputs: two FIXED16s, two INT32s 00312 Outputs: None 00313 Returns: INT32 result of fix1*l1 + fix2*l2 00314 Scope: Only to be used in the Matrix classes. 00315 Purpose: Matrix multiply primitive done using intermediate 64-bit results 00316 so accuracy is maintained. Special cases are when fix1 or fix2 00317 are 0.0 or 1.0 as multiplications may be avoided. 00318 Note that the result is rounded to the nearest integer. 00319 Errors: None (overflow ignored). 00320 00321 ********************************************************************************************/ 00322 00323 INT32 MatrixCalc( fixed16 fix1, INT32 l1, fixed16 fix2, INT32 l2) 00324 { 00325 return INT32( ( (xlong)fix1.all * l1 + (xlong)fix2.all * l2 ) >> 16 ); 00326 } 00327 00328 00329 00330 /******************************************************************************************** 00331 00332 > INT32 LongMulFixed16( INT32 arg, FIXED16 fix ) 00333 00334 Author: Andy_Pennell (Xara Group Ltd) <camelotdev@xara.com> 00335 Created: 14/5/93 00336 Inputs: one FIXED16, one INT32 00337 Outputs: None 00338 Returns: INT32 result of fix * arg 00339 Scope: Only to be used in the Matrix class. 00340 Purpose: Method of multiplying a INT32 by a fixed point number keeping 00341 as much accuracy as possible. 00342 Errors: None (overflow ignored). 00343 00344 ********************************************************************************************/ 00345 00346 INT32 LongMulFixed16( INT32 arg, fixed16 fix ) 00347 { 00348 return INT32( ((xlong)arg * fix.all + 0x8000) >> 16 ); 00349 } 00350 00351 00352 /******************************************************************************************** 00353 00354 > INT32 LongDivFixed16( INT32 arg, FIXED16 fix ) 00355 00356 Author: Andy_Pennell (Xara Group Ltd) <camelotdev@xara.com> 00357 Created: 13/7/93 00358 Inputs: one FIXED16, one INT32 00359 Outputs: None 00360 Returns: INT32 result of arg / fix 00361 Purpose: Method of dividing an INT32 by a fixed point number keeping 00362 as much accuracy as possible. 00363 Errors: None (overflow ignored). Div0 and Overflow will take exception. 00364 00365 ********************************************************************************************/ 00366 00367 INT32 LongDivFixed16( INT32 arg, fixed16 fix ) 00368 { 00369 return INT32(((xlong)arg<<16)/fix.all) ; 00370 } 00371 00372 00373 00374 /******************************************************************************************** 00375 00376 > INT32 MPtoPixel( INT32 arg, FIXED16 fix ) 00377 00378 Author: Will_Cowling (Xara Group Ltd) <camelotdev@xara.com> 00379 Created: 14/9/93 00380 Inputs: one FIXED16, one INT32 00381 Outputs: None 00382 Returns: INT32 result of arg / fix 00383 Purpose: Method of dividing a INT32 by a fixed point number keeping 00384 as much accuracy as possible. 00385 This version will Round any fractional result to the NEAREST whole value. 00386 It is used for scaling Millipoints to Whole Pixels. 00387 Assumptions:Fix is +ve. 00388 Errors: None (overflow ignored). Div0 and Overflow will take exception. 00389 00390 ********************************************************************************************/ 00391 00392 INT32 MPtoPixel( INT32 arg, fixed16 fix ) 00393 { 00394 if ( arg<0 ) 00395 return INT32((((xlong)arg<<16)-(fix.all>>1))/fix.all) ; 00396 else 00397 return INT32((((xlong)arg<<16)+(fix.all>>1))/fix.all) ; 00398 00399 // Gavin says (07/03/2006) the following would be more accurate so long as 00400 // we could guarantee that fix.all<<1 does not overflow: 00401 // if ( arg<0 ) 00402 // return INT32((((xlong)arg<<17)-fix.all))/(fix.all<<1)) ; 00403 // else 00404 // return INT32((((xlong)arg<<17)+fix.all))/(fix.all<<1)) ; 00405 } 00406 00407 00408 /******************************************************************************************** 00409 00410 > INT32 MPtoOS256( INT32 arg, FIXED16 fix ) 00411 00412 Author: Will_Cowling (Xara Group Ltd) <camelotdev@xara.com> 00413 Created: 14/9/93 00414 Inputs: one FIXED16, one INT32 00415 Outputs: None 00416 Returns: INT32 result of arg / fix 00417 Purpose: Method of dividing a INT32 by a fixed point number keeping 00418 as much accuracy as possible. 00419 This version will convert the MP value to 256ths of a Pixel. 00420 Errors: None (overflow ignored). Div0 and Overflow will take exception. 00421 00422 ********************************************************************************************/ 00423 00424 00425 INT32 MPtoOS256( INT32 arg, fixed16 fix ) 00426 { 00427 return INT32(((xlong)arg<<24)/fix.all) ; 00428 }