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
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #ifndef __EG_BIT_H__
00044 #define __EG_BIT_H__
00045 #include <stdlib.h>
00046 #include <string.h>
00047 #include <stdio.h>
00048 #include <limits.h>
00049 #include "eg_config.h"
00050 #include "eg_macros.h"
00051 #include "eg_mempool.h"
00052
00053
00054
00055
00056
00057
00058
00059
00060 #define __EGBIT_WORD_SIZE__ 32U
00061 #define __EGBIT_SHIFT__ 5U
00062 #define __EGBIT_MASK__ 31U
00063
00064
00065
00066
00067
00068 #define EGbitsetSize(__Bsize) (((__Bsize)>>__EGBIT_SHIFT__)+(((__Bsize)&__EGBIT_MASK__)?1:0))
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080 typedef int32_t bit_int_t;
00081 typedef bit_int_t EGbitset_t;
00082
00083
00084
00085
00086
00087 int EGbitSanity (void);
00088
00089
00090
00091
00092 extern inline void EGfreeBitSet (void *bitfield,
00093 const size_t size,
00094 EGmemPool_t * mem)
00095 {
00096 EGmemPoolFree (bitfield, size / CHAR_BIT, mem);
00097 return;
00098 }
00099
00100
00101
00102
00103
00104 extern inline EGbitset_t *EGnewBitSet (EGmemPool_t * mem,
00105 size_t * n)
00106 {
00107
00108
00109 EGbitset_t *res;
00110
00111
00112 if (*n & __EGBIT_MASK__)
00113 *n = *n - (*n & __EGBIT_MASK__) + __EGBIT_WORD_SIZE__;
00114
00115
00116 res = (EGbitset_t *) EGmemPoolMalloc (mem, (*n) / CHAR_BIT);
00117 memset (res, 0, (*n) / CHAR_BIT);
00118
00119
00120 return res;
00121 }
00122
00123
00124
00125
00126
00127 extern inline int EGbitOr (bit_int_t * dst,
00128 const bit_int_t * src,
00129 const unsigned int from,
00130 const unsigned int to)
00131 {
00132
00133
00134 unsigned int i;
00135 unsigned int end = to >> __EGBIT_SHIFT__;
00136
00137
00138 for (i = (from >> __EGBIT_SHIFT__); i <= end; i++)
00139 dst[i] = dst[i] | src[i];
00140
00141
00142 return 0;
00143 }
00144
00145
00146
00147
00148
00149 extern inline int EGbitAnd (bit_int_t * dst,
00150 const bit_int_t * src,
00151 const unsigned int from,
00152 const unsigned int to)
00153 {
00154
00155
00156 unsigned int i;
00157 unsigned int end = to >> __EGBIT_SHIFT__;
00158
00159
00160 for (i = (from >> __EGBIT_SHIFT__); i <= end; i++)
00161 dst[i] &= src[i];
00162
00163
00164 return 0;
00165 }
00166
00167
00168
00169
00170
00171 extern inline int EGbitRightShift (bit_int_t * dst,
00172 const bit_int_t * src,
00173 const unsigned int shift,
00174 const unsigned int from,
00175 const unsigned int to)
00176 {
00177
00178 bit_int_t carry_on;
00179 register int i;
00180 unsigned int start = from >> __EGBIT_SHIFT__;
00181
00182
00183 i = (to >> __EGBIT_SHIFT__);
00184 dst[i] = src[i] >> shift;
00185 carry_on = src[i] << (__EGBIT_WORD_SIZE__ - shift);
00186 for (i--; i >= (signed) start; i--)
00187 {
00188 dst[i] = (src[i] >> shift) | carry_on;
00189 carry_on = src[i] << (__EGBIT_WORD_SIZE__ - shift);
00190 }
00191
00192
00193 return 0;
00194 }
00195
00196
00197
00198
00199
00200 extern inline int EGbitLeftShift (bit_int_t * dst,
00201 const bit_int_t * src,
00202 const unsigned int shift,
00203 const unsigned int from,
00204 const unsigned int to)
00205 {
00206
00207 bit_int_t carry_on = 0;
00208 register unsigned int i;
00209 unsigned int end = to >> __EGBIT_SHIFT__;
00210
00211
00212 i = (from >> __EGBIT_SHIFT__);
00213 carry_on = src[i] >> (__EGBIT_WORD_SIZE__ - shift);
00214 dst[i] = src[i] << shift;
00215 for (i++; i <= end; i++)
00216 {
00217 dst[i] = (src[i] << shift) | carry_on;
00218 carry_on = src[i] >> (__EGBIT_WORD_SIZE__ - shift);
00219 }
00220
00221
00222 return 0;
00223 }
00224
00225
00226
00227
00228
00229 extern inline int EGbitUnset (bit_int_t * dst,
00230 const unsigned int pos)
00231 {
00232 MESSAGE (__EGBIT_DEBUG_LEVEL__,
00233 "set bit %u (chunk %u position %u)", pos,
00234 pos >> __EGBIT_SHIFT__, pos & __EGBIT_MASK__);
00235
00236 dst[(pos >> __EGBIT_SHIFT__)] &= (~(1 << (pos & __EGBIT_MASK__)));
00237
00238 return 0;
00239 }
00240
00241
00242
00243
00244
00245 extern inline int EGbitSet (bit_int_t * const dst,
00246 const unsigned int pos)
00247 {
00248 MESSAGE (__EGBIT_DEBUG_LEVEL__,
00249 "set bit %u (chunk %u position %u)", pos,
00250 pos >> __EGBIT_SHIFT__, pos & __EGBIT_MASK__);
00251
00252 dst[(pos >> __EGBIT_SHIFT__)] |= (1 << (pos & __EGBIT_MASK__));
00253
00254 return 0;
00255 }
00256
00257
00258
00259
00260
00261 extern inline int EGbitPlus (bit_int_t * dst,
00262 const bit_int_t * src,
00263 const unsigned int from,
00264 const unsigned int to)
00265 {
00266
00267 bit_int_t carry_on = 0,
00268 carry_on3 = 0,
00269 carry_on2 = 0;
00270 register unsigned int i;
00271 unsigned int end = to >> __EGBIT_SHIFT__;
00272
00273
00274 for (i = (from >> __EGBIT_SHIFT__); i <= end; i++)
00275 {
00276 carry_on2 =
00277 ((dst[i]) >> (__EGBIT_WORD_SIZE__ - 1)) | ((src[i]) >>
00278 (__EGBIT_WORD_SIZE__ - 1));
00279 carry_on3 =
00280 ((dst[i]) >> (__EGBIT_WORD_SIZE__ - 1)) & ((src[i]) >>
00281 (__EGBIT_WORD_SIZE__ - 1));
00282 dst[i] = dst[i] + src[i] + carry_on;
00283 carry_on2 &=
00284 (((~dst[i]) & (1ULL << (__EGBIT_WORD_SIZE__ - 1)))) >>
00285 (__EGBIT_WORD_SIZE__ - 1);
00286 carry_on = carry_on2 | carry_on3;
00287 }
00288
00289 return 0;
00290 }
00291
00292
00293
00294
00295 extern inline int EGbitCopy (bit_int_t * const a,
00296 const bit_int_t * const b,
00297 const unsigned int from,
00298 const unsigned int to)
00299 {
00300
00301 memcpy (a + (from >> __EGBIT_SHIFT__),
00302 b + (from >> __EGBIT_SHIFT__),
00303 sizeof (bit_int_t) * ((to - from) >> __EGBIT_SHIFT__));
00304
00305
00306 return 0;
00307 }
00308
00309
00310
00311
00312 extern inline int EGbitIsLeq (const bit_int_t * a,
00313 const bit_int_t * b,
00314 const unsigned int from,
00315 const unsigned int to)
00316 {
00317
00318 register unsigned int i;
00319 unsigned int start = from >> __EGBIT_SHIFT__;
00320
00321
00322 i = (to >> __EGBIT_SHIFT__) - start;
00323 do
00324 {
00325 if (a[i] < b[i])
00326 return 1;
00327 if (a[i] > b[i])
00328 return 0;
00329 } while (i--);
00330
00331
00332 return 1;
00333 }
00334
00335
00336
00337
00338 extern inline int EGbitIsEqual (const bit_int_t * a,
00339 const bit_int_t * b,
00340 const unsigned int from,
00341 const unsigned int to)
00342 {
00343
00344 register unsigned int i;
00345 unsigned int end = to >> __EGBIT_SHIFT__;
00346
00347
00348 for (i = (from >> __EGBIT_SHIFT__); i <= end; i++)
00349 {
00350 if (a[i] != b[i])
00351 return 0;
00352 }
00353
00354
00355 return 1;
00356 }
00357
00358
00359
00360
00361
00362 extern inline int EGbitXor (bit_int_t * dst,
00363 const bit_int_t * src,
00364 const unsigned int from,
00365 const unsigned int to)
00366 {
00367
00368
00369 unsigned int i;
00370 unsigned int end = to >> __EGBIT_SHIFT__;
00371
00372
00373 for (i = (from >> __EGBIT_SHIFT__); i <= end; i++)
00374 dst[i] ^= src[i];
00375
00376
00377 return 0;
00378 }
00379
00380
00381
00382
00383
00384 extern inline int EGbitReset (bit_int_t * dst,
00385 const unsigned int from,
00386 const unsigned int to)
00387 {
00388
00389
00390 unsigned int lo;
00391 unsigned int up;
00392
00393
00394 lo = from >> __EGBIT_SHIFT__;
00395 up = to >> __EGBIT_SHIFT__;
00396 memset (dst + lo, 0, (up + 1 - lo) * sizeof (bit_int_t));
00397
00398
00399 return 0;
00400 }
00401
00402
00403
00404
00405
00406 extern inline int EGbitTest (bit_int_t const *const dst,
00407 const unsigned int pos)
00408 {
00409 MESSAGE (__EGBIT_DEBUG_LEVEL__,
00410 "in EGbitTest, test bit %u (chunk %u position %u)", pos,
00411 pos >> __EGBIT_SHIFT__, pos & __EGBIT_MASK__);
00412
00413 return (dst[(pos >> __EGBIT_SHIFT__)] & (1 << (pos & __EGBIT_MASK__)));
00414 }
00415
00416
00417
00418
00419
00420 extern inline unsigned int EGbitNext (const bit_int_t * dst,
00421 const unsigned int pos,
00422 const unsigned int size)
00423 {
00424
00425
00426 unsigned int curChunk = pos >> __EGBIT_SHIFT__,
00427 curPos = pos;
00428 bit_int_t curMask = (1 << (pos & __EGBIT_MASK__));
00429
00430
00431 while (curMask)
00432 {
00433 if (dst[curChunk] & curMask)
00434 return curPos;
00435 curPos++;
00436 curMask = curMask << 1;
00437 }
00438
00439
00440 curChunk++;
00441 curMask = 1;
00442 while ((curPos < size) && (!dst[curChunk]))
00443 {
00444 curChunk++;
00445 curPos += __EGBIT_WORD_SIZE__;
00446 }
00447
00448
00449 if (curPos >= size)
00450 return (size << 1);
00451
00452
00453
00454 while (curMask)
00455 {
00456 if (dst[curChunk] & curMask)
00457 return curPos;
00458 curPos++;
00459 curMask = curMask << 1;
00460 }
00461
00462
00463
00464 EXIT (1, "This is due to a programming error or some memory overwrite");
00465 exit (1);
00466 return UINT_MAX;
00467 }
00468
00469
00470
00471
00472
00473 extern inline unsigned int EGbitPrev (const EGbitset_t * dst,
00474 const unsigned int pos)
00475 {
00476
00477
00478 register unsigned int curChunk = pos >> __EGBIT_SHIFT__,
00479 curPos = pos;
00480 EGbitset_t curMask = (1 << (pos & __EGBIT_MASK__));
00481
00482
00483 while (curMask)
00484 {
00485 if (dst[curChunk] & curMask)
00486 return curPos;
00487 curPos--;
00488 curMask = curMask >> 1;
00489 }
00490
00491
00492 curChunk--;
00493 while ((curChunk < pos) && (!dst[curChunk]))
00494 {
00495 curChunk--;
00496 curPos -= __EGBIT_WORD_SIZE__;
00497 }
00498
00499
00500 if (curChunk > pos)
00501 return UINT_MAX;
00502
00503
00504
00505 curMask = 1U << __EGBIT_MASK__;
00506 while (curMask)
00507 {
00508 if (dst[curChunk] & curMask)
00509 return curPos;
00510 curPos--;
00511 curMask = curMask >> 1;
00512 }
00513
00514
00515
00516 EXIT (1, "This is due to a programming error or some memory overwrite");
00517 exit (1);
00518 return UINT_MAX;
00519 }
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532 extern inline EGbitset_t EGbitElemBitCount (EGbitset_t n)
00533 {
00534 #define EGBIT_TWO(c) (0x1u << (c))
00535 #define EGBIT_MASK(c) (((unsigned int)(-1)) / (EGBIT_TWO(EGBIT_TWO(c)) + 1u))
00536 #define EGBIT_COUNT(x,c) ((x) & EGBIT_MASK(c)) + (((x) >> (EGBIT_TWO(c))) & EGBIT_MASK(c))
00537 n = EGBIT_COUNT (n, 0);
00538 n = EGBIT_COUNT (n, 1);
00539 n = EGBIT_COUNT (n, 2);
00540 n = EGBIT_COUNT (n, 3);
00541 n = EGBIT_COUNT (n, 4);
00542 #if __EGBIT_WORD_SIZE__ == 64U
00543 n = EGBIT_COUNT (n, 5);
00544 #endif
00545 #undef EGBIT_TWO
00546 #undef EGBIT_MASK
00547 #undef EGBIT_COUNT
00548 return n;
00549 }
00550
00551
00552 extern inline EGbitset_t EGbitCount (bit_int_t * bitset,
00553 const unsigned int from,
00554 const unsigned int to)
00555 {
00556
00557 unsigned int i = from >> __EGBIT_SHIFT__;
00558 unsigned int end = to >> __EGBIT_SHIFT__;
00559 EGbitset_t res = 0;
00560 for (; i <= end; i++)
00561 res += EGbitElemBitCount (bitset[i]);
00562 return res;
00563 }
00564
00565 #endif