Timeline for Unexpected behaviour with pow(2,x) in Embedded C
Current License: CC BY-SA 3.0
6 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Sep 24, 2016 at 16:29 | comment | added | Andrew Morton |
@ChrisStratton In view of the OP's "actual code" posted, would it be even faster to use an if instead of the modulo function, in conjunction with a single bit shift per loop?
|
|
Sep 24, 2016 at 15:44 | comment | added | Michael Karas | @ChrisStratton - The AVRs have very good index registers. When using the Data Indirect addressing mode the index registers X, Y and Z can be used to access the array. A decent compiler (IAR for one) can optimize a simple loop to make the loop very efficient. My main point was not to argue about whether the bit shift was better or not but to point out the not desirable inclusion of code heavy math computation routines inside a program on a relatively simple processor. | |
Sep 24, 2016 at 4:11 | comment | added | Chris Stratton | Good point (And another reason not to chose an AVR). In the code in question though, the shift count is iteratively increasing so the actual fastest would be to use the single bit shift instruction once per cycle of the loop. | |
Sep 24, 2016 at 4:06 | comment | added | pipe | @ChrisStratton AFAIK, there's no barrel shifter in the ATmega2560, so when the index is unknown at compile-time, an indexed lookup is likely faster. | |
Sep 24, 2016 at 3:28 | comment | added | Chris Stratton | An indexed memory operation like this will actually be quite a bit slower than a bit shift in the ALU. But if the calculation were instead a different one more costly compute, this could be a good idea. | |
Sep 24, 2016 at 3:08 | history | answered | Michael Karas | CC BY-SA 3.0 |