[llvm-dev] __parityti2(), __paritydi2() and __paritysi2() vs. __builtin_parity (original) (raw)

Craig Topper via llvm-dev llvm-dev at lists.llvm.org
Tue Dec 4 12:28:05 PST 2018


I don't believe clang/llvm will ever emit a call to the parity library routines today. But it might be needed so we can say that we match the libgcc interface. gcc doesn't use it for x86 either as far as I know. And I don't know if they have it in their x86 libgcc. But it might be easier for compiler-rt to be a superset of libgcc rather than trying to track exactly what they have on each target.

Not sure about the inlining question.

~Craig

On Tue, Dec 4, 2018 at 11:51 AM Stefan Kanthak via llvm-dev < llvm-dev at lists.llvm.org> wrote:

Hi @ll,

compiler-rt/lib/builtins/parityti2.c compiler-rt/lib/builtins/paritydi2.c compiler-rt/lib/builtins/paritysi2.c implement the parity function as matroschka: siint _paritysi2(siint a) { suint x = (suint)a; x ^= x >> 16; x ^= x >> 8; x ^= x >> 4; return (0x6996 >> (x & 0xF)) & 1; // see optimisation below! } siint _paritydi2(diint a) { dwords x; x.all = a; _return paritysi2(x.s.high ^ x.s.low); } siint _parityti2(tiint a) { twords x; x.all = a; _return paritydi2(x.s.high ^ x.s.low); } Questions: ~~~~~~~~~~ _1. are these functions still needed, given that builtinparity is available? 2. will the optimiser "inline" the internal function calls (as part of LTO)? If NOT, they should be inlined manually! JFTR: if the 3 functions are part of a single source or compilation unit, they are inlined by the compiler! Yes, parity is seldomly used, so this optimisation may not seem necessary. siint _paritydi2(diint a) { suint x = (suint)a; x ^= (duint)a >> 32; x ^= x >> 16; x ^= x >> 8; x ^= x >> 4; return (0x69966996 >> x) & 1; } siint _parityti2(tiint a) { duint x = (duint)a; x ^= (tuint)a >> 64; x ^= x >> 32; x ^= x >> 16; x ^= x >> 8; x ^= x >> 4; return (0x69966996 >> x) & 1; } CAVEAT: the last right-shift MAY BE undefined behaviour, the optimisation shown here only works on CPUs which perform shifts modulo word-size! stay tuned Stefan Kanthak


LLVM Developers mailing list llvm-dev at lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20181204/cbcac064/attachment.html>



More information about the llvm-dev mailing list