An option in these cases is to compile a relevant code snippet with appropriate architecture options and see what assembly is generated.
For example, take this code.
long mod17( long x )
{
return x % 17;
}
Compiling it with gcc -mcpu=cortex-m4 -O2
, gives -
mod17(long):
ldr r3, .L3
smull r3, r2, r3, r0
asrs r3, r0, #31
rsb r3, r3, r2, asr #3
add r3, r3, r3, lsl #4
subs r0, r0, r3
bx lr
.L3:
.word 2021161081 // 0x78787879
The output assembly gives some clues that you could look for in your firmware -
- the 'magic' constant
0x78787879
- a multiplication instruction closely followed by 3 instructions containing shifts
Tests of similar code using int
instead of long
results in the same magic constant.
However, using unsigned integers results in a different magic constant - 0xF0F0F0F1
and just 1 shift instruction so you might want to look for these too.
(Compulsory godbolt link.)