Why set zero in zero.c rather than use xor_n

Vincent Lefevre vincent at vinc17.net
Mon Feb 5 22:24:37 UTC 2018


On 2018-02-05 22:46:58 +0100, Torbjorn Granlund wrote:
> As suspected, memset adds quite some moverhead.

This depends on what the compiler knows about the size.
If GCC knows the size at compile time, it does not generate
a call to memset:

#include <string.h>

void foo (long *p)
{
  memset (p, 0, 1);
}

gives with gcc -O2 (7.3.0) on x86_64:

foo:
.LFB0:
        .cfi_startproc
        movb    $0, (%rdi)
        ret
        .cfi_endproc

When the size is not known at compile time, I wonder whether there
is a way to tell the compiler how to optimize. The best code may be
different whether the size can be often small or not, i.e. whether
to inline a test on the size or not. But this is not even obvious
as the code size may be important for the instruction cache.

-- 
Vincent Lefèvre <vincent at vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)


More information about the gmp-devel mailing list