home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / opendc12.zip / od124os2.exe / od12osp1.exe / src / utils / ODMath.h < prev    next >
C/C++ Source or Header  |  1997-04-02  |  6KB  |  170 lines

  1. /*====START_GENERATED_PROLOG======================================
  2.  */
  3. /*
  4.  *   COMPONENT_NAME: odsamples
  5.  *
  6.  *   CLASSES: none
  7.  *
  8.  *   ORIGINS: 82,27
  9.  *
  10.  *
  11.  *   (C) COPYRIGHT International Business Machines Corp. 1995,1996
  12.  *   All Rights Reserved
  13.  *   Licensed Materials - Property of IBM
  14.  *   US Government Users Restricted Rights - Use, duplication or
  15.  *   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  16.  *       
  17.  *   IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  18.  *   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  19.  *   PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  20.  *   CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  21.  *   USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  22.  *   OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
  23.  *   OR PERFORMANCE OF THIS SOFTWARE.
  24.  */
  25. /*====END_GENERATED_PROLOG========================================
  26.  */
  27. // @(#) 1.9 com/src/samples/utils/ODMath.h, odutilspart, od96os2, odos29712d 10/10/96 16:10:01 [ 3/21/97 17:41:23 ]
  28. /*
  29.     File:        ODMath.h
  30.  
  31.     Contains:    Math routines (fixed-point and wide) for OpenDoc.
  32.  
  33.     Written by:    Jens Alfke
  34.  
  35.     Copyright:    c 1994 by Apple Computer, Inc., all rights reserved.
  36.  
  37. */
  38.  
  39.  
  40. #ifndef _ODMATH_
  41. #define _ODMATH_
  42.  
  43. #ifndef _ODTYPES_
  44. #include "ODTypes.h"
  45. #endif
  46.  
  47. #ifndef _ODMVCLNK_
  48. #include <ODMvcLnk.h>
  49. #endif
  50.  
  51. typedef double double_t;
  52.  
  53. struct ODWide {       // 64-bit integer
  54.     ODSLong hi;        // High order longword comes first
  55.     ODULong lo;        // Then low-order (which has no sign bit!)
  56. };
  57.  
  58. /*
  59. ** !! Use these ONLY FOR ASSIGNMENT, never for comparison !!
  60. */
  61. #define ODFALSE  ((int)0)
  62. #define ODTRUE   ((int)~ODFALSE)
  63.  
  64. #if defined(_PLATFORM_BIG_ENDIAN_)
  65. #define ODUHIWORD(fixptVar)            (((ODUShort *)&(fixptVar))[0])        // get hi unsined word
  66. #define ODULOWORD(fixptVar)            (((ODUShort *)&(fixptVar))[1])        // get lo unsined word
  67. #define ODFP_INTPART(fixptVar)        (((ODSShort *)&(fixptVar))[0])        // get hi signed word
  68. #define ODFP_FRACTPART(fixptVar)    (((ODUShort *)&(fixptVar))[1])        // get lo unsigned word    
  69. #else // !defined(_PLATFORM_BIG_ENDIAN_)
  70. #define ODUHIWORD(fixptVar)            (((ODUShort *)&(fixptVar))[1])        // get hi unsined word
  71. #define ODULOWORD(fixptVar)            (((ODUShort *)&(fixptVar))[0])        // get lo unsined word
  72. #define ODFP_INTPART(fixptVar)        (((ODSShort *)&(fixptVar))[1])        // get hi signed word
  73. #define ODFP_FRACTPART(fixptVar)    (((ODUShort *)&(fixptVar))[0])        // get lo unsigned word    
  74. #endif // defined(_PLATFORM_BIG_ENDIAN_)
  75.  
  76. #define    ODFPD_INTPART(wideVar)      (((ODWide*)wideVar)->hi)
  77. #define    ODFPD_FRACPART(wideVar)        (((ODWide*)wideVar)->lo)
  78.  
  79. #define    ODFRACT_MAX    ((ODULong)0x3fffffff)    /* max value in a 30 bit quantity*/
  80.  
  81.  
  82. #define    ODSWORD_MAX    ((ODSShort)0x7fff)        /* (32767) max value in a       */
  83.                                               /* signed 16 bit quantity       */
  84. #define ODULONG_MAX    ((ODULong)0xffffffffUL) /* max value in an unsigned 32  */
  85.                                             /* bit quantity                 */
  86. #define ODUDWORD_CARRY(Exp1 ,Exp2 )            \
  87.                 ((ODULONG_MAX - (ODULong)(Exp1 )) < (ODULong)(Exp2 ))
  88.  
  89. /* COMMENTS *****************************************************************
  90.  * Names:
  91.  *         ODFP_SET(fpVar, swordExp, uwordExp), 
  92.  *         ODFPD_SET(fpdVar, sdwordExp, udwordExp)
  93.  * 
  94.  *  Desc:
  95.  *      These macros take a signed integer and an unsigned integer (representing
  96.  *         the fraction of a fixed point number), and assign them to a fixed point 
  97.  *         (fixed point double) variable.
  98.  * In:
  99.  *         s(d)wordExp    - an expression defining the integer part of the variable.
  100.  *         u(d)wordExp    - an expression defining the fractional part of the variable.
  101.  * Out:
  102.  *          fp(d)Var - the single (double) fixed point variable to which the 2 parts 
  103.  *                      are assigned.
  104.  * Ret:
  105.  *         n/a
  106.  * Notes:
  107.  *         These macros should only be used to set POSITIVE fixed point values.
  108.  * 
  109.  * END **********************************************************************
  110.  */
  111. #define ODFP_SET(fpVar,swordExp,uwordExp)                     \
  112.         ((ODFP_INTPART(fpVar) = (ODSShort)(swordExp)),      \
  113.          (ODFP_FRACPART(fpVar) = (ODUShort)(uwordExp)))
  114.  
  115. #define ODFPD_SET(fpdVar,sdwordExp,udwordExp)                 \
  116.         ((ODFPD_INTPART(fpdVar) = (ODSLong)(sdwordExp)),      \
  117.          (ODFPD_FRACPART(fpdVar) = (ODULong)(udwordExp)))
  118.  
  119.  
  120. const ODFixed kODFixed1                = 0x00010000;
  121. const ODFixed kODFixedHalf            = 0x00008000;
  122. const ODFract kODFract1                = 0x40000000;
  123. const ODFixed kODFixedInfinity        = 0x7FFFFFFF;    // Fract as well
  124. const ODFixed kODFixedMinusInfinity    = 0x80000000;
  125.  
  126.  
  127. // Some of these fns are coded in assembly on 68k, so make calling conventions C:
  128. extern "C" {
  129.  
  130.  
  131. #define ODFixedRound(a)        ((ODSShort)((ODFixed)(a) + kODFixedHalf >> 16))
  132. #define ODIntToFixed(a)        ((ODFixed)(a) << 16)
  133. #define ODFixedToFract(a)    ((ODFract)(a) << 14)
  134. #define ODFractToFixed(a)    ((ODFixed)(a) + 8192L >> 14)
  135. #define ODFixedToFloat(a)  ((double_t)((ODFixed)(a)) / 65536.0)
  136. #define ODFloatToFixed(a)    ((ODFixed)((double_t)(a) * 65536.0))
  137.  
  138.  
  139. // These fixed-point math routines return infinity (see above) on overflow.
  140.  
  141. _DLLIMPORTEXPORT_ ODFixed    ODFixedMultiply( ODFixed num1, ODFixed num2 );
  142. _DLLIMPORTEXPORT_ ODFixed    ODFixedDivide( ODFixed num1, ODFixed num2 );
  143. _DLLIMPORTEXPORT_ ODFract    ODFractMultiply( ODFract a, ODFract b );
  144. _DLLIMPORTEXPORT_ ODFract    ODFractDivide( ODFract a, ODFract b );
  145. _DLLIMPORTEXPORT_ ODWide*    ODWideMultiply( ODSLong a, ODSLong b, ODWide *result );
  146.  
  147. _DLLIMPORTEXPORT_ ODSShort ODWideCompare( const ODWide*, const ODWide* );
  148. _DLLIMPORTEXPORT_ ODWide*    ODWideNegate( ODWide* );
  149. _DLLIMPORTEXPORT_ ODWide*    ODWideShift( ODWide*, ODSShort bits );    // Positive is toward MSB
  150. _DLLIMPORTEXPORT_ ODWide*    ODWideAdd( ODWide*, const ODWide* );
  151. _DLLIMPORTEXPORT_ ODWide*    ODWideSubtract( ODWide*, const ODWide* );
  152.  
  153. _DLLIMPORTEXPORT_ ODSLong    ODWideDivide( const ODWide *dividend,
  154.                             ODSLong divisor, ODSLong *remainder);
  155. _DLLIMPORTEXPORT_ ODULong  ODWideSquareRoot( const ODWide *src );
  156.  
  157. #define kODIgnoreRemainder  ((ODSLong*)-1L)    // Use as remainder in ODWideDivide
  158.  
  159. #define ODWideIsLong(w) ((w)->hi ==  0 && (long)(w)->lo >= 0 || \
  160.                           (w)->hi == -1 && (long)(w)->lo < 0)
  161.  
  162. _DLLIMPORTEXPORT_ ODFract    ODFractSinCos( ODFixed angle, ODFract *cos );
  163.  
  164. _DLLIMPORTEXPORT_ ODSShort    ODFirstBit( ODSLong );            // Returns index (0-32) of 1st bit
  165.  
  166.  
  167. }    // End of extern "C" {
  168.  
  169. #endif /*_ODMATH_*/
  170.