home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / xloadimg.zip / xloadimage.4.1 / tiff / machdep.h < prev    next >
C/C++ Source or Header  |  1993-10-21  |  8KB  |  259 lines

  1. /* $Header: /usr/people/sam/tiff/libtiff/junk/machdep.h,v 1.16 91/07/16 16:30:45 sam Exp $ */
  2.  
  3. /*
  4.  * Copyright (c) 1988, 1989, 1990, 1991 Sam Leffler
  5.  * Copyright (c) 1991 Silicon Graphics, Inc.
  6.  *
  7.  * Permission to use, copy, modify, distribute, and sell this software and 
  8.  * its documentation for any purpose is hereby granted without fee, provided
  9.  * that (i) the above copyright notices and this permission notice appear in
  10.  * all copies of the software and related documentation, and (ii) the names of
  11.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  12.  * publicity relating to the software without the specific, prior written
  13.  * permission of Sam Leffler and Silicon Graphics.
  14.  * 
  15.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  16.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  17.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  18.  * 
  19.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  20.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  21.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  22.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  23.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  24.  * OF THIS SOFTWARE.
  25.  */
  26.  
  27. #ifndef _MACHDEP_
  28. #define    _MACHDEP_
  29. /*
  30.  * Machine dependent definitions:
  31.  *   o floating point formats
  32.  *   o byte ordering
  33.  *
  34.  * NB, there are lots of assumptions here:
  35.  *   - 32-bit natural integers    (sign extension code)
  36.  *   - native float is 4 bytes    (floating point conversion)
  37.  *   - native double is 8 bytes    (floating point conversion)
  38.  */
  39.  
  40. #if defined(sun) || defined(sparc) || defined(stellar) || defined(MIPSEB) || defined(hpux) || defined(apollo) || defined(NeXT) || defined(_IBMR2)
  41. #define    BIGENDIAN    1
  42. #define    IEEEFP
  43. #endif /* sun || sparc || stellar || MIPSEB || hpux || apollo || NeXT || _IBMR2 */
  44.  
  45. /* MIPSEL = MIPS w/ Little Endian byte ordering (e.g. DEC 3100) */
  46. #if defined(MIPSEL)
  47. #define    BIGENDIAN    0
  48. #define    IEEEFP
  49. #endif /* MIPSEL */
  50.  
  51. #ifdef transputer
  52. #define    BIGENDIAN    0
  53. #define    IEEEFP            /* IEEE floating point supported */
  54. #endif /* transputer */
  55.  
  56. #if defined(m386) || defined(M_I86) || defined(i386)
  57. #define BIGENDIAN    0
  58. #define IEEEFP            /* IEEE floating point supported */
  59. #endif /* m386 || M_I86 || i386 */
  60.  
  61. #ifdef IEEEFP
  62. typedef    struct ieeedouble nativedouble;
  63. typedef    struct ieeefloat nativefloat;
  64. #define    ISFRACTION(e)    (1022 - 4 <= (e) && (e) <= 1022 + 15)
  65. #define    EXTRACTFRACTION(dp, fract) \
  66.    ((fract) = ((unsigned long)(1<<31)|((dp)->native.mant<<11)|\
  67.       ((dp)->native.mant2>>21)) >> (1022+16-(dp)->native.exp))
  68. #define    EXTRACTEXPONENT(dp, exponent) ((exponent) = (dp)->native.exp)
  69. #define    NATIVE2IEEEFLOAT(fp)
  70. #define    IEEEFLOAT2NATIVE(fp)
  71. #define    IEEEDOUBLE2NATIVE(dp)
  72.  
  73. #define    TIFFSwabArrayOfFloat(fp,n)  TIFFSwabArrayOfLong((unsigned long *)fp,n)
  74. #define    TIFFSwabArrayOfDouble(dp,n) TIFFSwabArrayOfLong((unsigned long *)dp,2*n)
  75. #endif /* IEEEFP */
  76.  
  77. #ifdef tahoe
  78. #define    BIGENDIAN    1
  79.  
  80. typedef    struct {
  81.     unsigned    sign:1;
  82.     unsigned    exp:8;
  83.     unsigned    mant:23;
  84.     unsigned    mant2;
  85. } nativedouble;
  86. typedef    struct {
  87.     unsigned    sign:1;
  88.     unsigned    exp:8;
  89.     unsigned    mant:23;
  90. } nativefloat;
  91. #define    ISFRACTION(e)    (128 - 4 <= (e) && (e) <= 128 + 15)
  92. #define    EXTRACTFRACTION(dp, fract) \
  93.     ((fract) = ((1<<31)|((dp)->native.mant<<8)|((dp)->native.mant2>>15)) >> \
  94.     (128+16-(dp)->native.exp))
  95. #define    EXTRACTEXPONENT(dp, exponent) ((exponent) = (dp)->native.exp - 2)
  96. /*
  97.  * Beware, over/under-flow in conversions will
  98.  * result in garbage values -- handling it would
  99.  * require a subroutine call or lots more code.
  100.  */
  101. #define    NATIVE2IEEEFLOAT(fp) { \
  102.     if ((fp)->native.exp) \
  103.         (fp)->ieee.exp = (fp)->native.exp - 129 + 127;    /* alter bias */\
  104. }
  105. #define    IEEEFLOAT2NATIVE(fp) { \
  106.     if ((fp)->ieee.exp) \
  107.         (fp)->native.exp = (fp)->ieee.exp - 127 + 129;     /* alter bias */\
  108. }
  109. #define    IEEEDOUBLE2NATIVE(dp) { \
  110.     if ((dp)->native.exp = (dp)->ieee.exp) \
  111.     (dp)->native.exp += -1023 + 129; \
  112.     (dp)->native.mant = ((dp)->ieee.mant<<3)|((dp)->native.mant2>>29); \
  113.     (dp)->native.mant2 <<= 3; \
  114. }
  115. /* the following is to work around a compiler bug... */
  116. #define    SIGNEXTEND(a,b)    { char ch; ch = (a); (b) = ch; }
  117.  
  118. #define    TIFFSwabArrayOfFloat(fp,n)  TIFFSwabArrayOfLong((unsigned long *)fp,n)
  119. #define    TIFFSwabArrayOfDouble(dp,n) TIFFSwabArrayOfLong((unsigned long *)dp,2*n)
  120. #endif /* tahoe */
  121.  
  122. #ifdef vax
  123. #define    BIGENDIAN    0
  124.  
  125. typedef    struct {
  126.     unsigned    mant1:7;
  127.     unsigned    exp:8;
  128.     unsigned    sign:1;
  129.     unsigned    mant2:16;
  130.     unsigned    mant3;
  131. } nativedouble;
  132. typedef    struct {
  133.     unsigned    mant1:7;
  134.     unsigned    exp:8;
  135.     unsigned    sign:1;
  136.     unsigned    mant2:16;
  137. } nativefloat;
  138. #define    ISFRACTION(e)    (128 - 4 <= (e) && (e) <= 128 + 15)
  139. #define    EXTRACTFRACTION(dp, fract) \
  140.     ((fract) = ((1<<31)|((dp)->native.mant1<<16)|(dp)->native.mant2)) >> \
  141.     (128+16-(dp)->native.exp))
  142. #define    EXTRACTEXPONENT(dp, exponent) ((exponent) = (dp)->native.exp - 2)
  143. /*
  144.  * Beware, these do not handle over/under-flow
  145.  * during conversion from ieee to native format.
  146.  */
  147. #define    NATIVE2IEEEFLOAT(fp) { \
  148.     float_t t; \
  149.     if (t.ieee.exp = (fp)->native.exp) \
  150.     t.ieee.exp += -129 + 127; \
  151.     t.ieee.sign = (fp)->native.sign; \
  152.     t.ieee.mant = ((fp)->native.mant1<<16)|(fp)->native.mant2; \
  153.     *(fp) = t; \
  154. }
  155. #define    IEEEFLOAT2NATIVE(fp) { \
  156.     float_t t; int v = (fp)->ieee.exp; \
  157.     if (v) v += -127 + 129;        /* alter bias of exponent */\
  158.     t.native.exp = v;            /* implicit truncation of exponent */\
  159.     t.native.sign = (fp)->ieee.sign; \
  160.     v = (fp)->ieee.mant; \
  161.     t.native.mant1 = v >> 16; \
  162.     t.native.mant2 = v;\
  163.     *(fp) = v; \
  164. }
  165. #define    IEEEDOUBLE2NATIVE(dp) { \
  166.     double_t t; int v = (dp)->ieee.exp; \
  167.     if (v) v += -1023 + 129;         /* if can alter bias of exponent */\
  168.     t.native.exp = v;            /* implicit truncation of exponent */\
  169.     v = (dp)->ieee.mant; \
  170.     t.native.sign = (dp)->ieee.sign; \
  171.     t.native.mant1 = v >> 16; \
  172.     t.native.mant2 = v;\
  173.     t.native.mant3 = (dp)->mant2; \
  174.     *(dp) = t; \
  175. }
  176.  
  177. #define    TIFFSwabArrayOfFloat(fp,n)  TIFFSwabArrayOfLong((unsigned long *)fp,n)
  178. #define    TIFFSwabArrayOfDouble(dp,n) TIFFSwabArrayOfLong((unsigned long *)dp,2*n)
  179. #endif /* vax */
  180.  
  181. /*
  182.  * These unions are used during floating point
  183.  * conversions.  The macros given above define
  184.  * the conversion operations.
  185.  */
  186.  
  187. typedef    struct ieeedouble {
  188. #if BIGENDIAN == 1
  189. #if !defined(_IBMR2)
  190.     unsigned    sign:1;
  191.     unsigned    exp:11;
  192.     unsigned long    mant:20;
  193.     unsigned    mant2;
  194. #else /* _IBMR2 */
  195.     unsigned    sign:1;
  196.     unsigned    exp:11;
  197.     unsigned    mant:20;
  198.     unsigned    mant2;
  199. #endif /* _IBMR2 */
  200. #else
  201. #if !defined(vax)
  202. #ifdef INT_16_BIT    /* MSDOS C compilers */
  203.     unsigned long    mant2;
  204.     unsigned long    mant:20;
  205.     unsigned long    exp:11;
  206.     unsigned long    sign:1;
  207. #else /* 32 bit ints */
  208.     unsigned    mant2;
  209.     unsigned long    mant:20;
  210.     unsigned    exp:11;
  211.     unsigned    sign:1;
  212. #endif /* 32 bit ints */
  213. #else
  214.     unsigned long    mant:20;
  215.     unsigned    exp:11;
  216.     unsigned    sign:1;
  217.     unsigned    mant2;
  218. #endif /* !vax */
  219. #endif
  220. } ieeedouble;
  221. typedef    struct ieeefloat {
  222. #if BIGENDIAN == 1
  223. #if !defined(_IBMR2)
  224.     unsigned    sign:1;
  225.     unsigned    exp:8;
  226.     unsigned long    mant:23;
  227. #else /* _IBMR2 */
  228.     unsigned    sign:1;
  229.     unsigned    exp:8;
  230.     unsigned    mant:23;
  231. #endif /* _IBMR2 */
  232. #else
  233. #ifdef INT_16_BIT    /* MSDOS C compilers */
  234.     unsigned long    mant:23;
  235.     unsigned long    exp:8;
  236.     unsigned long    sign:1;
  237. #else /* 32 bit ints */
  238.     unsigned long    mant:23;
  239.     unsigned    exp:8;
  240.     unsigned    sign:1;
  241. #endif /* 32 bit ints */
  242. #endif
  243. } ieeefloat;
  244.  
  245. typedef    union {
  246.     ieeedouble    ieee;
  247.     nativedouble    native;
  248.     char        b[8];
  249.     double        d;
  250. } double_t;
  251.  
  252. typedef    union {
  253.     ieeefloat    ieee;
  254.     nativefloat    native;
  255.     char        b[4];
  256.     float        f;
  257. } float_t;
  258. #endif /* _MACHDEP_ */
  259.