blob: 806323504f7edbc2351aeb181d484c1675289afb (
plain) (
blame)
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
|
#pragma once
static inline int sext(unsigned val, int bits) {
return val & (1 << (bits - 1)) ? ((int)val - (1 << bits)) : (int)val;
}
struct bitslice_run {
int inpos, outpos, len;
};
struct bitslice {
int nruns;
const struct bitslice_run *runs;
};
__attribute__((always_inline))
static inline unsigned bs_get(struct bitslice bs, unsigned op) {
unsigned ret = 0;
for(int i = 0; i < bs.nruns; i++) {
const struct bitslice_run *run = &bs.runs[i];
unsigned masked = op & ((1 << run->len) - 1);
if (run->outpos < run->inpos)
masked >>= run->inpos - run->outpos;
else if (run->outpos > run->inpos)
masked <<= run->outpos - run->inpos;
ret |= masked;
}
return ret;
}
__attribute__((always_inline))
static inline unsigned bs_set(struct bitslice bs, unsigned val, unsigned op) {
for(int i = 0; i < bs.nruns; i++) {
const struct bitslice_run *run = &bs.runs[i];
unsigned mask = (1 << run->len) - 1;
unsigned masked = val & mask;
if (run->outpos < run->inpos) {
masked <<= run->inpos - run->outpos;
mask <<= run->inpos - run->outpos;
} else if (run->outpos > run->inpos) {
masked >>= run->outpos - run->inpos;
mask >>= run->outpos - run->inpos;
}
op = (op & ~mask) | masked;
}
return op;
}
|