| 1 |
// +build ignore |
| 2 |
|
| 3 |
package main |
| 4 |
|
| 5 |
import ( |
| 6 |
. "github.com/mmcloughlin/avo/build" |
| 7 |
. "github.com/mmcloughlin/avo/operand" |
| 8 |
. "github.com/segmentio/asm/build/internal/asm" |
| 9 |
. "github.com/segmentio/asm/build/internal/x86" |
| 10 |
|
| 11 |
"github.com/mmcloughlin/avo/reg" |
| 12 |
"github.com/segmentio/asm/cpu" |
| 13 |
) |
| 14 |
|
| 15 |
const unroll = 4 |
| 16 |
|
| 17 |
func init() { |
| 18 |
ConstraintExpr("!purego") |
| 19 |
} |
| 20 |
|
| 21 |
func main() { |
| 22 |
TEXT("swap64", NOSPLIT, "func(b []byte)") |
| 23 |
Doc("swap64 performs an in-place byte swap on each qword of the input buffer.") |
| 24 |
|
| 25 |
// Load slice ptr + length, and calculate end ptr. |
| 26 |
ptr := Load(Param("b").Base(), GP64()) |
| 27 |
len := Load(Param("b").Len(), GP64()) |
| 28 |
end := GP64() |
| 29 |
MOVQ(ptr, end) |
| 30 |
ADDQ(len, end) |
| 31 |
|
| 32 |
JumpUnlessFeature("x86_loop", cpu.AVX2) |
| 33 |
|
| 34 |
// Prepare the shuffle mask. |
| 35 |
shuffleMaskData := ConstBytes("shuffle_mask", []byte{ |
| 36 |
7, 6, 5, 4, 3, 2, 1, 0, |
| 37 |
15, 14, 13, 12, 11, 10, 9, 8, |
| 38 |
7, 6, 5, 4, 3, 2, 1, 0, |
| 39 |
15, 14, 13, 12, 11, 10, 9, 8, |
| 40 |
}) |
| 41 |
shuffleMask := YMM() |
| 42 |
VMOVDQU(shuffleMaskData, shuffleMask) |
| 43 |
|
| 44 |
// Loop while we have at least unroll*32 bytes remaining. |
| 45 |
Label("avx2_loop") |
| 46 |
next := GP64() |
| 47 |
MOVQ(ptr, next) |
| 48 |
ADDQ(Imm(unroll*32), next) |
| 49 |
CMPQ(next, end) |
| 50 |
JAE(LabelRef("x86_loop")) |
| 51 |
|
| 52 |
// Load multiple chunks => byte swap => store. |
| 53 |
var vectors [unroll]reg.VecVirtual |
| 54 |
for i := 0; i < unroll; i++ { |
| 55 |
vectors[i] = YMM() |
| 56 |
} |
| 57 |
for i := 0; i < unroll; i++ { |
| 58 |
VMOVDQU(Mem{Base: ptr}.Offset(i*32), vectors[i]) |
| 59 |
} |
| 60 |
for i := 0; i < unroll; i++ { |
| 61 |
VPSHUFB(shuffleMask, vectors[i], vectors[i]) |
| 62 |
} |
| 63 |
for i := 0; i < unroll; i++ { |
| 64 |
VMOVDQU(vectors[i], Mem{Base: ptr}.Offset(i*32)) |
| 65 |
} |
| 66 |
|
| 67 |
// Increment ptr and loop. |
| 68 |
MOVQ(next, ptr) |
| 69 |
JMP(LabelRef("avx2_loop")) |
| 70 |
|
| 71 |
// Loop while we have at least unroll*8 bytes remaining. |
| 72 |
Label("x86_loop") |
| 73 |
next = GP64() |
| 74 |
MOVQ(ptr, next) |
| 75 |
ADDQ(Imm(unroll*8), next) |
| 76 |
CMPQ(next, end) |
| 77 |
JAE(LabelRef("slow_loop")) |
| 78 |
|
| 79 |
// Load qwords => byte swap => store. |
| 80 |
var chunks [unroll]reg.GPVirtual |
| 81 |
for i := 0; i < unroll; i++ { |
| 82 |
chunks[i] = GP64() |
| 83 |
} |
| 84 |
for i := 0; i < unroll; i++ { |
| 85 |
MOVQ(Mem{Base: ptr}.Offset(i*8), chunks[i]) |
| 86 |
} |
| 87 |
for i := 0; i < unroll; i++ { |
| 88 |
BSWAPQ(chunks[i]) |
| 89 |
} |
| 90 |
for i := 0; i < unroll; i++ { |
| 91 |
MOVQ(chunks[i], Mem{Base: ptr}.Offset(i*8)) |
| 92 |
} |
| 93 |
|
| 94 |
// Increment ptr and loop. |
| 95 |
MOVQ(next, ptr) |
| 96 |
JMP(LabelRef("x86_loop")) |
| 97 |
|
| 98 |
// Loop until ptr reaches the end. |
| 99 |
Label("slow_loop") |
| 100 |
CMPQ(ptr, end) |
| 101 |
JAE(LabelRef("done")) |
| 102 |
|
| 103 |
// Load a qword => byte swap => store. |
| 104 |
qword := GP64() |
| 105 |
MOVQ(Mem{Base: ptr}, qword) |
| 106 |
BSWAPQ(qword) |
| 107 |
MOVQ(qword, Mem{Base: ptr}) |
| 108 |
|
| 109 |
// Increment ptr and loop. |
| 110 |
ADDQ(Imm(8), ptr) |
| 111 |
JMP(LabelRef("slow_loop")) |
| 112 |
|
| 113 |
Label("done") |
| 114 |
RET() |
| 115 |
Generate() |
| 116 |
} |