home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / extensions / server / PEX / include / swapmacros.h < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-01  |  6.0 KB  |  157 lines

  1. /* $XConsortium: swapmacros.h,v 5.3 91/07/01 16:26:23 hersh Exp $ */
  2.  
  3. /***********************************************************
  4. Copyright 1989, 1990, 1991 by Sun Microsystems, Inc. and the X Consortium.
  5.  
  6.                         All Rights Reserved
  7.  
  8. Permission to use, copy, modify, and distribute this software and its 
  9. documentation for any purpose and without fee is hereby granted, 
  10. provided that the above copyright notice appear in all copies and that
  11. both that copyright notice and this permission notice appear in 
  12. supporting documentation, and that the names of Sun Microsystems,
  13. the X Consortium, and MIT not be used in advertising or publicity 
  14. pertaining to distribution of the software without specific, written 
  15. prior permission.  
  16.  
  17. SUN MICROSYSTEMS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 
  18. INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT 
  19. SHALL SUN MICROSYSTEMS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL 
  20. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  22. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  23. SOFTWARE.
  24.  
  25. ******************************************************************/
  26.  
  27.  
  28. /* 
  29.  * swapmacros.h - Macros used for byte swapping. and float converting
  30.  * 
  31.  * Copyright 1988-1991
  32.  * Center for Information Technology Integration (CITI)
  33.  * Information Technology Division
  34.  * University of Michigan
  35.  * Ann Arbor, Michigan
  36.  *
  37.  *                         All Rights Reserved
  38.  * 
  39.  * Permission to use, copy, modify, and distribute this software and
  40.  * its documentation for any purpose and without fee is hereby
  41.  * granted, provided that the above copyright notice appear in all
  42.  * copies and that both that copyright notice and this permission
  43.  * notice appear in supporting documentation, and that the names of
  44.  * CITI or THE UNIVERSITY OF MICHIGAN not be used in advertising or
  45.  * publicity pertaining to distribution of the software without
  46.  * specific, written prior permission.
  47.  * 
  48.  * THE SOFTWARE IS PROVIDED "AS IS." CITI AND THE UNIVERSITY OF
  49.  * MICHIGAN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  50.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
  51.  * NO EVENT SHALL CITI OR THE UNIVERSITY OF MICHIGAN BE LIABLE FOR ANY
  52.  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  53.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  54.  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  55.  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  56.  * SOFTWARE.
  57.  */
  58.  
  59. #include "misc.h"
  60.  
  61. /* byteswap a long in place.  (given a pointer) */
  62. #define pswaplong(x, n)\
  63.     n = ((char *) (x))[0];\
  64.     ((char *) (x))[0] = ((char *) (x))[3];\
  65.     ((char *) (x))[3] = n;\
  66.     n = ((char *) (x))[1];\
  67.     ((char *) (x))[1] = ((char *) (x))[2];\
  68.     ((char *) (x))[2] = n
  69.  
  70. /* byteswap a short in place.  (given a pointer) */
  71. #define pswapshort(x, n)\
  72.     n = ((char *) (x))[0];\
  73.     ((char *) (x))[0] = ((char *) (x))[1];\
  74.     ((char *) (x))[1] = n
  75.  
  76. /* byteswaps as above but no pointer is given. */
  77. #define swaplong(x, n) pswaplong(&(x), n)
  78. #define swapshort(x, n) pswapshort(&(x), n)
  79.  
  80. /* byte swap AND COPY a long (given pointer) */
  81. #define pswaplongc(x, y)\
  82.     ((char *) (y))[3] = ((char *) (x))[0];\
  83.     ((char *) (y))[0] = ((char *) (x))[3];\
  84.     ((char *) (y))[2] = ((char *) (x))[1];\
  85.     ((char *) (y))[1] = ((char *) (x))[2]
  86.  
  87. /* byte swap AND COPY a short (given pointer) */
  88. #define pswapshortc(x, y)\
  89.     ((char *) (y))[1] = ((char *) (x))[0];\
  90.     ((char *) (y))[0] = ((char *) (x))[1]
  91.  
  92. #define swaplongc(x, y) pswaplongc(&(x), &(y))
  93. #define swapshortc(x, y) pswapshortc(&(x), &(y))
  94. #define swapfloatc(x, y) pswapfloatc(&(x), &(y)) /* float convert and swap */
  95. #define plaincopy(x, y) (y)=(x)
  96.  
  97.     /* Macros below swap and convert a FLOAT in place.  This is still SLOW.
  98.      * The ones which copy have been made 5x10 times faster so we use them.
  99.      */
  100. #define swapConvFloat(x, n) pswaplong((CARD32 *)&(x), n); convFloat(x)
  101. #define pswapConvFloat(x, n) pswaplong((CARD32 *)(x), n); pconvFloat(x)
  102.  
  103.  
  104.  
  105. #if (TargetFloat == DEC_F_Floating)    /* IEEE to VAX conversions */
  106.  
  107.     /* exponent needs += 2, */
  108.     /* bit 0 of byte 0 is bit 1 of the exponent */
  109.     /* also we swap the word as two shorts!! */
  110. #define pswapfloatc(x, y)\
  111.     ((char *) (y))[1] = (((char *) (x))[0]+1);\
  112.     ((char *) (y))[0] = ((char *) (x))[1];\
  113.     ((char *) (y))[3] = ((char *) (x))[2];\
  114.     ((char *) (y))[2] = ((char *) (x))[3]
  115.  
  116. #define pswapConvFloatc(x, y)\
  117. if ((*((CARD32 *)(x)) == 0)|| (((((char *)(x))[0] + 1) & 0x7f) == 0))\
  118.     *((float *)y) = 0.0; else { pswapfloatc(x, y); }
  119.  
  120. #define swapConvFloatc(x, y) \
  121. if (((*((CARD32 *)&(x))) == 0)|| (((((char *)&(x))[0] + 1) & 0x7f) == 0))\
  122.     y = 0.0; else { swapfloatc(x, y); }
  123.  
  124.     
  125. #define pconvFloatc(x, y) *(PEXFLOAT *)(y) = ConvertIEEEtoVax((PEXFLOAT *)(x))
  126. #define convFloatc(x, y) (y) = ConvertIEEEtoVax((PEXFLOAT *)&(x))
  127. #define pconvFloat(x) *(PEXFLOAT *)(x) = ConvertIEEEtoVax((PEXFLOAT *)(x))
  128. #define convFloat(x) (x) = ConvertIEEEtoVax((PEXFLOAT *)&(x))
  129.  
  130. #else                    /* VAX to IEEE conversions */
  131.  
  132.     /* exponent needs -= 2, */
  133.     /* bit 0 of byte 1 is bit 1 of the exponent */ 
  134.     /* also we swap the word as two shorts!! */
  135. #define pswapfloatc(x, y)\
  136.     ((char *) (y))[0] = (((char *) (x))[1]-1);\
  137.     ((char *) (y))[1] = ((char *) (x))[0];\
  138.     ((char *) (y))[3] = ((char *) (x))[2];\
  139.     ((char *) (y))[2] = ((char *) (x))[3]
  140.  
  141. #define pswapConvFloatc(x, y) \
  142. if ((*((CARD32 *)(x)) == 0)|| ((( ((char *)(x))[1] - 1) & 0x7f) == 0x7f)) \
  143.     *((float *)y) = 0.0; else { pswapfloatc(x, y); }
  144.  
  145. #define swapConvFloatc(x, y) \
  146. if (((*((CARD32 *)&(x))) == 0)|| (((((char *)&(x))[1] - 1) & 0x7f) == 0x7f))\
  147.     y = 0.0; else { swapfloatc(x, y); }
  148.  
  149. #define pconvFloatc(x, y) *(PEXFLOAT *)(y) = ConvertVaxToIEEE((PEXFLOAT *)(x))
  150. #define convFloatc(x, y) (y) = ConvertVaxToIEEE((PEXFLOAT *)&(x))
  151. #define pconvFloat(x) *(PEXFLOAT *)(x) = ConvertVaxToIEEE((PEXFLOAT *)(x))
  152. #define convFloat(x) (x) = ConvertVaxToIEEE((PEXFLOAT *)&(x))
  153. #endif
  154.  
  155.  
  156.  
  157.