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 #ifndef __BITOPS_H__
00025 #define __BITOPS_H__
00026 #include "libts.h"
00027
00028
00029
00030
00031
00032
00033
00034 static inline int
00035 ink_ffs(int n)
00036 {
00037 return ffs(n);
00038 }
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 static inline int
00053 bitops_first_set(unsigned char *start, unsigned char *end, unsigned char **p)
00054 {
00055 extern unsigned char bit_table[];
00056
00057 int idx;
00058
00059 idx = 0;
00060 while (start != end) {
00061 idx = bit_table[*start];
00062 if (idx) {
00063 break;
00064 }
00065 start += 1;
00066 }
00067
00068 if (p) {
00069 *p = start;
00070 }
00071
00072 return idx;
00073 }
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087 static inline int
00088 bitops_first_unset(unsigned char *start, unsigned char *end, unsigned char **p)
00089 {
00090 extern unsigned char bit_table[];
00091
00092 int idx;
00093
00094 idx = 0;
00095 while (start != end) {
00096 idx = bit_table[~(*start)];
00097 if (idx) {
00098 break;
00099 }
00100 start += 1;
00101 }
00102
00103 if (p) {
00104 *p = start;
00105 }
00106
00107 return idx;
00108 }
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121 static inline int
00122 bitops_next_set(unsigned char *start, unsigned char *end, int offset)
00123 {
00124 extern unsigned char bit_table[];
00125
00126 unsigned char *p;
00127 unsigned char c;
00128 size_t idx;
00129 int t;
00130
00131 idx = 0;
00132 p = start + offset / 8;
00133 t = (offset % 8) + 1;
00134
00135 while (p != end) {
00136 idx = bit_table[*p];
00137 if (idx) {
00138 c = *p;
00139 while (idx && (idx <= (size_t) t)) {
00140 c &= ~(1 << (idx - 1));
00141 idx = bit_table[c];
00142 }
00143
00144 if (idx) {
00145 break;
00146 }
00147 }
00148 p += 1;
00149 t = 0;
00150 }
00151
00152 if (idx) {
00153 idx -= 1;
00154 idx += (p - start) * 8;
00155 } else {
00156 idx = (size_t) - 1;
00157 }
00158
00159 return (int) idx;
00160 }
00161
00162 static inline int
00163 bitops_next_unset(unsigned char *start, unsigned char *end, int offset)
00164 {
00165 extern unsigned char bit_table[];
00166
00167 unsigned char *p;
00168 unsigned char c;
00169 size_t idx;
00170 int t;
00171
00172 idx = 0;
00173 p = start + offset / 8;
00174 t = (offset % 8) + 1;
00175
00176 while (p != end) {
00177 c = ~(*p);
00178 idx = bit_table[c];
00179 if (idx) {
00180 while (idx && (idx <= (size_t) t)) {
00181 c &= ~(1 << (idx - 1));
00182 idx = bit_table[c];
00183 }
00184
00185 if (idx) {
00186 break;
00187 }
00188 }
00189 p += 1;
00190 t = 0;
00191 }
00192
00193 if (idx) {
00194 idx -= 1;
00195 idx += (p - start) * 8;
00196 } else {
00197 idx = (size_t) - 1;
00198 }
00199
00200 return (int) idx;
00201 }
00202
00203 static inline int
00204 bitops_count(unsigned char *start, unsigned char *end)
00205 {
00206 extern unsigned char bit_count_table[];
00207
00208 int count;
00209
00210 count = 0;
00211 while (start != end) {
00212 count += bit_count_table[*start++];
00213 }
00214
00215 return count;
00216 }
00217
00218 static inline void
00219 bitops_union(unsigned char *s1, unsigned char *s2, int len)
00220 {
00221 int i;
00222
00223 if (!s1 || !s2) {
00224 return;
00225 }
00226
00227 for (i = 0; i < len; i++) {
00228 s1[i] |= s2[i];
00229 }
00230 }
00231
00232 static inline unsigned char
00233 bitops_set(unsigned char val, int bit)
00234 {
00235 return (val | (1 << bit));
00236 }
00237
00238 static inline void
00239 bitops_set(unsigned char *val, int bit)
00240 {
00241 int pos = bit >> 3;
00242 int idx = bit & 0x7;
00243 val[pos] |= (1 << idx);
00244 }
00245
00246 static inline unsigned char
00247 bitops_unset(unsigned char val, int bit)
00248 {
00249 return (val & ~(1 << bit));
00250 }
00251
00252 static inline void
00253 bitops_unset(unsigned char *val, int bit)
00254 {
00255 int pos = bit >> 3;
00256 int idx = bit & 0x7;
00257 val[pos] &= ~(1 << idx);
00258 }
00259
00260 static inline int
00261 bitops_isset(unsigned char val, int bit)
00262 {
00263 return ((val & (1 << bit)) != 0);
00264 }
00265
00266 static inline int
00267 bitops_isset(unsigned char *val, int bit)
00268 {
00269 int pos = bit / 8;
00270 int idx = bit % 8;
00271 return ((val[pos] & (1 << idx)) != 0);
00272 }
00273
00274 #endif
00275