Multiplying with a constant
xania.org·4d
🧮Compute Optimization
Preview
Report Post

Written by me, proof-read by an LLM. Details at end.

So far we’ve covered addition, and subtraction mostly follows suit. There’s an obvious next step: multiplication. Specifically, let’s try multiplying by constants on x861. We’ll try several constants: 2, 3, 4, 16, 25 and 522.

Before you look at the code below, make your predictions for what instructions the compiler will pick, then see if you were right or not. Let’s start with x86:

So - it’s mostly lea again! By this point maybe you feel like the Square Hole Girl, for each of those multiplies I bet you thought, “it will use the mul instruction, surely?” Or, for the powers of two, a shift, right? As you probably know, generally speaking adds and shifts are much…

Similar Posts

Loading similar posts...