diff --git a/src/bytes/compare_test.go b/src/bytes/compare_test.go index a595d575d07..a0150abd992 100644 --- a/src/bytes/compare_test.go +++ b/src/bytes/compare_test.go @@ -6,6 +6,7 @@ package bytes_test import ( . "bytes" + "fmt" "internal/testenv" "testing" ) @@ -213,22 +214,30 @@ func BenchmarkCompareBytesDifferentLength(b *testing.B) { } } -func BenchmarkCompareBytesBigUnaligned(b *testing.B) { +func benchmarkCompareBytesBigUnaligned(b *testing.B, offset int) { b.StopTimer() b1 := make([]byte, 0, 1<<20) for len(b1) < 1<<20 { b1 = append(b1, "Hello Gophers!"...) } - b2 := append([]byte("hello"), b1...) + b2 := append([]byte("12345678")[:offset], b1...) b.StartTimer() - for i := 0; i < b.N; i++ { - if Compare(b1, b2[len("hello"):]) != 0 { + for j := 0; j < b.N; j++ { + if Compare(b1, b2[offset:]) != 0 { b.Fatal("b1 != b2") } } b.SetBytes(int64(len(b1))) } +func BenchmarkCompareBytesBigUnaligned(b *testing.B) { + for i := 1; i < 8; i++ { + b.Run(fmt.Sprintf("offset=%d", i), func(b *testing.B) { + benchmarkCompareBytesBigUnaligned(b, i) + }) + } +} + func BenchmarkCompareBytesBig(b *testing.B) { b.StopTimer() b1 := make([]byte, 0, 1<<20) diff --git a/src/internal/bytealg/compare_riscv64.s b/src/internal/bytealg/compare_riscv64.s index 7d2f8d6d0b8..e616577d534 100644 --- a/src/internal/bytealg/compare_riscv64.s +++ b/src/internal/bytealg/compare_riscv64.s @@ -58,8 +58,8 @@ use_a_len: BLT X5, X6, loop4_check // Check alignment - if alignment differs we have to do one byte at a time. - AND $3, X10, X7 - AND $3, X12, X8 + AND $7, X10, X7 + AND $7, X12, X8 BNE X7, X8, loop4_check BEQZ X7, loop32_check diff --git a/src/internal/bytealg/equal_riscv64.s b/src/internal/bytealg/equal_riscv64.s index 77202d60759..1e070beb3e4 100644 --- a/src/internal/bytealg/equal_riscv64.s +++ b/src/internal/bytealg/equal_riscv64.s @@ -42,8 +42,8 @@ TEXT memequal<>(SB),NOSPLIT|NOFRAME,$0 BLT X12, X23, loop4_check // Check alignment - if alignment differs we have to do one byte at a time. - AND $3, X10, X9 - AND $3, X11, X19 + AND $7, X10, X9 + AND $7, X11, X19 BNE X9, X19, loop4_check BEQZ X9, loop32_check