(no commit message)
[libreriscv.git] / openpower / sv / bitmanip.mdwn
index 0a9e6df0c1112a1dcd40c0b727799498ce9a3c75..fb95b2dbfc67cc6a709b8f2a0e433bdc1a67fa18 100644 (file)
@@ -33,19 +33,28 @@ Useful resource:
 
 # summary
 
-minor opcode allocation
-
-    |  28.30 |31| name      |
-    | ------ |--| --------- |
-    |  -00   |0 | ternlogi  |
-    |  -00   |1 | grevlog   |
-    |  -01   |  | grevlogi  |
-    |  010   |Rc| bitmask   |
-    |  011   |0 | gfbmadd*   |
-    |  011   |1 | clmadd*   |
-    |  110   |Rc| 1/2-op    |
-    |  111   |1 | ternlogv  |
-    |  111   |0 | ternlogcr |
+two major opcodes are needed
+
+ternlog has its own major opcode
+
+|  29.30 |31| name      |
+| ------ |--| --------- |
+|   0  0   |Rc| ternlogi  |
+|   0  1   |sz| ternlogv  |
+|   1 iv   |  | grevlogi |
+
+2nd major opcode for other bitmanip: minor opcode allocation
+
+|  28.30 |31| name      |
+| ------ |--| --------- |
+|  -00   |0 | xpermi    |
+|  -00   |1 | grevlog   |
+|  -01   |  | crternlog  |
+|  010   |Rc| bitmask   |
+|  011   |  | gf/cl madd*  |
+|  110   |Rc| 1/2-op    |
+|  111   |  | bmrevi   |
+
 
 1-op and variants
 
@@ -73,30 +82,29 @@ minor opcode allocation
 3 ops 
 
 * grevlog
-* ternlog bitops
 * GF mul-add
 * bitmask-reverse
 
 TODO: convert all instructions to use RT and not RS
 
+| 0.5|6.8 | 9.11|12.14|15.17|18.20|21.28 | 29.30|31|name|
+| -- | -- | --- | --- | --- |-----|----- | -----|--|----|
+| NN | BT | BA  | BB  | BC  |m0-2 | imm  |  10  |m3|crternlog|
+
 | 0.5|6.10|11.15|16.20 |21..25   | 26....30  |31| name |
 | -- | -- | --- | ---  | -----   | --------  |--| ------ |
-| NN | RT | RA  | RB   | im0-4   | im5-7  00 |0 | ternlogi |
+| NN | RT | RA  |itype/| im0-4   | im5-7  00 |0 | xpermi  |
 | NN | RT | RA  | RB   | im0-4   | im5-7  00 |1 | grevlog |
-| NN | RT | RA  | s0-4 | im0-4   | im5-7  01 |s5| grevlogi |
+| NN |    |     |      |         | .....  01 |0 | crternlog |
+| NN | RT | RA  | RB   | RC      | mode  010 |Rc| bitmask* |
 | NN | RS | RA  | RB   | RC      | 00    011 |0 | gfbmadd |
-| NN | RS | RA  | RB   | RC      | 10    011 |0 | gfbmaddsub |
-| NN | RS | RA  | RB   | RC      | 00    011 |1 | clmadd |
-| NN | RS | RA  | RB   | RC      | 10    011 |1 | clmaddsub |
-| NN | RT | RA  | RB   | sh0-4   | sh5 1 011 |Rc| bmrevi |
-
-| 0.5|6.10|11.15| 16.23 |24.27 | 28.30 |31| name |
-| -- | -- | --- | ----- | ---- | ----- |--| ------ |
-| NN | RT | RA  | imm   | mask | 111   |1 | ternlogv |
-
-| 0.5|6.8 | 9.11|12.14|15|16.23|24.27 | 28.30|31| name |
-| -- | -- | --- | --- |- |-----|----- | -----|--| -------|
-| NN | BA | BB  | BC  |0 |imm  | mask | 111  |0 | ternlogcr |
+| NN | RS | RA  | RB   | RC      | 00    011 |1 | gfbmaddsub |
+| NN | RS | RA  | RB   | RC      | 01    011 |0 | clmadd |
+| NN | RS | RA  | RB   | RC      | 01    011 |1 | clmaddsub |
+| NN | RS | RA  | RB   | RC      | 10    011 |0 | gfpmadd |
+| NN | RS | RA  | RB   | RC      | 10    011 |1 | gfpmaddsub |
+| NN | RS | RA  | RB   | RC      | 11    011 |  | rsvd |
+| NN | RT | RA  | RB   | sh0-4   | sh5 1 111 |Rc| bmrevi |
 
 ops (note that av avg and abs as well as vec scalar mask
 are included here)
@@ -106,15 +114,9 @@ double check that instructions didn't need 3 inputs.
 
 | 0.5|6.10|11.15|16.20| 21 | 22.23 | 24....30 |31| name |
 | -- | -- | --- | --- | -- | ----- | -------- |--| ---- |
-| NN | RA | RB  |     | 0  |       | 0000 110 |Rc| rsvd   |
-| NN | RA | RB  | RC  | 1  | itype | 0000 110 |Rc| xperm |
-| NN | RA | RB  | RC  | 0  | itype | 0100 110 |Rc| minmax |
-| NN | RA | RB  | RC  | 1  |   00  | 0100 110 |Rc| av avgadd |
-| NN | RA | RB  | RC  | 1  |   01  | 0100 110 |Rc| av abs |
-| NN | RA | RB  |     | 1  |   10  | 0100 110 |Rc| rsvd |
-| NN | RA | RB  |     | 1  |   11  | 0100 110 |Rc| rsvd |
-| NN | RA | RB  | sh  | SH | itype | 1000 110 |Rc| bmopsi |
-| NN | RA | RB  |     |    |       | 1100 110 |Rc| rsvd |
+| NN | RS | me  | sh  | SH | ME 0  | nn00 110 |Rc| bmopsi |
+| NN | RS | RB  | sh  | SH | /   0 | nn00 110 |Rc| bmopsi |
+| NN | RT | RA  | RB  |    |       | 1100 110 |Rc| srsvd |
 | NN | RT | RA  | RB  | 1  |  00   | 0001 110 |Rc| cldiv |
 | NN | RT | RA  | RB  | 1  |  01   | 0001 110 |Rc| clmod |
 | NN | RT | RA  | RB  | 1  |  10   | 0001 110 |Rc|       |
@@ -123,7 +125,12 @@ double check that instructions didn't need 3 inputs.
 | NN | RA | RB  | RC  | 0  |   01  | 0001 110 |Rc| vec sofm |
 | NN | RA | RB  | RC  | 0  |   10  | 0001 110 |Rc| vec sifm |
 | NN | RA | RB  | RC  | 0  |   11  | 0001 110 |Rc| vec cprop |
-| NN | RA | RB  |     | 0  |       | 0101 110 |Rc| rsvd |
+| NN | RT | RA  | RB  | 1  | itype | 0101 110 |Rc| xperm |
+| NN | RA | RB  | RC  | 0  | itype | 0101 110 |Rc| minmax |
+| NN | RA | RB  | RC  | 1  |   00  | 0101 110 |Rc| av avgadd |
+| NN | RA | RB  | RC  | 1  |   01  | 0101 110 |Rc| av abs |
+| NN | RA | RB  |     | 1  |   10  | 0101 110 |Rc| rsvd |
+| NN | RA | RB  |     | 1  |   11  | 0101 110 |Rc| rsvd |
 | NN | RA | RB  | RC  | 0  | 00    | 0010 110 |Rc| gorc |
 | NN | RA | RB  | sh  | SH | 00    | 1010 110 |Rc| gorci |
 | NN | RA | RB  | RC  | 0  | 00    | 0110 110 |Rc| gorcw |
@@ -135,45 +142,11 @@ double check that instructions didn't need 3 inputs.
 | NN | RA | RB  | RC  | 0  | 01    | 0110 110 |Rc| grevw |
 | NN | RA | RB  | sh  | 0  | 01    | 1110 110 |Rc| grevwi |
 | NN | RA | RB  | RC  | 1  | 01    | 1110 110 |Rc| bmatxor   |
-| NN | RA | RB  | RC  | 0  | 10    | 0010 110 |Rc| shfl |
-| NN | RA | RB  | sh  | SH | 10    | 1010 110 |Rc| shfli |
-| NN | RA | RB  | RC  | 0  | 10    | 0110 110 |Rc| shflw |
-| NN | RA | RB  | RC  |    | 10    | 1110 110 |Rc| rsvd    |
+| NN | RA | RB  | RC  |    | 10    | --10 110 |Rc| rsvd  |
 | NN | RA | RB  | RC  | 0  | 11    | 1110 110 |Rc| clmulr  |
 | NN | RA | RB  | RC  | 1  | 11    | 1110 110 |Rc| clmulh  |
 | NN |    |     |     |    |       | --11 110 |Rc| setvl  |
 
-# bit to byte permute
-
-similar to matrix permute in RV bitmanip, which has XOR and OR variants
-
-    do j = 0 to 7
-      do k = 0 to 7
-         b = VSR[VRB+32].dword[i].byte[k].bit[j]
-         VSR[VRT+32].dword[i].byte[j].bit[k] = b
-
-# int min/max
-
-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).
-
-signed/unsigned min/max gives more flexibility.
-
-```
-uint_xlen_t min(uint_xlen_t rs1, uint_xlen_t rs2)
-{ return (int_xlen_t)rs1 < (int_xlen_t)rs2 ? rs1 : rs2;
-}
-uint_xlen_t max(uint_xlen_t rs1, uint_xlen_t rs2)
-{ return (int_xlen_t)rs1 > (int_xlen_t)rs2 ? rs1 : rs2;
-}
-uint_xlen_t minu(uint_xlen_t rs1, uint_xlen_t rs2)
-{ return rs1 < rs2 ? rs1 : rs2;
-}
-uint_xlen_t maxu(uint_xlen_t rs1, uint_xlen_t rs2)
-{ return rs1 > rs2 ? rs1 : rs2;
-}
-```
-
-
 # ternlog bitops
 
 Similar to FPGA LUTs: for every bit perform a lookup into a table using an 8bit immediate, or in another register.
@@ -182,9 +155,9 @@ Like the x86 AVX512F [vpternlogd/vpternlogq](https://www.felixcloutier.com/x86/v
 
 ## ternlogi
 
-| 0.5|6.10|11.15|16.20| 21..25| 26..30   |31|
-| -- | -- | --- | --- | ----- | -------- |--|
-| NN | RT | RA  | RB  | im0-4 | im5-7 00 |0 |
+| 0.5|6.10|11.15|16.20| 21..28|29.30|31|
+| -- | -- | --- | --- | ----- | --- |--|
+| NN | RT | RA  | RB  | im0-7 |  00 |Rc|
 
     lut3(imm, a, b, c):
         idx = c << 2 | b << 1 | a
@@ -193,51 +166,73 @@ Like the x86 AVX512F [vpternlogd/vpternlogq](https://www.felixcloutier.com/x86/v
     for i in range(64): 
         RT[i] = lut3(imm, RB[i], RA[i], RT[i]) 
 
-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.
-
-## ternlog
-
-a 4 operand variant which becomes more along the lines of an FPGA:
+## ternlogv
 
-| 0.5|6.10|11.15|16.20|21.25| 26...30  |31|
-| -- | -- | --- | --- | --- | -------- |--|
-| NN | RT | RA  | RB  | RC  | mode 100 |1 |
+also, another possible variant involving swizzle-like selection
+and masking, this only requires 3 64 bit registers (RA, RS, RB) and
+only 16 LUT3s.
+
+Note however that unless XLEN matches sz, this instruction
+is a Read-Modify-Write: RS must be read as a second operand
+and all unmodified bits preserved.  SVP64 may provide limited
+alternative destination for RS from RS-as-source, but again
+all unmodified bits must still be copied.
+
+| 0.5|6.10|11.15|16.20|21.28 | 29.30 |31|
+| -- | -- | --- | --- | ---- | ----- |--|
+| NN | RS | RA  | RB  |idx0-3|  01   |sz|
+
+    SZ = (1+sz) * 8 # 8 or 16
+    raoff = MIN(XLEN, idx0 * SZ)
+    rboff = MIN(XLEN, idx1 * SZ)
+    rcoff = MIN(XLEN, idx2 * SZ)
+    rsoff = MIN(XLEN, idx3 * SZ)
+    imm = RB[0:8]
+    for i in range(MIN(XLEN, SZ)):
+        ra = RA[raoff:+i]
+        rb = RA[rboff+i]
+        rc = RA[rcoff+i]
+        res = lut3(imm, ra, rb, rc)
+        RS[rsoff+i] = res
 
-    for i in range(64):
-        idx = RT[i] << 2 | RA[i] << 1 | RB[i]
-        RT[i] = (RC & (1<<idx)) != 0
+## ternlogcr
 
-mode (2 bit) may be used to do inversion of ordering, similar to carryless mul,
-3 modes.
+another mode selection would be CRs not Ints. 
 
-## ternlogv
+| 0.5|6.8 | 9.11|12.14|15.17|18.20|21.28 | 29.30|31|
+| -- | -- | --- | --- | --- |-----|----- | -----|--|
+| NN | BT | BA  | BB  | BC  |m0-2 | imm  |  10  |m3|
 
-also, another possible variant involving swizzle and vec4:
+    mask = m0-3,m4
+    for i in range(4):
+        if not mask[i] continue
+        crregs[BT][i] = lut3(imm,
+                             crregs[BA][i],
+                             crregs[BB][i],
+                             crregs[BC][i])
 
-| 0.5|6.10|11.15| 16.23 |24.27 | 28.30 |31|
-| -- | -- | --- | ----- | ---- | ----- |--|
-| NN | RT | RA  | imm   | mask | 111   |1 |
 
-    for i in range(8):
-        idx = RA.x[i] << 2 | RA.y[i] << 1 | RA.z[i]   
-        res = (imm & (1<<idx)) != 0
-        for j in range(3):
-             if mask[j]: RT[i+j*8] = res
+# int min/max
 
-## ternlogcr
+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).
 
-another mode selection would be CRs not Ints. 
+signed/unsigned min/max gives more flexibility.
 
-| 0.5|6.8 | 9.11|12.14|15|16.23|24.27 | 28.30|31|
-| -- | -- | --- | --- |- |-----|----- | -----|--|
-| NN | BA | BB  | BC  |0 |imm  | mask | 111  |0 |
+```
+uint_xlen_t min(uint_xlen_t rs1, uint_xlen_t rs2)
+{ return (int_xlen_t)rs1 < (int_xlen_t)rs2 ? rs1 : rs2;
+}
+uint_xlen_t max(uint_xlen_t rs1, uint_xlen_t rs2)
+{ return (int_xlen_t)rs1 > (int_xlen_t)rs2 ? rs1 : rs2;
+}
+uint_xlen_t minu(uint_xlen_t rs1, uint_xlen_t rs2)
+{ return rs1 < rs2 ? rs1 : rs2;
+}
+uint_xlen_t maxu(uint_xlen_t rs1, uint_xlen_t rs2)
+{ return rs1 > rs2 ? rs1 : rs2;
+}
+```
 
-    for i in range(4):
-        if not mask[i] continue
-        idx = crregs[BA][i] << 2 |
-              crregs[BB][i] << 1 |
-              crregs[BC][i]
-        crregs[BA][i] = (imm & (1<<idx)) != 0
 
 ## cmix
 
@@ -265,51 +260,71 @@ bmset(RA=0, RB=0, RC=mask) will produce a run of ones of length "mask" in a sing
 
 | 0.5|6.10|11.15|16.20|21.25| 26..30  |31| name  |
 | -- | -- | --- | --- | --- | ------- |--| ----- |
-| NN | RT | RA  | RB  | RC  | mode 010 |Rc| bm*   |
+| NN | RS | RA  | RB  | RC  | mode 010 |Rc| bm*   |
 
+Immediate-variant is an overwrite form:
+
+| 0.5|6.10|11.15|16.20| 21 | 22.23 | 24....30 |31| name |
+| -- | -- | --- | --- | -- | ----- | -------- |--| ---- |
+| NN | RS | RB  | sh  | SH | itype | 1000 110 |Rc| bm*i |
 
 ```
-uint_xlen_t bmset(RA, RB, sh)
+def MASK(x, y):
+     if x < y:
+         x = x+1
+         mask_a = ((1 << x) - 1) & ((1 << 64) - 1)
+         mask_b = ((1 << y) - 1) & ((1 << 64) - 1)
+     elif x == y:
+         return 1 << x
+     else:
+         x = x+1
+         mask_a = ((1 << x) - 1) & ((1 << 64) - 1)
+         mask_b = (~((1 << y) - 1)) & ((1 << 64) - 1)
+     return mask_a ^ mask_b
+
+
+uint_xlen_t bmset(RS, RB, sh)
 {
     int shamt = RB & (XLEN - 1);
-    mask = (2<<sh)-1;
-    return RA | (mask << shamt);
+    return RS | MASK(shamt, sh)
 }
 
-uint_xlen_t bmclr(RA, RB, sh)
+uint_xlen_t bmclr(RS, RB, sh)
 {
     int shamt = RB & (XLEN - 1);
-    mask = (2<<sh)-1;
-    return RA & ~(mask << shamt);
+    return RS & ~MASK(shamt, sh)
 }
 
-uint_xlen_t bminv(RA, RB, sh)
+uint_xlen_t bminv(RS, RB, sh)
 {
     int shamt = RB & (XLEN - 1);
-    mask = (2<<sh)-1;
-    return RA ^ (mask << shamt);
+    return RS ^ MASK(shamt, sh)
 }
 
-uint_xlen_t bmext(RA, RB, sh)
+uint_xlen_t bmext(RS, RB, sh)
 {
     int shamt = RB & (XLEN - 1);
     mask = (2<<sh)-1;
-    return mask & (RA >> shamt);
+    return mask & (RS >> shamt);
 }
 ```
 
-bitmask extract with reverse.  can be done by bitinverting all of RA and getting bits of RA from the opposite end.
+bitmask extract with reverse.  can be done by bit-order-inverting all of RB and getting bits of RB from the opposite end.
+
+when RA is zero, no shift occurs. this makes bmextrev useful for
+simply reversing all bits of a register.
 
 ```
-msb = rb[5:0];
-rev[0:msb] = ra[msb:0];
+msb = ra[5:0];
+rev[0:msb] = rb[msb:0];
 rt = ZE(rev[msb:0]);
 
 uint_xlen_t bmextrev(RA, RB, sh)
 {
-    int shamt = (RB & (XLEN - 1));
+    int shamt = XLEN-1;
+    if (RA != 0) shamt = (GPR(RA) & (XLEN - 1));
     shamt = (XLEN-1)-shamt;  # shift other end
-    bra = bitreverse(RA)     # swap LSB-MSB
+    bra = bitreverse(RB)     # swap LSB-MSB
     mask = (2<<sh)-1;
     return mask & (bra >> shamt);
 }
@@ -323,16 +338,56 @@ uint_xlen_t bmextrev(RA, RB, sh)
 # grevlut
 
 generalised reverse combined with a pair of LUT2s and allowing
-zero when RA=0 provides a wide range of instructions
+a constant `0b0101...0101` when RA=0, and an option to invert
+(including when RA=0, giving a constant 0b1010...1010 as the
+initial value) provides a wide range of instructions
 and a means to set regular 64 bit patterns in one
 32 bit instruction.
 
 the two LUT2s are applied left-half (when not swapping)
 and right-half (when swapping) so as to allow a wider
-range of options
+range of options.
 
 <img src="/openpower/sv/grevlut2x2.jpg" width=700 />
 
+* A value of `0b11001010` for the immediate provides
+the functionality of a standard "grev".  
+* `0b11101110` provides gorc
+
+grevlut should be arranged so as to produce the constants
+needed to put into bext (bitextract) so as in turn to
+be able to emulate x86 pmovmask instructions <https://www.felixcloutier.com/x86/pmovmskb>.
+This only requires 2 instructions (grevlut, bext).
+
+Note that if the mask is required to be placed
+directly into CR Fields (for use as CR Predicate
+masks rather than a integer mask) then sv.ori
+may be used instead, bearing in mind that sv.ori
+is a 64-bit instruction, and `VL` must have been
+set to the required length:
+
+    sv.ori./elwid=8 r10.v, r10.v, 0
+
+The following settings provide the required mask constants:
+
+| RA       | RB      | imm        | iv | result        |
+| -------  | ------- | ---------- | -- | ----------    |
+| 0x555..  | 0b10    | 0b01101100 | 0  | 0x111111...   |
+| 0x555..  | 0b110   | 0b01101100 | 0  | 0x010101...   |
+| 0x555..  | 0b1110  | 0b01101100 | 0  | 0x00010001...   |
+| 0x555..  | 0b10    | 0b11000110 | 1  | 0x88888...   |
+| 0x555..  | 0b110   | 0b11000110 | 1  | 0x808080...   |
+| 0x555..  | 0b1110  | 0b11000110 | 1  | 0x80008000...   |
+
+Better diagram showing the correct ordering of shamt (RB).  A LUT2
+is applied to all locations marked in red using the first 4
+bits of the immediate, and a separate LUT2 applied to all
+locations in green using the upper 4 bits of the immediate.
+
+<img src="/openpower/sv/grevlut.png" width=700 />
+
+demo code [[openpower/sv/grevlut.py]]
+
 ```
 lut2(imm, a, b):
     idx = b << 1 | a
@@ -347,9 +402,11 @@ dorow(imm8, step_i, chunksize):
         step_o[j] = lut2(imm, step_i[j], step_i[j ^ chunk_size])
     return step_o
 
-uint64_t grevlut64(uint64_t RA, uint64_t RB, uint8 imm)
+uint64_t grevlut64(uint64_t RA, uint64_t RB, uint8 imm, bool iv)
 {
-    uint64_t x = RA;
+    uint64_t x = 0x5555_5555_5555_5555;
+    if (RA != 0) x = GPR(RA);
+    if (iv) x = ~x;
     int shamt = RB & 63;
     for i in 0 to 6
         step = 1<<i
@@ -359,6 +416,12 @@ uint64_t grevlut64(uint64_t RA, uint64_t RB, uint8 imm)
 
 ```
 
+| 0.5|6.10|11.15|16.20 |21..25   | 26....30    |31| name |
+| -- | -- | --- | ---  | -----   | --------    |--| ------ |
+| NN | RT | RA  | s0-4 | im0-4   | im5-7  1 iv |s5| grevlogi |
+| NN | RT | RA  | RB   | im0-4   | im5-7  00   |1 | grevlog |
+
+
 # grev
 
 based on RV bitmanip, this is also known as a butterfly network. however
@@ -391,86 +454,37 @@ uint64_t grev64(uint64_t RA, uint64_t RB)
 
 ```
 
-# shuffle / unshuffle
+# xperm
 
-based on RV bitmanip
+based on RV bitmanip.
 
-```
-uint32_t shfl32(uint32_t RA, uint32_t RB)
-{
-    uint32_t x = RA;
-    int shamt = RB & 15;
-    if (shamt & 8) x  = shuffle32_stage(x, 0x00ff0000, 0x0000ff00, 8);
-    if (shamt & 4) x  = shuffle32_stage(x, 0x0f000f00, 0x00f000f0, 4);
-    if (shamt & 2) x  = shuffle32_stage(x, 0x30303030, 0x0c0c0c0c, 2);
-    if (shamt & 1) x  = shuffle32_stage(x, 0x44444444, 0x22222222, 1);
-    return x;
-}
-uint32_t unshfl32(uint32_t RA, uint32_t RB)
-{
-    uint32_t x = RA;
-    int shamt = RB & 15;
-    if (shamt & 1) x  = shuffle32_stage(x, 0x44444444, 0x22222222, 1);
-    if (shamt & 2) x  = shuffle32_stage(x, 0x30303030, 0x0c0c0c0c, 2);
-    if (shamt & 4) x  = shuffle32_stage(x, 0x0f000f00, 0x00f000f0, 4);
-    if (shamt & 8) x  = shuffle32_stage(x, 0x00ff0000, 0x0000ff00, 8);
-    return x;
-}
+RA contains a vector of indices to select parts of RB to be
+copied to RT.  The immediate-variant allows up to an 8 bit
+pattern (repeated) to be targetted at different parts of RT
 
-uint64_t shuffle64_stage(uint64_t src, uint64_t maskL, uint64_t maskR, int N)
-{
-    uint64_t x = src & ~(maskL | maskR);
-    x |= ((src << N) & maskL) | ((src >> N) & maskR);
-    return x;
-}
-uint64_t shfl64(uint64_t RA, uint64_t RB)
-{
-    uint64_t x = RA;
-    int shamt = RB & 31;
-    if (shamt & 16) x = shuffle64_stage(x, 0x0000ffff00000000LL,
-                                           0x00000000ffff0000LL, 16);
-    if (shamt & 8) x = shuffle64_stage(x, 0x00ff000000ff0000LL,
-                                           0x0000ff000000ff00LL, 8);
-    if (shamt & 4) x = shuffle64_stage(x, 0x0f000f000f000f00LL,
-                                           0x00f000f000f000f0LL, 4);
-    if (shamt & 2) x = shuffle64_stage(x, 0x3030303030303030LL,
-                                           0x0c0c0c0c0c0c0c0cLL, 2);
-    if (shamt & 1) x = shuffle64_stage(x, 0x4444444444444444LL,
-                                           0x2222222222222222LL, 1);
-    return x;
-}
-uint64_t unshfl64(uint64_t RA, uint64_t RB)
+```
+uint_xlen_t xpermi(uint8_t imm8, uint_xlen_t RB, int sz_log2)
 {
-    uint64_t x = RA;
-    int shamt = RB & 31;
-    if (shamt &  1) x = shuffle64_stage(x, 0x4444444444444444LL,
-                                           0x2222222222222222LL, 1);
-    if (shamt &  2) x = shuffle64_stage(x, 0x3030303030303030LL,
-                                           0x0c0c0c0c0c0c0c0cLL, 2);
-    if (shamt &  4) x = shuffle64_stage(x, 0x0f000f000f000f00LL,
-                                           0x00f000f000f000f0LL, 4);
-    if (shamt &  8) x = shuffle64_stage(x, 0x00ff000000ff0000LL,
-                                           0x0000ff000000ff00LL, 8);
-    if (shamt & 16) x = shuffle64_stage(x, 0x0000ffff00000000LL,
-                                           0x00000000ffff0000LL, 16);
-    return x;
+    uint_xlen_t r = 0;
+    uint_xlen_t sz = 1LL << sz_log2;
+    uint_xlen_t mask = (1LL << sz) - 1;
+    uint_xlen_t RA = imm8 | imm8<<8 | ... | imm8<<56;
+    for (int i = 0; i < XLEN; i += sz) {
+        uint_xlen_t pos = ((RA >> i) & mask) << sz_log2;
+        if (pos < XLEN)
+            r |= ((RB >> pos) & mask) << i;
+    }
+    return r;
 }
-```
-
-# xperm
-
-based on RV bitmanip
-
-```
 uint_xlen_t xperm(uint_xlen_t RA, uint_xlen_t RB, int sz_log2)
 {
     uint_xlen_t r = 0;
     uint_xlen_t sz = 1LL << sz_log2;
     uint_xlen_t mask = (1LL << sz) - 1;
     for (int i = 0; i < XLEN; i += sz) {
-        uint_xlen_t pos = ((RB >> i) & mask) << sz_log2;
+        uint_xlen_t pos = ((RA >> i) & mask) << sz_log2;
         if (pos < XLEN)
-            r |= ((RA >> pos) & mask) << i;
+            r |= ((RB >> pos) & mask) << i;
     }
     return r;
 }
@@ -521,7 +535,99 @@ uint64_t gorc64(uint64_t RA, uint64_t RB)
 
 ```
 
-# Galois Field 2^M
+# Instructions for Carry-less Operations aka. Polynomials with coefficients in `GF(2)`
+
+Carry-less addition/subtraction is simply XOR, so a `cladd`
+instruction is not provided since the `xor[i]` instruction can be used instead.
+
+These are operations on polynomials with coefficients in `GF(2)`, with the
+polynomial's coefficients packed into integers with the following algorithm:
+
+[[!inline pagenames="openpower/sv/bitmanip/pack_poly.py" raw="true" feeds="no" actions="yes"]]
+
+## Carry-less Multiply Instructions
+
+based on RV bitmanip
+see <https://en.wikipedia.org/wiki/CLMUL_instruction_set> and
+<https://www.felixcloutier.com/x86/pclmulqdq> and
+<https://en.m.wikipedia.org/wiki/Carry-less_product>
+
+They are worth adding as their own non-overwrite operations
+(in the same pipeline).
+
+### `clmul` Carry-less Multiply
+
+[[!inline pagenames="openpower/sv/bitmanip/clmul.py" raw="true" feeds="no" actions="yes"]]
+
+### `clmulh` Carry-less Multiply High
+
+[[!inline pagenames="openpower/sv/bitmanip/clmulh.py" raw="true" feeds="no" actions="yes"]]
+
+### `clmulr` Carry-less Multiply (Reversed)
+
+Useful for CRCs. Equivalent to bit-reversing the result of `clmul` on
+bit-reversed inputs.
+
+[[!inline pagenames="openpower/sv/bitmanip/clmulr.py" raw="true" feeds="no" actions="yes"]]
+
+## `clmadd` Carry-less Multiply-Add
+
+```
+clmadd RT, RA, RB, RC
+```
+
+```
+(RT) = clmul((RA), (RB)) ^ (RC)
+```
+
+## `cltmadd` Twin Carry-less Multiply-Add (for FFTs)
+
+```
+cltmadd RT, RA, RB, RC
+```
+
+TODO: add link to explanation for where `RS` comes from.
+
+```
+temp = clmul((RA), (RB)) ^ (RC)
+(RT) = temp
+(RS) = temp
+```
+
+## `cldivrem` Carry-less Division and Remainder
+
+`cldivrem` isn't an actual instruction, but is just used in the pseudo-code
+for other instructions.
+
+[[!inline pagenames="openpower/sv/bitmanip/cldivrem.py" raw="true" feeds="no" actions="yes"]]
+
+## `cldiv` Carry-less Division
+
+```
+cldiv RT, RA, RB
+```
+
+```
+n = (RA)
+d = (RB)
+q, r = cldivrem(n, d, width=XLEN)
+(RT) = q
+```
+
+## `clrem` Carry-less Remainder
+
+```
+clrem RT, RA, RB
+```
+
+```
+n = (RA)
+d = (RB)
+q, r = cldivrem(n, d, width=XLEN)
+(RT) = r
+```
+
+# Instructions for Binary Galois Fields `GF(2^m)`
 
 see:
 
@@ -529,10 +635,208 @@ see:
 * <https://engineering.purdue.edu/kak/compsec/NewLectures/Lecture7.pdf>
 * <https://foss.heptapod.net/math/libgf2/-/blob/branch/default/src/libgf2/gf2.py>
 
-## SPRs to set modulo and degree
+Binary Galois Field addition/subtraction is simply XOR, so a `gfbadd`
+instruction is not provided since the `xor[i]` instruction can be used instead.
+
+## `GFBREDPOLY` SPR -- Reducing Polynomial
+
+In order to save registers and to make operations orthogonal with standard
+arithmetic, the reducing polynomial is stored in a dedicated SPR `GFBREDPOLY`.
+This also allows hardware to pre-compute useful parameters (such as the
+degree, or look-up tables) based on the reducing polynomial, and store them
+alongside the SPR in hidden registers, only recomputing them whenever the SPR
+is written to, rather than having to recompute those values for every
+instruction.
+
+Because Galois Fields require the reducing polynomial to be an irreducible
+polynomial, that guarantees that any polynomial of `degree > 1` must have
+the LSB set, since otherwise it would be divisible by the polynomial `x`,
+making it reducible, making whatever we're working on no longer a Field.
+Therefore, we can reuse the LSB to indicate `degree == XLEN`.
+
+```python
+def decode_reducing_polynomial(GFBREDPOLY, XLEN):
+    """returns the decoded coefficient list in LSB to MSB order,
+        len(retval) == degree + 1"""
+    v = GFBREDPOLY & ((1 << XLEN) - 1) # mask to XLEN bits
+    if v == 0 or v == 2: # GF(2)
+        return [0, 1] # degree = 1, poly = x
+    if v & 1:
+        degree = floor_log2(v)
+    else:
+        # all reducing polynomials of degree > 1 must have the LSB set,
+        # because they must be irreducible polynomials (meaning they
+        # can't be factored), if the LSB was clear, then they would
+        # have `x` as a factor. Therefore, we can reuse the LSB clear
+        # to instead mean the polynomial has degree XLEN.
+        degree = XLEN
+        v |= 1 << XLEN
+        v |= 1 # LSB must be set
+    return [(v >> i) & 1 for i in range(1 + degree)]
+```
+
+## `gfbredpoly` -- Set the Reducing Polynomial SPR `GFBREDPOLY`
+
+unless this is an immediate op, `mtspr` is completely sufficient.
+
+## `gfbmul` -- Binary Galois Field `GF(2^m)` Multiplication
+
+```
+gfbmul RT, RA, RB
+```
+
+```
+(RT) = gfbmul((RA), (RB))
+```
+
+## `gfbmadd` -- Binary Galois Field `GF(2^m)` Multiply-Add
+
+```
+gfbmadd RT, RA, RB, RC
+```
+
+```
+(RT) = gfbadd(gfbmul((RA), (RB)), (RC))
+```
+
+## `gfbtmadd` -- Binary Galois Field `GF(2^m)` Twin Multiply-Add (for FFT)
+
+```
+gfbtmadd RT, RA, RB, RC
+```
+
+TODO: add link to explanation for where `RS` comes from.
+
+```
+temp = gfbadd(gfbmul((RA), (RB)), (RC))
+(RT) = temp
+(RS) = temp
+```
+
+## `gfbinv` -- Binary Galois Field `GF(2^m)` Inverse
+
+```
+gfbinv RT, RA
+```
+
+```
+(RT) = gfbinv((RA))
+```
+
+# Instructions for Prime Galois Fields `GF(p)`
+
+## Helper algorithms
+
+```python
+def int_to_gfp(int_value, prime):
+    return int_value % prime # follows Python remainder semantics
+```
+
+## `GFPRIME` SPR -- Prime Modulus For `gfp*` Instructions
+
+## `gfpadd` Prime Galois Field `GF(p)` Addition
+
+```
+gfpadd RT, RA, RB
+```
+
+```
+(RT) = int_to_gfp((RA) + (RB), GFPRIME)
+```
+
+the addition happens on infinite-precision integers
+
+## `gfpsub` Prime Galois Field `GF(p)` Subtraction
+
+```
+gfpsub RT, RA, RB
+```
+
+```
+(RT) = int_to_gfp((RA) - (RB), GFPRIME)
+```
+
+the subtraction happens on infinite-precision integers
+
+## `gfpmul` Prime Galois Field `GF(p)` Multiplication
+
+```
+gfpmul RT, RA, RB
+```
+
+```
+(RT) = int_to_gfp((RA) * (RB), GFPRIME)
+```
+
+the multiplication happens on infinite-precision integers
+
+## `gfpinv` Prime Galois Field `GF(p)` Invert
+
+```
+gfpinv RT, RA
+```
+
+Some potential hardware implementations are found in:
+<https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.90.5233&rep=rep1&type=pdf>
+
+```
+(RT) = gfpinv((RA), GFPRIME)
+```
+
+the multiplication happens on infinite-precision integers
+
+## `gfpmadd` Prime Galois Field `GF(p)` Multiply-Add
+
+```
+gfpmadd RT, RA, RB, RC
+```
+
+```
+(RT) = int_to_gfp((RA) * (RB) + (RC), GFPRIME)
+```
+
+the multiplication and addition happens on infinite-precision integers
+
+## `gfpmsub` Prime Galois Field `GF(p)` Multiply-Subtract
+
+```
+gfpmsub RT, RA, RB, RC
+```
+
+```
+(RT) = int_to_gfp((RA) * (RB) - (RC), GFPRIME)
+```
+
+the multiplication and subtraction happens on infinite-precision integers
+
+## `gfpmsubr` Prime Galois Field `GF(p)` Multiply-Subtract-Reversed
+
+```
+gfpmsubr RT, RA, RB, RC
+```
+
+```
+(RT) = int_to_gfp((RC) - (RA) * (RB), GFPRIME)
+```
+
+the multiplication and subtraction happens on infinite-precision integers
+
+## `gfpmaddsubr` Prime Galois Field `GF(p)` Multiply-Add and Multiply-Sub-Reversed (for FFT)
 
-to save registers and make operations orthogonal with standard
-arithmetic the modulo is to be set in an SPR
+```
+gfpmaddsubr RT, RA, RB, RC
+```
+
+TODO: add link to explanation for where `RS` comes from.
+
+```
+product = (RA) * (RB)
+term = (RC)
+(RT) = int_to_gfp(product + term, GFPRIME)
+(RS) = int_to_gfp(term - product, GFPRIME)
+```
+
+the multiplication, addition, and subtraction happens on infinite-precision integers
 
 ## Twin Butterfly (Tukey-Cooley) Mul-add-sub
 
@@ -623,7 +927,45 @@ if __name__ == "__main__":
     # Evaluate the product (x^7)(x^7 + x + 1)
     print("{:02x}".format(multGF2(0b10000000, 0b10000011)))
 ```
-## GF div and mod
+
+## GF(2^M) Inverse
+
+```
+# https://bugs.libre-soc.org/show_bug.cgi?id=782#c33
+# https://ftp.libre-soc.org/ARITH18_Kobayashi.pdf
+def gf_invert(a) :
+
+    s = getGF2() # get the full polynomial (including the MSB)
+    r = a
+    v = 0
+    u = 1
+    j = 0
+
+    for i in range(1, 2*degree+1):
+        # could use count-trailing-1s here to skip ahead
+        if r & mask1:          # test MSB of r
+            if s & mask1:      # test MSB of s
+                s ^= r
+                v ^= u
+            s <<= 1            # shift left 1
+            if j == 0:
+                r, s = s, r    # swap r,s
+                u, v = v<<1, u # shift v and swap
+                j = 1
+            else:
+                u >>= 1        # right shift left
+                j -= 1
+        else:
+            r <<= 1            # shift left 1
+            u <<= 1            # shift left 1
+            j += 1
+
+    return u
+```
+
+# GF2 (Carryless)
+
+## GF2 (carryless) div and mod
 
 ```
 def gf_degree(a) :
@@ -649,29 +991,24 @@ def FullDivision(self, f, v):
 
         fDegree, vDegree = gf_degree(f), gf_degree(v)
         res, rem = 0, f
-        i = fDegree
-        mask = 1 << i
-        while (i >= vDegree):
-            if (mask & rem): # check MSB
+        for i in reversed(range(vDegree, fDegree+1):
+            if ((rem >> i) & 1): # check bit
                 res ^= (1 << (i - vDegree))
                 rem ^= ( v << (i - vDegree)))
-            i -= 1
-            mask >>= 1
         return (res, rem)
 ```
 
-| 0.5|6.10|11.15|16.20|21.25| 26..30  |31| name  |
-| -- | -- | --- | --- | --- | ------- |--| ----- |
-| NN | RS | RA  | deg | RC  | 0 1  011 |Rc| gfaddi |
-| NN | RS | RA  | RB  | RC  | 1 1  111 |Rc| gfadd |
-
-GFMOD is a pseudo-op where RA=0
+| 0.5|6.10|11.15|16.20| 21 | 22.23 | 24....30 |31| name |
+| -- | -- | --- | --- | -- | ----- | -------- |--| ---- |
+| NN | RT | RA  | RB  | 1  |  00   | 0001 110 |Rc| cldiv |
+| NN | RT | RA  | RB  | 1  |  01   | 0001 110 |Rc| clmod |
 
-## carryless mul
+## GF2 carryless mul
 
 based on RV bitmanip
 see <https://en.wikipedia.org/wiki/CLMUL_instruction_set> and
-<https://www.felixcloutier.com/x86/pclmulqdq>
+<https://www.felixcloutier.com/x86/pclmulqdq> and
+<https://en.m.wikipedia.org/wiki/Carry-less_product>
 
 these are GF2 operations with the modulo set to 2^degree.
 they are worth adding as their own non-overwrite operations
@@ -703,6 +1040,21 @@ uint_xlen_t clmulr(uint_xlen_t RA, uint_xlen_t RB)
     return x;
 }
 ```
+## carryless Twin Butterfly (Tukey-Cooley) Mul-add-sub
+
+used in combination with SV FFT REMAP to perform
+a full NTT in-place. possible by having 3-in 2-out,
+to avoid the need for a temp register.  RS is written
+to as well as RT.
+
+    clfmadd  RT,RA,RC,RB (Rc=0)
+    clfmadd. RT,RA,RC,RB (Rc=1)
+
+Pseudo-code:
+
+    RT <- CLMUL(RA, RC) ^ RB
+    RS <- CLMUL(RA, RC) ^ RB
+
 
 # bitmatrix
 
@@ -829,3 +1181,13 @@ do i = 0 to 63
 RA = result
 ```
 
+# bit to byte permute
+
+similar to matrix permute in RV bitmanip, which has XOR and OR variants,
+these perform a transpose.
+
+    do j = 0 to 7
+      do k = 0 to 7
+         b = VSR[VRB+32].dword[i].byte[k].bit[j]
+         VSR[VRT+32].dword[i].byte[j].bit[k] = b
+