1 #ifndef EDIANNESSMACROS_H
2 #define EDIANNESSMACROS_H
8 * Created by westcott on 7/9/10.
9 * Copyright 2010 Schloss Lab. All rights reserved.
13 /*********************************************************************/
14 /*********************************************************************/
15 // The following is copied from the staden io_lib-1.12.4 os.h - thanks!
16 /*********************************************************************/
17 /*********************************************************************/
21 * MRC Laboratory of Molecular Biology
26 * Description: operating system specific type definitions
31 /* Mac FAT binaries or unknown. Auto detect based on CPU type */
32 #if !defined(SP_BIG_ENDIAN) && !defined(SP_LITTLE_ENDIAN)
37 #if defined(__i386) || defined(__i386__) || defined(__ia64__) || defined(WIN32) || defined(__arm__) || (defined(__mips__) && defined(__MIPSEL__)) || defined(__SYMBIAN32__) || \
38 defined(__x86_64__) || defined(__x86_64) || defined(__i686__) || defined(__i686) || defined(__amd64__) || defined(__amd64) || defined(__LITTLE_ENDIAN__)
39 #define SP_LITTLE_ENDIAN
49 #if defined(__sparc__) || defined(__sparc)
50 # if defined(SP_LITTLE_ENDIAN)
51 # undef SP_LITTLE_ENDIAN
53 # define SP_BIG_ENDIAN
59 #if defined(__ppc__) || defined(__ppc)
60 # if defined(SP_LITTLE_ENDIAN)
61 # undef SP_LITTLE_ENDIAN
63 # define SP_BIG_ENDIAN
67 #if defined(__LITTLE_ENDIAN__) || defined(__LITTLEENDIAN__)
68 # define SP_LITTLE_ENDIAN
71 #if defined(__BIG_ENDIAN__) || defined(__BIGENDIAN__)
72 # define SP_BIG_ENDIAN
75 #if defined(SP_BIG_ENDIAN) && defined(SP_LITTLE_ENDIAN)
76 # error Both BIG and LITTLE endian defined. Fix os.h and/or Makefile
79 #if !defined(SP_BIG_ENDIAN) && !defined(SP_LITTLE_ENDIAN)
80 # error Neither BIG nor LITTLE endian defined. Fix os.h and/or Makefile
85 /*-----------------------------------------------------------------------------
86 * Byte swapping macros
90 * Our new swap runs at the same speed on Ultrix, but substantially faster
91 * (300% for swap_int4, ~50% for swap_int2) on an Alpha (due to the lack of
92 * decent 'char' support).
94 * They also have the ability to swap in situ (src == dst). Newer code now
95 * relies on this so don't change back!
97 #define iswap_int8(x) \
98 (((x & 0x00000000000000ffLL) << 56) + \
99 ((x & 0x000000000000ff00LL) << 40) + \
100 ((x & 0x0000000000ff0000LL) << 24) + \
101 ((x & 0x00000000ff000000LL) << 8) + \
102 ((x & 0x000000ff00000000LL) >> 8) + \
103 ((x & 0x0000ff0000000000LL) >> 24) + \
104 ((x & 0x00ff000000000000LL) >> 40) + \
105 ((x & 0xff00000000000000LL) >> 56))
107 #define iswap_int4(x) \
108 (((x & 0x000000ff) << 24) + \
109 ((x & 0x0000ff00) << 8) + \
110 ((x & 0x00ff0000) >> 8) + \
111 ((x & 0xff000000) >> 24))
113 #define iswap_int2(x) \
114 (((x & 0x00ff) << 8) + \
117 #define swap_int8(src, dst) ((dst) = iswap_int8(src))
118 #define swap_int4(src, dst) ((dst) = iswap_int4(src))
119 #define swap_int2(src, dst) ((dst) = iswap_int2(src))
123 * Linux systems may use byteswap.h to get assembly versions of byte-swap
124 * on intel systems. This can be as trivial as the bswap opcode, which works
125 * out at over 2-times faster than iswap_int4 above.
128 #if defined(__linux__)
129 # include <byteswap.h>
133 # define iswap_int8 bswap_64
134 # define iswap_int4 bswap_32
135 # define iswap_int2 bswap_16
141 * Macros to specify that data read in is of a particular endianness.
142 * The macros here swap to the appropriate order for the particular machine
143 * running the macro and return the new answer. These may also be used when
144 * writing to a file to specify that we wish to write in (eg) big endian
147 * This leads to efficient code as most of the time these macros are
151 #define be_int8(x) (x)
152 #define be_int4(x) (x)
153 #define be_int2(x) (x)
154 #define be_int1(x) (x)
156 #define le_int8(x) iswap_int8((x))
157 #define le_int4(x) iswap_int4((x))
158 #define le_int2(x) iswap_int2((x))
159 #define le_int1(x) (x)
162 #ifdef SP_LITTLE_ENDIAN
163 #define be_int8(x) iswap_int8((x))
164 #define be_int4(x) iswap_int4((x))
165 #define be_int2(x) iswap_int2((x))
166 #define be_int1(x) (x)
168 #define le_int8(x) (x)
169 #define le_int4(x) (x)
170 #define le_int2(x) (x)
171 #define le_int1(x) (x)