1
2
3
4
5 package s390xasm
6
7 import (
8 "fmt"
9 )
10
11
12
13 type BitField struct {
14 Offs uint8
15 Bits uint8
16 }
17
18 func (b BitField) String() string {
19 if b.Bits > 1 {
20 return fmt.Sprintf("[%d:%d]", b.Offs, int(b.Offs+b.Bits)-1)
21 } else if b.Bits == 1 {
22 return fmt.Sprintf("[%d]", b.Offs)
23 } else {
24 return fmt.Sprintf("[%d, len=0]", b.Offs)
25 }
26 }
27
28
29
30 func (b BitField) Parse(i uint64) uint64 {
31 if b.Bits > 64 || b.Bits == 0 || b.Offs > 63 || b.Offs+b.Bits > 64 {
32 panic(fmt.Sprintf("invalid bitfiled %v", b))
33 }
34 if b.Bits == 20 {
35 return ((((i >> (64 - b.Offs - b.Bits)) & ((1 << 8) - 1)) << 12) | ((i >> (64 - b.Offs - b.Bits + 8)) & 0xFFF))
36
37 } else {
38 return (i >> (64 - b.Offs - b.Bits)) & ((1 << b.Bits) - 1)
39 }
40 }
41
42
43
44 func (b BitField) ParseSigned(i uint64) int64 {
45 u := int64(b.Parse(i))
46 return u << (64 - b.Bits) >> (64 - b.Bits)
47 }
48
View as plain text