Debian Codegraph @

s390x/s390xasm/field.go

raw symbols SCIP
1 // Copyright 2024 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package s390xasm
6
7 import (
8 "fmt"
9 )
10
11 // A BitField is a bit-field in a 64-bit double word.
12 // Bits are counted from 0 from the MSB to 63 as the LSB.
13 type BitField struct {
14 Offs uint8 // the offset of the left-most bit.
15 Bits uint8 // length in bits.
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 // Parse extracts the bitfield b from i, and return it as an unsigned integer.
29 // Parse will panic if b is invalid.
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 // ParseSigned extracts the bitfield b from i, and return it as a signed integer.
43 // ParseSigned will panic if b is invalid.
44 func (b BitField) ParseSigned(i uint64) int64 {
45 u := int64(b.Parse(i))
46 return u << (64 - b.Bits) >> (64 - b.Bits)
47 }