7 | dest | src1 | subop | op |
8 | ---- | ---- | ----- | -------- |
9 | RT | RA | .. | bmatflip |
10 | RT | RA | size | crc32 |
11 | RT | RA | size | crc32c |
15 | dest | src1 | src2 | subop | op |
16 | ---- | ---- | ---- | ----- | -------- |
17 | RT | RA | RB | or | bmatflip |
18 | RT | RA | RB | xor | bmatflip |
19 | RT | RA | RB | bdep | dep/ext |
20 | RT | RA | RB | bext | dep/ext |
21 | RT | RA | RB | | grev |
22 | RT | RA | RB | | gorc |
23 | RT | RA | RB | shuf | shuffle |
24 | RT | RA | RB | unshuf| shuffle |
25 | RT | RA | RB | width | xperm |
26 | RT | RA | RB | type | clmul |
27 | RT | RA | RB | type | minmax |
39 | 0.5|6.10|11.15|16.20| 21.22 | 23 | 24..30 |31| name |
40 | -- | -- | --- | --- | ----- | -- | ------- |--| ---- |
41 | NN | RA | RB | | | 0 | 0000110 |Rc| rsvd |
42 | NN | RA | RB | RC | itype | 1 | 0000110 |Rc| xperm |
43 | NN | RA | RB | RC | itype | 0 | 0100110 |Rc| minmax |
44 | NN | RA | RB | | | 1 | 0100110 |Rc| rsvd |
45 | NN | RA | RB | sh | itype | SH | 1000110 |Rc| bmopsi |
46 | NN | RA | RB | | | | 1100110 |Rc| rsvd |
47 | NN | RA | RB | RC | itype | 0 | 0001110 |Rc| clmul |
48 | NN | RA | RB | sh | itype | 0 | 0101110 |Rc| clmulw |
49 | NN | RA | RB | RC | 00 | 0 | 0010110 |Rc| gorc |
50 | NN | RA | RB | sh | 00 | SH | 1010110 |Rc| gorci |
51 | NN | RA | RB | RC | 00 | 0 | 0110110 |Rc| gorcw |
52 | NN | RA | RB | sh | 00 | 0 | 1110110 |Rc| gorcwi |
53 | NN | RA | RB | RC | 00 | 1 | 1110110 |Rc| bmator |
54 | NN | RA | RB | RC | 01 | 0 | 0010110 |Rc| grev |
55 | NN | RA | RB | sh | 01 | SH | 1010110 |Rc| grevi |
56 | NN | RA | RB | RC | 01 | 0 | 0110110 |Rc| grevw |
57 | NN | RA | RB | sh | 01 | 0 | 1110110 |Rc| grevwi |
58 | NN | RA | RB | RC | 01 | 1 | 1110110 |Rc| bmatxor |
59 | NN | RA | RB | RC | 10 | 0 | 0010110 |Rc| shfl |
60 | NN | RA | RB | sh | 10 | SH | 1010110 |Rc| shfli |
61 | NN | RA | RB | RC | 10 | 0 | 0110110 |Rc| shflw |
62 | NN | RA | RB | RC | 10 | 0 | 1110110 |Rc| bdep |
63 | NN | RA | RB | RC | 10 | 1 | 1110110 |Rc| bext |
64 | NN | RA | RB | | 11 | | 1110110 |Rc| rsvd |
65 | NN | RA | RB | | | | NN11110 |Rc| rsvd |
69 similar to matrix permute in RV bitmanip, which has XOR and OR variants
73 b = VSR[VRB+32].dword[i].byte[k].bit[j]
74 VSR[VRT+32].dword[i].byte[j].bit[k] = b
78 vpdepd VRT,VRA,VRB, identical to RV bitmamip bdep
81 if VSR[VRB+32].dword[i].bit[63-m]=1 then do
82 result = VSR[VRA+32].dword[i].bit[63-k]
83 VSR[VRT+32].dword[i].bit[63-m] = result
89 uint_xlen_t bdep(uint_xlen_t RA, uint_xlen_t RB)
92 for (int i = 0, j = 0; i < XLEN; i++)
95 r |= uint_xlen_t(1) << i;
105 other way round: identical to RV bext
108 uint_xlen_t bext(uint_xlen_t RA, uint_xlen_t RB)
111 for (int i = 0, j = 0; i < XLEN; i++)
114 r |= uint_xlen_t(1) << j;
123 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).
125 signed/unsigned min/max gives more flexibility.
129 Similar to FPGA LUTs: for every bit perform a lookup into a table using an 8bit immediate, or in another register
131 | 0.5|6.10|11.15|16.20| 21..25| 26..30 |31|
132 | -- | -- | --- | --- | ----- | -------- |--|
133 | NN | RT | RA | RB | im0-4 | im5-7 00 |Rc|
136 idx = RT[i] << 2 | RA[i] << 1 | RB[i]
137 RT[i] = (imm & (1<<idx)) != 0
139 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.
141 a 4 operand variant which becomes more along the lines of an FPGA:
143 | 0.5|6.10|11.15|16.20|21.25| 26...30 |31|
144 | -- | -- | --- | --- | --- | -------- |--|
145 | NN | RT | RA | RB | RC | mode 001 |Rc|
148 idx = RT[i] << 2 | RA[i] << 1 | RB[i]
149 RT[i] = (RC & (1<<idx)) != 0
151 mode (2 bit) may be used to do inversion of ordering, similar to carryless mul,
154 also, another possible variant involving swizzle and vec4:
156 | 0.5|6.10|11.15| 16.23 |24.27 | 28.30 |31|
157 | -- | -- | --- | ----- | ---- | ----- |--|
158 | NN | RT | RA | imm | mask | 101 |1 |
161 idx = RA.x[i] << 2 | RA.y[i] << 1 | RA.z[i]
162 res = (imm & (1<<idx)) != 0
164 if mask[j]: RT[i+j*8] = res
166 another mode selection would be CRs not Ints.
168 | 0.5|6.8 | 9.11|12.14|15|16.23|24.27 | 28.30|31|
169 | -- | -- | --- | --- |- |-----|----- | -----|--|
170 | NN | BA | BB | BC |0 |imm | mask | 101 |0 |
173 if not mask[i] continue
174 idx = crregs[BA][i] << 2 |
177 crregs[BA][i] = (imm & (1<<idx)) != 0
181 based on RV bitmanip singlebit set, instruction format similar to shift
182 [[isa/fixedshift]]. bmext is actually covered already (shift-with-mask).
183 however bitmask-invert is not, and set/clr are not covered, although they can ise the same Shift ALU.
185 | 0.5|6.10|11.15|16.20|21.25| 26..30 |31|
186 | -- | -- | --- | --- | --- | ------- |--|
187 | NN | RT | RA | RB | RC | mode 010 |Rc|
190 uint_xlen_t bmset(RA, RB, sh)
192 int shamt = RB & (XLEN - 1);
194 return RA | (mask << shamt);
197 uint_xlen_t bmclr(RA, RB, sh)
199 int shamt = RB & (XLEN - 1);
201 return RA & ~(mask << shamt);
204 uint_xlen_t bminv(RA, RB, sh)
206 int shamt = RB & (XLEN - 1);
208 return RA ^ (mask << shamt);
211 uint_xlen_t bmext(RA, RB, sh)
213 int shamt = RB & (XLEN - 1);
215 return mask & (RA >> shamt);
224 uint64_t grev64(uint64_t RA, uint64_t RB)
228 if (shamt & 1) x = ((x & 0x5555555555555555LL) << 1) |
229 ((x & 0xAAAAAAAAAAAAAAAALL) >> 1);
230 if (shamt & 2) x = ((x & 0x3333333333333333LL) << 2) |
231 ((x & 0xCCCCCCCCCCCCCCCCLL) >> 2);
232 if (shamt & 4) x = ((x & 0x0F0F0F0F0F0F0F0FLL) << 4) |
233 ((x & 0xF0F0F0F0F0F0F0F0LL) >> 4);
234 if (shamt & 8) x = ((x & 0x00FF00FF00FF00FFLL) << 8) |
235 ((x & 0xFF00FF00FF00FF00LL) >> 8);
236 if (shamt & 16) x = ((x & 0x0000FFFF0000FFFFLL) << 16) |
237 ((x & 0xFFFF0000FFFF0000LL) >> 16);
238 if (shamt & 32) x = ((x & 0x00000000FFFFFFFFLL) << 32) |
239 ((x & 0xFFFFFFFF00000000LL) >> 32);
245 # shuffle / unshuffle
250 uint32_t shfl32(uint32_t RA, uint32_t RB)
254 if (shamt & 8) x = shuffle32_stage(x, 0x00ff0000, 0x0000ff00, 8);
255 if (shamt & 4) x = shuffle32_stage(x, 0x0f000f00, 0x00f000f0, 4);
256 if (shamt & 2) x = shuffle32_stage(x, 0x30303030, 0x0c0c0c0c, 2);
257 if (shamt & 1) x = shuffle32_stage(x, 0x44444444, 0x22222222, 1);
260 uint32_t unshfl32(uint32_t RA, uint32_t RB)
264 if (shamt & 1) x = shuffle32_stage(x, 0x44444444, 0x22222222, 1);
265 if (shamt & 2) x = shuffle32_stage(x, 0x30303030, 0x0c0c0c0c, 2);
266 if (shamt & 4) x = shuffle32_stage(x, 0x0f000f00, 0x00f000f0, 4);
267 if (shamt & 8) x = shuffle32_stage(x, 0x00ff0000, 0x0000ff00, 8);
271 uint64_t shuffle64_stage(uint64_t src, uint64_t maskL, uint64_t maskR, int N)
273 uint64_t x = src & ~(maskL | maskR);
274 x |= ((src << N) & maskL) | ((src >> N) & maskR);
277 uint64_t shfl64(uint64_t RA, uint64_t RB)
281 if (shamt & 16) x = shuffle64_stage(x, 0x0000ffff00000000LL,
282 0x00000000ffff0000LL, 16);
283 if (shamt & 8) x = shuffle64_stage(x, 0x00ff000000ff0000LL,
284 0x0000ff000000ff00LL, 8);
285 if (shamt & 4) x = shuffle64_stage(x, 0x0f000f000f000f00LL,
286 0x00f000f000f000f0LL, 4);
287 if (shamt & 2) x = shuffle64_stage(x, 0x3030303030303030LL,
288 0x0c0c0c0c0c0c0c0cLL, 2);
289 if (shamt & 1) x = shuffle64_stage(x, 0x4444444444444444LL,
290 0x2222222222222222LL, 1);
293 uint64_t unshfl64(uint64_t RA, uint64_t RB)
297 if (shamt & 1) x = shuffle64_stage(x, 0x4444444444444444LL,
298 0x2222222222222222LL, 1);
299 if (shamt & 2) x = shuffle64_stage(x, 0x3030303030303030LL,
300 0x0c0c0c0c0c0c0c0cLL, 2);
301 if (shamt & 4) x = shuffle64_stage(x, 0x0f000f000f000f00LL,
302 0x00f000f000f000f0LL, 4);
303 if (shamt & 8) x = shuffle64_stage(x, 0x00ff000000ff0000LL,
304 0x0000ff000000ff00LL, 8);
305 if (shamt & 16) x = shuffle64_stage(x, 0x0000ffff00000000LL,
306 0x00000000ffff0000LL, 16);
316 uint_xlen_t xperm(uint_xlen_t RA, uint_xlen_t RB, int sz_log2)
319 uint_xlen_t sz = 1LL << sz_log2;
320 uint_xlen_t mask = (1LL << sz) - 1;
321 for (int i = 0; i < XLEN; i += sz) {
322 uint_xlen_t pos = ((RB >> i) & mask) << sz_log2;
324 r |= ((RA >> pos) & mask) << i;
328 uint_xlen_t xperm_n (uint_xlen_t RA, uint_xlen_t RB)
329 { return xperm(RA, RB, 2); }
330 uint_xlen_t xperm_b (uint_xlen_t RA, uint_xlen_t RB)
331 { return xperm(RA, RB, 3); }
332 uint_xlen_t xperm_h (uint_xlen_t RA, uint_xlen_t RB)
333 { return xperm(RA, RB, 4); }
334 uint_xlen_t xperm_w (uint_xlen_t RA, uint_xlen_t RB)
335 { return xperm(RA, RB, 5); }
343 uint32_t gorc32(uint32_t RA, uint32_t RB)
347 if (shamt & 1) x |= ((x & 0x55555555) << 1) | ((x & 0xAAAAAAAA) >> 1);
348 if (shamt & 2) x |= ((x & 0x33333333) << 2) | ((x & 0xCCCCCCCC) >> 2);
349 if (shamt & 4) x |= ((x & 0x0F0F0F0F) << 4) | ((x & 0xF0F0F0F0) >> 4);
350 if (shamt & 8) x |= ((x & 0x00FF00FF) << 8) | ((x & 0xFF00FF00) >> 8);
351 if (shamt & 16) x |= ((x & 0x0000FFFF) << 16) | ((x & 0xFFFF0000) >> 16);
354 uint64_t gorc64(uint64_t RA, uint64_t RB)
358 if (shamt & 1) x |= ((x & 0x5555555555555555LL) << 1) |
359 ((x & 0xAAAAAAAAAAAAAAAALL) >> 1);
360 if (shamt & 2) x |= ((x & 0x3333333333333333LL) << 2) |
361 ((x & 0xCCCCCCCCCCCCCCCCLL) >> 2);
362 if (shamt & 4) x |= ((x & 0x0F0F0F0F0F0F0F0FLL) << 4) |
363 ((x & 0xF0F0F0F0F0F0F0F0LL) >> 4);
364 if (shamt & 8) x |= ((x & 0x00FF00FF00FF00FFLL) << 8) |
365 ((x & 0xFF00FF00FF00FF00LL) >> 8);
366 if (shamt & 16) x |= ((x & 0x0000FFFF0000FFFFLL) << 16) |
367 ((x & 0xFFFF0000FFFF0000LL) >> 16);
368 if (shamt & 32) x |= ((x & 0x00000000FFFFFFFFLL) << 32) |
369 ((x & 0xFFFFFFFF00000000LL) >> 32);
377 based on RV bitmanip, covered by ternary bitops
380 uint_xlen_t cmix(uint_xlen_t RA, uint_xlen_t RB, uint_xlen_t RC) {
381 return (RA & RB) | (RC & ~RB);
388 see https://en.wikipedia.org/wiki/CLMUL_instruction_set
391 uint_xlen_t clmul(uint_xlen_t RA, uint_xlen_t RB)
394 for (int i = 0; i < XLEN; i++)
399 uint_xlen_t clmulh(uint_xlen_t RA, uint_xlen_t RB)
402 for (int i = 1; i < XLEN; i++)
407 uint_xlen_t clmulr(uint_xlen_t RA, uint_xlen_t RB)
410 for (int i = 0; i < XLEN; i++)
412 x ^= RA >> (XLEN-i-1);
420 this requires 3 parameters and a "degree"
422 RT = GFMUL(RA, RB, gfdegree, modulo=RC)
424 realistically with the degree also needing to be an immediate it should be brought down to an overwrite version:
426 RS = GFMUL(RS, RA, gfdegree, modulo=RB)
428 | 0.5|6.10|11.15|16.20|21.25| 26..30 |31|
429 | -- | -- | --- | --- | --- | ------- |--|
430 | NN | RS | RA | RB | deg | 00 011 |Rc|
432 where the SimpleV variant may override RS-as-src differently from RS-as-dest
437 from functools import reduce
439 # constants used in the multGF2 function
440 mask1 = mask2 = polyred = None
442 def setGF2(degree, irPoly):
443 """Define parameters of binary finite field GF(2^m)/g(x)
444 - degree: extension degree of binary field
445 - irPoly: coefficients of irreducible polynomial g(x)
448 """Convert an integer into a polynomial"""
449 return [(sInt >> i) & 1
450 for i in reversed(range(sInt.bit_length()))]
452 global mask1, mask2, polyred
453 mask1 = mask2 = 1 << degree
455 polyred = reduce(lambda x, y: (x << 1) + y, i2P(irPoly)[1:])
458 """Multiply two polynomials in GF(2^m)/g(x)"""
469 if __name__ == "__main__":
471 # Define binary field GF(2^3)/x^3 + x + 1
474 # Evaluate the product (x^2 + x + 1)(x^2 + 1)
475 print("{:02x}".format(multGF2(0b111, 0b101)))
477 # Define binary field GF(2^8)/x^8 + x^4 + x^3 + x + 1
478 # (used in the Advanced Encryption Standard-AES)
479 setGF2(8, 0b100011011)
481 # Evaluate the product (x^7)(x^7 + x + 1)
482 print("{:02x}".format(multGF2(0b10000000, 0b10000011)))
486 RS = GFADD(RS, RA|0, gfdegree, modulo=RB)
488 | 0.5|6.10|11.15|16.20|21.25| 26..30 |31|
489 | -- | -- | --- | --- | --- | ------- |--|
490 | NN | RS | RA | RB | deg | 01 011 |Rc|
503 def gf_invert(a, mod=0x1B) :
518 a %= 256 # Emulating 8-bit overflow
519 g1 %= 256 # Emulating 8-bit overflow
521 j = gf_degree(a) - gf_degree(v)
528 * <https://stackoverflow.com/questions/21171733/calculating-constants-for-crc32-using-pclmulqdq>
529 * <https://en.wikipedia.org/wiki/Cyclic_redundancy_check>
532 uint_xlen_t crc32(uint_xlen_t x, int nbits)
534 for (int i = 0; i < nbits; i++)
535 x = (x >> 1) ^ (0xEDB88320 & ~((x&1)-1));
538 uint_xlen_t crc32c(uint_xlen_t x, int nbits)
540 for (int i = 0; i < nbits; i++)
541 x = (x >> 1) ^ (0x82F63B78 & ~((x&1)-1));
544 uint_xlen_t crc32_b(uint_xlen_t RA) { return crc32(RA, 8); }
545 uint_xlen_t crc32_h(uint_xlen_t RA) { return crc32(RA, 16); }
546 uint_xlen_t crc32_w(uint_xlen_t RA) { return crc32(RA, 32); }
547 uint_xlen_t crc32c_b(uint_xlen_t RA) { return crc32c(RA, 8); }
548 uint_xlen_t crc32c_h(uint_xlen_t RA) { return crc32c(RA, 16); }
549 uint_xlen_t crc32c_w(uint_xlen_t RA) { return crc32c(RA, 32); }
551 uint_xlen_t crc32_d (uint_xlen_t RA) { return crc32 (RA, 64); }
552 uint_xlen_t crc32c_d(uint_xlen_t RA) { return crc32c(RA, 64); }
559 uint64_t bmatflip(uint64_t RA)
567 uint64_t bmatxor(uint64_t RA, uint64_t RB)
570 uint64_t RBt = bmatflip(RB);
571 uint8_t u[8]; // rows of RA
572 uint8_t v[8]; // cols of RB
573 for (int i = 0; i < 8; i++) {
578 for (int i = 0; i < 64; i++) {
579 if (pcnt(u[i / 8] & v[i % 8]) & 1)
584 uint64_t bmator(uint64_t RA, uint64_t RB)
587 uint64_t RBt = bmatflip(RB);
588 uint8_t u[8]; // rows of RA
589 uint8_t v[8]; // cols of RB
590 for (int i = 0; i < 8; i++) {
595 for (int i = 0; i < 64; i++) {
596 if ((u[i / 8] & v[i % 8]) != 0)