NO

Author Topic: Possible bug with integer?  (Read 510 times)

Offline bitcoin

  • Member
  • *
  • Posts: 95
Possible bug with integer?
« on: July 22, 2019, 10:54:12 pm »
I have algo to compute CRC32. It similar to ntdll.RtlComputeCrc32 , but in Pelles it return another number. In VS all ok. Why? This is bug or feature?

So, code is

Code: [Select]
unsigned int crc32(unsigned int seed, void *msg, size_t len)
{
unsigned int crc = ~seed;

while (len--) {
crc ^= *((unsigned char *)msg)++;

for (size_t i = 0; i < 8; ++i)
crc = (crc >> 1) ^ (0xEDB88320 & ~(crc & 1) + 1);
}

return ~crc;
}

VS return 1267612143, RtlComputeCrc32 return 1267612143 , but Pelles return 3818104785

Code: [Select]
tmp = crc32(0, "abcdef", 6);

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 1893
Re: Possible bug with integer?
« Reply #1 on: July 23, 2019, 08:27:28 am »
a fixed version ?
Code: [Select]
unsigned int crc32(unsigned int seed, void *msg, size_t len)
{
unsigned int crc = ~seed;
unsigned char *p = msg;

while (len--) {
//crc ^= *((unsigned char *)msg)++;
crc ^= *(p++);

for (size_t i = 0; i < 8; ++i)
crc = (crc >> 1) ^ (0xEDB88320 & (~(crc & 1) + 1));
}

return ~crc;
}
clang:
Code: [Select]
test_crc.c(9,4): error: assignment to cast is illegal, lvalue casts are not supported
                ((unsigned char *)msg)++;
                ~^~~~~~~~~~~~~~~~~~~~~~~
« Last Edit: July 23, 2019, 08:42:53 am by TimoVJL »
May the source be with you

Offline bitcoin

  • Member
  • *
  • Posts: 95
Re: Possible bug with integer?
« Reply #2 on: July 23, 2019, 02:27:56 pm »
Yes, it works! I don't know C well.