-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbitpack.c
More file actions
113 lines (100 loc) · 2.65 KB
/
Copy pathbitpack.c
File metadata and controls
113 lines (100 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "bitpack.h"
#include "except.h"
#define SIZE 64
Except_T Bitpack_Overflow = {"Overflow packing bits"};
static inline uint64_t shiftl(uint64_t n, unsigned len)
{
if (len >= SIZE)
return (uint64_t)0;
n <<= len;
return n;
}
static inline uint64_t shiftr(uint64_t n, unsigned len)
{
if (len >= SIZE)
return (uint64_t)0;
n >>= len;
return n;
}
static inline uint64_t sshiftr(uint64_t n, unsigned len)
{
if (len >= SIZE)
return (uint64_t)0;
int64_t output = n;
output >>= len;
return (uint64_t)output;
}
bool Bitpack_fitsu(uint64_t n, unsigned width)
{
n = shiftr(n, width);
if (n == 0)
return true;
return false;
}
bool Bitpack_fitss(int64_t n, unsigned width)
{
n = sshiftr(n, width);
if (n == 0 || n == -1)
return true;
return false;
}
inline uint64_t Bitpack_getu(uint64_t word, unsigned width, unsigned lsb)
{
if(width+lsb > SIZE)
RAISE(Bitpack_Overflow);
// if (width == 0)
// return 0;
word = shiftl(word, SIZE-width-lsb);
word = shiftr(word, SIZE-width);
return word;
}
int64_t Bitpack_gets(uint64_t word, unsigned width, unsigned lsb)
{
int64_t output = word;
if (width == 0)
return 0;
if(width+lsb > SIZE)
RAISE(Bitpack_Overflow);
output = shiftl(output, SIZE-width-lsb);
output = sshiftr(output, SIZE-width);
return output;
}
uint64_t Bitpack_news(uint64_t word,
unsigned width,
unsigned lsb,
int64_t value)
{
if (!Bitpack_fitss(value, width))
RAISE(Bitpack_Overflow);
uint64_t mask = 0;
mask = ~mask;
mask = shiftl(mask, SIZE-width);
mask = shiftr(mask, SIZE-lsb-width);
mask = ~mask;
word &= mask;
value = shiftl(value, SIZE-width);
value = shiftr(value, SIZE-lsb-width);
word |= value;
return word;
}
uint64_t Bitpack_newu(uint64_t word,
unsigned width,
unsigned lsb,
uint64_t value)
{
if (!Bitpack_fitsu(value, width))
RAISE(Bitpack_Overflow);
uint64_t mask = 0;
mask = ~mask;
mask = shiftl(mask, SIZE-width);
mask = shiftr(mask, SIZE-lsb-width);
mask = ~mask;
word &= mask;
value = shiftl(value, lsb);
word |= value;
return word;
}
#undef SIZE