WojciechMula / ternary-logic

Support for ternary logic in SSE, XOP, AVX2 and x86 programs
Other
30 stars 9 forks source link

Many codes can be handled with fewer instructions/cycles #6

Open falk-hueffner opened 3 years ago

falk-hueffner commented 3 years ago

Hi,

I've written an optimizer using z3 that generates the minimum number of instructions/cycles. Many codes can be improved, e.g.:

// code=0x2b, function=(C ? (B nand A) : (B nor A)), lowered=((C and not ((B and A))) or (not (C) and not ((B or A)))), set=intel
template<> uint64_t ternary<0x2b>(const uint64_t A, const uint64_t B, const uint64_t C) {
    const uint64_t t0 = B & A;
    const uint64_t t1 = ~t0;
    const uint64_t t2 = C & t1;
    const uint64_t t3 = ~C;
    const uint64_t t4 = B | A;
    const uint64_t t5 = ~t4;
    const uint64_t t6 = t3 & t5;
    const uint64_t t7 = t2 | t6;
    return t7;
}

can be

template<> uint64_t ternary<0x2b>(const uint64_t A, const uint64_t B, const uint64_t C) {
    const uint64_t t0 = B ^ A;
    const uint64_t t1 = ~C;
    const uint64_t t2 = A ^ C;
    const uint64_t t3 = t0 | t2;
    const uint64_t t4 = t3 ^ t1;
    return t4;
}

I've attached the script.

ternary-logic.py.gz

falk-hueffner commented 3 years ago

I've updated the script a bit and put it here instead: https://github.com/falk-hueffner/ternary-logic-optimization