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

  1. /* Copyright (C) 1989, 1992, 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. /* gsmisc.c */
  20. /* Miscellaneous utilities for Ghostscript library */
  21. #include "gx.h"
  22. #include "memory_.h"
  23. #include "gxfixed.h"
  24.  
  25. /* Define private replacements for stdin, stdout, and stderr. */
  26. FILE *gs_stdin, *gs_stdout, *gs_stderr;
  27. /* Ghostscript writes debugging output to gs_debug_out. */
  28. /* We define gs_debug and gs_debug_out even if DEBUG isn't defined, */
  29. /* so that we can compile individual modules with DEBUG set. */
  30. char gs_debug[128];
  31. FILE *gs_debug_out;
  32.  
  33. /* ------ Substitutes for missing C library functions ------ */
  34.  
  35. #ifdef memory__need_memmove        /* see memory_.h */
  36. /* Copy bytes like memcpy, guaranteed to handle overlap correctly. */
  37. /* ANSI C defines the returned value as being the src argument, */
  38. /* but with the const restriction removed! */
  39. void *
  40. memmove(void *dest, const void *src, uint len)
  41. {    if ( !len )
  42.         return (void *)src;
  43. #define bdest ((byte *)dest)
  44. #define bsrc ((const byte *)src)
  45.     /* We use len-1 for comparisons because adding len */
  46.     /* might produce an offset overflow on segmented systems. */
  47.     if ( ptr_le(bdest, bsrc) )
  48.       {    register byte *end = bdest + (len - 1);
  49.         if ( ptr_le(bsrc, end) )
  50.           {    /* Source overlaps destination from above. */
  51.             register const byte *from = bsrc;
  52.             register byte *to = bdest;
  53.             for ( ; ; )
  54.               {    *to = *from;
  55.                 if ( to >= end )    /* faster than = */
  56.                   return (void *)src;
  57.                 to++; from++;
  58.               }
  59.           }
  60.       }
  61.     else
  62.       {    register const byte *from = bsrc + (len - 1);
  63.         if ( ptr_le(bdest, from) )
  64.           {    /* Source overlaps destination from below. */
  65.             register const byte *end = bsrc;
  66.             register byte *to = bdest + (len - 1);
  67.             for ( ; ; )
  68.               {    *to = *from;
  69.                 if ( from <= end )    /* faster than = */
  70.                   return (void *)src;
  71.                 to--; from--;
  72.               }
  73.           }
  74.       }
  75. #undef bdest
  76. #undef bsrc
  77.     /* No overlap, it's safe to use memcpy. */
  78.     memcpy(dest, src, len);
  79.     return (void *)src;
  80. }
  81. #endif
  82.  
  83. #ifdef memory__need_memchr        /* see memory_.h */
  84. /* ch should obviously be char rather than int, */
  85. /* but the ANSI standard declaration uses int. */
  86. const char *
  87. memchr(const char *ptr, int ch, size_t len)
  88. {    if ( len > 0 )
  89.     {    register const char *p = ptr;
  90.         register uint count = len;
  91.         do { if ( *p == (char)ch ) return p; p++; } while ( --count );
  92.     }
  93.     return 0;
  94. }
  95. #endif
  96.  
  97. #ifdef memory__need_memset        /* see memory_.h */
  98. /* ch should obviously be char rather than int, */
  99. /* but the ANSI standard declaration uses int. */
  100. void *
  101. memset(void *dest, register int ch, size_t len)
  102. {    if ( ch == 0 )
  103.         bzero(dest, len);
  104.     else if ( len > 0 )
  105.     {    register char *p = dest;
  106.         register uint count = len;
  107.         do { *p++ = (char)ch; } while ( --count );
  108.     }
  109.     return dest;
  110. }
  111. #endif
  112.  
  113. /* ------ Debugging support ------ */
  114.  
  115. /* Dump a region of memory. */
  116. void
  117. debug_dump_bytes(const byte *from, const byte *to, const char *msg)
  118. {    const byte *p = from;
  119.     if ( from < to && msg )
  120.         dprintf1("%s:\n", msg);
  121.     while ( p != to )
  122.        {    const byte *q = min(p + 16, to);
  123.         dprintf1("0x%lx:", (ulong)p);
  124.         while ( p != q ) dprintf1(" %02x", *p++);
  125.         dputc('\n');
  126.        }
  127. }
  128.  
  129. /* ------ Arithmetic ------ */
  130.  
  131. #if defined(fmul2fixed_vars) && !USE_ASM
  132.  
  133. /*
  134.  * Floating multiply with fixed result, for avoiding floating point in
  135.  * common coordinate transformations.  Assumes IEEE representation,
  136.  * 16-bit short, 32-bit long.  Optimized for the case where the first
  137.  * operand has no more than 16 mantissa bits, e.g., where it is a user space
  138.  * coordinate (which are often integers).
  139.  *
  140.  * The assembly language version of this code is actually faster than
  141.  * the FPU, if the code is compiled with FPU_TYPE=0 (which requires taking
  142.  * a trap on every FPU operation).  If there is no FPU, the assembly
  143.  * language version of this code is over 10 times as fast as the emulated FPU.
  144.  */
  145. /* Some of the following code has been tweaked for the Borland 16-bit */
  146. /* compiler.  The tweaks do not change the algorithms. */
  147. #if arch_ints_are_short && !defined(FOR80386)
  148. #  define SHORT_ARITH
  149. #endif
  150. fixed
  151. fmul2fixed_(long /*float*/ a, long /*float*/ b)
  152. {
  153. #ifdef SHORT_ARITH
  154. #  define long_rsh8_ushort(x)\
  155.     (((ushort)(x) >> 8) | ((ushort)((ulong)(x) >> 16) << 8))
  156. #  define utemp ushort
  157. #else
  158. #  define long_rsh8_ushort(x) ((ushort)((x) >> 8))
  159. #  define utemp ulong
  160. #endif
  161.     /* utemp may be either ushort or ulong.  This is OK because */
  162.     /* we only use ma and mb in multiplications involving */
  163.     /* a long or ulong operand. */
  164.     utemp ma = long_rsh8_ushort(a) | 0x8000;
  165.     utemp mb = long_rsh8_ushort(b) | 0x8000;
  166.     int e = 260 + _fixed_shift - ((
  167.         (((uint)((ulong)a >> 16)) & 0x7f80) +
  168.         (((uint)((ulong)b >> 16)) & 0x7f80)
  169.       ) >> 7);
  170.     ulong p1 = ma * (b & 0xff);
  171.     ulong p = (ulong)ma * mb;
  172.  
  173.     if ( (byte)a )        /* >16 mantissa bits */
  174.     {    ulong p2 = (a & 0xff) * mb;
  175.         p += ((((uint)(byte)a * (uint)(byte)b) >> 8) + p1 + p2) >> 8;
  176.     }
  177.     else
  178.         p += p1 >> 8;
  179.     if ( (uint)e < 32 )        /* e = -1 is possible */
  180.         p >>= e;
  181.     else if ( e >= 32 )        /* also detects a=0 or b=0 */
  182.         return fixed_0;
  183.     else
  184.         p <<= -e;
  185.     return ((a ^ b) < 0 ? -p : p);
  186. }
  187. fixed
  188. dfmul2fixed_(ulong /*double lo*/ xalo, long /*float*/ b, long /*double hi*/ xahi)
  189. {
  190. #ifdef SHORT_ARITH
  191. #  define long_lsh3(x) ((((x) << 1) << 1) << 1)
  192. #  define long_rsh(x,ng16) ((uint)((x) >> 16) >> (ng16 - 16))
  193. #else
  194. #  define long_lsh3(x) ((x) << 3)
  195. #  define long_rsh(x,ng16) ((x) >> ng16)
  196. #endif
  197.     return fmul2fixed_(
  198.         (xahi & 0xc0000000) +
  199.         (long_lsh3(xahi) & 0x3ffffff8) +
  200.         long_rsh(xalo, 29),
  201.       b);
  202. }
  203.  
  204. #endif
  205.