This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Type-punning


> I'll try to make a simple example on which GCC produces bad code.
You can find an example below.  GCC-4.1 generates bad code (GCC-4.2 is 
fine).  Neither version give a type-punning warning, though.  I'm sorry 
that I didn't check gcc-4.2 before I started this thread.

Geza

Here's the code, I couldn't simplify it further to still have bad code 
with gcc-4.1.

gcc version: 4.1.3 20070601 (prerelease) (Debian 4.1.2-12)

#include <time.h>

class Tuple {
	public:
		union {
			int c[2];
			struct {
				int x, y;
			};
		};
	public:
		Tuple() { }
		Tuple(int x, int y) : x(x), y(y) { }
};

class Vector;

class Point: public Tuple {
	public:
		Point() { }
		Point(int x, int y) : Tuple(x, y) { }

		inline Vector operator-(const Point &other) const;
};

class Vector: public Tuple {
	public:
		Vector() { }
		Vector(int x, int y) : Tuple(x, y) { }

		Point &asPoint() {
			return reinterpret_cast<Point&>(*this);
		}

		Point toPoint() const {
			return Point(x, y);
		}
};

Vector Point::operator-(const Point &other) const {
	return Vector(x-other.x, y-other.y);
}

inline bool foo(int x, Point a) {
	if (a.x<=x) { return false; }

	return true;
}

int main() {
	Point a;

	a.x = 1;
	a.y = 2;

	time(0);

	int n = 0;

	for (int y=0; y<=44; y++) {
		if (foo(3, (a-a).asPoint())) {
			n++;
		}
	}
	return n;
}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]