Jump to content
Tommi Prami

FYI: Stumbled upon interesting ASM optimization trick LLVm can do (most likely others also)

Recommended Posts

Posted (edited)

image.thumb.png.65318404e5157495e03a208928ab7c0f.png

Apparently LAEL ("address calculator") can do multiply by 2, 4 and 8 very fast. 

Don't know anything about details, difference to other op codes etc...

Subject starts from here, at C to llvm IR -> to asm : 

 

-Tee-

Edited by Tommi Prami
Link added

Share this post


Link to post

Dang, watching this led me to buy yet another book 🤖

  • Haha 1

Share this post


Link to post
15 minutes ago, Lars Fosdal said:

Dang, watching this led me to buy yet another book 🤖

Hackers Delight?

  • Like 1

Share this post


Link to post

I am afraid so, even if it probably is somewhat outdated by now, in the light of new CPUs.

  • Like 1

Share this post


Link to post
Posted (edited)
22 hours ago, Lars Fosdal said:

I am afraid so, even if it probably is somewhat outdated by now, in the light of new CPUs.

That might be true, I should have it somewhere. Should read it again. I have only checked few specific things from it.

 

Edited by Tommi Prami
Typo

Share this post


Link to post
1 hour ago, Lars Fosdal said:

I am afraid so, even if it probably is somewhat outdated by now, in the light of new CPUs.

I have no idea what is in that book, but I doubt that it is very much obsolete. Math behind it does not change. Nor the fact that some operations will always be more expensive than others, no matter the CPU.

Share this post


Link to post
1 hour ago, Dalija Prasnikar said:

I have no idea what is in that book, but I doubt that it is very much obsolete.

I have it and many of the techniques in it that were great 10-15 years ago are totally unnecessary today and even detrimental with modern CPUs.

It's still a good read and even though some techniques are obsolete, knowing them can help solving other similar problems simply because they can make you think about problems differently.

 

 

2 hours ago, Dalija Prasnikar said:

Nor the fact that some operations will always be more expensive than others, no matter the CPU.

Um... Always? There used to be a time, not that long ago, when integer math was far superior to floating point math (see: Hackers Delight). Not so anymore.

via https://stackoverflow.com/questions/2550281/floating-point-vs-integer-calculations-on-modern-hardware

 

 

3 hours ago, Tommi Prami said:

Apparently LAEL ("address calculator") can do multiply by 2, 4 and 8 very fast. 

The video is a bit long so I haven't watched it yet, but it's LEA: Load Effective Address, "LEAL" is AT&T syntax for LEA (Intel syntax).

Here's a good comment on the topic by Peter Cordes: Using LEA on values that aren't addresses / pointers?

 

Before you start replacing all your shifts with LEA you should be aware that it isn't always faster. As with almost everything in modern CPUs it depends on what the CPU is otherwise busy with.

https://stackoverflow.com/questions/70316686/assembly-why-is-lea-eax-eax-eaxconst-shl-eax-eax-const-combined-fast

  • Thanks 2

Share this post


Link to post
37 minutes ago, Anders Melander said:

It's still a good read and even though some techniques are obsolete, knowing them can help solving other similar problems simply because they can make you think about problems differently.

This is always the greatest benefit.

 

42 minutes ago, Anders Melander said:

Um... Always? There used to be a time, not that long ago, when integer math was far superior to floating point math (see: Hackers Delight). Not so anymore.

Integer math is still faster. Just not that much. And division is still more expensive than multiplication.

 

Anyway, when it comes to any optimizations, it always goes hand in hand with measuring. and making sure that you really need optimization in the first place and are not making a bad trade-off by complicating the code.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×