home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / GXFIXED.H < prev    next >
C/C++ Source or Header  |  1994-07-27  |  6KB  |  144 lines

  1. /* Copyright (C) 1989, 1990, 1993 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gxfixed.h */
  20. /* Fixed-point arithmetic for Ghostscript */
  21.  
  22. #ifndef gxfixed_INCLUDED
  23. #  define gxfixed_INCLUDED
  24.  
  25. /*
  26.  * Coordinates are generally represented internally by fixed-point
  27.  * quantities: integers lose accuracy in crucial places,
  28.  * and floating point arithmetic is slow.
  29.  */
  30. typedef long fixed;
  31. #define max_fixed max_long
  32. #define min_fixed min_long
  33. #define fixed_0 0L
  34. #define fixed_epsilon 1L
  35. /*
  36.  * 12 bits of fraction provides both the necessary accuracy and
  37.  * a sufficiently large range of coordinates.
  38.  */
  39. #define _fixed_shift 12
  40. #define _fixed_scale (1<<_fixed_shift)
  41. #define _fixed_rshift(x) arith_rshift(x,_fixed_shift)
  42. #define _fixed_round_v (_fixed_scale>>1)
  43. #define _fixed_fraction_v (_fixed_scale-1)
  44.  
  45. /*
  46.  * Most operations can be done directly on fixed-point quantities:
  47.  * addition, subtraction, shifting, multiplication or division by
  48.  * (integer) constants; assignment, assignment with zero;
  49.  * comparison, comparison against zero.
  50.  * Multiplication and division by floats is OK if the result is
  51.  * explicitly cast back to fixed.
  52.  * Conversion to and from int and float types must be done explicitly.
  53.  * Note that if we are casting a fixed to a float in a context where
  54.  * only ratios and not actual values are involved, we don't need to take
  55.  * the scale factor into account: we can simply cast to float directly.
  56.  */
  57. #define int2fixed(i) ((fixed)(i)<<_fixed_shift)
  58. /* Define some useful constants. */
  59. /* Avoid casts, so strict ANSI compilers will accept them in #ifs. */
  60. #define fixed_1 (fixed_epsilon << _fixed_shift)
  61. #define fixed_half (fixed_1 >> 1)
  62. /*
  63.  * On 16-bit systems, we can convert fixed variables to ints more efficiently
  64.  * than general fixed quantities.  For this reason, we define two separate
  65.  * sets of conversion macros.
  66.  */
  67. #define fixed2int(x) ((int)_fixed_rshift(x))
  68. #define fixed2int_rounded(x) ((int)_fixed_rshift((x)+_fixed_round_v))
  69. #define fixed2int_ceiling(x) ((int)_fixed_rshift((x)+_fixed_fraction_v))
  70. #if arch_ints_are_short & !arch_is_big_endian
  71. /* Do some of the shifting and extraction ourselves. */
  72. #  define _fixed_hi(x) *((uint *)&(x)+1)
  73. #  define _fixed_lo(x) *((uint *)&(x))
  74. #  define fixed2int_var(x)\
  75.     ((int)((_fixed_hi(x) << (16-_fixed_shift)) +\
  76.            (_fixed_lo(x) >> _fixed_shift)))
  77. #  define fixed2int_var_rounded(x)\
  78.     ((int)((_fixed_hi(x) << (16-_fixed_shift)) +\
  79.            (((_fixed_lo(x) >> (_fixed_shift-1))+1)>>1)))
  80. #  define fixed2int_var_ceiling(x)\
  81.     (fixed2int_var(x) -\
  82.      arith_rshift((int)-(_fixed_lo(x) & _fixed_fraction_v), _fixed_shift))
  83. #else
  84. /* Use reasonable definitions. */
  85. #  define fixed2int_var(x) fixed2int(x)
  86. #  define fixed2int_var_rounded(x) fixed2int_rounded(x)
  87. #  define fixed2int_var_ceiling(x) fixed2int_ceiling(x)
  88. #endif
  89. #define fixed2long(x) ((long)_fixed_rshift(x))
  90. #define fixed2long_rounded(x) ((long)_fixed_rshift((x)+_fixed_round_v))
  91. #define fixed2long_ceiling(x) ((long)_fixed_rshift((x)+_fixed_fraction_v))
  92. #define float2fixed(f) ((fixed)((f)*(float)_fixed_scale))
  93. /* Note that fixed2float actually produces a double result. */
  94. #define fixed2float(x) ((x)*(1.0/_fixed_scale))
  95.  
  96. /* Rounding and truncation on fixeds */
  97. #define fixed_floor(x) ((x)&(-1L<<_fixed_shift))
  98. #define fixed_rounded(x) (((x)+_fixed_round_v)&(-1L<<_fixed_shift))
  99. #define fixed_ceiling(x) (((x)+_fixed_fraction_v)&(-1L<<_fixed_shift))
  100. #define fixed_fraction(x) ((int)(x)&_fixed_fraction_v)
  101. /* I don't see how to do truncation towards 0 so easily.... */
  102. #define fixed_truncated(x) ((x) < 0 ? fixed_ceiling(x) : fixed_floor(x))
  103.  
  104. /*
  105.  * Transforming coordinates involves multiplying two floats, or a float
  106.  * and a double, and then converting the result to a fixed.  Since this
  107.  * operation is so common, we provide an alternative implementation of it
  108.  * on machines that use IEEE floating point representation but don't have
  109.  * floating point hardware.  The implementation may be in either C or
  110.  * assembler.
  111.  */
  112.  
  113. #ifndef USE_FPU
  114. #  define USE_FPU 0
  115. #endif
  116. #if USE_FPU < 0 && arch_sizeof_short == 2 && arch_sizeof_long == 4
  117. fixed    fmul2fixed_(P2(long, long));
  118. #define fmul2fixed_vars(vfa,vfb)\
  119.   fmul2fixed_(*(long *)&vfa, *(long *)&vfb)
  120. fixed    dfmul2fixed_(P3(ulong, long, long));
  121. #  if arch_is_big_endian
  122. #  define dfmul2fixed_vars(vda,vfb)\
  123.      dfmul2fixed_(((ulong *)&vda)[1], *(long *)&vfb, *(long *)&vda)
  124. #  else
  125. #  define dfmul2fixed_vars(vda,vfb)\
  126.      dfmul2fixed_(*(ulong *)&vda, *(long *)&vfb, ((long *)&vda)[1])
  127. #  endif
  128. #else            /* don't bother */
  129. #  define fmul2fixed_vars(vfa,vfb) float2fixed((vfa) * (vfb))
  130. #  define dfmul2fixed_vars(vda,vfb) float2fixed((vda) * (vfb))
  131. #endif
  132.  
  133. /* A point with fixed coordinates */
  134. typedef struct gs_fixed_point_s {
  135.     fixed x, y;
  136. } gs_fixed_point;
  137.  
  138. /* A rectangle with fixed coordinates */
  139. typedef struct gs_fixed_rect_s {
  140.     gs_fixed_point p, q;
  141. } gs_fixed_rect;
  142.  
  143. #endif                    /* gxfixed_INCLUDED */
  144.