aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/internal/obj/riscv/testdata/testbranch/branch_test.go
blob: 3fa95222ffd33fc3d0217a67e358dca7dce55597 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build riscv64

package testbranch

import (
	"testing"
)

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)
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)

func testGoBGE(a, b int64) bool { return a >= b }
func testGoBGEU(a, b int64) bool { return uint64(a) >= uint64(b) }
func testGoBGT(a, b int64) bool { return a > b }
func testGoBGTU(a, b int64) bool { return uint64(a) > uint64(b) }
func testGoBLE(a, b int64) bool { return a <= b }
func testGoBLEU(a, b int64) bool { return uint64(a) <= uint64(b) }
func testGoBLT(a, b int64) bool { return a < b }
func testGoBLTZ(a, b int64) bool { return uint64(a) < uint64(b) }

func TestBranchCondition(t *testing.T) {
	tests := []struct {
		ins  string
		a    int64
		b    int64
		fn   func(a, b int64) bool
		goFn func(a, b int64) bool
		want bool
	}{
		{"BGE", 0, 1, testBGE, testGoBGE, false},
		{"BGE", 0, 0, testBGE, testGoBGE, true},
		{"BGE", 0, -1, testBGE, testGoBGE, true},
		{"BGE", -1, 0, testBGE, testGoBGE, false},
		{"BGE", 1, 0, testBGE, testGoBGE, true},
		{"BGEU", 0, 1, testBGEU, testGoBGEU, false},
		{"BGEU", 0, 0, testBGEU, testGoBGEU, true},
		{"BGEU", 0, -1, testBGEU, testGoBGEU, false},
		{"BGEU", -1, 0, testBGEU, testGoBGEU, true},
		{"BGEU", 1, 0, testBGEU, testGoBGEU, true},
		{"BGT", 0, 1, testBGT, testGoBGT, false},
		{"BGT", 0, 0, testBGT, testGoBGT, false},
		{"BGT", 0, -1, testBGT, testGoBGT, true},
		{"BGT", -1, 0, testBGT, testGoBGT, false},
		{"BGT", 1, 0, testBGT, testGoBGT, true},
		{"BGTU", 0, 1, testBGTU, testGoBGTU, false},
		{"BGTU", 0, 0, testBGTU, testGoBGTU, false},
		{"BGTU", 0, -1, testBGTU, testGoBGTU, false},
		{"BGTU", -1, 0, testBGTU, testGoBGTU, true},
		{"BGTU", 1, 0, testBGTU, testGoBGTU, true},
		{"BLE", 0, 1, testBLE, testGoBLE, true},
		{"BLE", 0, 0, testBLE, testGoBLE, true},
		{"BLE", 0, -1, testBLE, testGoBLE, false},
		{"BLE", -1, 0, testBLE, testGoBLE, true},
		{"BLE", 1, 0, testBLE, testGoBLE, false},
		{"BLEU", 0, 1, testBLEU, testGoBLEU, true},
		{"BLEU", 0, 0, testBLEU, testGoBLEU, true},
		{"BLEU", 0, -1, testBLEU, testGoBLEU, true},
		{"BLEU", -1, 0, testBLEU, testGoBLEU, false},
		{"BLEU", 1, 0, testBLEU, testGoBLEU, false},
		{"BLT", 0, 1, testBLT, testGoBLT, true},
		{"BLT", 0, 0, testBLT, testGoBLT, false},
		{"BLT", 0, -1, testBLT, testGoBLT, false},
		{"BLT", -1, 0, testBLT, testGoBLT, true},
		{"BLT", 1, 0, testBLT, testGoBLT, false},
		{"BLTU", 0, 1, testBLTU, testGoBLTU, true},
		{"BLTU", 0, 0, testBLTU, testGoBLTU, false},
		{"BLTU", 0, -1, testBLTU, testGoBLTU, true},
		{"BLTU", -1, 0, testBLTU, testGoBLTU, false},
		{"BLTU", 1, 0, testBLTU, testGoBLTU, false},
	}
	for _, test := range tests {
		t.Run(test.ins, func(t *testing.T) {
			if got := test.fn(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)
			}
			if got := test.goFn(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)
			}
		})
	}
}

func TestBranchZero(t *testing.T) {
	tests := []struct {
		ins  string
		a    int64
		fn   func(a int64) bool
		want bool
	}{
		{"BEQZ", -1, testBEQZ, false},
		{"BEQZ", 0, testBEQZ, true},
		{"BEQZ", 1, testBEQZ, false},
		{"BGEZ", -1, testBGEZ, false},
		{"BGEZ", 0, testBGEZ, true},
		{"BGEZ", 1, testBGEZ, true},
		{"BGTZ", -1, testBGTZ, false},
		{"BGTZ", 0, testBGTZ, false},
		{"BGTZ", 1, testBGTZ, true},
		{"BLEZ", -1, testBLEZ, true},
		{"BLEZ", 0, testBLEZ, true},
		{"BLEZ", 1, testBLEZ, false},
		{"BLTZ", -1, testBLTZ, true},
		{"BLTZ", 0, testBLTZ, false},
		{"BLTZ", 1, testBLTZ, false},
		{"BNEZ", -1, testBNEZ, true},
		{"BNEZ", 0, testBNEZ, false},
		{"BNEZ", 1, testBNEZ, true},
	}
	for _, test := range tests {
		t.Run(test.ins, func(t *testing.T) {
			if got := test.fn(test.a); got != test.want {
				t.Errorf("%v %v = %v, want %v", test.ins, test.a, got, test.want)
			}
		})
	}
}