0
|
1 #ifndef BAM_ENDIAN_H
|
|
2 #define BAM_ENDIAN_H
|
|
3
|
|
4 #include <stdint.h>
|
|
5
|
|
6 static inline int bam_is_big_endian()
|
|
7 {
|
|
8 long one= 1;
|
|
9 return !(*((char *)(&one)));
|
|
10 }
|
|
11 static inline uint16_t bam_swap_endian_2(uint16_t v)
|
|
12 {
|
|
13 return (uint16_t)(((v & 0x00FF00FFU) << 8) | ((v & 0xFF00FF00U) >> 8));
|
|
14 }
|
|
15 static inline void *bam_swap_endian_2p(void *x)
|
|
16 {
|
|
17 *(uint16_t*)x = bam_swap_endian_2(*(uint16_t*)x);
|
|
18 return x;
|
|
19 }
|
|
20 static inline uint32_t bam_swap_endian_4(uint32_t v)
|
|
21 {
|
|
22 v = ((v & 0x0000FFFFU) << 16) | (v >> 16);
|
|
23 return ((v & 0x00FF00FFU) << 8) | ((v & 0xFF00FF00U) >> 8);
|
|
24 }
|
|
25 static inline void *bam_swap_endian_4p(void *x)
|
|
26 {
|
|
27 *(uint32_t*)x = bam_swap_endian_4(*(uint32_t*)x);
|
|
28 return x;
|
|
29 }
|
|
30 static inline uint64_t bam_swap_endian_8(uint64_t v)
|
|
31 {
|
|
32 v = ((v & 0x00000000FFFFFFFFLLU) << 32) | (v >> 32);
|
|
33 v = ((v & 0x0000FFFF0000FFFFLLU) << 16) | ((v & 0xFFFF0000FFFF0000LLU) >> 16);
|
|
34 return ((v & 0x00FF00FF00FF00FFLLU) << 8) | ((v & 0xFF00FF00FF00FF00LLU) >> 8);
|
|
35 }
|
|
36 static inline void *bam_swap_endian_8p(void *x)
|
|
37 {
|
|
38 *(uint64_t*)x = bam_swap_endian_8(*(uint64_t*)x);
|
|
39 return x;
|
|
40 }
|
|
41
|
|
42 #endif
|