linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
From: Linus Torvalds <torvalds@osdl.org>
To: Geoff Levand <geoffrey.levand@am.sony.com>
Cc: Geert Uytterhoeven <Geert.Uytterhoeven@sonycom.com>,
	Linux/PPC Development <linuxppc-dev@ozlabs.org>,
	linux-sparse@vger.kernel.org,
	Andrew Pinski <Andrew_Pinski@PlayStation.Sony.Com>
Subject: Re: cast truncates bits from constant value (8000000000000000 becomes 0)
Date: Fri, 1 Dec 2006 12:50:35 -0800 (PST)	[thread overview]
Message-ID: <Pine.LNX.4.64.0612011242050.3695@woody.osdl.org> (raw)
In-Reply-To: <Pine.LNX.4.64.0612011215110.3695@woody.osdl.org>



On Fri, 1 Dec 2006, Linus Torvalds wrote:
> 
> But at times, some of the gcc extensions aren't necessarily that well 
> defined or thought out, or simply not worth it. The extended type system 
> for enums in gcc is just basically messy, and it doesn't really offer you 
> anything important.

Btw, try this stupid program, to see just how _strange_ gcc enums are.. A 
sizeof of the enum is not the same as the size of the individual entries. 

Notice also how the size of the enum entry is _not_ tied to the type of 
the expression it had, but literally to its _value_. The size of "one" 
ends up being 4, even though it was initialized with a "1ll" value.

So with gcc-enums, you CANNOT get a sane type result.

In contrast, if you want sane types, you could easily do

	#define one (1ull)
	#define other (0x10000ull)
	#define strange (0x100000000ull)

and they'd all have the same type (and having the same type means that 
they act the same in expressions - you get the same expression type in 
mixing these values, _unlike_ the insane gcc enum cases)

		Linus

---
enum hello {
	one = 1ll,
	other = 0x10000,
	bigval = 0x1000000000000ll,
};

int main(int argc, char **argv)
{
	printf("%zu %zu %zu %zu\n",
		sizeof(enum hello),
		sizeof(one),
		sizeof(other),
		sizeof(bigval));
	return 0;
}

  parent reply	other threads:[~2006-12-01 20:50 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-11-22  4:20 [PATCH 8/14] powerpc: add ps3 platform repository support Geoff Levand
2006-12-01 14:25 ` cast truncates bits from constant value (8000000000000000 becomes 0) Geert Uytterhoeven
2006-12-01 14:39   ` Al Viro
2006-12-01 14:55     ` Geert Uytterhoeven
2006-12-02  7:50       ` Michael Ellerman
2006-12-01 15:30   ` Linus Torvalds
2006-12-01 16:51     ` Geoff Levand
2006-12-01 20:20       ` Linus Torvalds
2006-12-01 20:49         ` Derek M Jones
2006-12-01 21:00           ` Al Viro
2006-12-01 21:19             ` Linus Torvalds
2006-12-01 20:50         ` Linus Torvalds [this message]
2006-12-01 21:17           ` Geoff Levand

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.64.0612011242050.3695@woody.osdl.org \
    --to=torvalds@osdl.org \
    --cc=Andrew_Pinski@PlayStation.Sony.Com \
    --cc=Geert.Uytterhoeven@sonycom.com \
    --cc=geoffrey.levand@am.sony.com \
    --cc=linux-sparse@vger.kernel.org \
    --cc=linuxppc-dev@ozlabs.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).