[LAD] Bit shift

karl at aspodata.se karl at aspodata.se
Sat Mar 16 23:11:53 CET 2019


Will Godfrey:
> On Wed, 13 Mar 2019 00:09:17 +0100 (CET)
> karl at aspodata.se wrote:
> 
> >Will Godfrey:
> >> Does anyone know if GCC will replace power of 2 multiplications/divisions of
> >> unsigned integers with bit shifts?  
...
> >$ cat a.c
> >#include <stdint.h>
> >
> >int main(int argc, char *argv[]) {
> >  uint16_t b = argc * 2;
> >
> >  return b;
> >}
> >$ gcc -S a.c 
,,,
> >It seems it just adds the value with itself here.
...
> Thanks, that seems to hold true for a mixture of AMD and Intel machines.

BTW, testing with 4, 8 and 16 instead of 2, it does actually do bit 
shifts:

$ diff a.02.s a.04.s 
16c16
<       addl    %eax, %eax
---
>       sall    $2, %eax
$ diff a.02.s a.08.s 
16c16
<       addl    %eax, %eax
---
>       sall    $3, %eax
$ diff a.02.s a.16.s 
16c16
<       addl    %eax, %eax
---
>       sall    $4, %eax

Regards,
/Karl Hammar


More information about the Linux-audio-dev mailing list