Skip to content

Commit a15d036

Browse files
committed
cmd/internal/obj/riscv: implement better bit pattern encoding
Replace the extractBitAndShift function with an encodeBitPattern function. This allows the caller to specify a slice of bits that are to be extracted and encoded, rather than making multiple function calls and combining the results. Change-Id: I3d51caa10ecf714f2ad2fb66d38376202c4e0628 Reviewed-on: https://go-review.googlesource.com/c/go/+/702397 Reviewed-by: Junyang Shao <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Mark Ryan <[email protected]> Reviewed-by: Meng Zhuo <[email protected]> Reviewed-by: Cherry Mui <[email protected]>
1 parent abb241a commit a15d036

File tree

1 file changed

+12
-23
lines changed
  • src/cmd/internal/obj/riscv

1 file changed

+12
-23
lines changed

src/cmd/internal/obj/riscv/obj.go

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,10 +1422,16 @@ func validateRaw(ctxt *obj.Link, ins *instruction) {
14221422
wantImmU(ctxt, ins, ins.imm, 32)
14231423
}
14241424

1425-
// extractBitAndShift extracts the specified bit from the given immediate,
1426-
// before shifting it to the requested position and returning it.
1427-
func extractBitAndShift(imm uint32, bit, pos int) uint32 {
1428-
return ((imm >> bit) & 1) << pos
1425+
// encodeBitPattern encodes an immediate value by extracting the specified
1426+
// bit pattern from the given immediate. Each value in the pattern specifies
1427+
// the position of the bit to extract from the immediate, which are then
1428+
// encoded in sequence.
1429+
func encodeBitPattern(imm uint32, pattern []int) uint32 {
1430+
outImm := uint32(0)
1431+
for _, bit := range pattern {
1432+
outImm = outImm<<1 | (imm>>bit)&1
1433+
}
1434+
return outImm
14291435
}
14301436

14311437
// encodeR encodes an R-type RISC-V instruction.
@@ -1650,31 +1656,14 @@ func encodeJ(ins *instruction) uint32 {
16501656
// encodeCBImmediate encodes an immediate for a CB-type RISC-V instruction.
16511657
func encodeCBImmediate(imm uint32) uint32 {
16521658
// Bit order - [8|4:3|7:6|2:1|5]
1653-
bits := extractBitAndShift(imm, 8, 7)
1654-
bits |= extractBitAndShift(imm, 4, 6)
1655-
bits |= extractBitAndShift(imm, 3, 5)
1656-
bits |= extractBitAndShift(imm, 7, 4)
1657-
bits |= extractBitAndShift(imm, 6, 3)
1658-
bits |= extractBitAndShift(imm, 2, 2)
1659-
bits |= extractBitAndShift(imm, 1, 1)
1660-
bits |= extractBitAndShift(imm, 5, 0)
1659+
bits := encodeBitPattern(imm, []int{8, 4, 3, 7, 6, 2, 1, 5})
16611660
return (bits>>5)<<10 | (bits&0x1f)<<2
16621661
}
16631662

16641663
// encodeCJImmediate encodes an immediate for a CJ-type RISC-V instruction.
16651664
func encodeCJImmediate(imm uint32) uint32 {
16661665
// Bit order - [11|4|9:8|10|6|7|3:1|5]
1667-
bits := extractBitAndShift(imm, 11, 10)
1668-
bits |= extractBitAndShift(imm, 4, 9)
1669-
bits |= extractBitAndShift(imm, 9, 8)
1670-
bits |= extractBitAndShift(imm, 8, 7)
1671-
bits |= extractBitAndShift(imm, 10, 6)
1672-
bits |= extractBitAndShift(imm, 6, 5)
1673-
bits |= extractBitAndShift(imm, 7, 4)
1674-
bits |= extractBitAndShift(imm, 3, 3)
1675-
bits |= extractBitAndShift(imm, 2, 2)
1676-
bits |= extractBitAndShift(imm, 1, 1)
1677-
bits |= extractBitAndShift(imm, 5, 0)
1666+
bits := encodeBitPattern(imm, []int{11, 4, 9, 8, 10, 6, 7, 3, 2, 1, 5})
16781667
return bits << 2
16791668
}
16801669

0 commit comments

Comments
 (0)