Bug in gcc-20011231
Peter Barada
pbarada@mail.wm.sps.mot.com
Fri Jan 4 11:34:00 GMT 2002
>That's pretty weird. 3.0.1 on my VIA C3 box works fine without using an
>IMUL: looks like somebody patched the modulus code to perform the
>division using multiply by the reciprocal and got it wrong. I don't
>happen to have gcc-20011231
>
>If you change the code this way, does it perform the modulus using IDIV
>and thus get it right?
>
>main(int argc, char *argv[])
>{
> ufix8 result;
> ufix8 i;
>
> for (i=2; i<7; i++)
> {
> result = val % i;
> if (result != expected)
> fprintf(stderr,"Huh? modulus is %d\n", result);
> }
> return(0);
>}
>
That runs fine, and the code generated is:
.L5:
movzbl val, %eax
movzbl %bl, %ecx
movl $0, %edx
divl %ecx
cmpb expected, %dl
je .L4
Whihc is what I'd expect.
I modified the code to look like:
#include <stdio.h>
typedef unsigned char ufix8;
ufix8 val = 1;
ufix8 expected = 1;
#define CHECK(val,i,result) \
result = val % i; \
if (result != expected) { \
fprintf(stderr,"Huh? %d %% %d = %d\n", val, i, result); \
}
main(int argc, char *argv[])
{
ufix8 result;
ufix8 i;
CHECK(val, 2, result);
CHECK(val, 3, result);
CHECK(val, 4, result);
CHECK(val, 5, result);
CHECK(val, 6, result);
CHECK(val, 7, result);
CHECK(val, 8, result);
CHECK(val, 9, result);
CHECK(val, 10, result);
CHECK(val, 11, result);
CHECK(val, 12, result);
}
And the ouptut is:
Huh? 1 % 3 = 132
Huh? 1 % 5 = 198
Huh? 1 % 6 = 135
Huh? 1 % 10 = 203
Huh? 1 % 12 = 141
So modulus of 2,4,7,8,9,11 worked correctly(for these particular values).
If I change:
typedef unsigned char ufix8;
to:
typedef unsigned short ufix8;
then everything works fine, so the modulus replacement by imul plus
cleanup is broken for char types...
--
Peter Barada Peter.Barada@motorola.com
Wizard 781-852-2768 (direct)
WaveMark Solutions(wholly owned by Motorola) 781-270-0193 (fax)
More information about the Gcc
mailing list