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,2024 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 
28 static inline FXushort swap16(FXushort x){
29 #if ((__GNUC__ >= 5) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 8)))
30  return __builtin_bswap16(x);
31 #elif (_MSC_VER >= 1500)
32  return _byteswap_ushort(x);
33 #else
34  return (x>>8) | (x<<8);
35 #endif
36  }
37 
38 
40 static inline FXshort swap16(FXshort x){
41  return (FXushort)swap16((FXushort)x);
42  }
43 
44 
46 static inline FXuint swap32(FXuint x){
47 #if ((__GNUC__ >= 5) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 8)))
48  return __builtin_bswap32(x);
49 #elif (_MSC_VER >= 1500)
50  return _byteswap_ulong(x);
51 #else
52  x=((x<<8)&0xFF00FF00)|((x>>8)&0x00FF00FF);
53  return (x>>16)|(x<<16);
54 #endif
55  }
56 
57 
59 static inline FXint swap32(FXint x){
60  return (FXint)swap32((FXuint)x);
61  }
62 
63 
65 static inline FXfloat swap32(FXfloat x){
66  union { FXfloat f; FXuint n; } xx={x};
67  xx.n=swap32(xx.n);
68  return xx.f;
69  }
70 
71 
73 static inline FXulong swap64(FXulong x){
74 #if ((__GNUC__ >= 5) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 8)))
75  return __builtin_bswap64(x);
76 #elif (_MSC_VER >= 1500)
77  return _byteswap_uint64(x);
78 #else
79  x=((x<< 8)&FXULONG(0xFF00FF00FF00FF00))|((x>> 8)&FXULONG(0x00FF00FF00FF00FF));
80  x=((x<<16)&FXULONG(0xFFFF0000FFFF0000))|((x>>16)&FXULONG(0x0000FFFF0000FFFF));
81  return (x>>32)|(x<<32);
82 #endif
83  }
84 
85 
87 static inline FXlong swap64(FXlong x){
88  return (FXlong)swap64((FXulong)x);
89  }
90 
91 
93 static inline FXdouble swap64(FXdouble x){
94  union { FXdouble d; FXulong n; } xx={x};
95  xx.n=swap64(xx.n);
96  return xx.d;
97  }
98 
99 
101 static inline FXuchar reverse8(FXuchar x){
102  x=((x<<1)&0xAA) | ((x>>1)&0x55);
103  x=((x<<2)&0xCC) | ((x>>2)&0x33);
104  return (x<<4) | (x>>4);
105  }
106 
107 
109 static inline FXushort reverse16(FXushort x){
110  x=((x<<1)&0xAAAA) | ((x>>1)&0x5555);
111  x=((x<<2)&0xCCCC) | ((x>>2)&0x3333);
112  x=((x<<4)&0xF0F0) | ((x>>4)&0x0F0F);
113  return swap16(x);
114  }
115 
116 
118 static inline FXuint reverse32(FXuint x){
119  x=((x<<1)&0xAAAAAAAA) | ((x>>1)&0x55555555);
120  x=((x<<2)&0xCCCCCCCC) | ((x>>2)&0x33333333);
121  x=((x<<4)&0xF0F0F0F0) | ((x>>4)&0x0F0F0F0F);
122  return swap32(x);
123  }
124 
125 
127 static inline FXulong reverse64(FXulong x){
128  x=((x<< 1)&FXULONG(0xAAAAAAAAAAAAAAAA)) | ((x>> 1)&FXULONG(0x5555555555555555));
129  x=((x<< 2)&FXULONG(0xCCCCCCCCCCCCCCCC)) | ((x>> 2)&FXULONG(0x3333333333333333));
130  x=((x<< 4)&FXULONG(0xF0F0F0F0F0F0F0F0)) | ((x>> 4)&FXULONG(0x0F0F0F0F0F0F0F0F));
131  return swap64(x);
132  }
133 
134 
136 static inline FXuint lsb32(FXuint x){
137  return FXuint(x&(-FXint(x)));
138  }
139 
140 
142 static inline FXulong lsb64(FXulong x){
143  return FXulong(x&(-FXlong(x)));
144  }
145 
146 
148 static inline FXuint msb32(FXuint x){
149  x|=(x>>1);
150  x|=(x>>2);
151  x|=(x>>4);
152  x|=(x>>8);
153  x|=(x>>16);
154  return x-(x>>1);
155  }
156 
157 
159 static inline FXulong msb64(FXulong x){
160  x|=(x>>1);
161  x|=(x>>2);
162  x|=(x>>4);
163  x|=(x>>8);
164  x|=(x>>16);
165  x|=(x>>32);
166  return x-(x>>1);
167  }
168 
169 
171 static inline FXuint pop32(FXuint x){
172 #if ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4)))
173  return __builtin_popcount(x);
174 #elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
175  return __popcnt(x);
176 #else
177  x=x-((x>>1)&0x55555555);
178  x=(x&0x33333333)+((x>>2)&0x33333333);
179  return (((x+(x>>4))&0x0F0F0F0F)*0x01010101)>>24;
180 #endif
181  }
182 
183 
185 static inline FXulong pop64(FXulong x){
186 #if ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4)))
187 #if defined(__LP64__) || defined(_LP64) || (__WORDSIZE == 64)
188  return __builtin_popcountl(x);
189 #else
190  return __builtin_popcountll(x);
191 #endif
192 #elif defined(_MSC_VER) && (defined(_M_X64))
193  return __popcnt64(x);
194 #else
195  x=x-((x>>1)&FXULONG(0x5555555555555555));
196  x=(x&FXULONG(0x3333333333333333))+((x>>2)&FXULONG(0x3333333333333333));
197  return (((x+(x>>4))&FXULONG(0xf0f0f0f0f0f0f0f))*FXULONG(0x101010101010101))>>56;
198 #endif
199  }
200 
201 
203 static inline FXuint clz32(FXuint x){
204 #if ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4)))
205  return __builtin_clz(x);
206 #elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
207  unsigned long result;
208  _BitScanReverse(&result,x);
209  return 31-result;
210 #else
211  FXuint f,e,d,c,b;
212  f=(((FXint)(x-0x00010000))>>31)&16; x<<=f;
213  e=(((FXint)(x-0x01000000))>>31)&8; x<<=e;
214  d=(((FXint)(x-0x10000000))>>31)&4; x<<=d;
215  c=(((FXint)(x-0x40000000))>>31)&2; x<<=c;
216  b=(((FXint)(x-0x80000000))>>31)&1;
217  return f+e+d+c+b;
218 #endif
219  }
220 
221 
223 static inline FXulong clz64(FXulong x){
224 #if ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4)))
225 #if defined(__LP64__) || defined(_LP64) || (__WORDSIZE == 64)
226  return __builtin_clzl(x);
227 #else
228  return __builtin_clzll(x);
229 #endif
230 #elif defined(_MSC_VER) && defined(_M_X64)
231  unsigned long result;
232  _BitScanReverse64(&result,x);
233  return 63-result;
234 #else
235  FXulong g,f,e,d,c,b;
236  g=(((FXlong)(x-FXULONG(0x0000000100000000)))>>63)&32; x<<=g;
237  f=(((FXlong)(x-FXULONG(0x0001000000000000)))>>63)&16; x<<=f;
238  e=(((FXlong)(x-FXULONG(0x0100000000000000)))>>63)&8; x<<=e;
239  d=(((FXlong)(x-FXULONG(0x1000000000000000)))>>63)&4; x<<=d;
240  c=(((FXlong)(x-FXULONG(0x4000000000000000)))>>63)&2; x<<=c;
241  b=(((FXlong)(x-FXULONG(0x8000000000000000)))>>63)&1;
242  return g+f+e+d+c+b;
243 #endif
244  }
245 
246 
248 static inline FXuint ctz32(FXuint x){
249 #if ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4)))
250  return __builtin_ctz(x);
251 #elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
252  unsigned long result;
253  _BitScanForward(&result,x);
254  return result;
255 #else
256  return 31-clz32(x&-x);
257 #endif
258  }
259 
260 
262 static inline FXulong ctz64(FXulong x){
263 #if ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4)))
264 #if defined(__LP64__) || defined(_LP64) || (__WORDSIZE == 64)
265  return __builtin_ctzl(x);
266 #else
267  return __builtin_ctzll(x);
268 #endif
269 #elif defined(_MSC_VER) && defined(_M_X64)
270  unsigned long result;
271  _BitScanForward64(&result,x);
272  return result;
273 #else
274  return FXULONG(63)-clz64(x&-x);
275 #endif
276  }
277 
278 
280 static inline FXuint rol32(FXuint value,FXuint count){
281  return (value<<count) | (value>>(32-count));
282  }
283 
285 static inline FXuint ror32(FXuint value,FXuint count){
286  return (value>>count) | (value<<(32-count));
287  }
288 
289 
291 static inline FXulong rol64(FXulong value,FXulong count){
292  return (value<<count) | (value>>(FXULONG(64)-count));
293  }
294 
296 static inline FXulong ror64(FXulong value,FXulong count){
297  return (value>>count) | (value<<(FXULONG(64)-count));
298  }
299 
300 
302 static inline FXuint shl32(FXuint value,FXuint count){
303  return (value<<count);
304  }
305 
307 static inline FXuint shr32(FXuint value,FXuint count){
308  return (value>>count);
309  }
310 
312 static inline FXuint sar32(FXuint value,FXuint count){
313  return (FXuint)(((FXint)value)>>count);
314  }
315 
316 
318 static inline FXulong shl64(FXulong value,FXulong count){
319  return (value<<count);
320  }
321 
323 static inline FXulong shr64(FXulong value,FXulong count){
324  return (value>>count);
325  }
326 
328 static inline FXulong sar64(FXulong value,FXulong count){
329  return (FXulong)(((FXlong)value)>>count);
330  }
331 
332 
334 static inline FXuint hash32(FXuint x){
335  x=((x>>16)^x)*0x21F0AAAD;
336  x=((x>>15)^x)*0x735A2D97;
337  x=((x>>15)^x);
338  return x;
339  }
340 
341 
343 static inline FXuint unhash32(FXuint x){
344  x=((x>>15)^(x>>30)^x)*0x97132227;
345  x=((x>>15)^(x>>30)^x)*0x333C4925;
346  x=((x>>16)^x);
347  return x;
348  }
349 
350 
352 static inline FXulong hash64(FXulong x){
353  x=(x^(x>>30))*FXULONG(0xBF58476D1CE4E5B9);
354  x=(x^(x>>27))*FXULONG(0x94D049BB133111EB);
355  x=x^(x>>31);
356  return x;
357  }
358 
359 
361 static inline FXulong unhash64(FXulong x){
362  x=(x^(x>>31)^(x>>62))*FXULONG(0x319642B2D24D8EC3);
363  x=(x^(x>>27)^(x>>54))*FXULONG(0x96DE1B173F119089);
364  x=x^(x>>30)^(x>>60);
365  return x;
366  }
367 
368 
370 static inline FXuint hash32(FXfloat x){
371  return x!=0.0f ? hash32(Math::fpBits(x)) : 0;
372  }
373 
374 
376 static inline FXulong hash64(FXdouble x){
377  return x!=0.0 ? hash64(Math::fpBits(x)) : 0;
378  }
379 
380 }
381 
382 #endif
Definition: FX4Splitter.h:28

Copyright © 1997-2022 Jeroen van der Zijp