OK, I'm stumped. I'm also up way past my bedtime.
The following code is supposed to calculate the average of a triplet of readings which has been previously summed, then display them in the form "2.3,-4.9, 3.3". For some reason this step seems to not be working (I've already verified that the summation is happening correctly).
Integers are 32-bit. _count is incremented each time a triplet is summed into _readings (not shown here). _count is expected to be in the 50-500 range.
_scale = 20000.
Positive numbers seem to be getting calculated correctly, but negative numbers are getting turned into numbers around 400. Given the nature of the beast, controlling the numbers going in isn't really in the cards (unless I get desperate).
The generated machine code (ARM) looks absolutely wacky to me -- I suspect clever optimizations, but I'm hoping they're not TOO clever.
I've been over this about a bazzilion times looking for overflows, and can't see any opportunities.
Help?
{ int readBuf[3];
for (size_t n = 0; n < 3; ++n) { readBuf[n] = (_scale * ((_readings[n] + _count / 2) / _count) +
32768) / 65536; _readings[n] = 0; }_count = 0;
sprintf(_scratchText, "%2d.%d,%2d.%d,%2d.%d", readBuf[0] / 10, readBuf[0] % 10, readBuf[1] / 10, readBuf[1] % 10, readBuf[2] / 10, readBuf[2] % 10);
return _scratchText; }