]> git.donarmstrong.com Git - mothur.git/blob - endiannessmacros.h
started work on sffinfo command. fixed bug across all paralellized commands if the...
[mothur.git] / endiannessmacros.h
1 #ifndef EDIANNESSMACROS_H
2 #define EDIANNESSMACROS_H
3
4 /*
5  *   endiannessmacros.h
6  *  Mothur
7  *
8  *  Created by westcott on 7/9/10.
9  *  Copyright 2010 Schloss Lab. All rights reserved.
10  *
11  */
12
13 /*********************************************************************/
14 // The following is copied from the staden io_lib-1.12.4 os.h
15 /*
16  * Author: 
17  *         MRC Laboratory of Molecular Biology
18  *         Hills Road
19  *         Cambridge CB2 2QH
20  *         United Kingdom
21  *
22  * Description: operating system specific type definitions
23  *
24  */
25
26 #include <limits.h>
27 #include <inttypes.h>
28
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
32  * processor type too.
33  */
34
35 /* Set by autoconf */
36 #define SP_LITTLE_ENDIAN
37
38 /* Mac FAT binaries or unknown. Auto detect based on CPU type */
39 #if !defined(SP_BIG_ENDIAN) && !defined(SP_LITTLE_ENDIAN)
40
41 /*
42  * x86 equivalents
43  */
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)
46 #    undef SP_BIG_ENDIAN
47 #  endif
48 #  define SP_LITTLE_ENDIAN
49 #endif
50
51 /*
52  * DEC Alpha
53  */
54 #if defined(__alpha__) || defined(__alpha)
55 #  if defined(SP_LITTLE_ENDIAN)
56 #    undef SP_LITTLE_ENDIAN
57 #  endif
58 #  define SP_BIG_ENDIAN
59 #endif
60
61 /*
62  * SUN Sparc
63  */
64 #if defined(__sparc__) || defined(__sparc)
65 #  if defined(SP_LITTLE_ENDIAN)
66 #    undef SP_LITTLE_ENDIAN
67 #  endif
68 #  define SP_BIG_ENDIAN
69 #endif
70
71 /*
72  * PowerPC
73  */
74 #if defined(__ppc__) || defined(__ppc)
75 #  if defined(SP_LITTLE_ENDIAN)
76 #    undef SP_LITTLE_ENDIAN
77 #  endif
78 #  define SP_BIG_ENDIAN
79 #endif
80
81 /* Some catch-alls */
82 #if defined(__LITTLE_ENDIAN__) || defined(__LITTLEENDIAN__)
83 #    define SP_LITTLE_ENDIAN
84 #endif
85
86 #if defined(__BIG_ENDIAN__) || defined(__BIGENDIAN__)
87 #    define SP_BIG_ENDIAN
88 #endif
89
90 #if defined(SP_BIG_ENDIAN) && defined(SP_LITTLE_ENDIAN)
91 #    error Both BIG and LITTLE endian defined. Fix os.h and/or Makefile
92 #endif
93
94 #if !defined(SP_BIG_ENDIAN) && !defined(SP_LITTLE_ENDIAN)
95 #    error Neither BIG nor LITTLE endian defined. Fix os.h and/or Makefile
96 #endif
97
98 #endif
99
100 /*-----------------------------------------------------------------------------
101  * Byte swapping macros
102  */
103
104 /*
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).
108  *
109  * They also have the ability to swap in situ (src == dst). Newer code now
110  * relies on this so don't change back!
111  */
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))
121
122 #define iswap_int4(x) \
123     (((x & 0x000000ff) << 24) + \
124      ((x & 0x0000ff00) <<  8) + \
125      ((x & 0x00ff0000) >>  8) + \
126      ((x & 0xff000000) >> 24))
127
128 #define iswap_int2(x) \
129     (((x & 0x00ff) << 8) + \
130      ((x & 0xff00) >> 8))
131
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))
135
136
137 /*
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.
141  */
142 #if 0
143 #if defined(__linux__)
144 #    include <byteswap.h>
145 #    undef iswap_int8
146 #    undef iswap_int4
147 #    undef iswap_int2
148 #    define iswap_int8 bswap_64
149 #    define iswap_int4 bswap_32
150 #    define iswap_int2 bswap_16
151 #endif
152 #endif
153
154
155 /*
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
160  * format.
161  *
162  * This leads to efficient code as most of the time these macros are
163  * trivial.
164  */
165 #ifdef SP_BIG_ENDIAN
166 #define be_int8(x) (x)
167 #define be_int4(x) (x)
168 #define be_int2(x) (x)
169 #define be_int1(x) (x)
170
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)
175 #endif
176
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)
182
183 #define le_int8(x) (x)
184 #define le_int4(x) (x)
185 #define le_int2(x) (x)
186 #define le_int1(x) (x)
187 #endif
188
189 #endif