2 ms·
One great example to try: add -O3 to the compiler options, then write a function returning its argument multiplied by 2, 3, 4, ... You'll see the various ways
by JoshTriplett 10y ago
One great example to try: add -O3 to the compiler options, then write a function returning its argument multiplied by 2, 3, 4, ...
You'll see the various ways the compiler avoids emitting an actual multiplication instruction. The smallest multiplier I found that actually produced an imul: 46.
- boterock 10y agoInteresting example, I came up with clang 3.9 producing an imul with 11 though.
- JoshTriplett 10y agoWith optimization turned on? GCC produces an lea that multiplies by 5 into %rax for the return value, then another lea that multiplies that by 2 and adds the original.