fixed16.h

Go to the documentation of this file.
00001 // $Id: fixed16.h 1261 2006-06-06 11:58:26Z 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 class StringBase;
00100 
00101 /*
00102  */ 
00103 
00104 /*********************************************************************************************
00105 
00106 >   class FIXED16
00107 
00108     Author:     Mario_Shamtani (Xara Group Ltd) <camelotdev@xara.com>
00109     Created:    26/4/1993
00110     SeeAlso:    CCMaths.h
00111     SeeAlso:    Fixedasm.h
00112     SeeAlso:    Div32By32
00113     SeeAlso:    Keyword FIXED16
00114     Purpose:    
00115         An FIXED16 is a signed 32-bit value with a binary point between bits 15 and 16. 
00116         Its purpose is to represent 32-bit floating point numbers.
00117         FIXED16 is a fixed point number contained within a 32-bit longword.
00118         The high 16-bit half contains the integer part, the low half the fractional. (Intel
00119         implementations store this as the low half first in memory). The integer half ranges
00120         from -32768 to 32767, the fractional part from 0 to 65535/65536ths (1/65536th=
00121         0.000015259, 65535/65536=0.999984741). For efficiency all primitives are written
00122         in assembly-language, though most are used transparently by operator overloading.
00123         No-one should include fixed16.h directly - always use ccmath.h. You should also
00124         never use the lower-case type 'fixed16' - always used 'FIXED16'.    
00125     Errors:     None.
00126     
00127 ********************************************************************************************/
00128 
00129 #ifndef INC_FIXED16
00130 #define INC_FIXED16
00131 
00132 // One as a fixed16 as a longword, and 0.5 as...
00133 const INT32 FIXED16_ONE = 0x10000L;
00134 const INT32 FIXED16_HALF= 0x08000L;
00135 
00136 #if !defined(USE_NATIVE_XLONG)
00137 class CCAPI xlong;
00138 #endif
00139 
00140 class CCAPI String;
00141 
00142 class CCAPI fixed16
00143 {
00144 public:
00145     INT32 all;
00146 
00147 
00148 
00149 public:
00150 
00151     #define F16SHIFT 16                                 // marks binary point position
00152 
00153     // this converts a short into a fixed16 longword by casting to an INT32 then shifting it
00154     #define SHORT_FIXED(x)  ( ((INT32)(x)) << F16SHIFT )
00155 
00156     inline fixed16() {};                
00157     inline fixed16(const fixed16&);
00158     inline fixed16(INT32);
00159     inline fixed16(short);
00160     inline fixed16(double);
00161 
00162     fixed16& Sin();
00163     fixed16& Cos();
00164 
00165     inline fixed16 trunc() const;
00166     inline fixed16 round() const;
00167     inline fixed16 abs() const;
00168     inline fixed16 sgn() const;
00169     
00170     void ToString(StringBase* out) const;
00171 
00172     // Friend Functions
00173 
00174     inline friend fixed16 Div32By32(const INT32 a, const INT32 b);
00175     inline friend fixed16 CCAPI operator+ (const fixed16&, const fixed16&);
00176     inline friend fixed16 CCAPI operator- (const fixed16&, const fixed16&);
00177     inline friend fixed16 CCAPI operator- (const fixed16&);
00178     inline friend fixed16 CCAPI operator* (const fixed16&, const fixed16&);
00179     inline friend fixed16 CCAPI operator/ (const fixed16&, const fixed16&);
00180     inline friend fixed16 CCAPI operator>> (const fixed16&, UINT32);
00181     inline friend fixed16 CCAPI operator<< (const fixed16&, UINT32);
00182 
00183     // Relational Operators
00184     
00185     inline friend INT32 CCAPI operator== (const fixed16&, const fixed16&);
00186     inline friend INT32 CCAPI operator== (const fixed16&, const short);
00187     inline friend INT32 CCAPI operator!= (const fixed16&, const fixed16&);
00188     inline friend INT32 CCAPI operator!= (const fixed16&, const short);
00189     inline friend INT32 CCAPI operator> (const fixed16&, const fixed16&);
00190     inline friend INT32 CCAPI operator> (const fixed16&, const short);
00191     inline friend INT32 CCAPI operator< (const fixed16&, const fixed16&);
00192     inline friend INT32 CCAPI operator< (const fixed16&, const short);
00193     inline friend INT32 CCAPI operator>= (const fixed16&, const fixed16&);
00194     inline friend INT32 CCAPI operator>= (const fixed16&, const short);
00195     inline friend INT32 CCAPI operator<= (const fixed16&, const fixed16&);
00196     inline friend INT32 CCAPI operator<= (const fixed16&, const short);
00197     
00198     // Assignment Operators
00199 
00200     inline fixed16& operator= (const fixed16&);
00201     inline fixed16& operator= (const INT32);
00202     inline fixed16& operator= (const short);
00203     inline fixed16& operator= (const double);
00204     inline fixed16& operator+= (const fixed16&);
00205     inline fixed16& operator+= (const short);
00206     inline fixed16& operator-= (const fixed16&); 
00207     inline fixed16& operator-= (const short);
00208     inline fixed16& operator*= (const fixed16&); 
00209     inline fixed16& operator*= (const short);
00210     inline fixed16& operator/= (const fixed16&);
00211     inline fixed16& operator/= (const short); 
00212     inline fixed16& operator<<= (const UINT32); 
00213     inline fixed16& operator>>= (const UINT32); 
00214 
00215     // Increment\Decrement Operators
00216 
00217     inline fixed16& operator++ ();                          // prefix
00218     inline fixed16 operator++ (INT32);                      // postfix 
00219     inline fixed16& operator-- ();                          // prefix 
00220     inline fixed16 operator-- (INT32);                      // postfix 
00221 
00222     // Conversion functions
00223 
00224     inline INT32     MakeInt() const;
00225     inline short MakeShort() const;
00226     inline double MakeDouble() const;
00227     inline INT32  MakeLong() const;
00228     inline xlong MakeXlong() const;
00229 
00230     inline static fixed16 FromRawLong(INT32 d);
00231     inline static fixed16 FromShifted16(INT32 d);
00232     inline INT32 GetRawLong() const;
00233     inline INT32 GetShifted16() const;
00234 };
00235 
00236 
00237 // this is a temporary kludge for the 32-bit MS compiler
00238 #define F16ASSIGN( it )     it
00239 #define F16ASSIGNTHIS       *this
00240 
00241 #include "fixed.h"
00242 
00243 /********************************************************************************************
00244 
00245 Note that all the comment blocks live in the cpp files so they get compiled in with the help
00246 compiler
00247 
00248 ********************************************************************************************/
00249 
00250 #define FIXED16_DBL(d) fixed16::FromRawLong((INT32)((d)*65536))
00251 
00252 inline fixed16::fixed16(const fixed16& initValue) 
00253 {
00254     all = initValue.all;
00255 }
00256 
00257 inline fixed16::fixed16(INT32 initValue) 
00258 {
00259     all = SHORT_FIXED( initValue );
00260 }
00261 
00262 inline fixed16::fixed16(short initValue) 
00263 {
00264     all = SHORT_FIXED( initValue );
00265 }
00266 
00267 inline fixed16::fixed16(double initValue) 
00268 {
00269     *this = DoubleToFixed16(initValue);
00270 }
00271 
00272 inline fixed16 fixed16::FromRawLong(INT32 d)
00273 {
00274     fixed16 temp;
00275     temp.all = d;
00276     return temp;
00277 }
00278 
00279 inline fixed16 fixed16::FromShifted16(INT32 d)
00280 {
00281     fixed16 temp;
00282     temp.all = d;
00283     return temp;
00284 }
00285 
00286 inline INT32 fixed16::MakeInt() const
00287 {
00288     return (INT32) (all >> F16SHIFT);
00289 }
00290 
00291 inline short fixed16::MakeShort() const
00292 {
00293     return (short) (all >> F16SHIFT);
00294 }
00295 
00296 inline INT32 fixed16::MakeLong() const
00297 {
00298     return all >> F16SHIFT;
00299 }
00300 
00301 inline xlong fixed16::MakeXlong() const
00302 {
00303     return xlong( (short) (all >> F16SHIFT) );
00304 }
00305 
00306 
00307 inline fixed16& fixed16::operator= (const fixed16& operand)
00308 { 
00309     this->all = operand.all;
00310     
00311     return *this; 
00312 }
00313 
00314 inline double fixed16::MakeDouble() const
00315 {
00316     return Fixed16ToDouble(*this);
00317 }
00318 
00319 inline fixed16& fixed16::operator= (const short operand)
00320 { 
00321     this->all = SHORT_FIXED( operand );
00322     return *this;
00323 }
00324 
00325 inline INT32 fixed16::GetRawLong() const
00326 {
00327     return this->all;
00328 }
00329 
00330 inline INT32 fixed16::GetShifted16() const
00331 {
00332     return this->all;
00333 }
00334 
00335 inline fixed16 fixed16::trunc() const
00336 {
00337     return FromRawLong( all & 0xFFFF0000L );
00338 }
00339     
00340 inline fixed16 fixed16::round() const
00341 {
00342     return FromRawLong( ( all + FIXED16_HALF ) & 0xFFFF0000L );
00343 }
00344     
00345 inline fixed16 fixed16::abs() const
00346 {
00347     return FromRawLong( all<0 ? -all : all );
00348 }
00349     
00350 inline fixed16 fixed16::sgn() const
00351 {
00352     return FromRawLong( all<0 ? -FIXED16_ONE : FIXED16_ONE );
00353 }
00354     
00355 inline fixed16 operator+ (const fixed16& operand1, const fixed16& operand2)
00356 {
00357     fixed16 result;
00358     result.all = operand1.all + operand2.all;
00359     return result;
00360 }
00361 
00362 inline fixed16 operator- (const fixed16& operand1, const fixed16& operand2)
00363 {
00364     fixed16 result;
00365     result.all = operand1.all - operand2.all;
00366     return result;
00367 }
00368 
00369 inline fixed16 operator- (const fixed16& operand)
00370 {
00371     fixed16 temp;
00372     temp.all = -operand.all;
00373     return temp;
00374 }
00375 
00376 inline fixed16 operator* (const fixed16& operand1, const fixed16& operand2)
00377 {
00378     fixed16 result;
00379     F16ASSIGN(result) = Fixed16Mul( operand1, operand2 );
00380     return result;
00381 }
00382 
00383 inline fixed16 operator/ (const fixed16& operand1, const fixed16& operand2)
00384 {
00385     fixed16 result;
00386     F16ASSIGN(result) = Fixed16Div( operand1, operand2 );
00387     return result;
00388 }
00389 
00390 inline fixed16 operator>> (const fixed16& operand1, UINT32 operand2)
00391 {
00392     fixed16 result;
00393     result.all = operand1.all >> operand2;
00394     return result;
00395 }
00396 
00397 inline fixed16 operator<< (const fixed16& operand1, UINT32 operand2)
00398 {
00399     fixed16 result;
00400     
00401     result.all = operand1.all << operand2;
00402     
00403     return result;
00404 }
00405 
00406 inline INT32 operator== (const fixed16& operand1, const fixed16& operand2) 
00407 {
00408     return operand1.all == operand2.all;
00409 }
00410 
00411 inline INT32 operator== (const fixed16& operand1, const short operand2) 
00412 {
00413     return operand1.all == SHORT_FIXED( operand2 );
00414 }
00415 
00416 inline INT32 operator!= (const fixed16& operand1, const fixed16& operand2) 
00417 {
00418     return operand1.all != operand2.all;
00419 }
00420 
00421 inline INT32 operator!= (const fixed16& operand1, const short operand2) 
00422 {
00423     return operand1.all != SHORT_FIXED( operand2 );
00424 }
00425 
00426 inline INT32 operator> (const fixed16& operand1, const fixed16& operand2) 
00427 {
00428     return operand1.all > operand2.all;
00429 }
00430 
00431 inline INT32 operator> (const fixed16& operand1, const short operand2) 
00432 {
00433     return operand1.all > SHORT_FIXED(operand2);
00434 }
00435 
00436 inline INT32 operator< (const fixed16& operand1, const fixed16& operand2) 
00437 {
00438     return operand1.all < operand2.all;
00439 }
00440 
00441 inline INT32 operator< (const fixed16& operand1, const short operand2) 
00442 {
00443     return operand1.all < SHORT_FIXED( operand2 );
00444 }
00445 
00446 inline INT32 operator>= (const fixed16& operand1, const fixed16& operand2) 
00447 {
00448     return operand1.all >= operand2.all;
00449 }
00450 
00451 inline INT32 operator>= (const fixed16& operand1, const short operand2) 
00452 {
00453     return operand1.all >= SHORT_FIXED( operand2 );
00454 }
00455 
00456 inline INT32 operator<= (const fixed16& operand1, const fixed16& operand2) 
00457 {
00458     return operand1.all <= operand2.all;
00459 }
00460 
00461 inline INT32 operator<= (const fixed16& operand1, const short operand2) 
00462 {
00463     return operand1.all <= SHORT_FIXED( operand2 );
00464 }
00465 
00466 inline fixed16& fixed16::operator= (const INT32 operand)
00467 {
00468     this->all = SHORT_FIXED( operand );
00469     return *this;
00470 }
00471 
00472 inline fixed16& fixed16::operator= (const double operand)
00473 { 
00474     F16ASSIGNTHIS = DoubleToFixed16(operand);
00475     return *this;
00476 }
00477 
00478 inline fixed16& fixed16::operator+= (const fixed16& operand)
00479 { 
00480     this->all += operand.all;
00481     return *this;
00482 }
00483 
00484 inline fixed16& fixed16::operator+= (const short operand)
00485 { 
00486     this->all += SHORT_FIXED( operand );
00487     return *this;
00488 }
00489 
00490 inline fixed16& fixed16::operator-= (const fixed16& operand)
00491 {
00492     this->all -= operand.all;
00493     return *this;
00494 }
00495 
00496 inline fixed16& fixed16::operator-= (const short operand)
00497 {
00498     this->all -= SHORT_FIXED( operand );
00499     return *this;
00500 }
00501 
00502 inline fixed16& fixed16::operator*= (const fixed16& operand)
00503 {
00504     F16ASSIGNTHIS = Fixed16Mul(*this, operand);
00505     
00506     return *this;
00507 }
00508 
00509 inline fixed16& fixed16::operator*= (const short operand)
00510 {                                  
00511     fixed16 temp;
00512     
00513     temp.all = SHORT_FIXED( operand );
00514     
00515     F16ASSIGNTHIS = Fixed16Mul(*this, temp);
00516     
00517     return *this;
00518 }
00519 
00520 inline fixed16& fixed16::operator/= (const fixed16& operand)
00521 {
00522     F16ASSIGNTHIS = Fixed16Div(*this, operand);
00523     
00524     return *this;
00525 }
00526 
00527 inline fixed16& fixed16::operator/= (const short operand)
00528 {
00529     fixed16 temp;
00530     
00531     temp.all = SHORT_FIXED( operand );
00532     
00533     F16ASSIGNTHIS = Fixed16Div(*this, temp);
00534     
00535     return *this;
00536 }
00537 
00538 inline fixed16& fixed16::operator<<= (const UINT32 operand)
00539 {
00540     this->all <<= operand;
00541     return *this;
00542 }
00543   
00544 inline fixed16& fixed16::operator>>= (const UINT32 operand)
00545 {
00546     this->all >>= operand;
00547     return *this;
00548 } 
00549 
00550 inline fixed16& fixed16::operator++ ()                          // prefix
00551 {
00552     this->all += FIXED16_ONE;
00553     return *this;
00554 }
00555 
00556 inline fixed16 fixed16::operator++ (INT32 dummy)                // postfix 
00557 {                
00558     fixed16 result = *this;
00559     
00560     this->all += FIXED16_ONE;
00561     
00562     return result;
00563 }
00564 
00565 inline fixed16& fixed16::operator-- ()                          // prefix 
00566 {
00567     this->all -= FIXED16_ONE;
00568     return *this;
00569 }
00570 
00571 inline fixed16 fixed16::operator-- (INT32 dummy)                // postfix 
00572 {
00573     fixed16 result = *this;
00574     
00575     this->all -= FIXED16_ONE;
00576     
00577     return result;
00578 }
00579     
00580 #endif

Generated on Sat Nov 10 03:45:20 2007 for Camelot by  doxygen 1.4.4