| 1 | # md5x16 |
| 2 | |
| 3 | AVX-512 accelerated 16-lane [MD5](https://en.wikipedia.org/wiki/MD5) in `avo`. |
| 4 | |
| 5 | Inspired by [`minio/md5-simd`](https://github.com/minio/md5-simd) and |
| 6 | [`igneous-systems/md5vec`](https://github.com/igneous-systems/md5vec). |
| 7 | |
| 8 | Note that the focus of this example is the core assembly `block` function. The |
| 9 | `Sum` function can only handle parallel hashes of exactly the same length. In |
| 10 | practice you'd likely need hash server functionality provided by |
| 11 | [`md5-simd`](https://github.com/minio/md5-simd) to multiplex independent hashes |
| 12 | of different lengths into the 16 SIMD lanes. |
| 13 | |
| 14 | [embedmd]:# (asm.go /func main/ /^}/) |
| 15 | ```go |
| 16 | func main() { |
| 17 | // Define round constants data section. |
| 18 | // |
| 19 | // These may be computed as the integer part of abs(sin(i+1))*2^32. |
| 20 | T := GLOBL("consts", RODATA|NOPTR) |
| 21 | for i := 0; i < 64; i++ { |
| 22 | k := uint32(math.Floor(math.Ldexp(math.Abs(math.Sin(float64(i+1))), 32))) |
| 23 | DATA(4*i, U32(k)) |
| 24 | } |
| 25 | |
| 26 | // MD5 16-lane block function. |
| 27 | TEXT("block", 0, "func(h *[4][16]uint32, base uintptr, offsets *[16]uint32, mask uint16)") |
| 28 | Doc( |
| 29 | "block MD5 hashes 16 messages into the running hash states h. Messages are", |
| 30 | "at the given offsets from the base pointer. The 16-bit mask specifies", |
| 31 | "which lanes are active: when bit i is not set loads will be disabled and", |
| 32 | "the value of the resulting hash is undefined.", |
| 33 | ) |
| 34 | h := Mem{Base: Load(Param("h"), GP64())} |
| 35 | base := Mem{Base: Load(Param("base"), GP64())} |
| 36 | offsetsptr := Mem{Base: Load(Param("offsets"), GP64())} |
| 37 | mask := Load(Param("mask"), K()) |
| 38 | |
| 39 | Comment("Load offsets.") |
| 40 | offsets := ZMM() |
| 41 | VMOVUPD(offsetsptr, offsets) |
| 42 | |
| 43 | Comment("Load initial hash.") |
| 44 | hash := [4]Register{ZMM(), ZMM(), ZMM(), ZMM()} |
| 45 | for i, r := range hash { |
| 46 | VMOVUPD(h.Offset(64*i), r) |
| 47 | } |
| 48 | |
| 49 | Comment("Initialize registers.") |
| 50 | a, b, c, d := ZMM(), ZMM(), ZMM(), ZMM() |
| 51 | for i, r := range []Register{a, b, c, d} { |
| 52 | VMOVUPD(hash[i], r) |
| 53 | } |
| 54 | |
| 55 | // Allocate message registers. |
| 56 | m := make([]Register, 16) |
| 57 | for i := range m { |
| 58 | m[i] = ZMM() |
| 59 | } |
| 60 | |
| 61 | // Generate round updates. |
| 62 | // |
| 63 | // Each 16-round block is parameterized based on the btiwise function, |
| 64 | // message indexes and shift amounts. Constants B, C, D are helpers in |
| 65 | // computing the logic table required by VPTERNLOGD. |
| 66 | const ( |
| 67 | B = uint8(0b10101010) |
| 68 | C = uint8(0b11001100) |
| 69 | D = uint8(0b11110000) |
| 70 | ) |
| 71 | quarter := []struct { |
| 72 | F uint8 // ternary logic table |
| 73 | i func(int) int // message index at round r |
| 74 | s []int // shift amounts |
| 75 | }{ |
| 76 | { |
| 77 | F: (B & C) | (^B & D), |
| 78 | i: func(r int) int { return r % 16 }, |
| 79 | s: []int{7, 12, 17, 22}, |
| 80 | }, |
| 81 | { |
| 82 | F: (D & B) | (^D & C), |
| 83 | i: func(r int) int { return (5*r + 1) % 16 }, |
| 84 | s: []int{5, 9, 14, 20}, |
| 85 | }, |
| 86 | { |
| 87 | F: B ^ C ^ D, |
| 88 | i: func(r int) int { return (3*r + 5) % 16 }, |
| 89 | s: []int{4, 11, 16, 23}, |
| 90 | }, |
| 91 | { |
| 92 | F: C ^ (B | ^D), |
| 93 | i: func(r int) int { return (7 * r) % 16 }, |
| 94 | s: []int{6, 10, 15, 21}, |
| 95 | }, |
| 96 | } |
| 97 | |
| 98 | for r := 0; r < 64; r++ { |
| 99 | Commentf("Round %d.", r) |
| 100 | q := quarter[r/16] |
| 101 | |
| 102 | // Load message words. |
| 103 | if r < 16 { |
| 104 | k := K() |
| 105 | KMOVW(mask, k) |
| 106 | VPGATHERDD(base.Offset(4*r).Idx(offsets, 1), k, m[r]) |
| 107 | } |
| 108 | |
| 109 | VPADDD(m[q.i(r)], a, a) |
| 110 | VPADDD_BCST(T.Offset(4*r), a, a) |
| 111 | f := ZMM() |
| 112 | VMOVUPD(d, f) |
| 113 | VPTERNLOGD(U8(q.F), b, c, f) |
| 114 | VPADDD(f, a, a) |
| 115 | VPROLD(U8(q.s[r%4]), a, a) |
| 116 | VPADDD(b, a, a) |
| 117 | a, b, c, d = d, a, b, c |
| 118 | } |
| 119 | |
| 120 | Comment("Final add.") |
| 121 | for i, r := range []Register{a, b, c, d} { |
| 122 | VPADDD(r, hash[i], hash[i]) |
| 123 | } |
| 124 | |
| 125 | Comment("Store results back.") |
| 126 | for i, r := range hash { |
| 127 | VMOVUPD(r, h.Offset(64*i)) |
| 128 | } |
| 129 | |
| 130 | VZEROUPPER() |
| 131 | RET() |
| 132 | |
| 133 | Generate() |
| 134 | } |
| 135 | ``` |