Universal boolean instruction
Posted: 2021-06-27, 7:52:00
I have an idea for a multi-purpose 3-input bitwise boolean instruction.
This instruction can implement all functions of the type RESULT = (A AND/OR B) AND/OR/XOR C with optional inversion on all inputs and outputs. It can also do A XOR B XOR C and bit selection: A AND B OR NOT A AND C. The latter can be interpreted as A ? B : C.
The general multiformat instructions can have six option bits, which I am making full use of in this design. It is defined by the diagram below, or by this C code:
Intel processors with AVX512 have a fully universal 3-input boolean instruction named VPTERNLOGD with an 8-bit truth table. I think it is too expensive to implement a full dynamic truth table in hardware for every bit in an integer or vector. I think the bitwise_logic instruction defined above is cheaper to implement - or is it?
The current ForwardCom specification has the truth_tab3 instruction which implements a truth table only for single-bit boolean variables, because a full n-bit implementation may be too expensive. The truth_tab3 instruction is probably not needed anymore if we implement the bitwise_logic instruction.
Another question is whether compilers can handle the complications of selecting the right option bits. Considering what compilers like clang and gcc can do nowadays, I think this is realistic.
The bitwise_logic instruction can do all possible boolean functions with 2 inputs, and the most important functions with 3 inputs. It cannot do everything. For example, it cannot do A & B & C | ~A & ~B & ~C. Are there any other important 3-input boolean functions that you think should be supported?
Examples of option bits
Do you think this instruction is sufficiently useful to justify the complexity? It is possible to make a simpler implementation that cannot do XOR.
This instruction can implement all functions of the type RESULT = (A AND/OR B) AND/OR/XOR C with optional inversion on all inputs and outputs. It can also do A XOR B XOR C and bit selection: A AND B OR NOT A AND C. The latter can be interpreted as A ? B : C.
The general multiformat instructions can have six option bits, which I am making full use of in this design. It is defined by the diagram below, or by this C code:
Code: Select all
int bitwise_logic (int A, int B, int C, bool op[6]) {
int A1 = op[0] ? ~A : A;
int B1 = op[1] ? ~B : B;
int C1 = op[2] ? ~C : C;
int D = A1 & B1;
int D1 = op[3] ? ~D : D;
int E = C1 & D1;
int E1 = op[4] ? ~E : E;
int result;
if (op[5]) {
result = E1;
}
else if (op[4]) {
if (op[3]) {
result = A1 ^ B1 ^ C1;
}
else {
result = D ^ C1;
}
}
else {
result = D | ~A1 & C1;
}
return result;
}
Intel processors with AVX512 have a fully universal 3-input boolean instruction named VPTERNLOGD with an 8-bit truth table. I think it is too expensive to implement a full dynamic truth table in hardware for every bit in an integer or vector. I think the bitwise_logic instruction defined above is cheaper to implement - or is it?
The current ForwardCom specification has the truth_tab3 instruction which implements a truth table only for single-bit boolean variables, because a full n-bit implementation may be too expensive. The truth_tab3 instruction is probably not needed anymore if we implement the bitwise_logic instruction.
Another question is whether compilers can handle the complications of selecting the right option bits. Considering what compilers like clang and gcc can do nowadays, I think this is realistic.
The bitwise_logic instruction can do all possible boolean functions with 2 inputs, and the most important functions with 3 inputs. It cannot do everything. For example, it cannot do A & B & C | ~A & ~B & ~C. Are there any other important 3-input boolean functions that you think should be supported?
Examples of option bits
Option bits | Function |
---|---|
0b100000 | A & B & C |
0b110100 | ~(A & B & ~C) |
0b110111 | A | B | C |
0b111100 | A & B | C |
0b010000 | A & B ^ C |
0b011001 | ~(A ^ B ^ C) |
0b000000 | A & B | ~A & C |
0b000001 | A & B | ~A & ~C |