| 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 reedsolomonscip-go gomod github.com/klauspost/reedsolomon . `github.com/klauspost/reedsolomon`/ |
| 10 | |
| 11 | import ( |
| 12 | "unsafescip-go gomod github.com/golang/go/src go1.24 unsafe/" |
| 13 | ) |
| 14 | |
| 15 | const unsafeEnabledscip-go gomod github.com/klauspost/reedsolomon . `github.com/klauspost/reedsolomon`/unsafeEnabled. = true |
| 16 | |
| 17 | // AllocAligned allocates 'shards' slices, with 'each' bytes. |
| 18 | // Each slice will start on a 64 byte aligned boundary. |
| 19 | func AllocAlignedscip-go gomod github.com/klauspost/reedsolomon . `github.com/klauspost/reedsolomon`/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(unsafescip-go gomod github.com/golang/go/src go1.24 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 load64scip-go gomod github.com/klauspost/reedsolomon . `github.com/klauspost/reedsolomon`/load64().[I indexerscip-go gomod github.com/klauspost/reedsolomon . `github.com/klauspost/reedsolomon`/indexer#](b []byte, i I) uint64 { |
| 47 | //return binary.LittleEndian.Uint64(b[i:]) |
| 48 | //return *(*uint64)(unsafe.Pointer(&b[i])) |
| 49 | return *(*uint64)(unsafescip-go gomod github.com/golang/go/src go1.24 unsafe/.Add(unsafescip-go gomod github.com/golang/go/src go1.24 unsafe/.Pointer(unsafescip-go gomod github.com/golang/go/src go1.24 unsafe/.SliceData(b)), i)) |
| 50 | } |
| 51 | |
| 52 | // Store64 will store v at b. |
| 53 | func store64scip-go gomod github.com/klauspost/reedsolomon . `github.com/klauspost/reedsolomon`/store64().[I indexerscip-go gomod github.com/klauspost/reedsolomon . `github.com/klauspost/reedsolomon`/indexer#](b []byte, v uint64, i I) { |
| 54 | //binary.LittleEndian.PutUint64(b[i:], v) |
| 55 | *(*uint64)(unsafescip-go gomod github.com/golang/go/src go1.24 unsafe/.Add(unsafescip-go gomod github.com/golang/go/src go1.24 unsafe/.Pointer(unsafescip-go gomod github.com/golang/go/src go1.24 unsafe/.SliceData(b)), i)) = v |
| 56 | } |
| 57 | |
| 58 | // load16 will load from b at index i. |
| 59 | func load16scip-go gomod github.com/klauspost/reedsolomon . `github.com/klauspost/reedsolomon`/load16().[I indexerscip-go gomod github.com/klauspost/reedsolomon . `github.com/klauspost/reedsolomon`/indexer#](b []byte, i I) uint16 { |
| 60 | //return binary.LittleEndian.Uint64(b[i:]) |
| 61 | //return *(*uint64)(unsafe.Pointer(&b[i])) |
| 62 | return *(*uint16)(unsafescip-go gomod github.com/golang/go/src go1.24 unsafe/.Add(unsafescip-go gomod github.com/golang/go/src go1.24 unsafe/.Pointer(unsafescip-go gomod github.com/golang/go/src go1.24 unsafe/.SliceData(b)), i)) |
| 63 | } |
| 64 | |
| 65 | // Store16 will store v at b. |
| 66 | func store16scip-go gomod github.com/klauspost/reedsolomon . `github.com/klauspost/reedsolomon`/store16().[I indexerscip-go gomod github.com/klauspost/reedsolomon . `github.com/klauspost/reedsolomon`/indexer#](b []byte, v uint16, i I) { |
| 67 | //binary.LittleEndian.PutUint64(b[i:], v) |
| 68 | *(*uint16)(unsafescip-go gomod github.com/golang/go/src go1.24 unsafe/.Add(unsafescip-go gomod github.com/golang/go/src go1.24 unsafe/.Pointer(unsafescip-go gomod github.com/golang/go/src go1.24 unsafe/.SliceData(b)), i)) = v |
| 69 | } |
| 70 | |
| 71 | func load8scip-go gomod github.com/klauspost/reedsolomon . `github.com/klauspost/reedsolomon`/load8().[I indexerscip-go gomod github.com/klauspost/reedsolomon . `github.com/klauspost/reedsolomon`/indexer#](b []byte, i I) uint8 { |
| 72 | return *(*uint8)(unsafescip-go gomod github.com/golang/go/src go1.24 unsafe/.Add(unsafescip-go gomod github.com/golang/go/src go1.24 unsafe/.Pointer(unsafescip-go gomod github.com/golang/go/src go1.24 unsafe/.SliceData(b)), i)) |
| 73 | } |
| 74 | |
| 75 | func store8scip-go gomod github.com/klauspost/reedsolomon . `github.com/klauspost/reedsolomon`/store8().[I indexerscip-go gomod github.com/klauspost/reedsolomon . `github.com/klauspost/reedsolomon`/indexer#](b []byte, v uint8, i I) { |
| 76 | *(*uint8)(unsafescip-go gomod github.com/golang/go/src go1.24 unsafe/.Add(unsafescip-go gomod github.com/golang/go/src go1.24 unsafe/.Pointer(unsafescip-go gomod github.com/golang/go/src go1.24 unsafe/.SliceData(b)), i)) = v |
| 77 | } |