Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members

fxendian.h
1 /********************************************************************************
2 * *
3 * B y t e S w a p p i n g S u p p o r t *
4 * *
5 *********************************************************************************
6 * Copyright (C) 2010,2022 by Jeroen van der Zijp. All Rights Reserved. *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU Lesser General Public License as published by *
10 * the Free Software Foundation; either version 3 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public License *
19 * along with this program. If not, see <http://www.gnu.org/licenses/> *
20 ********************************************************************************/
21 #ifndef FXENDIAN_H
22 #define FXENDIAN_H
23 
24 namespace FX {
25 
26 
27 // Bit reverse in a byte
28 static inline FXuchar reverse8(FXuchar x){
29  x=((x<<1)&0xAA) | ((x>>1)&0x55);
30  x=((x<<2)&0xCC) | ((x>>2)&0x33);
31  return (x<<4) | (x>>4);
32  }
33 
34 
35 // Bit reverse in a unsigned short
36 static inline FXushort reverse16(FXushort x){
37  x=((x<<1)&0xAAAA) | ((x>>1)&0x5555);
38  x=((x<<2)&0xCCCC) | ((x>>2)&0x3333);
39  x=((x<<4)&0xF0F0) | ((x>>4)&0x0F0F);
40  return (x<<8) | (x>>8);
41  }
42 
43 
44 // Bit reverse in an unsigned integer
45 static inline FXuint reverse32(FXuint x){
46  x=((x<<1)&0xAAAAAAAA) | ((x>>1)&0x55555555);
47  x=((x<<2)&0xCCCCCCCC) | ((x>>2)&0x33333333);
48  x=((x<<4)&0xF0F0F0F0) | ((x>>4)&0x0F0F0F0F);
49  x=((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
50  return (x<<16) | (x>>16);
51  }
52 
53 
54 // Bit reverse in an unsigned long
55 static inline FXulong reverse64(FXulong x){
56  x=((x<< 1)&FXULONG(0xAAAAAAAAAAAAAAAA)) | ((x>> 1)&FXULONG(0x5555555555555555));
57  x=((x<< 2)&FXULONG(0xCCCCCCCCCCCCCCCC)) | ((x>> 2)&FXULONG(0x3333333333333333));
58  x=((x<< 4)&FXULONG(0xF0F0F0F0F0F0F0F0)) | ((x>> 4)&FXULONG(0x0F0F0F0F0F0F0F0F));
59  x=((x<< 8)&FXULONG(0xFF00FF00FF00FF00)) | ((x>> 8)&FXULONG(0x00FF00FF00FF00FF));
60  x=((x<<16)&FXULONG(0xFFFF0000FFFF0000)) | ((x>>16)&FXULONG(0x0000FFFF0000FFFF));
61  return (x<<32) | (x>>32);
62  }
63 
64 
65 // Byte swap unsigned short
66 static inline FXushort swap16(FXushort x){
67 #if ((__GNUC__ >= 5) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 8)))
68  return __builtin_bswap16(x);
69 #elif (_MSC_VER >= 1500)
70  return _byteswap_ushort(x);
71 #else
72  return (x>>8) | (x<<8);
73 #endif
74  }
75 
76 
77 // Byte swap unsiged int
78 static inline FXuint swap32(FXuint x){
79 #if ((__GNUC__ >= 5) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 8)))
80  return __builtin_bswap32(x);
81 #elif (_MSC_VER >= 1500)
82  return _byteswap_ulong(x);
83 #else
84  x=((x<<8)&0xFF00FF00)|((x>>8)&0x00FF00FF);
85  return (x>>16)|(x<<16);
86 #endif
87  }
88 
89 
90 // Byte swap unsigned long
91 static inline FXulong swap64(FXulong x){
92 #if ((__GNUC__ >= 5) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 8)))
93  return __builtin_bswap64(x);
94 #elif (_MSC_VER >= 1500)
95  return _byteswap_uint64(x);
96 #else
97  x=((x<< 8)&FXULONG(0xFF00FF00FF00FF00))|((x>> 8)&FXULONG(0x00FF00FF00FF00FF));
98  x=((x<<16)&FXULONG(0xFFFF0000FFFF0000))|((x>>16)&FXULONG(0x0000FFFF0000FFFF));
99  return (x>>32)|(x<<32);
100 #endif
101  }
102 
103 
104 // Isolate least significant bit set
105 static inline FXuint lsb32(FXuint x){
106  return FXuint(x&(-FXint(x)));
107  }
108 
109 
110 // Isolate least significant bit set
111 static inline FXulong lsb64(FXulong x){
112  return FXulong(x&(-FXlong(x)));
113  }
114 
115 
116 // Isolate most significant bit set
117 static inline FXuint msb32(FXuint x){
118  x|=(x>>1);
119  x|=(x>>2);
120  x|=(x>>4);
121  x|=(x>>8);
122  x|=(x>>16);
123  return x-(x>>1);
124  }
125 
126 
127 // Isolate most significant bit set
128 static inline FXulong msb64(FXulong x){
129  x|=(x>>1);
130  x|=(x>>2);
131  x|=(x>>4);
132  x|=(x>>8);
133  x|=(x>>16);
134  x|=(x>>32);
135  return x-(x>>1);
136  }
137 
138 
139 // Count one-bits in integer
140 static inline FXuint pop32(FXuint x){
141 #if ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4)))
142  return __builtin_popcount(x);
143 #elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
144  return __popcnt(x);
145 #else
146  x=x-((x>>1)&0x55555555);
147  x=(x&0x33333333)+((x>>2)&0x33333333);
148  return (((x+(x>>4))&0x0F0F0F0F)*0x01010101)>>24;
149 #endif
150  }
151 
152 
153 // Count one-bits in long
154 static inline FXulong pop64(FXulong x){
155 #if ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4)))
156 #if defined(__LP64__) || defined(_LP64) || (__WORDSIZE == 64)
157  return __builtin_popcountl(x);
158 #else
159  return __builtin_popcountll(x);
160 #endif
161 #elif defined(_MSC_VER) && (defined(_M_X64))
162  return __popcnt64(x);
163 #else
164  x=x-((x>>1)&FXULONG(0x5555555555555555));
165  x=(x&FXULONG(0x3333333333333333))+((x>>2)&FXULONG(0x3333333333333333));
166  return (((x+(x>>4))&FXULONG(0xf0f0f0f0f0f0f0f))*FXULONG(0x101010101010101))>>56;
167 #endif
168  }
169 
170 
171 // Count leading zeros in non-zero integer
172 static inline FXuint clz32(FXuint x){
173 #if ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4)))
174  return __builtin_clz(x);
175 #elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
176  unsigned long result;
177  _BitScanReverse(&result,x);
178  return 31-result;
179 #else
180  FXuint f,e,d,c,b;
181  f=(((FXint)(x-0x00010000))>>31)&16; x<<=f;
182  e=(((FXint)(x-0x01000000))>>31)&8; x<<=e;
183  d=(((FXint)(x-0x10000000))>>31)&4; x<<=d;
184  c=(((FXint)(x-0x40000000))>>31)&2; x<<=c;
185  b=(((FXint)(x-0x80000000))>>31)&1;
186  return f+e+d+c+b;
187 #endif
188  }
189 
190 
191 // Count leading zeros in non-zero long
192 static inline FXulong clz64(FXulong x){
193 #if ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4)))
194 #if defined(__LP64__) || defined(_LP64) || (__WORDSIZE == 64)
195  return __builtin_clzl(x);
196 #else
197  return __builtin_clzll(x);
198 #endif
199 #elif defined(_MSC_VER) && defined(_M_X64)
200  unsigned long result;
201  _BitScanReverse64(&result,x);
202  return 63-result;
203 #else
204  FXulong g,f,e,d,c,b;
205  g=(((FXlong)(x-FXULONG(0x0000000100000000)))>>63)&32; x<<=g;
206  f=(((FXlong)(x-FXULONG(0x0001000000000000)))>>63)&16; x<<=f;
207  e=(((FXlong)(x-FXULONG(0x0100000000000000)))>>63)&8; x<<=e;
208  d=(((FXlong)(x-FXULONG(0x1000000000000000)))>>63)&4; x<<=d;
209  c=(((FXlong)(x-FXULONG(0x4000000000000000)))>>63)&2; x<<=c;
210  b=(((FXlong)(x-FXULONG(0x8000000000000000)))>>63)&1;
211  return g+f+e+d+c+b;
212 #endif
213  }
214 
215 
216 // Count trailing zeros in non-zero integer
217 static inline FXuint ctz32(FXuint x){
218 #if ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4)))
219  return __builtin_ctz(x);
220 #elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
221  unsigned long result;
222  _BitScanForward(&result,x);
223  return result;
224 #else
225  return 31-clz32(x&-x);
226 #endif
227  }
228 
229 
230 // Count trailing zeros in non-zero long
231 static inline FXulong ctz64(FXulong x){
232 #if ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4)))
233 #if defined(__LP64__) || defined(_LP64) || (__WORDSIZE == 64)
234  return __builtin_ctzl(x);
235 #else
236  return __builtin_ctzll(x);
237 #endif
238 #elif defined(_MSC_VER) && defined(_M_X64)
239  unsigned long result;
240  _BitScanForward64(&result,x);
241  return result;
242 #else
243  return FXULONG(63)-clz64(x&-x);
244 #endif
245  }
246 
247 
248 // Roll bits left, 32-bit flavor (count<32)
249 static inline FXuint rol32(FXuint value,FXuint count){
250  return (value<<count) | (value>>(32-count));
251  }
252 
253 // Roll bits right, 32-bit flavor (count<32)
254 static inline FXuint ror32(FXuint value,FXuint count){
255  return (value>>count) | (value<<(32-count));
256  }
257 
258 
259 // Roll bits left, 64-bit flavor (count<64)
260 static inline FXulong rol64(FXulong value,FXulong count){
261  return (value<<count) | (value>>(FXULONG(64)-count));
262  }
263 
264 // Roll bits right, 64-bit flavor (count<64)
265 static inline FXulong ror64(FXulong value,FXulong count){
266  return (value>>count) | (value<<(FXULONG(64)-count));
267  }
268 
269 
270 // Shift bits left, 32-bit flavor (count<32)
271 static inline FXuint shl32(FXuint value,FXuint count){
272  return (value<<count);
273  }
274 
275 // Shift bits right, 32-bit flavor (count<32)
276 static inline FXuint shr32(FXuint value,FXuint count){
277  return (value>>count);
278  }
279 
280 
281 // Shift bits left, 64-bit flavor (count<64)
282 static inline FXulong shl64(FXulong value,FXulong count){
283  return (value<<count);
284  }
285 
286 // Shift bits right, 64-bit flavor (count<64)
287 static inline FXulong shr64(FXulong value,FXulong count){
288  return (value>>count);
289  }
290 
291 }
292 
293 #endif
Definition: FX4Splitter.h:28

Copyright © 1997-2022 Jeroen van der Zijp