cmd/internal/obj/riscv: add tests for BGE/BGEU/BLT/BLTU

Add tests for BGE/BGEU/BLT/BLTU branch instructions. Also add pure Go variants
of these to ensure that the test data, Go and assembly all match up.

Change-Id: I84c68605e116a4e57f6c5c765bf0aaecab84b675
Reviewed-on: https://go-review.googlesource.com/c/go/+/271913
Trust: Joel Sing <joel@sing.id.au>
Reviewed-by: Quey-Liang Kao <s101062801@m101.nthu.edu.tw>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
This commit is contained in:
Joel Sing 2020-11-22 11:52:55 +11:00
parent a36ba090fd
commit f5978a0958
2 changed files with 97 additions and 1 deletions

View file

@ -11,6 +11,8 @@ import (
)
func testBEQZ(a int64) (r bool)
func testBGE(a, b int64) (r bool)
func testBGEU(a, b int64) (r bool)
func testBGEZ(a int64) (r bool)
func testBGT(a, b int64) (r bool)
func testBGTU(a, b int64) (r bool)
@ -18,6 +20,8 @@ func testBGTZ(a int64) (r bool)
func testBLE(a, b int64) (r bool)
func testBLEU(a, b int64) (r bool)
func testBLEZ(a int64) (r bool)
func testBLT(a, b int64) (r bool)
func testBLTU(a, b int64) (r bool)
func testBLTZ(a int64) (r bool)
func testBNEZ(a int64) (r bool)
@ -29,6 +33,16 @@ func TestBranchCondition(t *testing.T) {
fn func(a, b int64) bool
want bool
}{
{"BGE", 0, 1, testBGE, false},
{"BGE", 0, 0, testBGE, true},
{"BGE", 0, -1, testBGE, true},
{"BGE", -1, 0, testBGE, false},
{"BGE", 1, 0, testBGE, true},
{"BGEU", 0, 1, testBGEU, false},
{"BGEU", 0, 0, testBGEU, true},
{"BGEU", 0, -1, testBGEU, false},
{"BGEU", -1, 0, testBGEU, true},
{"BGEU", 1, 0, testBGEU, true},
{"BGT", 0, 1, testBGT, true},
{"BGT", 0, 0, testBGT, false},
{"BGT", 0, -1, testBGT, false},
@ -48,11 +62,49 @@ func TestBranchCondition(t *testing.T) {
{"BLEU", 0, 0, testBLEU, true},
{"BLEU", -1, 0, testBLEU, true},
{"BLEU", 1, 0, testBLEU, true},
{"BLT", 0, 1, testBLT, true},
{"BLT", 0, -1, testBLT, false},
{"BLT", 0, 0, testBLT, false},
{"BLT", -1, 0, testBLT, true},
{"BLT", 1, 0, testBLT, false},
{"BLTU", 0, 1, testBLTU, true},
{"BLTU", 0, -1, testBLTU, true},
{"BLTU", 0, 0, testBLTU, false},
{"BLTU", -1, 0, testBLTU, false},
{"BLTU", 1, 0, testBLTU, false},
}
for _, test := range tests {
t.Run(test.ins, func(t *testing.T) {
var fn func(a, b int64) bool
switch test.ins {
case "BGE":
fn = func(a, b int64) bool { return a >= b }
case "BGEU":
fn = func(a, b int64) bool { return uint64(a) >= uint64(b) }
case "BGT":
// TODO: Currently reversed.
fn = func(a, b int64) bool { return b > a }
case "BGTU":
// TODO: Currently reversed.
fn = func(a, b int64) bool { return uint64(b) > uint64(a) }
case "BLE":
// TODO: Currently reversed.
fn = func(a, b int64) bool { return b <= a }
case "BLEU":
// TODO: Currently reversed.
fn = func(a, b int64) bool { return uint64(b) <= uint64(a) }
case "BLT":
fn = func(a, b int64) bool { return a < b }
case "BLTU":
fn = func(a, b int64) bool { return uint64(a) < uint64(b) }
default:
t.Fatalf("Unknown instruction %q", test.ins)
}
if got := fn(test.a, test.b); got != test.want {
t.Errorf("Go %v %v, %v = %v, want %v", test.ins, test.a, test.b, got, test.want)
}
if got := test.fn(test.a, test.b); got != test.want {
t.Errorf("%v %v, %v = %v, want %v", test.ins, test.a, test.b, got, test.want)
t.Errorf("Assembly %v %v, %v = %v, want %v", test.ins, test.a, test.b, got, test.want)
}
})
}

View file

@ -16,6 +16,28 @@ b:
MOV X6, r+8(FP)
RET
// func testBGE(a, b int64) (r bool)
TEXT ·testBGE(SB),NOSPLIT,$0-0
MOV a+0(FP), X5
MOV b+8(FP), X6
MOV $1, X7
BGE X5, X6, b
MOV $0, X7
b:
MOV X7, r+16(FP)
RET
// func testBGEU(a, b int64) (r bool)
TEXT ·testBGEU(SB),NOSPLIT,$0-0
MOV a+0(FP), X5
MOV b+8(FP), X6
MOV $1, X7
BGEU X5, X6, b
MOV $0, X7
b:
MOV X7, r+16(FP)
RET
// func testBGEZ(a int64) (r bool)
TEXT ·testBGEZ(SB),NOSPLIT,$0-0
MOV a+0(FP), X5
@ -90,6 +112,28 @@ b:
MOV X6, r+8(FP)
RET
// func testBLT(a, b int64) (r bool)
TEXT ·testBLT(SB),NOSPLIT,$0-0
MOV a+0(FP), X5
MOV b+8(FP), X6
MOV $1, X7
BLT X5, X6, b
MOV $0, X7
b:
MOV X7, r+16(FP)
RET
// func testBLTU(a, b int64) (r bool)
TEXT ·testBLTU(SB),NOSPLIT,$0-0
MOV a+0(FP), X5
MOV b+8(FP), X6
MOV $1, X7
BLTU X5, X6, b
MOV $0, X7
b:
MOV X7, r+16(FP)
RET
// func testBLTZ(a int64) (r bool)
TEXT ·testBLTZ(SB),NOSPLIT,$0-0
MOV a+0(FP), X5