| 1 |
//go:build !appengine && !noasm && gc && !nogen && !nopshufb |
| 2 |
|
| 3 |
package reedsolomon |
| 4 |
|
| 5 |
import ( |
| 6 |
"fmt" |
| 7 |
) |
| 8 |
|
| 9 |
const ( |
| 10 |
codeGen = true |
| 11 |
codeGenMaxGoroutines = 16 |
| 12 |
codeGenMaxInputs = 10 |
| 13 |
codeGenMaxOutputs = 10 |
| 14 |
minCodeGenSize = 64 |
| 15 |
// ARM64 asm always reads 10 inputs; pad matrix and in-slice to this minimum. |
| 16 |
codeGenPadInputs = codeGenMaxInputs |
| 17 |
) |
| 18 |
|
| 19 |
var ( |
| 20 |
fSve = galMulSlicesSve |
| 21 |
fSveXor = galMulSlicesSveXor |
| 22 |
fNeon = galMulSlicesNeon |
| 23 |
fNeonXor = galMulSlicesNeonXor |
| 24 |
) |
| 25 |
|
| 26 |
func (r *reedSolomon) hasCodeGen(byteCount int, inputs, outputs int) (_, _ *func(matrix []byte, in, out [][]byte, start, stop int) int, ok bool) { |
| 27 |
if r.o.useSVE { |
| 28 |
return &fSve, &fSveXor, codeGen && pshufb && |
| 29 |
byteCount >= codeGenMinSize && inputs+outputs >= codeGenMinShards && |
| 30 |
inputs <= codeGenMaxInputs && outputs <= codeGenMaxOutputs |
| 31 |
} |
| 32 |
return &fNeon, &fNeonXor, codeGen && pshufb && r.o.useNEON && |
| 33 |
byteCount >= codeGenMinSize && inputs+outputs >= codeGenMinShards && |
| 34 |
inputs <= codeGenMaxInputs && outputs <= codeGenMaxOutputs |
| 35 |
} |
| 36 |
|
| 37 |
func (r *reedSolomon) canGFNI(byteCount int, inputs, outputs int) (_, _ *func(matrix []uint64, in, out [][]byte, start, stop int) int, ok bool) { |
| 38 |
return nil, nil, false |
| 39 |
} |
| 40 |
|
| 41 |
func galMulSlicesSve(matrix []byte, in, out [][]byte, start, stop int) (n int) { |
| 42 |
n = stop - start |
| 43 |
|
| 44 |
if raceEnabled { |
| 45 |
defer func() { |
| 46 |
raceReadSlices(in, start, n) |
| 47 |
raceWriteSlices(out, start, n) |
| 48 |
}() |
| 49 |
} |
| 50 |
if len(in) < codeGenMaxInputs { |
| 51 |
var padded [codeGenMaxInputs][]byte |
| 52 |
copy(padded[:], in) |
| 53 |
for i := len(in); i < codeGenMaxInputs; i++ { |
| 54 |
padded[i] = in[0] |
| 55 |
} |
| 56 |
in = padded[:] |
| 57 |
} |
| 58 |
switch len(out) { |
| 59 |
case 1: |
| 60 |
mulSve_10x1_64(matrix, in, out, start, n) |
| 61 |
return n & (maxInt - 63) |
| 62 |
case 2: |
| 63 |
mulSve_10x2_64(matrix, in, out, start, n) |
| 64 |
return n & (maxInt - 63) |
| 65 |
case 3: |
| 66 |
mulSve_10x3_64(matrix, in, out, start, n) |
| 67 |
return n & (maxInt - 63) |
| 68 |
case 4: |
| 69 |
mulSve_10x4(matrix, in, out, start, n) |
| 70 |
return n & (maxInt - 31) |
| 71 |
case 5: |
| 72 |
mulSve_10x5(matrix, in, out, start, n) |
| 73 |
return n & (maxInt - 31) |
| 74 |
case 6: |
| 75 |
mulSve_10x6(matrix, in, out, start, n) |
| 76 |
return n & (maxInt - 31) |
| 77 |
case 7: |
| 78 |
mulSve_10x7(matrix, in, out, start, n) |
| 79 |
return n & (maxInt - 31) |
| 80 |
case 8: |
| 81 |
mulSve_10x8(matrix, in, out, start, n) |
| 82 |
return n & (maxInt - 31) |
| 83 |
case 9: |
| 84 |
mulSve_10x9(matrix, in, out, start, n) |
| 85 |
return n & (maxInt - 31) |
| 86 |
case 10: |
| 87 |
mulSve_10x10(matrix, in, out, start, n) |
| 88 |
return n & (maxInt - 31) |
| 89 |
} |
| 90 |
panic(fmt.Sprintf("ARM SVE: unhandled size: %dx%d", len(in), len(out))) |
| 91 |
} |
| 92 |
|
| 93 |
func galMulSlicesSveXor(matrix []byte, in, out [][]byte, start, stop int) (n int) { |
| 94 |
n = (stop - start) |
| 95 |
|
| 96 |
if raceEnabled { |
| 97 |
defer func() { |
| 98 |
raceReadSlices(in, start, n) |
| 99 |
raceWriteSlices(out, start, n) |
| 100 |
}() |
| 101 |
} |
| 102 |
if len(in) < codeGenMaxInputs { |
| 103 |
var padded [codeGenMaxInputs][]byte |
| 104 |
copy(padded[:], in) |
| 105 |
for i := len(in); i < codeGenMaxInputs; i++ { |
| 106 |
padded[i] = in[0] |
| 107 |
} |
| 108 |
in = padded[:] |
| 109 |
} |
| 110 |
switch len(out) { |
| 111 |
case 1: |
| 112 |
mulSve_10x1_64Xor(matrix, in, out, start, n) |
| 113 |
return n & (maxInt - 63) |
| 114 |
case 2: |
| 115 |
mulSve_10x2_64Xor(matrix, in, out, start, n) |
| 116 |
return n & (maxInt - 63) |
| 117 |
case 3: |
| 118 |
mulSve_10x3_64Xor(matrix, in, out, start, n) |
| 119 |
return n & (maxInt - 63) |
| 120 |
case 4: |
| 121 |
mulSve_10x4Xor(matrix, in, out, start, n) |
| 122 |
return n & (maxInt - 31) |
| 123 |
case 5: |
| 124 |
mulSve_10x5Xor(matrix, in, out, start, n) |
| 125 |
return n & (maxInt - 31) |
| 126 |
case 6: |
| 127 |
mulSve_10x6Xor(matrix, in, out, start, n) |
| 128 |
return n & (maxInt - 31) |
| 129 |
case 7: |
| 130 |
mulSve_10x7Xor(matrix, in, out, start, n) |
| 131 |
return n & (maxInt - 31) |
| 132 |
case 8: |
| 133 |
mulSve_10x8Xor(matrix, in, out, start, n) |
| 134 |
return n & (maxInt - 31) |
| 135 |
case 9: |
| 136 |
mulSve_10x9Xor(matrix, in, out, start, n) |
| 137 |
return n & (maxInt - 31) |
| 138 |
case 10: |
| 139 |
mulSve_10x10Xor(matrix, in, out, start, n) |
| 140 |
return n & (maxInt - 31) |
| 141 |
} |
| 142 |
panic(fmt.Sprintf("ARM SVE: unhandled size: %dx%d", len(in), len(out))) |
| 143 |
} |
| 144 |
|
| 145 |
// isFirstParityRowAllOne checks if the first parity row (first output) has all |
| 146 |
// coefficients equal to 1 in the generator matrix. |
| 147 |
// |
| 148 |
// In Jerasure matrix, the first parity row is always all-ones, enabling XOR-only |
| 149 |
// optimization for the first output. Subsequent parity rows typically have non-unity |
| 150 |
// coefficients and require full GF multiplication. |
| 151 |
// |
| 152 |
// The matrix layout in memory is: (input * outputs + output_index) * 64 |
| 153 |
// This function checks only output_index=0 (first parity row). |
| 154 |
func isFirstParityRowAllOne(matrix []byte, inputs, outputs int) bool { |
| 155 |
for i := 0; i < inputs; i++ { |
| 156 |
checkIndex := (i*outputs+0)*64 + 1 |
| 157 |
if checkIndex >= len(matrix) { |
| 158 |
return false |
| 159 |
} |
| 160 |
if matrix[checkIndex] != 0x1 { |
| 161 |
return false |
| 162 |
} |
| 163 |
} |
| 164 |
return true |
| 165 |
} |
| 166 |
|
| 167 |
func galMulSlicesNeon(matrix []byte, in, out [][]byte, start, stop int) (n int) { |
| 168 |
n = stop - start |
| 169 |
if raceEnabled { |
| 170 |
defer func() { |
| 171 |
raceReadSlices(in, start, n) |
| 172 |
raceWriteSlices(out, start, n) |
| 173 |
}() |
| 174 |
} |
| 175 |
actualInputs := len(in) |
| 176 |
if len(in) < codeGenMaxInputs { |
| 177 |
var padded [codeGenMaxInputs][]byte |
| 178 |
copy(padded[:], in) |
| 179 |
for i := len(in); i < codeGenMaxInputs; i++ { |
| 180 |
padded[i] = in[0] |
| 181 |
} |
| 182 |
in = padded[:] |
| 183 |
} |
| 184 |
if 0 < len(out) && len(out) <= 4 && isFirstParityRowAllOne(matrix, actualInputs, len(out)) { |
| 185 |
eorIn := in[:actualInputs] |
| 186 |
switch len(out) { |
| 187 |
case 1: |
| 188 |
mulNeon_10x1_64eor(matrix, eorIn, out, start, n) |
| 189 |
return n & (maxInt - 63) |
| 190 |
case 2: |
| 191 |
mulNeon_10x2_64eor(matrix, eorIn, out, start, n) |
| 192 |
return n & (maxInt - 63) |
| 193 |
case 3: |
| 194 |
mulNeon_10x3_64eor(matrix, eorIn, out, start, n) |
| 195 |
return n & (maxInt - 63) |
| 196 |
case 4: |
| 197 |
mulNeon_10x4eor(matrix, eorIn, out, start, n) |
| 198 |
return n & (maxInt - 31) |
| 199 |
} |
| 200 |
} |
| 201 |
switch len(out) { |
| 202 |
case 1: |
| 203 |
mulNeon_10x1_64(matrix, in, out, start, n) |
| 204 |
return n & (maxInt - 63) |
| 205 |
case 2: |
| 206 |
mulNeon_10x2_64(matrix, in, out, start, n) |
| 207 |
return n & (maxInt - 63) |
| 208 |
case 3: |
| 209 |
mulNeon_10x3_64(matrix, in, out, start, n) |
| 210 |
return n & (maxInt - 63) |
| 211 |
case 4: |
| 212 |
mulNeon_10x4(matrix, in, out, start, n) |
| 213 |
return n & (maxInt - 31) |
| 214 |
case 5: |
| 215 |
mulNeon_10x5(matrix, in, out, start, n) |
| 216 |
return n & (maxInt - 31) |
| 217 |
case 6: |
| 218 |
mulNeon_10x6(matrix, in, out, start, n) |
| 219 |
return n & (maxInt - 31) |
| 220 |
case 7: |
| 221 |
mulNeon_10x7(matrix, in, out, start, n) |
| 222 |
return n & (maxInt - 31) |
| 223 |
case 8: |
| 224 |
mulNeon_10x8(matrix, in, out, start, n) |
| 225 |
return n & (maxInt - 31) |
| 226 |
case 9: |
| 227 |
mulNeon_10x9(matrix, in, out, start, n) |
| 228 |
return n & (maxInt - 31) |
| 229 |
case 10: |
| 230 |
mulNeon_10x10(matrix, in, out, start, n) |
| 231 |
return n & (maxInt - 31) |
| 232 |
} |
| 233 |
panic(fmt.Sprintf("ARM NEON: unhandled size: %dx%d", len(in), len(out))) |
| 234 |
} |
| 235 |
|
| 236 |
func galMulSlicesNeonXor(matrix []byte, in, out [][]byte, start, stop int) (n int) { |
| 237 |
n = (stop - start) |
| 238 |
if raceEnabled { |
| 239 |
defer func() { |
| 240 |
raceReadSlices(in, start, n) |
| 241 |
raceWriteSlices(out, start, n) |
| 242 |
}() |
| 243 |
} |
| 244 |
if len(in) < codeGenMaxInputs { |
| 245 |
var padded [codeGenMaxInputs][]byte |
| 246 |
copy(padded[:], in) |
| 247 |
for i := len(in); i < codeGenMaxInputs; i++ { |
| 248 |
padded[i] = in[0] |
| 249 |
} |
| 250 |
in = padded[:] |
| 251 |
} |
| 252 |
switch len(out) { |
| 253 |
case 1: |
| 254 |
mulNeon_10x1_64Xor(matrix, in, out, start, n) |
| 255 |
return n & (maxInt - 63) |
| 256 |
case 2: |
| 257 |
mulNeon_10x2_64Xor(matrix, in, out, start, n) |
| 258 |
return n & (maxInt - 63) |
| 259 |
case 3: |
| 260 |
mulNeon_10x3_64Xor(matrix, in, out, start, n) |
| 261 |
return n & (maxInt - 63) |
| 262 |
case 4: |
| 263 |
mulNeon_10x4Xor(matrix, in, out, start, n) |
| 264 |
return n & (maxInt - 31) |
| 265 |
case 5: |
| 266 |
mulNeon_10x5Xor(matrix, in, out, start, n) |
| 267 |
return n & (maxInt - 31) |
| 268 |
case 6: |
| 269 |
mulNeon_10x6Xor(matrix, in, out, start, n) |
| 270 |
return n & (maxInt - 31) |
| 271 |
case 7: |
| 272 |
mulNeon_10x7Xor(matrix, in, out, start, n) |
| 273 |
return n & (maxInt - 31) |
| 274 |
case 8: |
| 275 |
mulNeon_10x8Xor(matrix, in, out, start, n) |
| 276 |
return n & (maxInt - 31) |
| 277 |
case 9: |
| 278 |
mulNeon_10x9Xor(matrix, in, out, start, n) |
| 279 |
return n & (maxInt - 31) |
| 280 |
case 10: |
| 281 |
mulNeon_10x10Xor(matrix, in, out, start, n) |
| 282 |
return n & (maxInt - 31) |
| 283 |
} |
| 284 |
panic(fmt.Sprintf("ARM NEON: unhandled size: %dx%d", len(in), len(out))) |
| 285 |
} |