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,2017 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 (defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)))
70  __asm__ __volatile__("rorw $8,%0\n\t" : "=r"(x) : "0"(x) : "cc");
71  return x;
72 #elif (_MSC_VER >= 1500)
73  return _byteswap_ushort(x);
74 #else
75  return (x>>8) | (x<<8);
76 #endif
77  }
78 
79 
80 // Byte swap unsiged int
81 static inline FXuint swap32(FXuint x){
82 #if ((__GNUC__ >= 5) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 8)))
83  return __builtin_bswap32(x);
84 #elif (defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)))
85  __asm__ __volatile__("bswapl %0\n\t" : "=r"(x): "0"(x));
86  return x;
87 #elif (_MSC_VER >= 1500)
88  return _byteswap_ulong(x);
89 #else
90  x=((x<<8)&0xFF00FF00)|((x>>8)&0x00FF00FF);
91  return (x>>16)|(x<<16);
92 #endif
93  }
94 
95 
96 // Byte swap unsigned long
97 static inline FXulong swap64(FXulong x){
98 #if ((__GNUC__ >= 5) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 8)))
99  return __builtin_bswap64(x);
100 #elif (defined(__GNUC__) && defined(__i386__))
101  union { struct { FXuint l; FXuint h; } s; FXulong x; } n;
102  n.x=x;
103  __asm__ __volatile__("bswapl %0\n\t"
104  "bswapl %1\n\t"
105  "xchgl %0,%1\n\t" : "=r"(n.s.l), "=r" (n.s.h) : "0"(n.s.l), "1"(n.s.h));
106  return n.x;
107 #elif (defined(__GNUC__) && defined(__x86_64__))
108  __asm__ __volatile__("bswapq %0\n\t" : "=r"(x) : "0"(x));
109  return x;
110 #elif (_MSC_VER >= 1500)
111  return _byteswap_uint64(x);
112 #else
113  x=((x<< 8)&FXULONG(0xFF00FF00FF00FF00))|((x>> 8)&FXULONG(0x00FF00FF00FF00FF));
114  x=((x<<16)&FXULONG(0xFFFF0000FFFF0000))|((x>>16)&FXULONG(0x0000FFFF0000FFFF));
115  return (x>>32)|(x<<32);
116 #endif
117  }
118 
119 
120 // Isolate least significant bit set
121 static inline FXuint lsb32(FXuint x){
122  return FXuint(x&(-FXint(x)));
123  }
124 
125 
126 // Isolate least significant bit set
127 static inline FXulong lsb64(FXulong x){
128  return FXulong(x&(-FXlong(x)));
129  }
130 
131 
132 // Isolate most significant bit set
133 static inline FXuint msb32(FXuint x){
134  x|=(x>>1);
135  x|=(x>>2);
136  x|=(x>>4);
137  x|=(x>>8);
138  x|=(x>>16);
139  return x-(x>>1);
140  }
141 
142 
143 // Isolate most significant bit set
144 static inline FXulong msb64(FXulong x){
145  x|=(x>>1);
146  x|=(x>>2);
147  x|=(x>>4);
148  x|=(x>>8);
149  x|=(x>>16);
150  x|=(x>>32);
151  return x-(x>>1);
152  }
153 
154 
155 // Count one-bits in non-zero integer
156 static inline FXuint pop32(FXuint x){
157 #if ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4)))
158  return __builtin_popcount(x);
159 #else
160  x=x-((x>>1)&0x55555555);
161  x=(x&0x33333333)+((x>>2)&0x33333333);
162  x=(x+(x>>4))&0x0F0F0F0F;
163  x=x+(x>>8);
164  x=x+(x>>16);
165  return (x&0x3F);
166 #endif
167  }
168 
169 
170 // Count leading zeros in non-zero integer
171 static inline FXuint clz32(FXuint x){
172 #if ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4)))
173  return __builtin_clz(x);
174 #else
175  FXuint f,e,d,c,b;
176  f=!(x&0xffff0000)<<4; x<<=f;
177  e=!(x&0xff000000)<<3; x<<=e;
178  d=!(x&0xf0000000)<<2; x<<=d;
179  c=!(x&0xC0000000)<<1; x<<=c;
180  b=!(x&0x80000000);
181  return f+e+d+c+b;
182 #endif
183  }
184 
185 
186 // Count trailing zeros in non-zero integer
187 static inline FXuint ctz32(FXuint x){
188 #if ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4)))
189  return __builtin_ctz(x);
190 #else
191  FXuint f,e,d,c,b;
192  f=!(x&0x0000ffff)<<4; x>>=f;
193  e=!(x&0x000000ff)<<3; x>>=e;
194  d=!(x&0x0000000f)<<2; x>>=d;
195  c=!(x&0x00000003)<<1; x>>=c;
196  b=!(x&0x00000001);
197  return f+e+d+c+b;
198 #endif
199  }
200 
201 
202 }
203 
204 #endif

Copyright © 1997-2016 Jeroen van der Zijp