T-EMU  2
The Terma Emulator
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
Bitmanip.h
Go to the documentation of this file.
1 #ifndef TEMU_BITMANIP_H
2 #define TEMU_BITMANIP_H
3 
4 #include <stdint.h>
5 
6 #ifdef __cplusplus
7 extern "C" {
8 #endif
9 
10 // TODO: Make this platform independent
11 
12 static inline uint16_t
13 temu_swap16(uint16_t HWord)
14 {
15  uint16_t Res = HWord << 8 | HWord >> 8;
16  return Res;
17 }
18 
19 static inline uint32_t
20 temu_swap32(uint32_t Word)
21 {
22  uint32_t Res = (((uint32_t)temu_swap16(Word)) << 16)
23  | (uint32_t)temu_swap16(Word >> 16);
24  return Res;
25 }
26 
27 static inline uint64_t
28 temu_swap64(uint64_t DWord)
29 {
30  uint64_t Res = (((uint64_t)temu_swap32(DWord)) << 32)
31  | (uint64_t)temu_swap32(DWord >> 32);
32  return Res;
33 }
34 
35 
36 static inline uint16_t
37 temu_swapBigHost16(uint16_t HWord)
38 {
39  return temu_swap16(HWord);
40 }
41 
42 static inline uint32_t
43 temu_swapBigHost32(uint32_t Word)
44 {
45  return temu_swap32(Word);
46 }
47 
48 static inline uint64_t
49 temu_swapBigHost64(uint64_t DWord)
50 {
51  return temu_swap64(DWord);
52 }
53 
54 static inline uint16_t
55 temu_swapLittleHost16(uint16_t HWord)
56 {
57  return HWord;
58 }
59 
60 static inline uint32_t
61 temu_swapLittleHost32(uint32_t Word)
62 {
63  return Word;
64 }
65 
66 static inline uint64_t
67 temu_swapLittleHost64(uint64_t DWord)
68 {
69  return DWord;
70 }
71 
72 
73 // TODO: We should rename this header...
74 static inline int
75 temu_ctz32(uint32_t Word)
76 {
77  if (Word == 0) return 32;
78  int Res = __builtin_ctz(Word);
79  return Res;
80 }
81 
82 // TODO: We should rename this header...
83 static inline int
84 temu_clz32(uint32_t Word)
85 {
86  if (Word == 0) return 32;
87  int Res = __builtin_clz(Word);
88  return Res;
89 }
90 
91 // TODO: We should rename this header...
92 static inline int
93 temu_popcount32(uint32_t Word)
94 {
95  int Res = __builtin_popcount(Word);
96  return Res;
97 }
98 
99 static inline int
100 temu_parity32(uint32_t Word)
101 {
102  int Res = __builtin_parity(Word);
103  return Res;
104 }
105 
106 static inline int
107 temu_ctz64(uint64_t Word)
108 {
109  if (Word == 0) return 64;
110  int Res = __builtin_ctzl(Word);
111  return Res;
112 }
113 
114 // TODO: We should rename this header...
115 static inline int
116 temu_clz64(uint64_t Word)
117 {
118  if (Word == 0) return 64;
119  int Res = __builtin_clzl(Word);
120  return Res;
121 }
122 
123 // TODO: We should rename this header...
124 static inline int
125 temu_popcount64(uint64_t Word)
126 {
127  int Res = __builtin_popcountl(Word);
128  return Res;
129 }
130 
131 static inline int
132 temu_parity64(uint64_t Word)
133 {
134  int Res = __builtin_parityl(Word);
135  return Res;
136 }
137 
138 
139 #ifdef __cplusplus
140 }
141 #endif
142 
143 #endif /* !TEMU_BITMANIP_H */