Affichage des articles dont le libellé est gcc. Afficher tous les articles
Affichage des articles dont le libellé est gcc. Afficher tous les articles

dimanche 19 février 2017

Multiplication optimization

GCC tries its best not use the costly mutiplication mul instruction when possible, in favor of cheaper addition and shift. Multiplication by power of 2 are easy because it's a simple shift.

Some 'complex' example:

- (x * 3) can be expressed as (x + x * 2) which outputs leal (%rdi,%rdi,2), %eax

- (x * 40) is (x * 5 * 8) ie. ((x + x * 4) * 8) which gives leal (%rdi,%rdi,4), %eax + sall $3, %eax


And it seems that gcc does this trick for all factors until 42. I know.