public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: "J.A. Magallon" <jamagallon@able.es>
To: Lista Linux-Kernel <linux-kernel@vger.kernel.org>
Subject: Re: [2.4] gcc3 warns about type-punned pointers ?
Date: Sun, 31 Aug 2003 03:27:59 +0200	[thread overview]
Message-ID: <20030831012759.GA3276@werewolf.able.es> (raw)
In-Reply-To: 20030828223511.GA23528@werewolf.able.es


On 08.29, J.A. Magallon wrote:
> Hi all...
> 
> gcc3 gives this warning when using the __set_64bit_var function:
> 
> /usr/src/linux/include/asm/system.h:190: warning: dereferencing type-punned pointer will break strict-aliasing rules
> 
> Is it a potential problem ?
> 
> This seems to cure it:
> 
> --- linux-2.4.22-jam1m/include/asm-i386/system.h.orig	2003-08-29 00:26:41.000000000 +0200
> +++ linux-2.4.22-jam1m/include/asm-i386/system.h	2003-08-29 00:26:55.000000000 +0200
> @@ -181,8 +181,8 @@
>  {
>  	__set_64bit(ptr,(unsigned int)(value), (unsigned int)((value)>>32ULL));
>  }
> -#define ll_low(x)	*(((unsigned int*)&(x))+0)
> -#define ll_high(x)	*(((unsigned int*)&(x))+1)
> +#define ll_low(x)	*(((unsigned int*)(void*)&(x))+0)
> +#define ll_high(x)	*(((unsigned int*)(void*)&(x))+1)
>  

How about something like this:

typedef unsigned long long	u64;
typedef unsigned long		u32;
typedef unsigned short		u16;
typedef unsigned char		u8;

union u64_split {
	u64	ll;
	u32 l[2];
	u16 w[4];
	u8  b[8];
};

void f()
{
	u64 a;

	int l = ((union u64_split)a).l[0];
	int h = ((union u64_split)a).l[1];
}

The clean way todo it without pointer arithmetic...

And with a modern gcc (anonymous structs):

union u64_split {
	u64	x;
	struct {
		u32 	l32,h32;
	};
	struct {
		u16 ll16,lh16,hl16,hh16;
	};
};

So you just write:

int l = ((union u64_split)a).l32;
u16 word = ((union u64_split)a).ll16;

I think it is better with structs, to take care of endianness if needed.

-- 
J.A. Magallon <jamagallon@able.es>      \                 Software is like sex:
werewolf.able.es                         \           It's better when it's free
Mandrake Linux release 9.2 (Cooker) for i586
Linux 2.4.22-jam1m (gcc 3.3.1 (Mandrake Linux 9.2 3.3.1-1mdk))

      parent reply	other threads:[~2003-08-31  1:28 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-08-28 22:35 [2.4] gcc3 warns about type-punned pointers ? J.A. Magallon
2003-08-29 15:24 ` Antonio Vargas
2003-08-29 18:48   ` J.A. Magallon
2003-08-29 19:41     ` Richard B. Johnson
2003-08-30  6:27       ` Antonio Vargas
2003-08-30  9:56         ` Willy Tarreau
2003-08-30 11:31           ` Jamie Lokier
2003-08-30  2:37 ` insecure
2003-08-30 12:33   ` Mikulas Patocka
2003-08-30 13:09     ` Gabriel Paubert
2003-08-30 23:27     ` insecure
2003-08-31  1:27 ` J.A. Magallon [this message]

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=20030831012759.GA3276@werewolf.able.es \
    --to=jamagallon@able.es \
    --cc=linux-kernel@vger.kernel.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