Silence a printf warning for %lu on 32bits archs.
[SXSI/libbp.git] / bp-utils.c
1 #include <stdio.h>
2 #include "bp-utils.h"
3
4 unsigned char popCount[] = {
5   0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,
6   1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
7   1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
8   2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
9   1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
10   2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
11   2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
12   3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
13   1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
14   2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
15   2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
16   3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
17   2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
18   3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
19   3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
20   4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8
21 };
22
23
24
25 static size_t allocated = 0;
26
27 void * bp_malloc(size_t n)
28 {
29   void * res = malloc(n);
30   if (!res) {
31     fprintf(stderr, __FILE__  ": failure to allocate %lu bytes\n", (long unsigned int) n);
32     exit(1);
33   };
34   allocated += n;
35   return res;
36 }
37
38 void bp_free(void * p)
39 {
40   if (p) free(p);
41 }
42 size_t bp_get_alloc_stats(void)
43 {
44   return allocated;
45 }
46 void bp_reset_alloc_states(void)
47 {
48   allocated = 0;
49 }
50
51 int bp_setbit(unsigned int *B, int i,int x)
52 {
53   int j,l;
54
55   j = i / D;
56   l = i % D;
57   if (x==0) B[j] &= (~(1<<(D-1-l)));
58   else if (x==1) B[j] |= (1<<(D-1-l));
59   else {
60     printf("error setbit x=%d\n",x);
61     exit(1);
62   }
63   return x;
64 }
65
66 int bp_setbits(unsigned int *B, int i, int d, int x)
67 {
68   int j;
69
70   for (j=0; j<d; j++) {
71     bp_setbit(B,i+j,(x>>(d-j-1))&1);
72   }
73   return x;
74 }