I encountered this in a larger program and reduced it to this example. I'm not sure what triggers the problem and I didn't try to reduce it further.
In the program below the values for ar and w4 are computed two different ways which should result in the same value for w4. In the first case (the #if 1 as shown) the compiler seems to think the value of ar is 0 and warns about the division in the follow line and the value of w4 is wrong. In the second case (the #else as shown) the reciprocal of ar is computed and w4 is computed using * ar instead of /ar and in this case there is no warning and the value of w4 is correct.
#include <stdio.h>
int main(int argc, char **argv)
{
int mx = 384, x0 = 1, x1 = (mx - 1) - 1;
int my = 192, y0 = 1, y1 = (my - 1) - 8;
float w1 = -0.866, w3 = -0.791, w2 = -0.24;
#if 1
float ar = (float)(my*4)/(float)(mx*3);
float w4 = (w3 - w1)*(float)(y1 - y0)/(float)(x1 - x0)/ar + w2;
#else
float ar = (float)(mx*3)/(float)(my*4);
float w4 = (w3 - w1)*(float)(y1 - y0)/(float)(x1 - x0)*ar + w2;
#endif
printf("%f: %f < x < %f %f < y < %f\n", ar, w1, w3, w2, w4);
return 0;
}
sdcc -mz80 --std-sdcc99 -c test.c
test.c:17: warning 122: dividing by 0
test.c:17: warning 85: in function main unreferenced function argument : 'argc'
test.c:17: warning 85: in function main unreferenced function argument : 'argv'
Here is the output. Note the first value is ar is correct and the last value is w4 is wrong.
0.666667: -0.866000 < X < -0.791000 -0.240000 < Y < 0.426667
If I change the #if 1 to #if 0 to select the calculation using the reciprocal I don't get the warning and w4 is correct.
sdcc -mz80 --std-sdcc99 -c test.c
test.c:17: warning 85: in function main unreferenced function argument : 'argc'
test.c:17: warning 85: in function main unreferenced function argument : 'argv'
1.500000: -0.866000 < X < -0.791000 -0.240000 < Y < -0.186260
This is using 4.2.0 Windows 64 bit
sdcc -v
SDCC : mcs51/z80/z180/r2k/r2ka/r3ka/sm83/tlcs90/ez80_z80/z80n/ds390/pic16/pic14/
TININative/ds400/hc08/s08/stm8/pdk13/pdk14/pdk15/mos6502 4.2.0 #13081 (MINGW64)
published under GNU General Public License (GPL)
but I first encountered it with my Debian linux 64 bit which has version 3.8.0
sdcc -mz80 --std-sdcc99 -c test.c
test.c:17: warning 122: dividing by ZERO
test.c:17: warning 85: in function main unreferenced function argument : 'argc'
test.c:17: warning 85: in function main unreferenced function argument : 'argv'
sdcc -v
SDCC : mcs51/z80/z180/r2k/r3ka/gbz80/tlcs90/ds390/TININative/ds400/hc08/s08/stm8 3.8.0 #10562 (Linux)
published under GNU General Public License (GPL)
I just tried it with 4.2.0 to see if it had been fixed but apparently it hasn't.
Matt
Fixed in [r13855].