This is the mail archive of the
mailing list for the GCC project.
Re: Letter to DDJ
- From: Joel Sherrill <joel dot sherrill at OARcorp dot com>
- To: Andrew Haley <aph at gcc dot gnu dot org>
- Cc: gcc at gcc dot gnu dot org
- Date: Tue, 04 May 2004 12:55:11 -0500
- Subject: Re: Letter to DDJ
- Organization: OAR Corporation
- References: <200405041648.i44GmOuI010642@redhat.com>
Andrew Haley wrote:
The latest DDJ contains a horrible example of GCC extended asm.
Here's my leter to the editor in response.
I don't think I made any mistakes with my extended asm, but I thought
I'd better run it by y'all before sending it.
RTEMS has essentially the same code with two differences (1) we
have it returning an unsigned long long and (2) it has been in RTEMS
so long that gas must not have known the rdtsc instructions.
So we have:
static inline unsigned long long
unsigned long long result;
asm volatile(".byte 0x0F, 0x31" : "=A" (result));
From: Andrew Haley <email@example.com>
Subject: Optimization Techniques
X-Mailer: VM 7.14 under Emacs 184.108.40.206
Tom Kientzle's fascinating article "Optimization Techniques" contains
a rather long-winded and complicated defintion for readTSC() in GCC.
The simple way to do it in GCC is
inline long long readTSC(void)
long long result;
__asm__ volatile ("rdtsc"
(Of course, you may as well use "inline" here because there's no point
in making a call to a single-instruction subroutine!)
The "=A" constraint tells gcc that the result is returned in EDX:EAX,
as per the gcc documentation for Machine Constraints:
Specifies the `a' or `d' registers. This is primarily useful
for 64-bit integer values (when in 32-bit mode) intended to
be returned with the `d' register holding the most
significant bits and the `a' register holding the least
The "volatile" is essential because it tells gcc that the result might
change; without this, gcc may only invoke readTSC() once.