[LAD] Bit shift

karl at aspodata.se karl at aspodata.se
Wed Mar 13 00:09:17 CET 2019


Will Godfrey:
> Does anyone know if GCC will replace power of 2 multiplications/divisions of
> unsigned integers with bit shifts?

Test on your system:

$ cat a.c
#include <stdint.h>

int main(int argc, char *argv[]) {
  uint16_t b = argc * 2;

  return b;
}
$ gcc -S a.c 
$ cat a.s 
...
main:
.LFB0:
        .cfi_startproc
        pushq   %rbp
        .cfi_def_cfa_offset 16
        .cfi_offset 6, -16
        movq    %rsp, %rbp
        .cfi_def_cfa_register 6
        movl    %edi, -20(%rbp)
        movq    %rsi, -32(%rbp)
        movl    -20(%rbp), %eax
        addl    %eax, %eax
        movw    %ax, -2(%rbp)
        movzwl  -2(%rbp), %eax
        popq    %rbp
        .cfi_def_cfa 7, 8
        ret
        .cfi_endproc
...
$

It seems it just adds the value with itself here.

Regards,
/Karl Hammar


More information about the Linux-audio-dev mailing list