home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 13 / AACD13.ISO / AACD / Graphics / PPT / include / fixmath.h < prev    next >
C/C++ Source or Header  |  1996-05-07  |  2KB  |  90 lines

  1. /*
  2.  *  This is a header file for fixed point maths.
  3.  *  It can be easily configured to run on any precision,
  4.  *  just change the defines below.
  5.  *
  6.  *  $Id: fixmath.h 1.3 1996/05/08 00:06:08 jj Exp jj $
  7.  *
  8.  *  The fixed point type is always signed. Must be 32 bits.
  9.  */
  10.  
  11. #ifndef FIXMATH_H
  12. #define FIXMATH_H
  13.  
  14. typedef signed long fixed;
  15.  
  16.  
  17. /*
  18.  *  This define tells how much bits accuracy we should use.
  19.  *  10 = the system is accurate to 1024ths.
  20.  *  We make a reasonable estimation, unless the user has
  21.  *  overriden it by setting his own FIX_DECIMALBITS before
  22.  *  including this file.
  23.  *
  24.  *  I don't suggest setting it higher than 16, because the
  25.  *  values overflow pretty easily during multiplication.
  26.  */
  27.  
  28. #ifndef FIX_DECIMALBITS
  29. #define FIX_DECIMALBITS     12
  30. #endif
  31.  
  32. /*
  33.  *  These calculate the minimum and maximum values
  34.  */
  35.  
  36. #define FIX_INTEGERBITS     (31-FIX_DECIMALBITS)
  37. #define FIX_INTMASK         (0xFFFFFFFF << FIX_DECIMALBITS)
  38.  
  39. #define FIX_MAX             ((1 << FIX_INTEGERBITS) - 1)
  40. #define FIX_MIN             (-(1 << FIX_INTEGERBITS))
  41.  
  42. /*
  43.  *  Some useful values
  44.  */
  45.  
  46. #define FIX_SHIFT           (FIX_DECIMALBITS)
  47. #define FIX_DIVISOR         (1<<FIX_DECIMALBITS)
  48.  
  49. #define FIX_ZERO            0L
  50. #define FIX_ONE             (1 << FIX_SHIFT)
  51. #define FIX_HALF            (FIX_ONE >> 1)
  52.  
  53. #define FIX_EPSILON         1
  54.  
  55. /*
  56.  *  Fixed point calculations:
  57.  *
  58.  *  Addition : just say a+b
  59.  *  Subtraction: just say a-b
  60.  *  Multiplication: use FIXMUL()
  61.  *  Division: just use FIXDIV()
  62.  *    BUG: The division loses some decimals...
  63.  */
  64.  
  65. #define FIXMUL(a,b)         (((a)*(b)) >> FIX_SHIFT)
  66. #define FIXDIV(a,b)         (((a) << FIX_SHIFT/2)/((b)) << FIX_SHIFT/2)
  67.  
  68.  
  69. /*
  70.  *  Some mathematical operations
  71.  *  BUG: FIXFRAC() does not work properly for negative numbers
  72.  */
  73.  
  74. #define FIXFRAC(a)          ( (a) & ~FIX_INTMASK )
  75. #define FIXFLOOR(a)         ( (a) & FIX_INTMASK )
  76. #define FIXCEIL(a)          ( ((a) + FIX_ONE-FIX_EPSILON) & FIX_INTMASK )
  77.  
  78. /*
  79.  *  Type conversions
  80.  */
  81.  
  82. #define FIX2INT(a)          ((a) >> FIX_SHIFT)
  83. #define INT2FIX(i)          ((i) << FIX_SHIFT)
  84.  
  85. #define FIX2FLOAT(a)        ( (float)(a) / FIX_DIVISOR )
  86. #define FLOAT2FIX(f)        ( (fixed)((f) * FIX_DIVISOR) )
  87.  
  88. #endif /* FIXMATH_H */
  89.  
  90.