Undefined compiler behaviour or a compiler bug?

From: Hans Petter Selasky <hps_at_selasky.org>
Date: Thu, 29 Apr 2021 23:06:42 +0200
Hi,

Can someone please explain what C-compiler flag I'm missing, to make 
this simple C-program terminate?

I have a function _abs() which at some point is equal to one, but the 
compiler thinks it is not required to emit the test for the while() at 
all, looking at the assembly code! A printf() clearly verifies that the 
_abs() function has returned one.

I even added an own if() check, but apparently the compiler is lost!

cat << EOF > test.c
#include <stdio.h>
#include <stdint.h>

typedef struct {
	int32_t x;
	int32_t y;
} c32_t;

static c32_t
_mul(c32_t a, c32_t b)
{
	return (c32_t){
		a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x
	};
}

static uint32_t
_abs(c32_t a)
{
	return (a.x * a.x + a.y * a.y);
}

int
main()
{
	c32_t a = {4, 1};
	c32_t b = {};
	uint32_t l1 = 0;

	while (_abs(a) != 1) {
		uint32_t l0 = _abs(a) & ~1;

		/* figure out least significant bit */
		l0 &= ~(l0 - 1);

		if (l0 == 0 || l0 > l1) {
			l1 = l0;
			if (_abs(a) == 1)
				printf("VALUE IS ONE\n");
			printf("Y:0x%08x ABS:0x%08x\n", a.y, _abs(a));
		}
		a.y += 2;
	}

	size_t count = 0;

	do {
		count++;
		b = _mul(b, a);
	} while (b.x != 1 || b.y != 0);

	printf("%d,%d,0x%x COUNT=%zd\n", a.x, a.y, _abs(a), count);

	return (0);
}
EOF

clang11 -O2 test.c && ./a.out

--HPS
Received on Thu Apr 29 2021 - 19:08:08 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:41:28 UTC