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,2025 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 #elif defined(_MSC_VER) && defined(_M_IX86)
235  unsigned long result;
236  if(x>>32){
237  _BitScanReverse(&result,(TUInt)(x>>32));
238  return 63-(32+result);
239  }
240  _BitScanReverse(&result,(TUInt)x);
241  return 63-result;
242 #else
243  FXulong g,f,e,d,c,b;
244  g=(((FXlong)(x-FXULONG(0x0000000100000000)))>>63)&32; x<<=g;
245  f=(((FXlong)(x-FXULONG(0x0001000000000000)))>>63)&16; x<<=f;
246  e=(((FXlong)(x-FXULONG(0x0100000000000000)))>>63)&8; x<<=e;
247  d=(((FXlong)(x-FXULONG(0x1000000000000000)))>>63)&4; x<<=d;
248  c=(((FXlong)(x-FXULONG(0x4000000000000000)))>>63)&2; x<<=c;
249  b=(((FXlong)(x-FXULONG(0x8000000000000000)))>>63)&1;
250  return g+f+e+d+c+b;
251 #endif
252  }
253 
254 
256 static inline FXuint ctz32(FXuint x){
257 #if ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4)))
258  return __builtin_ctz(x);
259 #elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
260  unsigned long result;
261  _BitScanForward(&result,x);
262  return result;
263 #else
264  return 31-clz32(x&-x);
265 #endif
266  }
267 
268 
270 static inline FXulong ctz64(FXulong x){
271 #if ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4)))
272 #if defined(__LP64__) || defined(_LP64) || (__WORDSIZE == 64)
273  return __builtin_ctzl(x);
274 #else
275  return __builtin_ctzll(x);
276 #endif
277 #elif defined(_MSC_VER) && defined(_M_X64)
278  unsigned long result;
279  _BitScanForward64(&result,x);
280  return result;
281 #elif defined(_MSC_VER) && defined(_M_IX86)
282  unsigned long result;
283  if((TUInt)x){
284  _BitScanForward(&result,(TUInt)x);
285  return result;
286  }
287  _BitScanForward(&result,(TUInt)(x>>32));
288  return result+32;
289 #else
290  return FXULONG(63)-clz64(x&-x);
291 #endif
292  }
293 
294 
296 static inline FXuint rol32(FXuint value,FXuint count){
297  return (value<<count) | (value>>(32-count));
298  }
299 
301 static inline FXuint ror32(FXuint value,FXuint count){
302  return (value>>count) | (value<<(32-count));
303  }
304 
305 
307 static inline FXulong rol64(FXulong value,FXulong count){
308  return (value<<count) | (value>>(FXULONG(64)-count));
309  }
310 
312 static inline FXulong ror64(FXulong value,FXulong count){
313  return (value>>count) | (value<<(FXULONG(64)-count));
314  }
315 
316 
318 static inline FXuint shl32(FXuint value,FXuint count){
319  return (value<<count);
320  }
321 
323 static inline FXuint shr32(FXuint value,FXuint count){
324  return (value>>count);
325  }
326 
328 static inline FXuint sar32(FXuint value,FXuint count){
329  return (FXuint)(((FXint)value)>>count);
330  }
331 
332 
334 static inline FXulong shl64(FXulong value,FXulong count){
335  return (value<<count);
336  }
337 
339 static inline FXulong shr64(FXulong value,FXulong count){
340  return (value>>count);
341  }
342 
344 static inline FXulong sar64(FXulong value,FXulong count){
345  return (FXulong)(((FXlong)value)>>count);
346  }
347 
348 
350 static inline FXuchar usatb(FXint x){
351  x&=~sar32(x,31); x|=sar32(0xff-x,31);
352  return (FXuchar)x;
353  }
354 
355 
357 static inline FXuchar usatb(FXuint x){
358  return (FXuchar)(x|sar32(0xff-x,31));
359  }
360 
361 
363 static inline FXushort usats(FXint x){
364  x&=~sar32(x,31); x|=sar32(0xffff-x,31);
365  return (FXushort)x;
366  }
367 
368 
370 static inline FXushort usats(FXuint x){
371  return (FXushort)(x|sar32(0xffff-x,31));
372  }
373 
374 
376 static inline FXuint hash32(FXuint x){
377  x=((x>>16)^x)*0x21F0AAAD;
378  x=((x>>15)^x)*0x735A2D97;
379  x=((x>>15)^x);
380  return x;
381  }
382 
383 
385 static inline FXuint unhash32(FXuint x){
386  x=((x>>15)^(x>>30)^x)*0x97132227;
387  x=((x>>15)^(x>>30)^x)*0x333C4925;
388  x=((x>>16)^x);
389  return x;
390  }
391 
392 
394 static inline FXulong hash64(FXulong x){
395  x=(x^(x>>30))*FXULONG(0xBF58476D1CE4E5B9);
396  x=(x^(x>>27))*FXULONG(0x94D049BB133111EB);
397  x=x^(x>>31);
398  return x;
399  }
400 
401 
403 static inline FXulong unhash64(FXulong x){
404  x=(x^(x>>31)^(x>>62))*FXULONG(0x319642B2D24D8EC3);
405  x=(x^(x>>27)^(x>>54))*FXULONG(0x96DE1B173F119089);
406  x=x^(x>>30)^(x>>60);
407  return x;
408  }
409 
410 
412 static inline FXuint hash32(FXfloat x){
413  return x!=0.0f ? hash32(Math::fpBits(x)) : 0;
414  }
415 
416 
418 static inline FXulong hash64(FXdouble x){
419  return x!=0.0 ? hash64(Math::fpBits(x)) : 0;
420  }
421 
422 }
423 
424 #endif
Definition: FX4Splitter.h:28

Copyright © 1997-2022 Jeroen van der Zijp