home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / Peter's Final Project / src / fixed.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-10  |  2.4 KB  |  81 lines  |  [TEXT/KAHL]

  1. /*
  2.  *  Peter's Final Project -- A texture mapping demonstration
  3.  *  © 1995, Peter Mattis
  4.  *
  5.  *  E-mail:
  6.  *  petm@soda.csua.berkeley.edu
  7.  *
  8.  *  Snail-mail:
  9.  *   Peter Mattis
  10.  *   557 Fort Laramie Dr.
  11.  *   Sunnyvale, CA 94087
  12.  *
  13.  *  Avaible from:
  14.  *  http://www.csua.berkeley.edu/~petm/final.html
  15.  *
  16.  *  This program is free software; you can redistribute it and/or modify
  17.  *  it under the terms of the GNU General Public License as published by
  18.  *  the Free Software Foundation; either version 2 of the License, or
  19.  *  (at your option) any later version.
  20.  *
  21.  *  This program is distributed in the hope that it will be useful,
  22.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  23.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24.  *  GNU General Public License for more details.
  25.  *
  26.  *  You should have received a copy of the GNU General Public License
  27.  *  along with this program; if not, write to the Free Software
  28.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  29.  */
  30.  
  31. #ifndef __FIXED_H__
  32. #define __FIXED_H__
  33.  
  34. typedef long FIX_NUM;
  35.  
  36. #define fix_to_float(x)        ((float) (x) / 65536.0)
  37. #define fix_to_int(x)        ((long) ((x) >> 16))
  38. #define float_to_fix(x)        ((FIX_NUM) ((x) * 65536))
  39. #define int_to_fix(x)        ((FIX_NUM) (((long) x) << 16))
  40.  
  41. #define fix_frac(x)         ((x) & 0x0000FFFF)
  42. #define fix_floor(x)        ((x) & 0xFFFF0000)
  43. #define fix_ceil(x)         (fix_frac(x) ? (fix_floor(x) + 0x00010000) : fix_floor(x))
  44.  
  45. #if 1
  46.  
  47. #pragma parameter __d2 fix_mul(__d0,__d2)
  48. pascal FIX_NUM fix_mul(FIX_NUM a, FIX_NUM b) =
  49. {
  50.     0x4C00, 0x2C01,     /* MULS.L    D0,D1:D2 */
  51.     0x3401,             /* MOVE.W    D1,D2    */
  52.     0x4842              /* SWAP      D2       */
  53. };
  54.  
  55. #pragma parameter __d0 fix_div(__d2,__d1)
  56. pascal FIX_NUM fix_div(FIX_NUM a, FIX_NUM b) = 
  57. {
  58.     0x3002,             /* MOVE.W    D2,D0    */
  59.     0x4840,             /* SWAP      D0       */
  60.     0x4240,             /* CLR.W     D0       */
  61.     0x4842,             /* SWAP      D2       */
  62.     0x48C2,             /* EXT.L     D2       */
  63.     0x4C41, 0x0C02      /* DIVS.L    D1,D2:D0 */
  64. };
  65.  
  66. #else
  67.  
  68. /*
  69.  * Define some macros for fixed point multiply and divide.
  70.  * These should be portable across platforms, but assembly
  71.  *  versions that do the same will be much faster.
  72.  */
  73.  
  74. #define fix_mul(a, b)   ((long) ((double) (a) * (double) (b) / 65536.0))
  75. #define fix_div(a, b)   ((long) ((double) (a) * 65536.0 / (double) (b)))
  76.  
  77. #endif
  78.  
  79. FIX_NUM fix_sqrt(FIX_NUM x);
  80.  
  81. #endif /* __FIXED_H__ */