github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/cmd/obj/riscv/testdata/testbranch/branch_test.go (about)

     1  // Copyright 2020 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  //go:build riscv64
     6  // +build riscv64
     7  
     8  package testbranch
     9  
    10  import (
    11  	"testing"
    12  )
    13  
    14  func testBEQZ(a int64) (r bool)
    15  func testBGE(a, b int64) (r bool)
    16  func testBGEU(a, b int64) (r bool)
    17  func testBGEZ(a int64) (r bool)
    18  func testBGT(a, b int64) (r bool)
    19  func testBGTU(a, b int64) (r bool)
    20  func testBGTZ(a int64) (r bool)
    21  func testBLE(a, b int64) (r bool)
    22  func testBLEU(a, b int64) (r bool)
    23  func testBLEZ(a int64) (r bool)
    24  func testBLT(a, b int64) (r bool)
    25  func testBLTU(a, b int64) (r bool)
    26  func testBLTZ(a int64) (r bool)
    27  func testBNEZ(a int64) (r bool)
    28  
    29  func testGoBGE(a, b int64) bool  { return a >= b }
    30  func testGoBGEU(a, b int64) bool { return uint64(a) >= uint64(b) }
    31  func testGoBGT(a, b int64) bool  { return a > b }
    32  func testGoBGTU(a, b int64) bool { return uint64(a) > uint64(b) }
    33  func testGoBLE(a, b int64) bool  { return a <= b }
    34  func testGoBLEU(a, b int64) bool { return uint64(a) <= uint64(b) }
    35  func testGoBLT(a, b int64) bool  { return a < b }
    36  func testGoBLTU(a, b int64) bool { return uint64(a) < uint64(b) }
    37  
    38  func TestBranchCondition(t *testing.T) {
    39  	tests := []struct {
    40  		ins  string
    41  		a    int64
    42  		b    int64
    43  		fn   func(a, b int64) bool
    44  		goFn func(a, b int64) bool
    45  		want bool
    46  	}{
    47  		{"BGE", 0, 1, testBGE, testGoBGE, false},
    48  		{"BGE", 0, 0, testBGE, testGoBGE, true},
    49  		{"BGE", 0, -1, testBGE, testGoBGE, true},
    50  		{"BGE", -1, 0, testBGE, testGoBGE, false},
    51  		{"BGE", 1, 0, testBGE, testGoBGE, true},
    52  		{"BGEU", 0, 1, testBGEU, testGoBGEU, false},
    53  		{"BGEU", 0, 0, testBGEU, testGoBGEU, true},
    54  		{"BGEU", 0, -1, testBGEU, testGoBGEU, false},
    55  		{"BGEU", -1, 0, testBGEU, testGoBGEU, true},
    56  		{"BGEU", 1, 0, testBGEU, testGoBGEU, true},
    57  		{"BGT", 0, 1, testBGT, testGoBGT, false},
    58  		{"BGT", 0, 0, testBGT, testGoBGT, false},
    59  		{"BGT", 0, -1, testBGT, testGoBGT, true},
    60  		{"BGT", -1, 0, testBGT, testGoBGT, false},
    61  		{"BGT", 1, 0, testBGT, testGoBGT, true},
    62  		{"BGTU", 0, 1, testBGTU, testGoBGTU, false},
    63  		{"BGTU", 0, 0, testBGTU, testGoBGTU, false},
    64  		{"BGTU", 0, -1, testBGTU, testGoBGTU, false},
    65  		{"BGTU", -1, 0, testBGTU, testGoBGTU, true},
    66  		{"BGTU", 1, 0, testBGTU, testGoBGTU, true},
    67  		{"BLE", 0, 1, testBLE, testGoBLE, true},
    68  		{"BLE", 0, 0, testBLE, testGoBLE, true},
    69  		{"BLE", 0, -1, testBLE, testGoBLE, false},
    70  		{"BLE", -1, 0, testBLE, testGoBLE, true},
    71  		{"BLE", 1, 0, testBLE, testGoBLE, false},
    72  		{"BLEU", 0, 1, testBLEU, testGoBLEU, true},
    73  		{"BLEU", 0, 0, testBLEU, testGoBLEU, true},
    74  		{"BLEU", 0, -1, testBLEU, testGoBLEU, true},
    75  		{"BLEU", -1, 0, testBLEU, testGoBLEU, false},
    76  		{"BLEU", 1, 0, testBLEU, testGoBLEU, false},
    77  		{"BLT", 0, 1, testBLT, testGoBLT, true},
    78  		{"BLT", 0, 0, testBLT, testGoBLT, false},
    79  		{"BLT", 0, -1, testBLT, testGoBLT, false},
    80  		{"BLT", -1, 0, testBLT, testGoBLT, true},
    81  		{"BLT", 1, 0, testBLT, testGoBLT, false},
    82  		{"BLTU", 0, 1, testBLTU, testGoBLTU, true},
    83  		{"BLTU", 0, 0, testBLTU, testGoBLTU, false},
    84  		{"BLTU", 0, -1, testBLTU, testGoBLTU, true},
    85  		{"BLTU", -1, 0, testBLTU, testGoBLTU, false},
    86  		{"BLTU", 1, 0, testBLTU, testGoBLTU, false},
    87  	}
    88  	for _, test := range tests {
    89  		t.Run(test.ins, func(t *testing.T) {
    90  			if got := test.fn(test.a, test.b); got != test.want {
    91  				t.Errorf("Assembly %v %v, %v = %v, want %v", test.ins, test.a, test.b, got, test.want)
    92  			}
    93  			if got := test.goFn(test.a, test.b); got != test.want {
    94  				t.Errorf("Go %v %v, %v = %v, want %v", test.ins, test.a, test.b, got, test.want)
    95  			}
    96  		})
    97  	}
    98  }
    99  
   100  func TestBranchZero(t *testing.T) {
   101  	tests := []struct {
   102  		ins  string
   103  		a    int64
   104  		fn   func(a int64) bool
   105  		want bool
   106  	}{
   107  		{"BEQZ", -1, testBEQZ, false},
   108  		{"BEQZ", 0, testBEQZ, true},
   109  		{"BEQZ", 1, testBEQZ, false},
   110  		{"BGEZ", -1, testBGEZ, false},
   111  		{"BGEZ", 0, testBGEZ, true},
   112  		{"BGEZ", 1, testBGEZ, true},
   113  		{"BGTZ", -1, testBGTZ, false},
   114  		{"BGTZ", 0, testBGTZ, false},
   115  		{"BGTZ", 1, testBGTZ, true},
   116  		{"BLEZ", -1, testBLEZ, true},
   117  		{"BLEZ", 0, testBLEZ, true},
   118  		{"BLEZ", 1, testBLEZ, false},
   119  		{"BLTZ", -1, testBLTZ, true},
   120  		{"BLTZ", 0, testBLTZ, false},
   121  		{"BLTZ", 1, testBLTZ, false},
   122  		{"BNEZ", -1, testBNEZ, true},
   123  		{"BNEZ", 0, testBNEZ, false},
   124  		{"BNEZ", 1, testBNEZ, true},
   125  	}
   126  	for _, test := range tests {
   127  		t.Run(test.ins, func(t *testing.T) {
   128  			if got := test.fn(test.a); got != test.want {
   129  				t.Errorf("%v %v = %v, want %v", test.ins, test.a, got, test.want)
   130  			}
   131  		})
   132  	}
   133  }