forked from klauspost/reedsolomon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unsafe.go
41 lines (37 loc) · 885 Bytes
/
unsafe.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//go:build !noasm && !nounsafe && !gccgo && !appengine
/**
* Reed-Solomon Coding over 8-bit values.
*
* Copyright 2023, Klaus Post
*/
package reedsolomon
import (
"unsafe"
)
// AllocAligned allocates 'shards' slices, with 'each' bytes.
// Each slice will start on a 64 byte aligned boundary.
func AllocAligned(shards, each int) [][]byte {
if false {
res := make([][]byte, shards)
for i := range res {
res[i] = make([]byte, each)
}
return res
}
const (
alignEach = 64
alignStart = 64
)
eachAligned := ((each + alignEach - 1) / alignEach) * alignEach
total := make([]byte, eachAligned*shards+63)
align := uint(uintptr(unsafe.Pointer(&total[0]))) & (alignStart - 1)
if align > 0 {
total = total[alignStart-align:]
}
res := make([][]byte, shards)
for i := range res {
res[i] = total[:each:eachAligned]
total = total[eachAligned:]
}
return res
}