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 // The following is copied from the staden io_lib-1.12.4 os.h
17 * MRC Laboratory of Molecular Biology
22 * Description: operating system specific type definitions
29 /*-----------------------------------------------------------------------------
30 * Detection of endianness. The main part of this is done in autoconf, but
31 * for the case of MacOS FAT binaries we fall back on auto-sensing based on
36 #define SP_LITTLE_ENDIAN
38 /* Mac FAT binaries or unknown. Auto detect based on CPU type */
39 #if !defined(SP_BIG_ENDIAN) && !defined(SP_LITTLE_ENDIAN)
44 #if defined(__i386__) || defined(__i386) || defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || defined(__i686__) || defined(__i686)
45 # if defined(SP_BIG_ENDIAN)
48 # define SP_LITTLE_ENDIAN
54 #if defined(__alpha__) || defined(__alpha)
55 # if defined(SP_LITTLE_ENDIAN)
56 # undef SP_LITTLE_ENDIAN
58 # define SP_BIG_ENDIAN
64 #if defined(__sparc__) || defined(__sparc)
65 # if defined(SP_LITTLE_ENDIAN)
66 # undef SP_LITTLE_ENDIAN
68 # define SP_BIG_ENDIAN
74 #if defined(__ppc__) || defined(__ppc)
75 # if defined(SP_LITTLE_ENDIAN)
76 # undef SP_LITTLE_ENDIAN
78 # define SP_BIG_ENDIAN
82 #if defined(__LITTLE_ENDIAN__) || defined(__LITTLEENDIAN__)
83 # define SP_LITTLE_ENDIAN
86 #if defined(__BIG_ENDIAN__) || defined(__BIGENDIAN__)
87 # define SP_BIG_ENDIAN
90 #if defined(SP_BIG_ENDIAN) && defined(SP_LITTLE_ENDIAN)
91 # error Both BIG and LITTLE endian defined. Fix os.h and/or Makefile
94 #if !defined(SP_BIG_ENDIAN) && !defined(SP_LITTLE_ENDIAN)
95 # error Neither BIG nor LITTLE endian defined. Fix os.h and/or Makefile
100 /*-----------------------------------------------------------------------------
101 * Byte swapping macros
105 * Our new swap runs at the same speed on Ultrix, but substantially faster
106 * (300% for swap_int4, ~50% for swap_int2) on an Alpha (due to the lack of
107 * decent 'char' support).
109 * They also have the ability to swap in situ (src == dst). Newer code now
110 * relies on this so don't change back!
112 #define iswap_int8(x) \
113 (((x & 0x00000000000000ffLL) << 56) + \
114 ((x & 0x000000000000ff00LL) << 40) + \
115 ((x & 0x0000000000ff0000LL) << 24) + \
116 ((x & 0x00000000ff000000LL) << 8) + \
117 ((x & 0x000000ff00000000LL) >> 8) + \
118 ((x & 0x0000ff0000000000LL) >> 24) + \
119 ((x & 0x00ff000000000000LL) >> 40) + \
120 ((x & 0xff00000000000000LL) >> 56))
122 #define iswap_int4(x) \
123 (((x & 0x000000ff) << 24) + \
124 ((x & 0x0000ff00) << 8) + \
125 ((x & 0x00ff0000) >> 8) + \
126 ((x & 0xff000000) >> 24))
128 #define iswap_int2(x) \
129 (((x & 0x00ff) << 8) + \
132 #define swap_int8(src, dst) ((dst) = iswap_int8(src))
133 #define swap_int4(src, dst) ((dst) = iswap_int4(src))
134 #define swap_int2(src, dst) ((dst) = iswap_int2(src))
138 * Linux systems may use byteswap.h to get assembly versions of byte-swap
139 * on intel systems. This can be as trivial as the bswap opcode, which works
140 * out at over 2-times faster than iswap_int4 above.
143 #if defined(__linux__)
144 # include <byteswap.h>
148 # define iswap_int8 bswap_64
149 # define iswap_int4 bswap_32
150 # define iswap_int2 bswap_16
156 * Macros to specify that data read in is of a particular endianness.
157 * The macros here swap to the appropriate order for the particular machine
158 * running the macro and return the new answer. These may also be used when
159 * writing to a file to specify that we wish to write in (eg) big endian
162 * This leads to efficient code as most of the time these macros are
166 #define be_int8(x) (x)
167 #define be_int4(x) (x)
168 #define be_int2(x) (x)
169 #define be_int1(x) (x)
171 #define le_int8(x) iswap_int8((x))
172 #define le_int4(x) iswap_int4((x))
173 #define le_int2(x) iswap_int2((x))
174 #define le_int1(x) (x)
177 #ifdef SP_LITTLE_ENDIAN
178 #define be_int8(x) iswap_int8((x))
179 #define be_int4(x) iswap_int4((x))
180 #define be_int2(x) iswap_int2((x))
181 #define be_int1(x) (x)
183 #define le_int8(x) (x)
184 #define le_int4(x) (x)
185 #define le_int2(x) (x)
186 #define le_int1(x) (x)