7 this extension amalgamates bitnanipulation primitives from many sources, including RISC-V bitmanip, Packed SIMD, AVX-512 and OpenPOWER VSX. Vectorisation and SIMD are removed: these are straight scalar (element) operations. Vectorisation Context is provided by [[openpower/sv]].
9 ternaryv is experimental and is the only operation that may be considered a "Packed SIMD". It is added as a variant of the already well-justified ternary operation (done in AVX512 as an immediate only) "because it looks fun". As it is based on the LUT4 concept it will allow accelerated emulation of FPGAs. Other vendors of ISAs are buying FPGA companies to achieve a similar objective.
11 general-purpose Galois Field operations are added so as to avoid huge opcode proliferation across many areas of Computer Science. however for convenience and also to avoid setup costs, some of the more common operations (clmul, crc32) are also added. The expectation is that these operations would all be covered by the same pipeline.
15 minor opcode allocation
18 | ------ |--| --------- |
24 | 101 |0 | ternarycr |
30 | dest | src1 | subop | op |
31 | ---- | ---- | ----- | -------- |
32 | RT | RA | .. | bmatflip |
36 | dest | src1 | src2 | subop | op |
37 | ---- | ---- | ---- | ----- | -------- |
38 | RT | RA | RB | or | bmatflip |
39 | RT | RA | RB | xor | bmatflip |
40 | RT | RA | RB | bdep | dep/ext |
41 | RT | RA | RB | bext | dep/ext |
42 | RT | RA | RB | | grev |
43 | RT | RA | RB | | clmul* |
44 | RT | RA | RB | | gorc |
45 | RT | RA | RB | shuf | shuffle |
46 | RT | RA | RB | unshuf| shuffle |
47 | RT | RA | RB | width | xperm |
48 | RT | RA | RB | type | minmax |
59 | 0.5|6.10|11.15|16.20|21..25 | 26....30 |31| name |
60 | -- | -- | --- | --- | ----- | -------- |--| ------ |
61 | NN | RT | RA | RB | RC | mode 001 |Rc| ternary |
62 | NN | RT | RA | RB | im0-4 | im5-7 00 |Rc| ternaryi |
63 | NN | RS | RA | RB | RC | 00 011 |Rc| gfmul |
64 | NN | RS | RA | RB | RC | 01 011 |Rc| gfadd |
65 | NN | RT | RA | RB | deg | 10 011 |Rc| gfinv |
66 | NN | RS | RA | RB | deg | 11 011 |Rc| gfmuli |
67 | NN | RS | RA | RB | deg | 11 111 |Rc| gfaddi |
69 | 0.5|6.10|11.15| 16.23 |24.27 | 28.30 |31| name |
70 | -- | -- | --- | ----- | ---- | ----- |--| ------ |
71 | NN | RT | RA | imm | mask | 101 |1 | ternaryv |
73 | 0.5|6.8 | 9.11|12.14|15|16.23|24.27 | 28.30|31| name |
74 | -- | -- | --- | --- |- |-----|----- | -----|--| -------|
75 | NN | BA | BB | BC |0 |imm | mask | 101 |0 | ternarycr |
79 | 0.5|6.10|11.15|16.20| 21.22 | 23 | 24....30 |31| name |
80 | -- | -- | --- | --- | ----- | -- | -------- |--| ---- |
81 | NN | RA | RB | | | 0 | 0000 110 |Rc| rsvd |
82 | NN | RA | RB | RC | itype | 1 | 0000 110 |Rc| xperm |
83 | NN | RA | RB | RC | itype | 0 | 0100 110 |Rc| minmax |
84 | NN | RA | RB | | | 1 | 0100 110 |Rc| rsvd |
85 | NN | RA | RB | sh | itype | SH | 1000 110 |Rc| bmopsi |
86 | NN | RA | RB | | | | 1100 110 |Rc| rsvd |
87 | NN | RA | RB | | | | 1100 110 |Rc| rsvd |
88 | NN | RA | RB | | | | 1100 110 |Rc| rsvd |
89 | NN | RA | RB | | | | 1100 110 |Rc| rsvd |
90 | NN | RA | RB | | | 0 | 0001 110 |Rc| rsvd |
91 | NN | RA | RB | | | 0 | 0101 110 |Rc| rsvd |
92 | NN | RA | RB | RC | 00 | 0 | 0010 110 |Rc| gorc |
93 | NN | RA | RB | sh | 00 | SH | 1010 110 |Rc| gorci |
94 | NN | RA | RB | RC | 00 | 0 | 0110 110 |Rc| gorcw |
95 | NN | RA | RB | sh | 00 | 0 | 1110 110 |Rc| gorcwi |
96 | NN | RA | RB | RC | 00 | 1 | 1110 110 |Rc| bmator |
97 | NN | RA | RB | RC | 01 | 0 | 0010 110 |Rc| grev |
98 | NN | RA | RB | RC | 01 | 1 | 0010 110 |Rc| clmul |
99 | NN | RA | RB | sh | 01 | SH | 1010 110 |Rc| grevi |
100 | NN | RA | RB | RC | 01 | 0 | 0110 110 |Rc| grevw |
101 | NN | RA | RB | sh | 01 | 0 | 1110 110 |Rc| grevwi |
102 | NN | RA | RB | RC | 01 | 1 | 1110 110 |Rc| bmatxor |
103 | NN | RA | RB | RC | 10 | 0 | 0010 110 |Rc| shfl |
104 | NN | RA | RB | sh | 10 | SH | 1010 110 |Rc| shfli |
105 | NN | RA | RB | RC | 10 | 0 | 0110 110 |Rc| shflw |
106 | NN | RA | RB | RC | 10 | 0 | 1110 110 |Rc| bdep |
107 | NN | RA | RB | RC | 10 | 1 | 1110 110 |Rc| bext |
108 | NN | RA | RB | RC | 11 | 0 | 1110 110 |Rc| clmulr |
109 | NN | RA | RB | RC | 11 | 1 | 1110 110 |Rc| clmulh |
110 | NN | RA | RB | | | | NN11 110 |Rc| rsvd |
112 # bit to byte permute
114 similar to matrix permute in RV bitmanip, which has XOR and OR variants
118 b = VSR[VRB+32].dword[i].byte[k].bit[j]
119 VSR[VRT+32].dword[i].byte[j].bit[k] = b
123 vpdepd VRT,VRA,VRB, identical to RV bitmamip bdep
126 if VSR[VRB+32].dword[i].bit[63-m]=1 then do
127 result = VSR[VRA+32].dword[i].bit[63-k]
128 VSR[VRT+32].dword[i].bit[63-m] = result
134 uint_xlen_t bdep(uint_xlen_t RA, uint_xlen_t RB)
137 for (int i = 0, j = 0; i < XLEN; i++)
140 r |= uint_xlen_t(1) << i;
150 other way round: identical to RV bext
153 uint_xlen_t bext(uint_xlen_t RA, uint_xlen_t RB)
156 for (int i = 0, j = 0; i < XLEN; i++)
159 r |= uint_xlen_t(1) << j;
168 signed and unsigned min/max for integer. this is sort-of partly synthesiseable in [[sv/svp64]] with pred-result as long as the dest reg is one of the sources, but not both signed and unsigned. when the dest is also one of the srces and the mv fails due to the CR bittest failing this will only overwrite the dest where the src is greater (or less).
170 signed/unsigned min/max gives more flexibility.
173 uint_xlen_t min(uint_xlen_t rs1, uint_xlen_t rs2)
174 { return (int_xlen_t)rs1 < (int_xlen_t)rs2 ? rs1 : rs2;
176 uint_xlen_t max(uint_xlen_t rs1, uint_xlen_t rs2)
177 { return (int_xlen_t)rs1 > (int_xlen_t)rs2 ? rs1 : rs2;
179 uint_xlen_t minu(uint_xlen_t rs1, uint_xlen_t rs2)
180 { return rs1 < rs2 ? rs1 : rs2;
182 uint_xlen_t maxu(uint_xlen_t rs1, uint_xlen_t rs2)
183 { return rs1 > rs2 ? rs1 : rs2;
190 Similar to FPGA LUTs: for every bit perform a lookup into a table using an 8bit immediate, or in another register
192 | 0.5|6.10|11.15|16.20| 21..25| 26..30 |31|
193 | -- | -- | --- | --- | ----- | -------- |--|
194 | NN | RT | RA | RB | im0-4 | im5-7 00 |Rc|
197 idx = RT[i] << 2 | RA[i] << 1 | RB[i]
198 RT[i] = (imm & (1<<idx)) != 0
200 bits 21..22 may be used to specify a mode, such as treating the whole integer zero/nonzero and putting 1/0 in the result, rather than bitwise test.
202 a 4 operand variant which becomes more along the lines of an FPGA:
204 | 0.5|6.10|11.15|16.20|21.25| 26...30 |31|
205 | -- | -- | --- | --- | --- | -------- |--|
206 | NN | RT | RA | RB | RC | mode 001 |Rc|
209 idx = RT[i] << 2 | RA[i] << 1 | RB[i]
210 RT[i] = (RC & (1<<idx)) != 0
212 mode (2 bit) may be used to do inversion of ordering, similar to carryless mul,
215 also, another possible variant involving swizzle and vec4:
217 | 0.5|6.10|11.15| 16.23 |24.27 | 28.30 |31|
218 | -- | -- | --- | ----- | ---- | ----- |--|
219 | NN | RT | RA | imm | mask | 101 |1 |
222 idx = RA.x[i] << 2 | RA.y[i] << 1 | RA.z[i]
223 res = (imm & (1<<idx)) != 0
225 if mask[j]: RT[i+j*8] = res
227 another mode selection would be CRs not Ints.
229 | 0.5|6.8 | 9.11|12.14|15|16.23|24.27 | 28.30|31|
230 | -- | -- | --- | --- |- |-----|----- | -----|--|
231 | NN | BA | BB | BC |0 |imm | mask | 101 |0 |
234 if not mask[i] continue
235 idx = crregs[BA][i] << 2 |
238 crregs[BA][i] = (imm & (1<<idx)) != 0
242 based on RV bitmanip singlebit set, instruction format similar to shift
243 [[isa/fixedshift]]. bmext is actually covered already (shift-with-mask rldicl but only immediate version).
244 however bitmask-invert is not, and set/clr are not covered, although they can use the same Shift ALU.
246 bmext (RB) version is not the same as rldicl because bmext is a right shift by RC, where rldicl is a left rotate. for the immediate version this does not matter, so a bmexti is not required.
247 bmrev however there is no direct equivalent and consequently a bmrevi is required.
249 | 0.5|6.10|11.15|16.20|21.25| 26..30 |31| name |
250 | -- | -- | --- | --- | --- | ------- |--| ----- |
251 | NN | RT | RA | RB | RC | mode 010 |Rc| bm* |
252 | NN | RT | RA | RB | RC | 0 1 111 |Rc| bmrev |
256 uint_xlen_t bmset(RA, RB, sh)
258 int shamt = RB & (XLEN - 1);
260 return RA | (mask << shamt);
263 uint_xlen_t bmclr(RA, RB, sh)
265 int shamt = RB & (XLEN - 1);
267 return RA & ~(mask << shamt);
270 uint_xlen_t bminv(RA, RB, sh)
272 int shamt = RB & (XLEN - 1);
274 return RA ^ (mask << shamt);
277 uint_xlen_t bmext(RA, RB, sh)
279 int shamt = RB & (XLEN - 1);
281 return mask & (RA >> shamt);
285 bitmask extract with reverse. can be done by bitinverting all of RA and getting bits of RA from the opposite end.
289 rev[0:msb] = ra[msb:0];
292 uint_xlen_t bmextrev(RA, RB, sh)
294 int shamt = (RB & (XLEN - 1));
295 shamt = (XLEN-1)-shamt; # shift other end
296 bra = bitreverse(RA) # swap LSB-MSB
298 return mask & (bra >> shamt);
302 | 0.5|6.10|11.15|16.20|21.26| 27..30 |31| name |
303 | -- | -- | --- | --- | --- | ------- |--| ------ |
304 | NN | RT | RA | RB | sh | 0 111 |Rc| bmrevi |
313 uint64_t grev64(uint64_t RA, uint64_t RB)
317 if (shamt & 1) x = ((x & 0x5555555555555555LL) << 1) |
318 ((x & 0xAAAAAAAAAAAAAAAALL) >> 1);
319 if (shamt & 2) x = ((x & 0x3333333333333333LL) << 2) |
320 ((x & 0xCCCCCCCCCCCCCCCCLL) >> 2);
321 if (shamt & 4) x = ((x & 0x0F0F0F0F0F0F0F0FLL) << 4) |
322 ((x & 0xF0F0F0F0F0F0F0F0LL) >> 4);
323 if (shamt & 8) x = ((x & 0x00FF00FF00FF00FFLL) << 8) |
324 ((x & 0xFF00FF00FF00FF00LL) >> 8);
325 if (shamt & 16) x = ((x & 0x0000FFFF0000FFFFLL) << 16) |
326 ((x & 0xFFFF0000FFFF0000LL) >> 16);
327 if (shamt & 32) x = ((x & 0x00000000FFFFFFFFLL) << 32) |
328 ((x & 0xFFFFFFFF00000000LL) >> 32);
334 # shuffle / unshuffle
339 uint32_t shfl32(uint32_t RA, uint32_t RB)
343 if (shamt & 8) x = shuffle32_stage(x, 0x00ff0000, 0x0000ff00, 8);
344 if (shamt & 4) x = shuffle32_stage(x, 0x0f000f00, 0x00f000f0, 4);
345 if (shamt & 2) x = shuffle32_stage(x, 0x30303030, 0x0c0c0c0c, 2);
346 if (shamt & 1) x = shuffle32_stage(x, 0x44444444, 0x22222222, 1);
349 uint32_t unshfl32(uint32_t RA, uint32_t RB)
353 if (shamt & 1) x = shuffle32_stage(x, 0x44444444, 0x22222222, 1);
354 if (shamt & 2) x = shuffle32_stage(x, 0x30303030, 0x0c0c0c0c, 2);
355 if (shamt & 4) x = shuffle32_stage(x, 0x0f000f00, 0x00f000f0, 4);
356 if (shamt & 8) x = shuffle32_stage(x, 0x00ff0000, 0x0000ff00, 8);
360 uint64_t shuffle64_stage(uint64_t src, uint64_t maskL, uint64_t maskR, int N)
362 uint64_t x = src & ~(maskL | maskR);
363 x |= ((src << N) & maskL) | ((src >> N) & maskR);
366 uint64_t shfl64(uint64_t RA, uint64_t RB)
370 if (shamt & 16) x = shuffle64_stage(x, 0x0000ffff00000000LL,
371 0x00000000ffff0000LL, 16);
372 if (shamt & 8) x = shuffle64_stage(x, 0x00ff000000ff0000LL,
373 0x0000ff000000ff00LL, 8);
374 if (shamt & 4) x = shuffle64_stage(x, 0x0f000f000f000f00LL,
375 0x00f000f000f000f0LL, 4);
376 if (shamt & 2) x = shuffle64_stage(x, 0x3030303030303030LL,
377 0x0c0c0c0c0c0c0c0cLL, 2);
378 if (shamt & 1) x = shuffle64_stage(x, 0x4444444444444444LL,
379 0x2222222222222222LL, 1);
382 uint64_t unshfl64(uint64_t RA, uint64_t RB)
386 if (shamt & 1) x = shuffle64_stage(x, 0x4444444444444444LL,
387 0x2222222222222222LL, 1);
388 if (shamt & 2) x = shuffle64_stage(x, 0x3030303030303030LL,
389 0x0c0c0c0c0c0c0c0cLL, 2);
390 if (shamt & 4) x = shuffle64_stage(x, 0x0f000f000f000f00LL,
391 0x00f000f000f000f0LL, 4);
392 if (shamt & 8) x = shuffle64_stage(x, 0x00ff000000ff0000LL,
393 0x0000ff000000ff00LL, 8);
394 if (shamt & 16) x = shuffle64_stage(x, 0x0000ffff00000000LL,
395 0x00000000ffff0000LL, 16);
405 uint_xlen_t xperm(uint_xlen_t RA, uint_xlen_t RB, int sz_log2)
408 uint_xlen_t sz = 1LL << sz_log2;
409 uint_xlen_t mask = (1LL << sz) - 1;
410 for (int i = 0; i < XLEN; i += sz) {
411 uint_xlen_t pos = ((RB >> i) & mask) << sz_log2;
413 r |= ((RA >> pos) & mask) << i;
417 uint_xlen_t xperm_n (uint_xlen_t RA, uint_xlen_t RB)
418 { return xperm(RA, RB, 2); }
419 uint_xlen_t xperm_b (uint_xlen_t RA, uint_xlen_t RB)
420 { return xperm(RA, RB, 3); }
421 uint_xlen_t xperm_h (uint_xlen_t RA, uint_xlen_t RB)
422 { return xperm(RA, RB, 4); }
423 uint_xlen_t xperm_w (uint_xlen_t RA, uint_xlen_t RB)
424 { return xperm(RA, RB, 5); }
432 uint32_t gorc32(uint32_t RA, uint32_t RB)
436 if (shamt & 1) x |= ((x & 0x55555555) << 1) | ((x & 0xAAAAAAAA) >> 1);
437 if (shamt & 2) x |= ((x & 0x33333333) << 2) | ((x & 0xCCCCCCCC) >> 2);
438 if (shamt & 4) x |= ((x & 0x0F0F0F0F) << 4) | ((x & 0xF0F0F0F0) >> 4);
439 if (shamt & 8) x |= ((x & 0x00FF00FF) << 8) | ((x & 0xFF00FF00) >> 8);
440 if (shamt & 16) x |= ((x & 0x0000FFFF) << 16) | ((x & 0xFFFF0000) >> 16);
443 uint64_t gorc64(uint64_t RA, uint64_t RB)
447 if (shamt & 1) x |= ((x & 0x5555555555555555LL) << 1) |
448 ((x & 0xAAAAAAAAAAAAAAAALL) >> 1);
449 if (shamt & 2) x |= ((x & 0x3333333333333333LL) << 2) |
450 ((x & 0xCCCCCCCCCCCCCCCCLL) >> 2);
451 if (shamt & 4) x |= ((x & 0x0F0F0F0F0F0F0F0FLL) << 4) |
452 ((x & 0xF0F0F0F0F0F0F0F0LL) >> 4);
453 if (shamt & 8) x |= ((x & 0x00FF00FF00FF00FFLL) << 8) |
454 ((x & 0xFF00FF00FF00FF00LL) >> 8);
455 if (shamt & 16) x |= ((x & 0x0000FFFF0000FFFFLL) << 16) |
456 ((x & 0xFFFF0000FFFF0000LL) >> 16);
457 if (shamt & 32) x |= ((x & 0x00000000FFFFFFFFLL) << 32) |
458 ((x & 0xFFFFFFFF00000000LL) >> 32);
466 based on RV bitmanip, covered by ternary bitops
469 uint_xlen_t cmix(uint_xlen_t RA, uint_xlen_t RB, uint_xlen_t RC) {
470 return (RA & RB) | (RC & ~RB);
477 see https://en.wikipedia.org/wiki/CLMUL_instruction_set
480 uint_xlen_t clmul(uint_xlen_t RA, uint_xlen_t RB)
483 for (int i = 0; i < XLEN; i++)
488 uint_xlen_t clmulh(uint_xlen_t RA, uint_xlen_t RB)
491 for (int i = 1; i < XLEN; i++)
496 uint_xlen_t clmulr(uint_xlen_t RA, uint_xlen_t RB)
499 for (int i = 0; i < XLEN; i++)
501 x ^= RA >> (XLEN-i-1);
507 see <https://courses.csail.mit.edu/6.857/2016/files/ffield.py>
511 this requires 3 parameters and a "degree"
513 RT = GFMUL(RA, RB, gfdegree, modulo=RC)
515 realistically with the degree also needing to be an immediate it should be brought down to an overwrite version:
517 RS = GFMUL(RS, RA, gfdegree, modulo=RB)
518 RS = GFMUL(RS, RA, gfdegree=RC, modulo=RB)
520 | 0.5|6.10|11.15|16.20|21.25| 26..30 |31|
521 | -- | -- | --- | --- | --- | ------- |--|
522 | NN | RS | RA | RB | deg | 00 011 |Rc|
523 | NN | RS | RA | RB | RC | 11 011 |Rc|
525 where the SimpleV variant may override RS-as-src differently from RS-as-dest
530 from functools import reduce
532 # constants used in the multGF2 function
533 mask1 = mask2 = polyred = None
535 def setGF2(degree, irPoly):
536 """Define parameters of binary finite field GF(2^m)/g(x)
537 - degree: extension degree of binary field
538 - irPoly: coefficients of irreducible polynomial g(x)
541 """Convert an integer into a polynomial"""
542 return [(sInt >> i) & 1
543 for i in reversed(range(sInt.bit_length()))]
545 global mask1, mask2, polyred
546 mask1 = mask2 = 1 << degree
548 polyred = reduce(lambda x, y: (x << 1) + y, i2P(irPoly)[1:])
551 """Multiply two polynomials in GF(2^m)/g(x)"""
562 if __name__ == "__main__":
564 # Define binary field GF(2^3)/x^3 + x + 1
567 # Evaluate the product (x^2 + x + 1)(x^2 + 1)
568 print("{:02x}".format(multGF2(0b111, 0b101)))
570 # Define binary field GF(2^8)/x^8 + x^4 + x^3 + x + 1
571 # (used in the Advanced Encryption Standard-AES)
572 setGF2(8, 0b100011011)
574 # Evaluate the product (x^7)(x^7 + x + 1)
575 print("{:02x}".format(multGF2(0b10000000, 0b10000011)))
579 RS = GFADDI(RS, RA|0, gfdegree, modulo=RB)
580 RS = GFADD(RS, RA|0, gfdegree=RC, modulo=RB)
582 | 0.5|6.10|11.15|16.20|21.25| 26..30 |31| name |
583 | -- | -- | --- | --- | --- | ------- |--| ----- |
584 | NN | RS | RA | RB | deg | 0 1 011 |Rc| gfaddi |
585 | NN | RS | RA | RB | RC | 1 1 111 |Rc| gfadd |
587 GFMOD is a pseudo-op where RA=0
600 def gf_invert(a, mod=0x1B) :
615 a %= 256 # Emulating 8-bit overflow
616 g1 %= 256 # Emulating 8-bit overflow
618 j = gf_degree(a) - gf_degree(v)
626 uint64_t bmatflip(uint64_t RA)
634 uint64_t bmatxor(uint64_t RA, uint64_t RB)
637 uint64_t RBt = bmatflip(RB);
638 uint8_t u[8]; // rows of RA
639 uint8_t v[8]; // cols of RB
640 for (int i = 0; i < 8; i++) {
645 for (int i = 0; i < 64; i++) {
646 if (pcnt(u[i / 8] & v[i % 8]) & 1)
651 uint64_t bmator(uint64_t RA, uint64_t RB)
654 uint64_t RBt = bmatflip(RB);
655 uint8_t u[8]; // rows of RA
656 uint8_t v[8]; // cols of RB
657 for (int i = 0; i < 8; i++) {
662 for (int i = 0; i < 64; i++) {
663 if ((u[i / 8] & v[i % 8]) != 0)