LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2012 The Go Authors. All rights reserved. | 1 // Copyright 2012 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // +build ignore | 5 // +build ignore |
6 | 6 |
7 // This program generates md5block.go | 7 // This program generates md5block.go |
8 // Invoke as | 8 // Invoke as |
9 // | 9 // |
10 // go run gen.go [-full] |gofmt >md5block.go | 10 // go run gen.go [-full] |gofmt >md5block.go |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 0xa3014314, | 154 0xa3014314, |
155 0x4e0811a1, | 155 0x4e0811a1, |
156 0xf7537e82, | 156 0xf7537e82, |
157 0xbd3af235, | 157 0xbd3af235, |
158 0x2ad7d2bb, | 158 0x2ad7d2bb, |
159 0xeb86d391, | 159 0xeb86d391, |
160 }, | 160 }, |
161 } | 161 } |
162 | 162 |
163 var program = ` | 163 var program = ` |
| 164 // DO NOT EDIT. |
| 165 // Generate with: go run gen.go{{if .Full}} -full{{end}} | gofmt >md5block.go |
| 166 |
| 167 // +build !amd64 |
| 168 |
164 package md5 | 169 package md5 |
165 | 170 |
166 import ( | 171 import ( |
167 "unsafe" | 172 "unsafe" |
168 "runtime" | 173 "runtime" |
169 ) | 174 ) |
170 | 175 |
171 {{if not .Full}} | 176 {{if not .Full}} |
172 var t1 = [...]uint32{ | 177 var t1 = [...]uint32{ |
173 {{range .Table1}}{{printf "\t%#x,\n" .}}{{end}} | 178 {{range .Table1}}{{printf "\t%#x,\n" .}}{{end}} |
174 } | 179 } |
175 ········ | 180 ········ |
176 var t2 = [...]uint32{ | 181 var t2 = [...]uint32{ |
177 {{range .Table2}}{{printf "\t%#x,\n" .}}{{end}} | 182 {{range .Table2}}{{printf "\t%#x,\n" .}}{{end}} |
178 } | 183 } |
179 ········ | 184 ········ |
180 var t3 = [...]uint32{ | 185 var t3 = [...]uint32{ |
181 {{range .Table3}}{{printf "\t%#x,\n" .}}{{end}} | 186 {{range .Table3}}{{printf "\t%#x,\n" .}}{{end}} |
182 } | 187 } |
183 ········ | 188 ········ |
184 var t4 = [...]uint32{ | 189 var t4 = [...]uint32{ |
185 {{range .Table4}}{{printf "\t%#x,\n" .}}{{end}} | 190 {{range .Table4}}{{printf "\t%#x,\n" .}}{{end}} |
186 } | 191 } |
187 {{end}} | 192 {{end}} |
| 193 |
| 194 const x86 = runtime.GOARCH == "amd64" || runtime.GOARCH == "386" |
| 195 |
| 196 var littleEndian bool |
| 197 |
| 198 func init() { |
| 199 x := uint32(0x04030201) |
| 200 y := [4]byte{0x1, 0x2, 0x3, 0x4} |
| 201 littleEndian = *(*[4]byte)(unsafe.Pointer(&x)) == y |
| 202 } |
188 | 203 |
189 func block(dig *digest, p []byte) { | 204 func block(dig *digest, p []byte) { |
190 a := dig.s[0] | 205 a := dig.s[0] |
191 b := dig.s[1] | 206 b := dig.s[1] |
192 c := dig.s[2] | 207 c := dig.s[2] |
193 d := dig.s[3] | 208 d := dig.s[3] |
194 var X *[16]uint32 | 209 var X *[16]uint32 |
195 var xbuf [16]uint32 | 210 var xbuf [16]uint32 |
196 for len(p) >= chunk { | 211 for len(p) >= chunk { |
197 aa, bb, cc, dd := a, b, c, d | 212 aa, bb, cc, dd := a, b, c, d |
198 | 213 |
199 // This is a constant condition - it is not evaluated on each it
eration. | 214 // This is a constant condition - it is not evaluated on each it
eration. |
200 » » if runtime.GOARCH == "amd64" || runtime.GOARCH == "386" { | 215 » » if x86 { |
201 // MD5 was designed so that x86 processors can just iter
ate | 216 // MD5 was designed so that x86 processors can just iter
ate |
202 // over the block data directly as uint32s, and we gener
ate | 217 // over the block data directly as uint32s, and we gener
ate |
203 // less code and run 1.3x faster if we take advantage of
that. | 218 // less code and run 1.3x faster if we take advantage of
that. |
204 // My apologies. | 219 // My apologies. |
205 X = (*[16]uint32)(unsafe.Pointer(&p[0])) | 220 X = (*[16]uint32)(unsafe.Pointer(&p[0])) |
206 » » } else if uintptr(unsafe.Pointer(&p[0]))&(unsafe.Alignof(uint32(
0))-1) == 0 { | 221 » » } else if littleEndian && uintptr(unsafe.Pointer(&p[0]))&(unsafe
.Alignof(uint32(0))-1) == 0 { |
207 X = (*[16]uint32)(unsafe.Pointer(&p[0])) | 222 X = (*[16]uint32)(unsafe.Pointer(&p[0])) |
208 } else { | 223 } else { |
209 X = &xbuf | 224 X = &xbuf |
210 j := 0 | 225 j := 0 |
211 for i := 0; i < 16; i++ { | 226 for i := 0; i < 16; i++ { |
212 X[i&15] = uint32(p[j]) | uint32(p[j+1])<<8 | uin
t32(p[j+2])<<16 | uint32(p[j+3])<<24 | 227 X[i&15] = uint32(p[j]) | uint32(p[j+1])<<8 | uin
t32(p[j+2])<<16 | uint32(p[j+3])<<24 |
213 j += 4 | 228 j += 4 |
214 } | 229 } |
215 } | 230 } |
216 | 231 |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 | 306 |
292 p = p[chunk:] | 307 p = p[chunk:] |
293 } | 308 } |
294 | 309 |
295 dig.s[0] = a | 310 dig.s[0] = a |
296 dig.s[1] = b | 311 dig.s[1] = b |
297 dig.s[2] = c | 312 dig.s[2] = c |
298 dig.s[3] = d | 313 dig.s[3] = d |
299 } | 314 } |
300 ` | 315 ` |
LEFT | RIGHT |