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
33 /*-----------------------------------------------------------------------------
34 * Detection of endianness. The main part of this is done in autoconf, but
35 * for the case of MacOS FAT binaries we fall back on auto-sensing based on
40 #define SP_LITTLE_ENDIAN
42 /* Mac FAT binaries or unknown. Auto detect based on CPU type */
43 #if !defined(SP_BIG_ENDIAN) && !defined(SP_LITTLE_ENDIAN)
48 #if defined(__i386__) || defined(__i386) || defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || defined(__i686__) || defined(__i686)
49 # if defined(SP_BIG_ENDIAN)
52 # define SP_LITTLE_ENDIAN
58 #if defined(__alpha__) || defined(__alpha)
59 # if defined(SP_LITTLE_ENDIAN)
60 # undef SP_LITTLE_ENDIAN
62 # define SP_BIG_ENDIAN
68 #if defined(__sparc__) || defined(__sparc)
69 # if defined(SP_LITTLE_ENDIAN)
70 # undef SP_LITTLE_ENDIAN
72 # define SP_BIG_ENDIAN
78 #if defined(__ppc__) || defined(__ppc)
79 # if defined(SP_LITTLE_ENDIAN)
80 # undef SP_LITTLE_ENDIAN
82 # define SP_BIG_ENDIAN
86 #if defined(__LITTLE_ENDIAN__) || defined(__LITTLEENDIAN__)
87 # define SP_LITTLE_ENDIAN
90 #if defined(__BIG_ENDIAN__) || defined(__BIGENDIAN__)
91 # define SP_BIG_ENDIAN
94 #if defined(SP_BIG_ENDIAN) && defined(SP_LITTLE_ENDIAN)
95 # error Both BIG and LITTLE endian defined. Fix os.h and/or Makefile
98 #if !defined(SP_BIG_ENDIAN) && !defined(SP_LITTLE_ENDIAN)
99 # error Neither BIG nor LITTLE endian defined. Fix os.h and/or Makefile
104 /*-----------------------------------------------------------------------------
105 * Byte swapping macros
109 * Our new swap runs at the same speed on Ultrix, but substantially faster
110 * (300% for swap_int4, ~50% for swap_int2) on an Alpha (due to the lack of
111 * decent 'char' support).
113 * They also have the ability to swap in situ (src == dst). Newer code now
114 * relies on this so don't change back!
116 #define iswap_int8(x) \
117 (((x & 0x00000000000000ffLL) << 56) + \
118 ((x & 0x000000000000ff00LL) << 40) + \
119 ((x & 0x0000000000ff0000LL) << 24) + \
120 ((x & 0x00000000ff000000LL) << 8) + \
121 ((x & 0x000000ff00000000LL) >> 8) + \
122 ((x & 0x0000ff0000000000LL) >> 24) + \
123 ((x & 0x00ff000000000000LL) >> 40) + \
124 ((x & 0xff00000000000000LL) >> 56))
126 #define iswap_int4(x) \
127 (((x & 0x000000ff) << 24) + \
128 ((x & 0x0000ff00) << 8) + \
129 ((x & 0x00ff0000) >> 8) + \
130 ((x & 0xff000000) >> 24))
132 #define iswap_int2(x) \
133 (((x & 0x00ff) << 8) + \
136 #define swap_int8(src, dst) ((dst) = iswap_int8(src))
137 #define swap_int4(src, dst) ((dst) = iswap_int4(src))
138 #define swap_int2(src, dst) ((dst) = iswap_int2(src))
142 * Linux systems may use byteswap.h to get assembly versions of byte-swap
143 * on intel systems. This can be as trivial as the bswap opcode, which works
144 * out at over 2-times faster than iswap_int4 above.
147 #if defined(__linux__)
148 # include <byteswap.h>
152 # define iswap_int8 bswap_64
153 # define iswap_int4 bswap_32
154 # define iswap_int2 bswap_16
160 * Macros to specify that data read in is of a particular endianness.
161 * The macros here swap to the appropriate order for the particular machine
162 * running the macro and return the new answer. These may also be used when
163 * writing to a file to specify that we wish to write in (eg) big endian
166 * This leads to efficient code as most of the time these macros are
170 #define be_int8(x) (x)
171 #define be_int4(x) (x)
172 #define be_int2(x) (x)
173 #define be_int1(x) (x)
175 #define le_int8(x) iswap_int8((x))
176 #define le_int4(x) iswap_int4((x))
177 #define le_int2(x) iswap_int2((x))
178 #define le_int1(x) (x)
181 #ifdef SP_LITTLE_ENDIAN
182 #define be_int8(x) iswap_int8((x))
183 #define be_int4(x) iswap_int4((x))
184 #define be_int2(x) iswap_int2((x))
185 #define be_int1(x) (x)
187 #define le_int8(x) (x)
188 #define le_int4(x) (x)
189 #define le_int2(x) (x)
190 #define le_int1(x) (x)