LEFT | RIGHT |
1 // Copyright 2013 The Go Authors. All rights reserved. | 1 // Copyright 2013 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 package cipher | 5 package cipher |
6 | 6 |
7 import ( | 7 import ( |
8 "runtime" | 8 "runtime" |
9 "unsafe" | 9 "unsafe" |
10 ) | 10 ) |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 // TODO(hanwen): if (dst, a, b) have common alignment | 57 // TODO(hanwen): if (dst, a, b) have common alignment |
58 // we could still try fastXORBytes. It is not clear | 58 // we could still try fastXORBytes. It is not clear |
59 // how often this happens, and it's only worth it if | 59 // how often this happens, and it's only worth it if |
60 // the block encryption itself is hardware | 60 // the block encryption itself is hardware |
61 // accelerated. | 61 // accelerated. |
62 return safeXORBytes(dst, a, b) | 62 return safeXORBytes(dst, a, b) |
63 } | 63 } |
64 } | 64 } |
65 | 65 |
66 // fastXORWords XORs multiples of 4 or 8 bytes (depending on architecture.) | 66 // fastXORWords XORs multiples of 4 or 8 bytes (depending on architecture.) |
| 67 // The arguments are assumed to be of equal length. |
67 func fastXORWords(dst, a, b []byte) { | 68 func fastXORWords(dst, a, b []byte) { |
68 dw := *(*[]uintptr)(unsafe.Pointer(&dst)) | 69 dw := *(*[]uintptr)(unsafe.Pointer(&dst)) |
69 aw := *(*[]uintptr)(unsafe.Pointer(&a)) | 70 aw := *(*[]uintptr)(unsafe.Pointer(&a)) |
70 bw := *(*[]uintptr)(unsafe.Pointer(&b)) | 71 bw := *(*[]uintptr)(unsafe.Pointer(&b)) |
71 | |
72 n := len(b) / wordSize | 72 n := len(b) / wordSize |
73 for i := 0; i < n; i++ { | 73 for i := 0; i < n; i++ { |
74 dw[i] = aw[i] ^ bw[i] | 74 dw[i] = aw[i] ^ bw[i] |
75 } | 75 } |
76 } | 76 } |
77 | 77 |
78 func xorWords(dst, a, b []byte) { | 78 func xorWords(dst, a, b []byte) { |
79 if supportsUnaligned { | 79 if supportsUnaligned { |
80 fastXORWords(dst, a, b) | 80 fastXORWords(dst, a, b) |
81 } else { | 81 } else { |
82 safeXORBytes(dst, a, b) | 82 safeXORBytes(dst, a, b) |
83 } | 83 } |
84 } | 84 } |
LEFT | RIGHT |