Debian Codegraph @

unsafe.go

raw
1 //go:build !nounsafe && !gccgo && !appengine
2
3 /**
4 * Reed-Solomon Coding over 8-bit values.
5 *
6 * Copyright 2023, Klaus Post
7 */
8
9 package reedsolomon
10
11 import (
12 "unsafe"
13 )
14
15 const unsafeEnabled = true
16
17 // AllocAligned allocates 'shards' slices, with 'each' bytes.
18 // Each slice will start on a 64 byte aligned boundary.
19 func AllocAligned(shards, each int) [][]byte {
20 if false {
21 res := make([][]byte, shards)
22 for i := range res {
23 res[i] = make([]byte, each)
24 }
25 return res
26 }
27 const (
28 alignEach = 64
29 alignStart = 64
30 )
31 eachAligned := ((each + alignEach - 1) / alignEach) * alignEach
32 total := make([]byte, eachAligned*shards+63)
33 align := uint(uintptr(unsafe.Pointer(&total[0]))) & (alignStart - 1)
34 if align > 0 {
35 total = total[alignStart-align:]
36 }
37 res := make([][]byte, shards)
38 for i := range res {
39 res[i] = total[:each:eachAligned]
40 total = total[eachAligned:]
41 }
42 return res
43 }
44
45 // load64 will load from b at index i.
46 func load64[I indexer](b []byte, i I) uint64 {
47 //return binary.LittleEndian.Uint64(b[i:])
48 //return *(*uint64)(unsafe.Pointer(&b[i]))
49 return *(*uint64)(unsafe.Add(unsafe.Pointer(unsafe.SliceData(b)), i))
50 }
51
52 // Store64 will store v at b.
53 func store64[I indexer](b []byte, v uint64, i I) {
54 //binary.LittleEndian.PutUint64(b[i:], v)
55 *(*uint64)(unsafe.Add(unsafe.Pointer(unsafe.SliceData(b)), i)) = v
56 }
57
58 // load16 will load from b at index i.
59 func load16[I indexer](b []byte, i I) uint16 {
60 //return binary.LittleEndian.Uint64(b[i:])
61 //return *(*uint64)(unsafe.Pointer(&b[i]))
62 return *(*uint16)(unsafe.Add(unsafe.Pointer(unsafe.SliceData(b)), i))
63 }
64
65 // Store16 will store v at b.
66 func store16[I indexer](b []byte, v uint16, i I) {
67 //binary.LittleEndian.PutUint64(b[i:], v)
68 *(*uint16)(unsafe.Add(unsafe.Pointer(unsafe.SliceData(b)), i)) = v
69 }
70
71 func load8[I indexer](b []byte, i I) uint8 {
72 return *(*uint8)(unsafe.Add(unsafe.Pointer(unsafe.SliceData(b)), i))
73 }
74
75 func store8[I indexer](b []byte, v uint8, i I) {
76 *(*uint8)(unsafe.Add(unsafe.Pointer(unsafe.SliceData(b)), i)) = v
77 }