1 # RFC ls007 Ternary/Binary GPR and CR Field bit-operations
5 * <https://libre-soc.org/openpower/sv/bitmanip/>
6 * <https://libre-soc.org/openpower/sv/rfc/ls007/>
7 * <https://bugs.libre-soc.org/show_bug.cgi?id=1017>
8 * <https://git.openpower.foundation/isa/PowerISA/issues/117>
20 **Books and Section affected**: **UPDATE**
22 * Book I 2.5.1 Condition Register Logical Instructions
23 * Book I 3.3.13 Fixed-Point Logical Instructions
24 * Appendix E Power ISA sorted by opcode
25 * Appendix F Power ISA sorted by version
26 * Appendix G Power ISA sorted by Compliancy Subset
27 * Appendix H Power ISA sorted by mnemonic
33 * `ternlogi` -- GPR Ternary Logic Immediate
34 * `crternlogi` -- Condition Register Field Ternary Logic Immediate
35 * `binlog` -- GPR Dynamic Binary Logic
36 * `crbinlog` -- Condition Register Field Dynamic Binary Logic
38 **Submitter**: Luke Leighton (Libre-SOC)
40 **Requester**: Libre-SOC
42 **Impact on processor**:
44 * Addition of two new GPR-based instructions
45 * Addition of two new CR-field-based instructions
47 **Impact on software**:
49 * Requires support for new instructions in assembler, debuggers,
55 GPR, CR-Field, bit-manipulation, ternary, binary, dynamic, look-up-table
61 * `ternlogi` is similar to existing `and`/`or`/`xor`/etc. instructions, but
62 allows any arbitrary 3-input 1-output bitwise operation. This can be used to
63 combine several instructions into one. E.g. `A ^ (~B & (C | A))` can become
64 one instruction. This can also be used to have one instruction for
65 bitwise MUX `(A & B) | (~A & C)`.
66 * `binlog` is like `ternlogi` except it supports any arbitrary 2-input
67 1-output bitwise operation, where the operation can be selected dynamically
68 at runtime. This operates similarly to a Programmable LUT in a FPGA.
69 * `crternlogi` is like `ternlogi` except it works with CRs instead of GPRs.
70 * `crbinlog` is like `binlog` except it works with CRs instead of GPRs. Likewise it
71 is similar to a Programmable LUT in an FPGA.
72 * Combined these instructions save on insttuction count and also help accelerate
75 **Notes and Observations**:
77 * `ternlogi` is like the existing `xxeval` instruction, except operates on GPRs instead
78 of VSRs and doesn't require VSX/VMX. SFS and SFFS are comparatively compromised.
79 * `crternlogi` is similar to the group of CR Operations (crand, cror etc) which have
80 been identified as a Binary Lookup Group, except an 8-bit
81 immediate is used instead of a 4-bit one, and up to 4 bits of a CR Field may
82 be computed at once, saving at least 3 groups of CR operations.
83 * `crbinlut` is similar to the Binary Lookup Group of CR Operations except that the
84 4-bit lookup table comes from a CR Field instead of from an Immediate. Also
85 like `crternlogi` up to 4 bits may be computed at once.
89 Add the following entries to:
91 * Book I 2.5.1 Condition Register Logical Instructions
92 * Book I 3.3.13 Fixed-Point Logical Instructions
93 * Book I 1.6.1 and 1.6.2
99 # GPR Ternary Logic Immediate
101 Add this section to Book I 3.3.13
105 * `ternlogi RT, RA, RB, TLI` (`Rc=0`)
106 * `ternlogi. RT, RA, RB, TLI` (`Rc=1`)
108 | 0-5 | 6-10 | 11-15 | 16-20 | 21-28 | 29-30 | 31 | Form |
109 |-----|------|-------|-------|-------|-------|----|----------|
110 | PO | RT | RA | RB | TLI | XO | Rc | TLI-Form |
115 result <- (~RT & ~RA & ~RB & TLI[0]*64) | # 64 copies of TLI[0]
116 (~RT & ~RA & RB & TLI[1]*64) | # ...
117 (~RT & RA & ~RB & TLI[2]*64) |
118 (~RT & RA & RB & TLI[3]*64) |
119 ( RT & ~RA & ~RB & TLI[4]*64) |
120 ( RT & ~RA & RB & TLI[5]*64) |
121 ( RT & RA & ~RB & TLI[6]*64) | # ...
122 ( RT & RA & RB & TLI[7]*64) # 64 copies of TLI[7]
126 For each integer value i, 0 to 63, do the following.
128 Let j be the value of the concatenation of the
129 contents of bit i of RT, bit i of RB, bit i of RT.
130 The value of bit j of TLI is placed into bit i of RT.
132 See Table 145, "xxeval(A, B, C, TLI) Equivalent
133 Functions," on page 968 for the equivalent function
134 evaluated by this instruction for any given value of TLI.
136 *Programmer's Note: this is a Read-Modify-Write instruction on RT.*
138 Special registers altered:
148 # Condition Register Ternary Logic Immediate
150 Add this section to Book I 2.5.1
154 * `crternlogi BF, BFA, BFB, BFC, TLI, msk`
156 | 0.5|6.8 |9.10|11.13|14.15|16.18|19.25|26.30| 31| Form |
157 |----|----|----|-----|-----|-----|-----|-----|---|----------|
158 | PO | BF | msk|BFA | msk | BFB | TLI | XO |TLI| CRB-Form |
163 a <- CR[4*BF+32:4*BF+35]
164 b <- CR[4*BFA+32:4*BFA+35]
165 c <- CR[4*BFB+32:4*BFB+35]
166 ternary <- (~a & ~b & ~c & TLI[0]*4) | # 4 copies of TLI[0]
167 (~a & ~b & c & TLI[1]*4) | # 4 copies of TLI[1]
168 (~a & b & ~c & TLI[2]*4) | # ...
169 (~a & b & c & TLI[3]*4) |
170 ( a & ~b & ~c & TLI[4]*4) |
171 ( a & ~b & c & TLI[5]*4) |
172 ( a & b & ~c & TLI[6]*4) | # ...
173 ( a & b & c & TLI[7]*4)) # 4 copies of TLI[7]
176 CR[4*BF+32+i] <- ternary[i]
179 For each integer value i, 0 to 3, do the following.
181 Let j be the value of the concatenation of the
182 contents of bit i of CR Field BF, bit i of CR Field BFA,
183 bit i of CR Field BFB.
185 If bit i of msk is set to 1 then the value of bit j of TLI
186 is placed into bit i of CR Field BF.
188 Otherwise, if bit i of msk is a zero then bit i of
189 CR Field BF is unchanged.
191 See Table 145, "xxeval(A, B, C, TLI) Equivalent
192 Functions," on page 968 for the equivalent function
193 evaluated by this instruction for any given value of TLI.
195 *Programmer's Note: this instruction is a "masked" overwrite on CR Field BF. For each bit set in msk a Write is performed but for each bit clear in msk the corresponding bit of BF is preserved. Overall this makes crbinlog a conditionally Read-Modify-Write instruction on CR Field BF*
197 Special registers altered:
207 # GPR Dynamic Binary Logic
209 Add this section to Book I 3.3.13
213 * `binlog RT, RA, RB, RC, nh`
215 | 0-5 | 6-10 | 11-15 | 16-20 | 21-25 | 26 | 27-31 | Form |
216 |-----|------|-------|-------|-------|----|-------|---------|
217 | PO | RT | RA | RB | RC | nh | XO | VA-Form |
222 if nh = 1 then lut <- (RC)[56:59]
223 else lut <- (RC)[60:63]
224 result <- (~RA & ~RB & lut[0]*64) |
225 (~RA & RB & lut[1]*64) |
226 ( RA & ~RB & lut[2]*64) |
227 ( RA & RB & lut[3]*64))
231 For each integer value i, 0 to 63, do the following.
233 If nh contains a 0, let lut be the four LSBs of RC
234 (bits 60 to 63). Otherwise let lut be the next
235 four LSBs of RC (bits 56 to 59).
237 Let j be the value of the concatenation of the
238 contents of bit i of RT with bit i of RB.
240 The value of bit j of lut is placed into bit i of RT.
242 Special registers altered:
248 **Programmer's Note**:
250 Dynamic (non-immediate-based) Ternary Logic, suitable for FPGA-style LUT3
251 dynamic lookups and for JIT runtime acceleration, may be emulated by
252 appropriate combination of `binlog` and `ternlogi`, using the `nh`
253 (next half) operand to select first and second nibble:
256 # compute r3 = ternlog(r4, r5, r6, table=r7)
257 # compute the values for when r6[i] = 0:
258 binlog r3, r4, r5, r7, 0 # takes look-up-table from LSB 4 bits
259 # compute the values for when r6[i] = 1:
260 binlog r4, r4, r5, r7, 1 # takes look-up-table from second-to-LSB 4 bits
261 # mux the two results together: r3 = (r3 & ~r6) | (r4 & r6)
262 ternlogi r3, r4, r6, 0b11011000
269 # Condition Register Field Dynamic Binary Logic
271 Add this section to Book I 2.5.1
275 | 0.5|6.8 |9.10|11.13|14.15|16.18|19.25|26.30| 31| Form |
276 |----|----|----|-----|-----|-----|-----|-----|---|----------|
277 | PO | BF | msk|BFA | msk | BFB | // | XO |// | CRB-Form |
280 a <- CR[4*BF+32:4*BFA+35]
281 b <- CR[4*BFA+32:4*BFA+35]
282 lut <- CR[4*BFB+32:4*BFB+35]
283 binary <- (~a & ~b & lut[0]*4) |
284 (~a & b & lut[1]*4) |
285 ( a & ~b & lut[2]*4) |
289 CR[4*BF+32+i] <- binary[i]
292 For each integer value i, 0 to 3, do the following.
294 Let j be the value of the concatenation of the
295 contents of bit i of CR Field BF with bit i of CR Field BFA.
297 If bit i of msk is set to 1 then the value of bit j of
298 CR Field BFB is placed into bit i of CR Field BF.
300 Otherwise, if bit i of msk is a zero then bit i of
301 CR Field BF is unchanged.
303 Special registers altered:
309 *Programmer's Note: just as with binlut and ternlogi, a pair
310 of crbinlog instructions followed by a merging crternlogi may
311 be deployed to synthesise dynamic ternary (LUT3) CR Field
314 *Programmer's Note: this instruction is a "masked" overwrite on CR
315 Field BF. For each bit set in `msk` a Write is performed
316 but for each bit clear in `msk` the corresponding bit of BF is
317 preserved. Overall this makes `crbinlog` a conditionally
318 Read-Modify-Write instruction on CR Field BF*
326 Add the following section to Book I 1.6.1
329 |0 |6 |9 |12 |15 |18 |21 |29 |31 |
330 | PO | BF | BFA | BFB | BFC | msk | TLI | XO | msk |
335 Add the following section to Book I 1.6.1
338 |0 |6 |11 |16 |21 |29 |31 |
339 | PO | RT | RA | RB | TLI | XO | Rc |
344 Add the following entry to VA-FORM in Book I 1.6.1.12
347 |0 |6 |11 |16 |21 |26|27 |
348 | PO | RT | RA | RB | RC |nh| XO |
351 # Word Instruction Fields
353 Add the following to Book I 1.6.2
357 Field used by crternlogi and crbinlut to decide which CR Field bits to
361 Nibble High. Field used by binlog to decide if the look-up-table should
362 be taken from bits 60:63 (nh=0) or 56:59 (nh=1) of RC.
365 Field used by the ternlogi instruction as the
369 Field used by the crternlogi instruction as the
374 * Add `TLI` to the `Formats:` list of all of `RA`, `RB`, `RT`, and `Rc`.
375 * Add `CRB` to the `Formats:` list of all of `BF`, `BFA`, `BFB`, and `BFC`.
376 * Add `TLI` to the `Formats:` list of `XO (29:30)`.
377 * Add `CRB` to the `Formats:` list of `XO (26:31)`.
378 * Add `VA` to the `Formats:` list of `XO (27:31)`.
384 Appendix E Power ISA sorted by opcode
385 Appendix F Power ISA sorted by version
386 Appendix G Power ISA sorted by Compliancy Subset
387 Appendix H Power ISA sorted by mnemonic
389 |Form| Book | Page | Version | mnemonic | Description |
390 |----|------|------|---------|------------|-------------|
391 |TLI | I | # | 3.2B | ternlogi | GPR Ternary Logic Immediate |
392 |VA | I | # | 3.2B | binlog | GPR Binary Logic |
393 |CRB | I | # | 3.2B | crternlogi | CR Field Ternary Logic Immediate |
394 |CRB | I | # | 3.2B | crbinlog | CR Field Binary Logic |