diff options
author | comex | 2015-01-10 22:44:55 -0500 |
---|---|---|
committer | comex | 2015-01-10 22:44:55 -0500 |
commit | 102a7371f0a19ee5569d1cd2e8761d7ab3fec75b (patch) | |
tree | 6e0f718543fde994a8f727b08a6cabfcb7122fae /lib/dis.h | |
parent | add licensing (diff) | |
download | substitute-102a7371f0a19ee5569d1cd2e8761d7ab3fec75b.tar.gz |
...
Diffstat (limited to '')
-rw-r--r-- | lib/dis.h | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/lib/dis.h b/lib/dis.h new file mode 100644 index 0000000..8063235 --- /dev/null +++ b/lib/dis.h @@ -0,0 +1,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; +} |