Skip to content

Instantly share code, notes, and snippets.

@josharian
Created November 3, 2021 23:52
Show Gist options
  • Save josharian/42e66bf022f32da3da0a9b1bdf0a974b to your computer and use it in GitHub Desktop.
Save josharian/42e66bf022f32da3da0a9b1bdf0a974b to your computer and use it in GitHub Desktop.
rewrite of generic md5 go code to be friendlier to the register allocator
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Code generated by go run gen.go -output md5block.go; DO NOT EDIT.
package md5
import (
// "encoding/binary"
"math/bits"
"unsafe"
)
func blockGeneric(dig *digest, p []byte) {
p = p[:len(p):len(p)]
for ; len(p) >= BlockSize; p = p[BlockSize:] {
// eliminate bounds checks on p
q := (*[BlockSize / 4]uint32)(unsafe.Pointer((*[BlockSize]byte)(p)))
a := dig.s[0]
b := dig.s[1]
c := dig.s[2]
d := dig.s[3]
// round 1
a = b + bits.RotateLeft32((((c^d)&b)^d)+a+q[0x0]+0xd76aa478, 7)
d = a + bits.RotateLeft32((((b^c)&a)^c)+d+q[0x1]+0xe8c7b756, 12)
c = d + bits.RotateLeft32((((a^b)&d)^b)+c+q[0x2]+0x242070db, 17)
b = c + bits.RotateLeft32((((d^a)&c)^a)+b+q[0x3]+0xc1bdceee, 22)
a = b + bits.RotateLeft32((((c^d)&b)^d)+a+q[0x4]+0xf57c0faf, 7)
d = a + bits.RotateLeft32((((b^c)&a)^c)+d+q[0x5]+0x4787c62a, 12)
c = d + bits.RotateLeft32((((a^b)&d)^b)+c+q[0x6]+0xa8304613, 17)
b = c + bits.RotateLeft32((((d^a)&c)^a)+b+q[0x7]+0xfd469501, 22)
a = b + bits.RotateLeft32((((c^d)&b)^d)+a+q[0x8]+0x698098d8, 7)
d = a + bits.RotateLeft32((((b^c)&a)^c)+d+q[0x9]+0x8b44f7af, 12)
c = d + bits.RotateLeft32((((a^b)&d)^b)+c+q[0xa]+0xffff5bb1, 17)
b = c + bits.RotateLeft32((((d^a)&c)^a)+b+q[0xb]+0x895cd7be, 22)
a = b + bits.RotateLeft32((((c^d)&b)^d)+a+q[0xc]+0x6b901122, 7)
d = a + bits.RotateLeft32((((b^c)&a)^c)+d+q[0xd]+0xfd987193, 12)
c = d + bits.RotateLeft32((((a^b)&d)^b)+c+q[0xe]+0xa679438e, 17)
b = c + bits.RotateLeft32((((d^a)&c)^a)+b+q[0xf]+0x49b40821, 22)
// round 2
a = b + bits.RotateLeft32(((b&d)|(c&^d))+a+q[0x1]+0xf61e2562, 5)
d = a + bits.RotateLeft32(((a&c)|(b&^c))+d+q[0x6]+0xc040b340, 9)
c = d + bits.RotateLeft32(((d&b)|(a&^b))+c+q[0xb]+0x265e5a51, 14)
b = c + bits.RotateLeft32(((c&a)|(d&^a))+b+q[0x0]+0xe9b6c7aa, 20)
a = b + bits.RotateLeft32(((b&d)|(c&^d))+a+q[0x5]+0xd62f105d, 5)
d = a + bits.RotateLeft32(((a&c)|(b&^c))+d+q[0xa]+0x02441453, 9)
c = d + bits.RotateLeft32(((d&b)|(a&^b))+c+q[0xf]+0xd8a1e681, 14)
b = c + bits.RotateLeft32(((c&a)|(d&^a))+b+q[0x4]+0xe7d3fbc8, 20)
a = b + bits.RotateLeft32(((b&d)|(c&^d))+a+q[0x9]+0x21e1cde6, 5)
d = a + bits.RotateLeft32(((a&c)|(b&^c))+d+q[0xe]+0xc33707d6, 9)
c = d + bits.RotateLeft32(((d&b)|(a&^b))+c+q[0x3]+0xf4d50d87, 14)
b = c + bits.RotateLeft32(((c&a)|(d&^a))+b+q[0x8]+0x455a14ed, 20)
a = b + bits.RotateLeft32(((b&d)|(c&^d))+a+q[0xd]+0xa9e3e905, 5)
d = a + bits.RotateLeft32(((a&c)|(b&^c))+d+q[0x2]+0xfcefa3f8, 9)
c = d + bits.RotateLeft32(((d&b)|(a&^b))+c+q[0x7]+0x676f02d9, 14)
b = c + bits.RotateLeft32(((c&a)|(d&^a))+b+q[0xc]+0x8d2a4c8a, 20)
// round 3
a = b + bits.RotateLeft32((b^c^d)+a+q[0x5]+0xfffa3942, 4)
d = a + bits.RotateLeft32((a^b^c)+d+q[0x8]+0x8771f681, 11)
c = d + bits.RotateLeft32((d^a^b)+c+q[0xb]+0x6d9d6122, 16)
b = c + bits.RotateLeft32((c^d^a)+b+q[0xe]+0xfde5380c, 23)
a = b + bits.RotateLeft32((b^c^d)+a+q[0x1]+0xa4beea44, 4)
d = a + bits.RotateLeft32((a^b^c)+d+q[0x4]+0x4bdecfa9, 11)
c = d + bits.RotateLeft32((d^a^b)+c+q[0x7]+0xf6bb4b60, 16)
b = c + bits.RotateLeft32((c^d^a)+b+q[0xa]+0xbebfbc70, 23)
a = b + bits.RotateLeft32((b^c^d)+a+q[0xd]+0x289b7ec6, 4)
d = a + bits.RotateLeft32((a^b^c)+d+q[0x0]+0xeaa127fa, 11)
c = d + bits.RotateLeft32((d^a^b)+c+q[0x3]+0xd4ef3085, 16)
b = c + bits.RotateLeft32((c^d^a)+b+q[0x6]+0x04881d05, 23)
a = b + bits.RotateLeft32((b^c^d)+a+q[0x9]+0xd9d4d039, 4)
d = a + bits.RotateLeft32((a^b^c)+d+q[0xc]+0xe6db99e5, 11)
c = d + bits.RotateLeft32((d^a^b)+c+q[0xf]+0x1fa27cf8, 16)
b = c + bits.RotateLeft32((c^d^a)+b+q[0x2]+0xc4ac5665, 23)
// round 4
a = b + bits.RotateLeft32((c^(b|^d))+a+q[0x0]+0xf4292244, 6)
d = a + bits.RotateLeft32((b^(a|^c))+d+q[0x7]+0x432aff97, 10)
c = d + bits.RotateLeft32((a^(d|^b))+c+q[0xe]+0xab9423a7, 15)
b = c + bits.RotateLeft32((d^(c|^a))+b+q[0x5]+0xfc93a039, 21)
a = b + bits.RotateLeft32((c^(b|^d))+a+q[0xc]+0x655b59c3, 6)
d = a + bits.RotateLeft32((b^(a|^c))+d+q[0x3]+0x8f0ccc92, 10)
c = d + bits.RotateLeft32((a^(d|^b))+c+q[0xa]+0xffeff47d, 15)
b = c + bits.RotateLeft32((d^(c|^a))+b+q[0x1]+0x85845dd1, 21)
a = b + bits.RotateLeft32((c^(b|^d))+a+q[0x8]+0x6fa87e4f, 6)
d = a + bits.RotateLeft32((b^(a|^c))+d+q[0xf]+0xfe2ce6e0, 10)
c = d + bits.RotateLeft32((a^(d|^b))+c+q[0x6]+0xa3014314, 15)
b = c + bits.RotateLeft32((d^(c|^a))+b+q[0xd]+0x4e0811a1, 21)
a = b + bits.RotateLeft32((c^(b|^d))+a+q[0x4]+0xf7537e82, 6)
d = a + bits.RotateLeft32((b^(a|^c))+d+q[0xb]+0xbd3af235, 10)
c = d + bits.RotateLeft32((a^(d|^b))+c+q[0x2]+0x2ad7d2bb, 15)
b = c + bits.RotateLeft32((d^(c|^a))+b+q[0x9]+0xeb86d391, 21)
// add saved state
a += dig.s[0]
b += dig.s[1]
c += dig.s[2]
d += dig.s[3]
dig.s[0] = a
dig.s[1] = b
dig.s[2] = c
dig.s[3] = d
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment