Main Page   Compound List   File List   Compound Members   File Members  

rr_libs/mips_swap.h

Go to the documentation of this file.
00001 /* MIPS_SWAP.H  */
00002 /*=====================================================================
00003                 =======   COPYRIGHT NOTICE   =======
00004 Copyright (C) 1994, Carnegie Mellon University and Ronald Rosenfeld.
00005 All rights reserved.
00006 
00007 This software is made available for research purposes only.  It may be
00008 redistributed freely for this purpose, in full or in part, provided
00009 that this entire copyright notice is included on any copies of this
00010 software and applications and derivations thereof.
00011 
00012 This software is provided on an "as is" basis, without warranty of any
00013 kind, either expressed or implied, as to any matter including, but not
00014 limited to warranty of fitness of purpose, or merchantability, or
00015 results obtained from use of this software.
00016 ======================================================================*/
00017 
00018 #ifndef _MIPS_SWAP_H_
00019 #define _MIPS_SWAP_H_
00020 
00021 #include "general.h"
00022 
00026 #ifdef SLM_SWAP_BYTES    /* reverse byteorder */
00027 
00028 /* the following works even for badly aligned pointers */
00029 
00030 #define SWAPFIELD(x) {if     (sizeof(*(x))==sizeof(short)) {SWAPHALF((x))}  \
00031                       else if (sizeof(*(x))==sizeof(int))   {SWAPWORD((x))}  \
00032                       else if (sizeof(*(x))==sizeof(double)){SWAPDOUBLE((x))}\
00033                      }
00034 
00035 #define SWAPHALF(x) {char tmp_byte;                        \
00036                                tmp_byte = *((char*)(x)+0); \
00037                         *((char*)(x)+0) = *((char*)(x)+1); \
00038                         *((char*)(x)+1) = tmp_byte;        \
00039                     }
00040 #define SWAPWORD(x) {char tmp_byte;                        \
00041                                tmp_byte = *((char*)(x)+0); \
00042                         *((char*)(x)+0) = *((char*)(x)+3); \
00043                         *((char*)(x)+3) = tmp_byte;        \
00044                                tmp_byte = *((char*)(x)+1); \
00045                         *((char*)(x)+1) = *((char*)(x)+2); \
00046                         *((char*)(x)+2) = tmp_byte;        \
00047                     }
00048 
00049 #define SWAPDOUBLE(x) {char tmp_byte;                      \
00050                                tmp_byte = *((char*)(x)+0); \
00051                         *((char*)(x)+0) = *((char*)(x)+7); \
00052                         *((char*)(x)+7) = tmp_byte;        \
00053                                tmp_byte = *((char*)(x)+1); \
00054                         *((char*)(x)+1) = *((char*)(x)+6); \
00055                         *((char*)(x)+6) = tmp_byte;        \
00056                                tmp_byte = *((char*)(x)+2); \
00057                         *((char*)(x)+2) = *((char*)(x)+5); \
00058                         *((char*)(x)+5) = tmp_byte;        \
00059                                tmp_byte = *((char*)(x)+3); \
00060                         *((char*)(x)+3) = *((char*)(x)+4); \
00061                         *((char*)(x)+4) = tmp_byte;        \
00062                     }
00063 
00064 #if 0 /* old */
00065 #define SWAPHALF(x) *(short*)(x) = ((0xff   & (*(short*)(x)) >> 8) | \
00066                                     (0xff00 & (*(short*)(x)) << 8))
00067 #define SWAPWORD(x) *(int*)  (x) = ((0xff       & (*(int*)(x)) >> 24) | \
00068                                     (0xff00     & (*(int*)(x)) >>  8) | \
00069                                     (0xff0000   & (*(int*)(x)) <<  8) | \
00070                                     (0xff000000 & (*(int*)(x)) << 24))
00071 #define SWAPDOUBLE(x) { int *low  = (int *) (x), \
00072                             *high = (int *) (x) + 1, temp;\
00073                         SWAPWORD(low);  SWAPWORD(high);\
00074                         temp = *low; *low = *high; *high = temp;}
00075 #endif /* old */
00076 #else
00077 
00078 #define SWAPFIELD(x)
00079 #define SWAPHALF(x)
00080 #define SWAPWORD(x)
00081 #define SWAPDOUBLE(x)
00082 
00083 #endif
00084 
00085 
00086 #define ALWAYS_SWAPFIELD(x) {\
00087                       if      (sizeof(*(x))==sizeof(short)) {SWAPHALF((x))}  \
00088                       else if (sizeof(*(x))==sizeof(int))   {SWAPWORD((x))}  \
00089                       else if (sizeof(*(x))==sizeof(double)){SWAPDOUBLE((x))}\
00090                      }
00091 
00092 #define ALWAYS_SWAPHALF(x) {char tmp_byte;                         \
00093                                tmp_byte = *((char*)(x)+0); \
00094                         *((char*)(x)+0) = *((char*)(x)+1); \
00095                         *((char*)(x)+1) = tmp_byte;        \
00096                     }
00097 #define ALWAYS_SWAPWORD(x) {char tmp_byte;                         \
00098                                tmp_byte = *((char*)(x)+0); \
00099                         *((char*)(x)+0) = *((char*)(x)+3); \
00100                         *((char*)(x)+3) = tmp_byte;        \
00101                                tmp_byte = *((char*)(x)+1); \
00102                         *((char*)(x)+1) = *((char*)(x)+2); \
00103                         *((char*)(x)+2) = tmp_byte;        \
00104                     }
00105 
00106 #define ALWAYS_SWAPDOUBLE(x) {char tmp_byte;                       \
00107                                tmp_byte = *((char*)(x)+0); \
00108                         *((char*)(x)+0) = *((char*)(x)+7); \
00109                         *((char*)(x)+7) = tmp_byte;        \
00110                                tmp_byte = *((char*)(x)+1); \
00111                         *((char*)(x)+1) = *((char*)(x)+6); \
00112                         *((char*)(x)+6) = tmp_byte;        \
00113                                tmp_byte = *((char*)(x)+2); \
00114                         *((char*)(x)+2) = *((char*)(x)+5); \
00115                         *((char*)(x)+5) = tmp_byte;        \
00116                                tmp_byte = *((char*)(x)+3); \
00117                         *((char*)(x)+3) = *((char*)(x)+4); \
00118                         *((char*)(x)+4) = tmp_byte;        \
00119                     }
00120 
00121 #endif
00122 

Generated on Tue Dec 21 13:54:46 2004 by doxygen1.2.18