Flash Translation Layer  1.0
bits.h
Go to the documentation of this file.
1 #ifndef BITS_H
2 #define BITS_H
3 
4 #include <stdint.h>
5 #include <limits.h>
6 
7 #define BITS_NOT_FOUND ((uint64_t)UINT64_MAX)
8 
9 #define BITS_PER_BYTE (8)
10 #define BITS_PER_UINT64 (BITS_PER_BYTE * sizeof(uint64_t))
11 
12 #define BITS_TO_UINT64_ALIGN(x) \
13  (((uint64_t)x / BITS_PER_UINT64 + 1) * sizeof(uint64_t))
14 #define BITS_TO_UINT64(x) ((uint64_t)x / BITS_PER_UINT64)
15 
22 static inline void set_bit(uint64_t *bits, uint64_t index)
23 {
24  bits[BITS_TO_UINT64(index)] |=
25  ((uint64_t)0x1 << (index % BITS_PER_UINT64));
26 }
27 
36 static inline int get_bit(uint64_t *bits, uint64_t index)
37 {
38  return (bits[BITS_TO_UINT64(index)] &
39  ((uint64_t)0x1 << (index % BITS_PER_UINT64))) > 0;
40 }
41 
48 static inline void reset_bit(uint64_t *bits, uint64_t index)
49 {
50  bits[BITS_TO_UINT64(index)] &=
51  ~((uint64_t)0x1 << (index % BITS_PER_UINT64));
52 }
53 
63 static inline uint64_t find_first_zero_bit(uint64_t *bits, uint64_t size,
64  uint64_t idx)
65 {
66  while (idx < size) {
67  uint64_t bucket = bits[BITS_TO_UINT64(idx)];
68  if (bucket < (uint64_t)UINT64_MAX) {
69  uint64_t diff = 0;
70  for (diff = 0; diff < BITS_PER_UINT64; diff++) {
71  if ((bucket & (uint64_t)((uint64_t)0x1
72  << diff)) == 0x0) {
73  break;
74  }
75  }
76  return idx + diff;
77  }
78  idx += BITS_PER_UINT64;
79  }
80  return BITS_NOT_FOUND;
81 }
82 
92 static inline uint64_t find_first_one_bit(uint64_t *bits, uint64_t size,
93  uint64_t idx)
94 {
95  while (idx < size) {
96  uint64_t bucket = bits[BITS_TO_UINT64(idx)];
97  if (bucket > (uint64_t)0x0) {
98  uint64_t diff = 0;
99  for (diff = 0; diff < BITS_PER_UINT64; diff++) {
100  if ((bucket &
101  (uint64_t)((uint64_t)0x1 << diff)) > 0) {
102  break;
103  }
104  }
105  return idx + diff;
106  }
107  idx += BITS_PER_UINT64;
108  }
109  return BITS_NOT_FOUND;
110 }
111 
112 #endif
#define BITS_TO_UINT64(x)
Definition: bits.h:14
static void reset_bit(uint64_t *bits, uint64_t index)
reset the index position bit in the array(uint64_t)
Definition: bits.h:48
static uint64_t find_first_zero_bit(uint64_t *bits, uint64_t size, uint64_t idx)
find first zero bit in the array(uint64_t)
Definition: bits.h:63
#define BITS_PER_UINT64
Definition: bits.h:10
static int get_bit(uint64_t *bits, uint64_t index)
get the value at the index position bit in the array(uint64_t)
Definition: bits.h:36
static void set_bit(uint64_t *bits, uint64_t index)
set the index position bit in the array(uint64_t)
Definition: bits.h:22
#define BITS_NOT_FOUND
Definition: bits.h:7
static uint64_t find_first_one_bit(uint64_t *bits, uint64_t size, uint64_t idx)
find first one bit in the array(uint64_t)
Definition: bits.h:92