(no commit message)
[libreriscv.git] / openpower / sv / bitmanip.mdwn
index 3f2fd4e3c77ae0c3ca870e64185af18cf25c9d03..0a2b37c75ab139032f64e2863f8bd30bb61c1819 100644 (file)
@@ -52,7 +52,7 @@ ternlog has its own major opcode
 |  -00   |1 | grevlog   |
 |  -01   |  | crternlog  |
 |  010   |Rc| bitmask   |
-|  011   |  | gf/cl madd*  |
+|  011   |  | SVP64  |
 |  110   |Rc| 1/2-op    |
 |  111   |  | bmrevi   |
 
@@ -88,23 +88,18 @@ ternlog has its own major opcode
 
 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  |itype/| im0-4   | im5-7  00 |0 | xpermi  |
 | NN | RT | RA  | RB   | im0-4   | im5-7  00 |1 | grevlog |
-| NN |    |     |      |         | .....  01 |0 | crternlog |
+| NN |    |     |      |         | -----  01 |m3| crternlog |
 | NN | RT | RA  | RB   | RC      | mode  010 |Rc| bitmask* |
-| NN | RS | RA  | RB   | RC      | 00    011 |0 | gfbmadd |
-| 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 |    |     |      |         | 00    011 |  | rsvd |
+| NN |    |     |      |         | 01    011 |0 | svshape |
+| NN |    |     |      |         | 01    011 |1 | svremap |
+| NN |    |     |      |         | 10    011 |Rc| svstep |
+| NN |    |     |      |         | 11    011 |Rc| setvl |
+| NN |    |     |      |         | ----  110 |  | 1/2 ops |
 | NN | RT | RA  | RB   | sh0-4   | sh5 1 111 |Rc| bmrevi |
 
 ops (note that av avg and abs as well as vec scalar mask
@@ -120,20 +115,20 @@ double check that instructions didn't need 3 inputs.
 | NN | RS | RB  | sh  | SH | 0   1 | nn00 110 |Rc| bmopsi |
 | 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|       |
-| NN | RT | RB  | RB  | 1  |  11   | 0001 110 |Rc| clinv |
+| NN | RT | RA  |     | 1  |  10   | 0001 110 |Rc| bmatflip |
+| NN |    |     |     | 1  |  11   | 0001 110 |Rc| rsvd  |
 | NN | RA | RB  | RC  | 0  |   00  | 0001 110 |Rc| vec sbfm |
 | 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 |    |     |     | 0  |       | 0101 110 |Rc| rsvd |
 | NN | RT | RA  | RB  | 1  | itype | 0101 110 |Rc| xperm |
-| NN | RA | RB  | RC  | 0  | itype | 0101 110 |Rc| av minmax |
-| NN | RA | RB  | RC  | 1  |   00  | 0101 110 |Rc| av abss |
-| NN | RA | RB  | RC  | 1  |   01  | 0101 110 |Rc| av absu|
-| NN | RA | RB  |     | 1  |   10  | 0101 110 |Rc| av avgadd |
-| NN | RA | RB  |     | 1  |   11  | 0101 110 |Rc| rsvd |
-| NN | RA | RB  |     |    |       | 1001 110 |Rc| rsvd |
-| NN | RA | RB  |     |    |       | 1101 110 |Rc| rsvd |
+| NN | RA | RB  | RC  | 0  | itype | 1001 110 |Rc| av minmax |
+| NN | RA | RB  | RC  | 1  |   00  | 1001 110 |Rc| av abss |
+| NN | RA | RB  | RC  | 1  |   01  | 1001 110 |Rc| av absu |
+| NN | RA | RB  |     | 1  |   10  | 1001 110 |Rc| av avgadd |
+| NN |    |     |     | 1  |   11  | 1001 110 |Rc| rsvd |
+| NN |    |     |     |    |       | 1101 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 |
@@ -145,10 +140,10 @@ 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  |    | 10    | --10 110 |Rc| rsvd  |
+| NN |    |     |     |    | 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  |
+| NN |    |     |     |    |       | --11 110 |Rc| rsvd  |
 
 # ternlog bitops
 
@@ -204,21 +199,18 @@ another mode selection would be CRs not Ints.
 
 | 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|
+| NN | BT | BA  | BB  | BC  |m0-2 | imm  |  01  |m3|
 
     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])
+        a,b,c = CRs[BA][i], CRs[BB][i], CRs[BC][i])
+        if mask[i] CRs[BT][i] = lut3(imm, a, b, c)
 
+# int ops
 
-# int min/max
+## min/m
 
-required for
-the [[sv/av_opcodes]]
+required for the [[sv/av_opcodes]]
 
 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).
 
@@ -239,8 +231,29 @@ uint_xlen_t maxu(uint_xlen_t rs1, uint_xlen_t rs2)
 }
 ```
 
+## average
+
+required for the [[sv/av_opcodes]], these exist in Packed SIMD (VSX)
+but not scalar
+
+```
+uint_xlen_t intavg(uint_xlen_t rs1, uint_xlen_t rs2) {
+     return (rs1 + rs2 + 1) >> 1:
+}
+```
+
+## abs
 
-## cmix
+required for the [[sv/av_opcodes]], these exist in Packed SIMD (VSX)
+but not scalar
+
+```
+uint_xlen_t intabs(uint_xlen_t rs1, uint_xlen_t rs2) {
+     return (src1 > src2) ? (src1-src2) : (src2-src1)
+}
+```
+
+# cmix
 
 based on RV bitmanip, covered by ternlog bitops
 
@@ -350,8 +363,8 @@ generalised reverse combined with a pair of LUT2s and allowing
 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.
+and a means to set hundreds of regular 64 bit patterns with one
+single 32 bit instruction.
 
 the two LUT2s are applied left-half (when not swapping)
 and right-half (when swapping) so as to allow a wider
@@ -370,7 +383,7 @@ 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
+masks rather than a integer mask) then sv.cmpi or 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:
@@ -469,7 +482,11 @@ based on RV bitmanip.
 
 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
+pattern (repeated) to be targetted at different parts of RT.
+
+xperm shares some similarity with one of the uses of bmator
+in that xperm indices are binary addressing where bitmator
+may be considered to be unary addressing.
 
 ```
 uint_xlen_t xpermi(uint8_t imm8, uint_xlen_t RB, int sz_log2)
@@ -543,6 +560,55 @@ uint64_t gorc64(uint64_t RA, uint64_t RB)
 }
 
 ```
+
+# bitmatrix
+
+```
+uint64_t bmatflip(uint64_t RA)
+{
+    uint64_t x = RA;
+    x = shfl64(x, 31);
+    x = shfl64(x, 31);
+    x = shfl64(x, 31);
+    return x;
+}
+uint64_t bmatxor(uint64_t RA, uint64_t RB)
+{
+    // transpose of RB
+    uint64_t RBt = bmatflip(RB);
+    uint8_t u[8]; // rows of RA
+    uint8_t v[8]; // cols of RB
+    for (int i = 0; i < 8; i++) {
+        u[i] = RA >> (i*8);
+        v[i] = RBt >> (i*8);
+    }
+    uint64_t x = 0;
+    for (int i = 0; i < 64; i++) {
+        if (pcnt(u[i / 8] & v[i % 8]) & 1)
+            x |= 1LL << i;
+    }
+    return x;
+}
+uint64_t bmator(uint64_t RA, uint64_t RB)
+{
+    // transpose of RB
+    uint64_t RBt = bmatflip(RB);
+    uint8_t u[8]; // rows of RA
+    uint8_t v[8]; // cols of RB
+    for (int i = 0; i < 8; i++) {
+        u[i] = RA >> (i*8);
+        v[i] = RBt >> (i*8);
+    }
+    uint64_t x = 0;
+    for (int i = 0; i < 64; i++) {
+        if ((u[i / 8] & v[i % 8]) != 0)
+            x |= 1LL << i;
+    }
+    return x;
+}
+
+```
+
 # Introduction to Carry-less and GF arithmetic
 
 * obligatory xkcd <https://xkcd.com/2595/>
@@ -918,54 +984,6 @@ term = (RC)
 (RS) = gfpmsubr(factor1, factor2, term)
 ```
 
-# bitmatrix
-
-```
-uint64_t bmatflip(uint64_t RA)
-{
-    uint64_t x = RA;
-    x = shfl64(x, 31);
-    x = shfl64(x, 31);
-    x = shfl64(x, 31);
-    return x;
-}
-uint64_t bmatxor(uint64_t RA, uint64_t RB)
-{
-    // transpose of RB
-    uint64_t RBt = bmatflip(RB);
-    uint8_t u[8]; // rows of RA
-    uint8_t v[8]; // cols of RB
-    for (int i = 0; i < 8; i++) {
-        u[i] = RA >> (i*8);
-        v[i] = RBt >> (i*8);
-    }
-    uint64_t x = 0;
-    for (int i = 0; i < 64; i++) {
-        if (pcnt(u[i / 8] & v[i % 8]) & 1)
-            x |= 1LL << i;
-    }
-    return x;
-}
-uint64_t bmator(uint64_t RA, uint64_t RB)
-{
-    // transpose of RB
-    uint64_t RBt = bmatflip(RB);
-    uint8_t u[8]; // rows of RA
-    uint8_t v[8]; // cols of RB
-    for (int i = 0; i < 8; i++) {
-        u[i] = RA >> (i*8);
-        v[i] = RBt >> (i*8);
-    }
-    uint64_t x = 0;
-    for (int i = 0; i < 64; i++) {
-        if ((u[i / 8] & v[i % 8]) != 0)
-            x |= 1LL << i;
-    }
-    return x;
-}
-
-```
-
 # Already in POWER ISA
 
 ## count leading/trailing zeros with mask
@@ -1054,3 +1072,7 @@ in v3.0/1 already
          b = VSR[VRB+32].dword[i].byte[k].bit[j]
          VSR[VRT+32].dword[i].byte[j].bit[k] = b
 
+# Appendix
+
+see [[bitmanip/appendix]]
+