mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Richard B. Johnson" <root@chaos.analogic.com>
To: "J.A. Magallon" <jamagallon@able.es>
Cc: Antonio Vargas <wind@cocodriloo.com>,
	Lista Linux-Kernel <linux-kernel@vger.kernel.org>
Subject: Re: [2.4] gcc3 warns about type-punned pointers ?
Date: Fri, 29 Aug 2003 15:41:32 -0400 (EDT)	[thread overview]
Message-ID: <Pine.LNX.4.53.0308291517001.32044@chaos> (raw)
In-Reply-To: <20030829184847.GA2069@werewolf.able.es>

On Fri, 29 Aug 2003, J.A. Magallon wrote:

>
> On 08.29, Antonio Vargas wrote:
> > On Fri, Aug 29, 2003 at 12:35:11AM +0200, J.A. Magallon wrote:
> [...]
> > >
> > > A collateral question: why is the reason for this function ?
> > > long long assignments are not atomic in gcc ?
> >
> > On x86, long long int == 64 bits but the chip is 32 bits wide,
> > so it uses 2 separate memory accesses. There are 64bit-wide
> > instructions which do bus-locking so that the are atomic,
> > but gcc will not use them directly.
> >
>
> I know, my question was why gcc does not generate cmpxchg8b on
> a 64 bits assign. Or it should not ?
>

It's not an assignment operator. The fact that you 'could' use
it as one is not relevant. For instance, using XOR you can
exchange the values of two operands. However, you would not
really like a 'C' compiler to do that. Instead, you would
expect it to stash some invisible temporary variable some-
where, hopefully in a register. If you really want to
swap values using the ^ operator, then you can code it yourself.


Wana play?

int main()
{
    int a, b;
    a = 0xaaaaaaaa;
    b = 0xbbbbbbbb;
   printf("a = %08x b = %08x \n", a, b);
// Swap
   a ^= b;
   b ^= a;
   a ^= b;
   printf("a = %08x b = %08x \n", a, b);
    return 0;
}

The generated code is awful:

	movl -8(%ebp),%edx
	xorl %edx,-4(%ebp)
	movl -4(%ebp),%edx
	xorl %edx,-8(%ebp)
	movl -8(%ebp),%edx
	xorl %edx,-4(%ebp)
	movl -8(%ebp),%eax

gcc doesn't care that some xchg operations are atomic. If there
was an 'atomic_t' type that 'C' (generically) knew about, then
the code-generator might try to find some strange sequence that
would perform 64-bit atomic operations on a 32-bit processor as
a side-effect, which is what it is with the compare/exchange-8-bytes
opcode.

Cheers,
Dick Johnson
Penguin : Linux version 2.4.22 on an i686 machine (794.73 BogoMips).
            Note 96.31% of all statistics are fiction.



  reply	other threads:[~2003-08-29 19:40 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-08-28 22:35 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 [this message]
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

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.53.0308291517001.32044@chaos \
    --to=root@chaos.analogic.com \
    --cc=jamagallon@able.es \
    --cc=linux-kernel@vger.kernel.org \
    --cc=wind@cocodriloo.com \
    /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

all inboxes | Powered by JetHome®