• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

Bitops.h

Go to the documentation of this file.
00001 /** @file
00002 
00003   Utility functions for efficient bit operations
00004 
00005   @section license License
00006 
00007   Licensed to the Apache Software Foundation (ASF) under one
00008   or more contributor license agreements.  See the NOTICE file
00009   distributed with this work for additional information
00010   regarding copyright ownership.  The ASF licenses this file
00011   to you under the Apache License, Version 2.0 (the
00012   "License"); you may not use this file except in compliance
00013   with the License.  You may obtain a copy of the License at
00014 
00015       http://www.apache.org/licenses/LICENSE-2.0
00016 
00017   Unless required by applicable law or agreed to in writing, software
00018   distributed under the License is distributed on an "AS IS" BASIS,
00019   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00020   See the License for the specific language governing permissions and
00021   limitations under the License.
00022  */
00023 
00024 #ifndef __BITOPS_H__
00025 #define __BITOPS_H__
00026 #include "libts.h"
00027 
00028 /**
00029   Find First (bit) Set. Index starts at 1.
00030 
00031   @return zero for zero arg.
00032 
00033 */
00034 static inline int
00035 ink_ffs(int n)
00036 {
00037   return ffs(n);
00038 }
00039 
00040 /**
00041   Returns the index of the first bit (least significant bit), set "1"
00042   for each char in the range (start end).
00043 
00044   @param start pointer to the first character.
00045   @param end pointer to the location after the last character.
00046   @param p (if not null) returns the location of the first char that
00047     has a bit set.
00048   @return index of the first bit set in the first character that has a
00049     bit turned on. Zero if all bits are zero.
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   Returns the index of the first bit (least significant bit), unset "0"
00077   for each char in the range (start end).
00078 
00079   @param start pointer to the first character.
00080   @param end pointer to the location after the last character.
00081   @param p (if not null) returns the location of the first char that
00082     has a bit unset.
00083   @return index of the first bit set in the first character that has a
00084     bit turned off. Zero if all bits are 1.
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   Returns the index of the first bit (least significant bit), set "1"
00112   for each char in the range (start end).
00113 
00114   @param start pointer to the first character.
00115   @param end pointer to the location after the last character.
00116   @param offset
00117   @return index of the first bit set in the first character that has a
00118     bit turned on. Zero if all bits are zero.
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 /* __BITOPS_H__ */
00275 

Generated by  doxygen 1.7.1