Arduino Arithmetic Acceleration Attempts... AVR Aces' Advice Appealed!

alpha_ninja wrote 05/31/2015 at 19:53 3 points

Alliterations are awesome! Anyway, Arduino Arithmetic appears altered by the size of variables. This makes sense, but I can't understand why it varies just the way it does.

I was starting to look into the speed of operations on ATMega328 for my project, Charles Jr.

However, some peculiarities arised when I ran some tests...

(100k operations per reading, reading in microseconds, I'm assigning the value to another variable, which explains the high base time for *2.)

Code link:  https://gist.github.com/alpha-ninja/befa075e2ae81810100f

----------------- BYTE
add 2x:      81904
add 3x:      107012
add 4x:      88144
multiply x2: 81856
multiply x3: 107008
multiply x4: 88148
square:      100724
cube:        125872
hypercube:   125872
----------------- UINT
add 2x:      88304
add 3x:      119588
add 4x:      100728
multiply x2: 88148
multiply x3: 119584
multiply x4: 100720
square:      138448
cube:        201332
hypercube:   176176
----------------- ULONG
add 2x:      100892
add 3x:      415128
add 4x:      163604
multiply x2: 100728
multiply x3: 415120
multiply x4: 163604
square:      578608
cube:        1138240
hypercube:   1050216

For BYTE:

add 2x, multiply x2, add 4x, multiply x4 — all are low because of shifting. makes sense.

However: why is cube == hypercube???  x*x*x should be faster than x*x*x*x, right?

For UINT:

why does shifting by 2 places take longer than shifting by one?

Why is hypercube faster than cube? This might be similar for the same thing for BYTE...

For ULONG:

Again, why does shifting by 2 places take so much longer?

And: Why is add 3x / multiply x3 suddenly so much faster than shifting?

Again, why is hypercube faster (albeit less than before...) than cube?