Skip to content

Commit 0ff6e5f

Browse files
committed
crypto/cipher: add VSX implementation of xorBytes for ppc64x
This change adds asm implementations of xorBytes for ppc64x that takes advantage of VSX registers and instructions. name old time/op new time/op delta XORBytes/8Bytes-8 16.4ns ± 0% 11.1ns ± 0% -32.32% (p=0.000 n=5+4) XORBytes/128Bytes-8 45.6ns ± 0% 16.2ns ± 0% -64.50% (p=0.008 n=5+5) XORBytes/2048Bytes-8 433ns ±13% 129ns ± 1% -70.29% (p=0.000 n=5+4) XORBytes/32768Bytes-8 7.16µs ± 0% 1.83µs ± 0% -74.39% (p=0.008 n=5+5) name old speed new speed delta XORBytes/8Bytes-8 488MB/s ± 0% 721MB/s ± 0% +47.75% (p=0.016 n=5+4) XORBytes/128Bytes-8 2.80GB/s ± 0% 7.89GB/s ± 0% +181.33% (p=0.008 n=5+5) XORBytes/2048Bytes-8 4.77GB/s ±13% 15.87GB/s ± 0% +232.68% (p=0.016 n=5+4) XORBytes/32768Bytes-8 4.58GB/s ± 0% 17.88GB/s ± 0% +290.47% (p=0.008 n=5+5) Change-Id: Ic27d9b858f8ec2d597fdabc68a288d6844eba701 Reviewed-on: https://go-review.googlesource.com/c/145997 Run-TryBot: Carlos Eduardo Seo <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Lynn Boger <[email protected]>
1 parent f5b6950 commit 0ff6e5f

3 files changed

Lines changed: 96 additions & 1 deletion

File tree

src/crypto/cipher/xor_generic.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
// +build !amd64
5+
// +build !amd64,!ppc64,!ppc64le
66

77
package cipher
88

src/crypto/cipher/xor_ppc64x.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2018 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+
// +build ppc64 ppc64le
6+
7+
package cipher
8+
9+
// xorBytes xors the bytes in a and b. The destination should have enough
10+
// space, otherwise xorBytes will panic. Returns the number of bytes xor'd.
11+
func xorBytes(dst, a, b []byte) int {
12+
n := len(a)
13+
if len(b) < n {
14+
n = len(b)
15+
}
16+
if n == 0 {
17+
return 0
18+
}
19+
_ = dst[n-1]
20+
xorBytesVSX(&dst[0], &a[0], &b[0], n)
21+
return n
22+
}
23+
24+
func xorWords(dst, a, b []byte) {
25+
xorBytes(dst, a, b)
26+
}
27+
28+
//go:noescape
29+
func xorBytesVSX(dst, a, b *byte, n int)

src/crypto/cipher/xor_ppc64x.s

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright 2018 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+
// +build ppc64 ppc64le
6+
7+
#include "textflag.h"
8+
9+
// func xorBytesVSX(dst, a, b *byte, n int)
10+
TEXT ·xorBytesVSX(SB), NOSPLIT, $0
11+
MOVD dst+0(FP), R3 // R3 = dst
12+
MOVD a+8(FP), R4 // R4 = a
13+
MOVD b+16(FP), R5 // R5 = b
14+
MOVD n+24(FP), R6 // R6 = n
15+
16+
CMPU R6, $16, CR7 // Check if n ≥ 16 bytes
17+
MOVD R0, R8 // R8 = index
18+
CMPU R6, $8, CR6 // Check if 8 ≤ n < 16 bytes
19+
BGE CR7, preloop16
20+
BLT CR6, small
21+
22+
// Case for 8 ≤ n < 16 bytes
23+
MOVD (R4)(R8), R14 // R14 = a[i,...,i+7]
24+
MOVD (R5)(R8), R15 // R15 = b[i,...,i+7]
25+
XOR R14, R15, R16 // R16 = a[] ^ b[]
26+
SUB $8, R6 // n = n - 8
27+
MOVD R16, (R3)(R8) // Store to dst
28+
ADD $8, R8
29+
30+
// Check if we're finished
31+
CMP R6, R0
32+
BGT small
33+
JMP done
34+
35+
// Case for n ≥ 16 bytes
36+
preloop16:
37+
SRD $4, R6, R7 // Setup loop counter
38+
MOVD R7, CTR
39+
ANDCC $15, R6, R9 // Check for tailing bytes for later
40+
loop16:
41+
LXVD2X (R4)(R8), VS32 // VS32 = a[i,...,i+15]
42+
LXVD2X (R5)(R8), VS33 // VS33 = b[i,...,i+15]
43+
XXLXOR VS32, VS33, VS34 // VS34 = a[] ^ b[]
44+
STXVD2X VS34, (R3)(R8) // Store to dst
45+
ADD $16, R8 // Update index
46+
BC 16, 0, loop16 // bdnz loop16
47+
48+
BEQ CR0, done
49+
SLD $4, R7
50+
SUB R7, R6 // R6 = n - (R7 * 16)
51+
52+
// Case for n < 8 bytes and tailing bytes from the
53+
// previous cases.
54+
small:
55+
MOVD R6, CTR // Setup loop counter
56+
57+
loop:
58+
MOVBZ (R4)(R8), R14 // R14 = a[i]
59+
MOVBZ (R5)(R8), R15 // R15 = b[i]
60+
XOR R14, R15, R16 // R16 = a[i] ^ b[i]
61+
MOVB R16, (R3)(R8) // Store to dst
62+
ADD $1, R8
63+
BC 16, 0, loop // bdnz loop
64+
65+
done:
66+
RET

0 commit comments

Comments
 (0)