News for hackers. Stuff that matters.
Re:Dangerous Embedded C Coding Standard Rules (2011) by __david__ on Monday December 30, 2013 @11:49AM (#6985342)
Even better rule: Know the limitations of your target processor and compiler and code accordingly.

Example 1: Some (bad) compilers just ignore the const keyword (and therefore treat consts like normal variables). Use #defines with these compilers (or get a better compiler).

Example 2: Some (older, smaller) processors don't have an integer division instruction. Avoid division at all costs on these processors unless you are not worried about space or execution speed.

Re:Dangerous Embedded C Coding Standard Rules (2011) by twoodfin on Monday December 30, 2013 @12:31PM (#6985618)
Some (older, smaller) processors don't have an integer division instruction. Avoid division at all costs on these processors unless you are not worried about space or execution speed.

...unless you know your compiler is capable of converting the division into cheaper operations via strength reduction, per your well-considered rule.

Re:Dangerous Embedded C Coding Standard Rules (2011) by kps on Monday December 30, 2013 @12:45PM (#6985718)
… which will (normally) only happen if the dividend is unsigned.

Another reason to prefer unsigned ints is that the C standards make signed overflow undefined. This is becoming increasingly important as code for embedded systems is built using ‘big’ optimizing compilers, rather than the simple ‘portable assemblers’ assumed by the old old rules of thumb like manually converting divides to shifts.

  “If you lie to the compiler, it will get its revenge.” — Henry Spencer
Re:Dangerous Embedded C Coding Standard Rules (2011) by nitrogen on Monday December 30, 2013 @01:05PM (#6985852)
Another option is reciprocal multiplication, possibly with a larger integer type. Reciprocal multiplication is an important element of my software's ability to run a Kinect at ~30fps on an ARM cpu with no FPU and no division instruction.
Re:Dangerous Embedded C Coding Standard Rules (2011) by Joeboy on Monday December 30, 2013 @11:55AM (#6985387)
> Example 2: Some (older, smaller) processors don't have an integer division instruction. Avoid division at all costs on these processors unless you are not worried about space or execution speed.

Ok, so steer clear of division on ARM CPUs.

Re:Dangerous Embedded C Coding Standard Rules (2011) by zhemao on Monday December 30, 2013 @12:56PM (#6985794)
Depends on the ARM CPU. Some variants of ARMv7 architecture have an integer divide function.

https://en.wikipedia.org/wiki/ARM_architecture#Arithmetic_in...

Re:Dangerous Embedded C Coding Standard Rules (2011) by __david__ on Monday December 30, 2013 @06:29PM (#6987830)
> Ok, so steer clear of division on ARM CPUs.

Don't ignore the last part of the sentence:

> …unless you are not worried about space or execution speed.

I'd argue that on ARMs you're generally not worried about execution speed (as most run pretty fast nowadays), nor space (RAMs and flashes generally have lots of space).

Now, do you have a really slow ARM, or are you running in a really constrained space (like a tiny metal-mask ROM)? Then yes, avoid it.