Benchmark memchar (with GCC builtins)

Marco Leise via Digitalmars-d digitalmars-d at puremagic.com
Fri Oct 30 16:13:54 PDT 2015


Am Fri, 30 Oct 2015 22:31:54 +0000
schrieb Iakh <iaktakh at gmail.com>:

> On Friday, 30 October 2015 at 21:33:25 UTC, Andrei Alexandrescu 
> wrote:
> > Could you please take a look at GCC's generated code and 
> > implementation of memchr? -- Andrei
> 
> glibc uses something like pseudo-SIMD with ordinal x86 
> instructions (XOR magic, etc).
> Deap comarison I left for next time :)

Instead of providing a library implementation, these functions
are often best left to compiler intrinsics which can provide
one of several instruction sequences based on the arguments
and the target CPU. In particular compilers often know runtime
length arguments from const-folding and can chose to use the
best matching SIMD instruction set if compiling for a specific
CPU. In regular D or C code you don't have access to this
information.

GlibC might use that particular implementation in its source,
but GCC will override these generic functions with builtin
intrinsics unless you disable them via command-line switch
(-fno-builtins). Same goes for e.g. ctlz (count leading
zeroes): It will be emulated somehow if compiled for older
CPUs and use fast native instructions on more recent CPUs.
Yes, sometimes the intrinsics are lacking, but in general I
trust the compiler devs more than myself, especially since
they likely tested it on several target architectures while I
mostly only do one.

Just ask yourself how you would select the optimal memchr
function matching the -march= (gdc) or -mcpu (ldc) flags at
compile-time.

-- 
Marco



More information about the Digitalmars-d mailing list