RE: [PATCH v3 3/3] riscv: optimized memset

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: Matteo Croce
> Sent: 23 June 2021 02:15
> 
> On Tue, Jun 22, 2021 at 10:38 AM David Laight <David.Laight@xxxxxxxxxx> wrote:
> >
> > From: Nick Kossifidis
...
> > You can just write:
> >         cu = (unsigned long)c * 0x0101010101010101ull;
> > and let the compiler sort out the best way to generate the constant.
> >
> 
> Interesting. I see that most compilers do an integer multiplication,
> is it faster than three shift and three or?
> 
> clang on riscv generates even more instructions to create the immediate:
> 
> unsigned long repeat_shift(int c)
> {
>   unsigned long cu = (unsigned long)c;
>   cu |= cu << 8;
>   cu |= cu << 16;
>   cu |= cu << 32;
> 
>   return cu;
> }
> 
> unsigned long repeat_mul(int c)
> {
>   return (unsigned long)c * 0x0101010101010101ull;
> }
> 
> repeat_shift:
>   slli a1, a0, 8
>   or a0, a0, a1
>   slli a1, a0, 16
>   or a0, a0, a1
>   slli a1, a0, 32
>   or a0, a0, a1
>   ret
> 
> repeat_mul:
>   lui a1, 4112
>   addiw a1, a1, 257
>   slli a1, a1, 16
>   addi a1, a1, 257
>   slli a1, a1, 16
>   addi a1, a1, 257
>   mul a0, a0, a1
>   ret

Hmmm... I expected the compiler to convert it to the first form.
It is also pretty crap at generating that constant.
Stupid compilers.

In any case, for the usual case of 'c' being a constant zero
you really don't want the latency of those instructions at all.

It is almost worth just pushing that expansion into the caller.

eg by having:
#define memset(p, v, l) memset_w(p, (v) * 0x0101010101010101, l)
(or some other byte replicator).

Really annoyingly you want to write the code that generates
the 64bit constant, and then have the compiler optimise away
the part that generates the high 32 bits on 32 bits systems.
But one of the compilers is going to 'bleat' about truncating
a constant value.
Stupid compilers (again).

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)




[Index of Archives]     [Linux Kernel]     [Kernel Newbies]     [x86 Platform Driver]     [Netdev]     [Linux Wireless]     [Netfilter]     [Bugtraq]     [Linux Filesystems]     [Yosemite Discussion]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Samba]     [Device Mapper]

  Powered by Linux