Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #if !defined(_SPANDSP_BIT_OPERATIONS_H_)
00031 #define _SPANDSP_BIT_OPERATIONS_H_
00032
00033 #if defined(__i386__) || defined(__x86_64__)
00034 #if !defined(__SUNPRO_C) || (__SUNPRO_C >= 0x0590)
00035 #define SPANDSP_USE_86_ASM
00036 #endif
00037 #endif
00038
00039 #if defined(__cplusplus)
00040 extern "C"
00041 {
00042 #endif
00043
00044
00045
00046
00047 static __inline__ int top_bit(unsigned int bits)
00048 {
00049 #if defined(SPANDSP_USE_86_ASM)
00050 int res;
00051
00052 __asm__ (" xorl %[res],%[res];\n"
00053 " decl %[res];\n"
00054 " bsrl %[bits],%[res]\n"
00055 : [res] "=&r" (res)
00056 : [bits] "rm" (bits));
00057 return res;
00058 #elif defined(__ppc__) || defined(__powerpc__)
00059 int res;
00060
00061 __asm__ ("cntlzw %[res],%[bits];\n"
00062 : [res] "=&r" (res)
00063 : [bits] "r" (bits));
00064 return 31 - res;
00065 #elif defined(_M_IX86)
00066
00067 __asm
00068 {
00069 xor eax, eax
00070 dec eax
00071 bsr eax, bits
00072 }
00073 #elif defined(_M_X64)
00074
00075
00076 int res;
00077
00078 if (bits == 0)
00079 return -1;
00080 res = 0;
00081 if (bits & 0xFFFF0000)
00082 {
00083 bits &= 0xFFFF0000;
00084 res += 16;
00085 }
00086 if (bits & 0xFF00FF00)
00087 {
00088 bits &= 0xFF00FF00;
00089 res += 8;
00090 }
00091 if (bits & 0xF0F0F0F0)
00092 {
00093 bits &= 0xF0F0F0F0;
00094 res += 4;
00095 }
00096 if (bits & 0xCCCCCCCC)
00097 {
00098 bits &= 0xCCCCCCCC;
00099 res += 2;
00100 }
00101 if (bits & 0xAAAAAAAA)
00102 {
00103 bits &= 0xAAAAAAAA;
00104 res += 1;
00105 }
00106 return res;
00107 #else
00108 int res;
00109
00110 if (bits == 0)
00111 return -1;
00112 res = 0;
00113 if (bits & 0xFFFF0000)
00114 {
00115 bits &= 0xFFFF0000;
00116 res += 16;
00117 }
00118 if (bits & 0xFF00FF00)
00119 {
00120 bits &= 0xFF00FF00;
00121 res += 8;
00122 }
00123 if (bits & 0xF0F0F0F0)
00124 {
00125 bits &= 0xF0F0F0F0;
00126 res += 4;
00127 }
00128 if (bits & 0xCCCCCCCC)
00129 {
00130 bits &= 0xCCCCCCCC;
00131 res += 2;
00132 }
00133 if (bits & 0xAAAAAAAA)
00134 {
00135 bits &= 0xAAAAAAAA;
00136 res += 1;
00137 }
00138 return res;
00139 #endif
00140 }
00141
00142
00143
00144
00145
00146 static __inline__ int bottom_bit(unsigned int bits)
00147 {
00148 int res;
00149
00150 #if defined(SPANDSP_USE_86_ASM)
00151 __asm__ (" xorl %[res],%[res];\n"
00152 " decl %[res];\n"
00153 " bsfl %[bits],%[res]\n"
00154 : [res] "=&r" (res)
00155 : [bits] "rm" (bits));
00156 return res;
00157 #else
00158 if (bits == 0)
00159 return -1;
00160 res = 31;
00161 if (bits & 0x0000FFFF)
00162 {
00163 bits &= 0x0000FFFF;
00164 res -= 16;
00165 }
00166 if (bits & 0x00FF00FF)
00167 {
00168 bits &= 0x00FF00FF;
00169 res -= 8;
00170 }
00171 if (bits & 0x0F0F0F0F)
00172 {
00173 bits &= 0x0F0F0F0F;
00174 res -= 4;
00175 }
00176 if (bits & 0x33333333)
00177 {
00178 bits &= 0x33333333;
00179 res -= 2;
00180 }
00181 if (bits & 0x55555555)
00182 {
00183 bits &= 0x55555555;
00184 res -= 1;
00185 }
00186 return res;
00187 #endif
00188 }
00189
00190
00191
00192
00193
00194 static __inline__ uint8_t bit_reverse8(uint8_t x)
00195 {
00196 #if defined(__i386__) || defined(__x86_64__) || defined(__ppc__) || defined(__powerpc__)
00197
00198 return ((x*0x0802U & 0x22110U) | (x*0x8020U & 0x88440U))*0x10101U >> 16;
00199 #else
00200
00201 x = (x >> 4) | (x << 4);
00202 x = ((x & 0xCC) >> 2) | ((x & 0x33) << 2);
00203 return ((x & 0xAA) >> 1) | ((x & 0x55) << 1);
00204 #endif
00205 }
00206
00207
00208
00209
00210
00211 SPAN_DECLARE(uint16_t) bit_reverse16(uint16_t data);
00212
00213
00214
00215
00216 SPAN_DECLARE(uint32_t) bit_reverse32(uint32_t data);
00217
00218
00219
00220
00221 SPAN_DECLARE(uint32_t) bit_reverse_4bytes(uint32_t data);
00222
00223 #if defined(__x86_64__)
00224
00225
00226
00227 SPAN_DECLARE(uint64_t) bit_reverse_8bytes(uint64_t data);
00228 #endif
00229
00230
00231
00232
00233
00234 SPAN_DECLARE(void) bit_reverse(uint8_t to[], const uint8_t from[], int len);
00235
00236
00237
00238
00239 SPAN_DECLARE(int) one_bits32(uint32_t x);
00240
00241
00242
00243
00244 SPAN_DECLARE(uint32_t) make_mask32(uint32_t x);
00245
00246
00247
00248
00249 SPAN_DECLARE(uint16_t) make_mask16(uint16_t x);
00250
00251
00252
00253
00254
00255 static __inline__ uint32_t least_significant_one32(uint32_t x)
00256 {
00257 return (x & (-(int32_t) x));
00258 }
00259
00260
00261
00262
00263
00264
00265 static __inline__ uint32_t most_significant_one32(uint32_t x)
00266 {
00267 #if defined(__i386__) || defined(__x86_64__) || defined(__ppc__) || defined(__powerpc__)
00268 return 1 << top_bit(x);
00269 #else
00270 x = make_mask32(x);
00271 return (x ^ (x >> 1));
00272 #endif
00273 }
00274
00275
00276
00277
00278
00279 static __inline__ int parity8(uint8_t x)
00280 {
00281 x = (x ^ (x >> 4)) & 0x0F;
00282 return (0x6996 >> x) & 1;
00283 }
00284
00285
00286
00287
00288
00289 static __inline__ int parity16(uint16_t x)
00290 {
00291 x ^= (x >> 8);
00292 x = (x ^ (x >> 4)) & 0x0F;
00293 return (0x6996 >> x) & 1;
00294 }
00295
00296
00297
00298
00299
00300 static __inline__ int parity32(uint32_t x)
00301 {
00302 x ^= (x >> 16);
00303 x ^= (x >> 8);
00304 x = (x ^ (x >> 4)) & 0x0F;
00305 return (0x6996 >> x) & 1;
00306 }
00307
00308
00309 #if defined(__cplusplus)
00310 }
00311 #endif
00312
00313 #endif
00314