Quantcast
Channel: Intel® C++ Compiler
Viewing all articles
Browse latest Browse all 1616

ICC -O3 generates wrong code

$
0
0

I used ICC (icc version 14.0.3 (gcc version 4.8.1 compatibility)) on OS X 10.9.4 to compile the following code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LOOPS 100
#define TEST_LEN (2*1024*1024)

static uint32_t g_Crc32Table[256];

void GetCrc32Table()
{
	for (int i = 0; i < 256; i++) {
		uint32_t Crc = i;
		for (int j = 0; j < 8; j++) {
			if (Crc & 1) {
				Crc = (Crc >> 1) ^ 0x82F63B78;
			} else {
				Crc >>= 1;
			}
		}
		g_Crc32Table[i] = Crc;
	}
}

uint32_t GetCrc32(const uint8_t* InStr, size_t len)
{
	uint32_t crc = 0xffffffff;
	for (size_t i = 0; i < len; i++) {
		uint32_t tmp = InStr[i];
		crc = (crc >> 8) ^ g_Crc32Table[(crc ^ tmp) & 0xFF];
	}
	return crc ^ 0xBAADBEEF;
}

int main(int argc, char** argv)
{
	GetCrc32Table();
	srand(1234);
	uint8_t *b = (uint8_t*) malloc(TEST_LEN);
	for (int i = 0; i < TEST_LEN; i++) b[i] = (uint8_t) rand();

	uint32_t crc32 = 0;
	for (int i = 0; i < LOOPS; i++) {
		crc32 = GetCrc32(b, TEST_LEN);
	}
	printf("%x\n", (uint32_t) crc32);
	free(b);
}

with the following compile command line:

 /opt/intel/bin/icc -O3 -std=c99 -o t t.c

The compilation succeeded and it gave the result:

47b54328

As you can see, my code just compute the CRC32 for a generated buffer for 100 times (as defined in LOOPS). But after I changed LOOPS to 99 and used the same compile command line, it gave a different result:

f8521437

There must be something wrong. So I changed the command line to use -O2 and LOOPS to 100:

/opt/intel/bin/icc -O2 -std=c99 -o t t.c

This time it gave the correct result (f8521437). I verified with clang (version 5.1 came with Xcode 5):

clang -O3 -std=c99 -o t t.c

 


Viewing all articles
Browse latest Browse all 1616

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>