Imported Upstream version 0.12.7
[bowtie.git] / bitpack.h
1 #ifndef BITPACK_H_
2 #define BITPACK_H_
3
4 #include <stdint.h>
5 #include "assert_helpers.h"
6
7 /**
8  * Routines for marshalling 2-bit values into and out of 8-bit or
9  * 32-bit hosts
10  */
11
12 static inline void pack_2b_in_8b(const int two, uint8_t& eight, const int off) {
13         assert_lt(two, 4);
14         assert_lt(off, 4);
15         eight |= (two << (off*2));
16 }
17
18 static inline int unpack_2b_from_8b(const uint8_t eight, const int off) {
19         assert_lt(off, 4);
20         return ((eight >> (off*2)) & 0x3);
21 }
22
23 static inline void pack_2b_in_32b(const int two, uint32_t& thirty2, const int off) {
24         assert_lt(two, 4);
25         assert_lt(off, 16);
26         thirty2 |= (two << (off*2));
27 }
28
29 static inline int unpack_2b_from_32b(const uint32_t thirty2, const int off) {
30         assert_lt(off, 16);
31         return ((thirty2 >> (off*2)) & 0x3);
32 }
33
34 #endif /*BITPACK_H_*/