| 1 |
|
| 2 |
|
| 3 |
package main |
| 4 |
|
| 5 |
import ( |
| 6 |
"math" |
| 7 |
|
| 8 |
. "github.com/mmcloughlin/avo/build" |
| 9 |
. "github.com/mmcloughlin/avo/operand" |
| 10 |
. "github.com/mmcloughlin/avo/reg" |
| 11 |
) |
| 12 |
|
| 13 |
func main() { |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
T := GLOBL("consts", RODATA|NOPTR) |
| 18 |
for i := 0; i < 64; i++ { |
| 19 |
k := uint32(math.Floor(math.Ldexp(math.Abs(math.Sin(float64(i+1))), 32))) |
| 20 |
DATA(4*i, U32(k)) |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
TEXT("block", 0, "func(h *[4][16]uint32, base uintptr, offsets *[16]uint32, mask uint16)") |
| 25 |
Doc( |
| 26 |
"block MD5 hashes 16 messages into the running hash states h. Messages are", |
| 27 |
"at the given offsets from the base pointer. The 16-bit mask specifies", |
| 28 |
"which lanes are active: when bit i is not set loads will be disabled and", |
| 29 |
"the value of the resulting hash is undefined.", |
| 30 |
) |
| 31 |
h := Mem{Base: Load(Param("h"), GP64())} |
| 32 |
base := Mem{Base: Load(Param("base"), GP64())} |
| 33 |
offsetsptr := Mem{Base: Load(Param("offsets"), GP64())} |
| 34 |
mask := Load(Param("mask"), K()) |
| 35 |
|
| 36 |
Comment("Load offsets.") |
| 37 |
offsets := ZMM() |
| 38 |
VMOVUPD(offsetsptr, offsets) |
| 39 |
|
| 40 |
Comment("Load initial hash.") |
| 41 |
hash := [4]Register{ZMM(), ZMM(), ZMM(), ZMM()} |
| 42 |
for i, r := range hash { |
| 43 |
VMOVUPD(h.Offset(64*i), r) |
| 44 |
} |
| 45 |
|
| 46 |
Comment("Initialize registers.") |
| 47 |
a, b, c, d := ZMM(), ZMM(), ZMM(), ZMM() |
| 48 |
for i, r := range []Register{a, b, c, d} { |
| 49 |
VMOVUPD(hash[i], r) |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
m := make([]Register, 16) |
| 54 |
for i := range m { |
| 55 |
m[i] = ZMM() |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
const ( |
| 64 |
B = uint8(0b10101010) |
| 65 |
C = uint8(0b11001100) |
| 66 |
D = uint8(0b11110000) |
| 67 |
) |
| 68 |
quarter := []struct { |
| 69 |
F uint8 |
| 70 |
i func(int) int |
| 71 |
s []int |
| 72 |
}{ |
| 73 |
{ |
| 74 |
F: (B & C) | (^B & D), |
| 75 |
i: func(r int) int { return r % 16 }, |
| 76 |
s: []int{7, 12, 17, 22}, |
| 77 |
}, |
| 78 |
{ |
| 79 |
F: (D & B) | (^D & C), |
| 80 |
i: func(r int) int { return (5*r + 1) % 16 }, |
| 81 |
s: []int{5, 9, 14, 20}, |
| 82 |
}, |
| 83 |
{ |
| 84 |
F: B ^ C ^ D, |
| 85 |
i: func(r int) int { return (3*r + 5) % 16 }, |
| 86 |
s: []int{4, 11, 16, 23}, |
| 87 |
}, |
| 88 |
{ |
| 89 |
F: C ^ (B | ^D), |
| 90 |
i: func(r int) int { return (7 * r) % 16 }, |
| 91 |
s: []int{6, 10, 15, 21}, |
| 92 |
}, |
| 93 |
} |
| 94 |
|
| 95 |
for r := 0; r < 64; r++ { |
| 96 |
Commentf("Round %d.", r) |
| 97 |
q := quarter[r/16] |
| 98 |
|
| 99 |
|
| 100 |
if r < 16 { |
| 101 |
k := K() |
| 102 |
KMOVW(mask, k) |
| 103 |
VPGATHERDD(base.Offset(4*r).Idx(offsets, 1), k, m[r]) |
| 104 |
} |
| 105 |
|
| 106 |
VPADDD(m[q.i(r)], a, a) |
| 107 |
VPADDD_BCST(T.Offset(4*r), a, a) |
| 108 |
f := ZMM() |
| 109 |
VMOVUPD(d, f) |
| 110 |
VPTERNLOGD(U8(q.F), b, c, f) |
| 111 |
VPADDD(f, a, a) |
| 112 |
VPROLD(U8(q.s[r%4]), a, a) |
| 113 |
VPADDD(b, a, a) |
| 114 |
a, b, c, d = d, a, b, c |
| 115 |
} |
| 116 |
|
| 117 |
Comment("Final add.") |
| 118 |
for i, r := range []Register{a, b, c, d} { |
| 119 |
VPADDD(r, hash[i], hash[i]) |
| 120 |
} |
| 121 |
|
| 122 |
Comment("Store results back.") |
| 123 |
for i, r := range hash { |
| 124 |
VMOVUPD(r, h.Offset(64*i)) |
| 125 |
} |
| 126 |
|
| 127 |
VZEROUPPER() |
| 128 |
RET() |
| 129 |
|
| 130 |
Generate() |
| 131 |
} |