Skip to content

Commit 3aa7bbd

Browse files
committed
runtime: simplify readUnaligned
We already have a pure go code sequence that is compiled into single load. Just use it everywhere, instead of pointer hackery. Passes toolstash-check. Change-Id: I0c42b5532fa9a5665da3385913609c6d42aaff27 Reviewed-on: https://go-review.googlesource.com/c/go/+/118568 Run-TryBot: Ilya Tocar <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Austin Clements <[email protected]>
1 parent 82bd939 commit 3aa7bbd

File tree

3 files changed

+18
-37
lines changed

3 files changed

+18
-37
lines changed

src/runtime/alg.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,21 @@ func initAlgAES() {
316316
// Initialize with random data so hash collisions will be hard to engineer.
317317
getRandomData(aeskeysched[:])
318318
}
319+
320+
// Note: These routines perform the read with an native endianness.
321+
func readUnaligned32(p unsafe.Pointer) uint32 {
322+
q := (*[4]byte)(p)
323+
if sys.BigEndian {
324+
return uint32(q[3]) | uint32(q[2])<<8 | uint32(q[1])<<16 | uint32(q[0])<<24
325+
}
326+
return uint32(q[0]) | uint32(q[1])<<8 | uint32(q[2])<<16 | uint32(q[3])<<24
327+
}
328+
329+
func readUnaligned64(p unsafe.Pointer) uint64 {
330+
q := (*[8]byte)(p)
331+
if sys.BigEndian {
332+
return uint64(q[7]) | uint64(q[6])<<8 | uint64(q[5])<<16 | uint64(q[4])<<24 |
333+
uint64(q[3])<<32 | uint64(q[2])<<40 | uint64(q[1])<<48 | uint64(q[0])<<56
334+
}
335+
return uint64(q[0]) | uint64(q[1])<<8 | uint64(q[2])<<16 | uint64(q[3])<<24 | uint64(q[4])<<32 | uint64(q[5])<<40 | uint64(q[6])<<48 | uint64(q[7])<<56
336+
}

src/runtime/unaligned1.go

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/runtime/unaligned2.go

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)