]> git.donarmstrong.com Git - mothur.git/blob - endiannessmacros.h
working on pam
[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 /*********************************************************************/
15 // The following is copied from the staden io_lib-1.12.4 os.h - thanks!
16 /*********************************************************************/
17 /*********************************************************************/
18
19 /*
20  * Author: 
21  *         MRC Laboratory of Molecular Biology
22  *         Hills Road
23  *         Cambridge CB2 2QH
24  *         United Kingdom
25  *
26  * Description: operating system specific type definitions
27  *
28  */
29
30
31 /* Mac FAT binaries or unknown. Auto detect based on CPU type */
32 #if !defined(SP_BIG_ENDIAN) && !defined(SP_LITTLE_ENDIAN)
33         
34 /*
35  * x86 equivalents
36  */
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
40 #else
41 #define SP_BIG_ENDIAN
42 #endif
43
44
45
46 /*
47  * SUN Sparc
48  */
49 #if defined(__sparc__) || defined(__sparc)
50 #  if defined(SP_LITTLE_ENDIAN)
51 #    undef SP_LITTLE_ENDIAN
52 #  endif
53 #  define SP_BIG_ENDIAN
54 #endif
55
56 /*
57  * PowerPC
58  */
59 #if defined(__ppc__) || defined(__ppc)
60 #  if defined(SP_LITTLE_ENDIAN)
61 #    undef SP_LITTLE_ENDIAN
62 #  endif
63 #  define SP_BIG_ENDIAN
64 #endif
65
66 /* Some catch-alls */
67 #if defined(__LITTLE_ENDIAN__) || defined(__LITTLEENDIAN__)
68 #    define SP_LITTLE_ENDIAN
69 #endif
70
71 #if defined(__BIG_ENDIAN__) || defined(__BIGENDIAN__)
72 #    define SP_BIG_ENDIAN
73 #endif
74
75 #if defined(SP_BIG_ENDIAN) && defined(SP_LITTLE_ENDIAN)
76 #    error Both BIG and LITTLE endian defined. Fix os.h and/or Makefile
77 #endif
78
79 #if !defined(SP_BIG_ENDIAN) && !defined(SP_LITTLE_ENDIAN)
80 #    error Neither BIG nor LITTLE endian defined. Fix os.h and/or Makefile
81 #endif
82
83 #endif
84
85 /*-----------------------------------------------------------------------------
86  * Byte swapping macros
87  */
88
89 /*
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).
93  *
94  * They also have the ability to swap in situ (src == dst). Newer code now
95  * relies on this so don't change back!
96  */
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))
106
107 #define iswap_int4(x) \
108     (((x & 0x000000ff) << 24) + \
109      ((x & 0x0000ff00) <<  8) + \
110      ((x & 0x00ff0000) >>  8) + \
111      ((x & 0xff000000) >> 24))
112
113 #define iswap_int2(x) \
114     (((x & 0x00ff) << 8) + \
115      ((x & 0xff00) >> 8))
116
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))
120
121
122 /*
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.
126  */
127 #if 0
128 #if defined(__linux__)
129 #    include <byteswap.h>
130 #    undef iswap_int8
131 #    undef iswap_int4
132 #    undef iswap_int2
133 #    define iswap_int8 bswap_64
134 #    define iswap_int4 bswap_32
135 #    define iswap_int2 bswap_16
136 #endif
137 #endif
138
139
140 /*
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
145  * format.
146  *
147  * This leads to efficient code as most of the time these macros are
148  * trivial.
149  */
150 #ifdef SP_BIG_ENDIAN
151 #define be_int8(x) (x)
152 #define be_int4(x) (x)
153 #define be_int2(x) (x)
154 #define be_int1(x) (x)
155
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)
160 #endif
161
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)
167
168 #define le_int8(x) (x)
169 #define le_int4(x) (x)
170 #define le_int2(x) (x)
171 #define le_int1(x) (x)
172 #endif
173
174 #endif
175