home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / xloadimg.zip / xloadimage.4.1 / tiff / tif_machdep.c < prev    next >
C/C++ Source or Header  |  1996-03-18  |  6KB  |  229 lines

  1. #ifndef lint
  2. static char rcsid[] = "$Header: /usr/people/sam/tiff/libtiff/RCS/tif_machdep.c,v 1.2 92/03/06 11:52:42 sam Exp $";
  3. #endif
  4.  
  5. /*
  6.  * Copyright (c) 1992 Sam Leffler
  7.  * Copyright (c) 1992 Silicon Graphics, Inc.
  8.  *
  9.  * Permission to use, copy, modify, distribute, and sell this software and 
  10.  * its documentation for any purpose is hereby granted without fee, provided
  11.  * that (i) the above copyright notices and this permission notice appear in
  12.  * all copies of the software and related documentation, and (ii) the names of
  13.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  14.  * publicity relating to the software without the specific, prior written
  15.  * permission of Sam Leffler and Silicon Graphics.
  16.  * 
  17.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  18.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  19.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  20.  * 
  21.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  22.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  23.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  24.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  25.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  26.  * OF THIS SOFTWARE.
  27.  */
  28.  
  29. /*
  30.  * TIFF Library Machine Dependent Routines.
  31.  */
  32. #include "tiffioP.h"
  33.  
  34. #if defined(sun) || defined(sparc) || defined(stellar) || defined(MIPSEB) || defined(hpux) || defined(apollo) || defined(NeXT) || defined(_IBMR2)
  35. #define    BIGENDIAN    1
  36. #define    IEEEFP
  37. #endif /* sun || sparc || stellar || MIPSEB || hpux || apollo || NeXT || _IBMR2 */
  38.  
  39. /* MIPSEL = MIPS w/ Little Endian byte ordering (e.g. DEC 3100) */
  40. #if defined(MIPSEL)
  41. #define    BIGENDIAN    0
  42. #define    IEEEFP
  43. #endif /* MIPSEL */
  44.  
  45. #ifdef transputer
  46. #define    BIGENDIAN    0
  47. #define    IEEEFP            /* IEEE floating point supported */
  48. #endif /* transputer */
  49.  
  50. #if defined(m386) || defined(M_I86) || defined(i386)
  51. #define BIGENDIAN    0
  52. #define IEEEFP            /* IEEE floating point supported */
  53. #endif /* m386 || M_I86 || i386 */
  54.  
  55. #ifdef IEEEFP
  56. typedef    struct ieeedouble nativedouble;
  57. typedef    struct ieeefloat nativefloat;
  58. #define    ISFRACTION(e)    (1022 - 4 <= (e) && (e) <= 1022 + 15)
  59. #define    EXTRACTFRACTION(dp, fract) \
  60.    ((fract) = ((unsigned long)(1<<31)|((dp)->native.mant<<11)|\
  61.       ((dp)->native.mant2>>21)) >> (1022+16-(dp)->native.exp))
  62. #define    EXTRACTEXPONENT(dp, exponent) ((exponent) = (dp)->native.exp)
  63. #define    NATIVE2IEEEFLOAT(fp)
  64. #define    IEEEFLOAT2NATIVE(fp)
  65. #define    IEEEDOUBLE2NATIVE(dp)
  66.  
  67. #define    TIFFSwabArrayOfFloat(fp,n)  TIFFSwabArrayOfLong((unsigned long *)fp,n)
  68. #define    TIFFSwabArrayOfDouble(dp,n) TIFFSwabArrayOfLong((unsigned long *)dp,2*n)
  69. #endif /* IEEEFP */
  70.  
  71.  
  72. #ifdef tahoe
  73. typedef    struct ieeedouble {
  74.     u_long    sign    : 1,
  75.         exp    : 11,
  76.         mant    : 20;
  77.     u_long    mant2;
  78. } ieeedouble;
  79. typedef    struct ieeefloat {
  80.     u_long    sign    : 1,
  81.         exp    : 8,
  82.         mant    : 23;
  83. } ieeefloat;
  84.  
  85. typedef    struct {
  86.     u_long    sign    : 1,
  87.         exp    : 8,
  88.         mant    : 23;
  89.     u_long    mant2;
  90. } nativedouble;
  91. typedef    struct {
  92.     u_long    sign    : 1,
  93.         exp    : 8,
  94.         mant    : 23;
  95. } nativefloat;
  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. #endif /* tahoe */
  116.  
  117. #ifdef vax
  118. typedef    struct ieeedouble {
  119.     u_long    mant    : 20,
  120.         exp    : 11,
  121.         sign    : 1;
  122.     u_long    mant2;
  123. } ieeedouble;
  124. typedef    struct ieeefloat {
  125.     u_long    mant    : 23,
  126.         exp    : 8,
  127.         sign    : 1;
  128. } ieeefloat;
  129.  
  130. typedef    struct {
  131.     u_long    mant1    : 7,
  132.         exp    : 8,
  133.         sign    : 1,
  134.         mant2    : 16;
  135.     u_long    mant3;
  136. } nativedouble;
  137. typedef    struct {
  138.     u_long    mant1    : 7,
  139.         exp    : 8,
  140.         sign    : 1,
  141.         mant2    : 16;
  142. } nativefloat;
  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) = t; \
  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. #endif /* vax */
  177.  
  178. #if !HAVE_IEEEFP
  179. #if !defined(IEEEFLOAT2NATIVE) || !defined(NATIVE2IEEEFLOAT)
  180. "Help, you've configured the library to not have IEEE floating point,\
  181. but not defined how to convert between IEEE and native formats!"
  182. #endif
  183.  
  184. /*
  185.  * These unions are used during floating point
  186.  * conversions.  The above macros define the
  187.  * conversion operations.
  188.  */
  189. typedef    union {
  190.     ieeedouble    ieee;
  191.     nativedouble    native;
  192.     char        b[8];
  193.     double        d;
  194. } double_t;
  195.  
  196. typedef    union {
  197.     ieeefloat    ieee;
  198.     nativefloat    native;
  199.     char        b[4];
  200.     float        f;
  201. } float_t;
  202.  
  203. TIFFCvtIEEEFloatToNative(tif, n, f)
  204.     TIFF *tif;
  205.     u_int n;
  206.     float *f;
  207. {
  208.     float_t *fp = (float_t *)f;
  209.  
  210.     while (n-- > 0) {
  211.         IEEEFLOAT2NATIVE(fp);
  212.         fp++;
  213.     }
  214. }
  215.  
  216. TIFFCvtNativeToIEEEFloat(tif, n, f)
  217.     TIFF *tif;
  218.     u_int n;
  219.     float *f;
  220. {
  221.     float_t *fp = (float_t *)f;
  222.  
  223.     while (n-- > 0) {
  224.         NATIVE2IEEEFLOAT(fp);
  225.         fp++;
  226.     }
  227. }
  228. #endif
  229.