This is the mail archive of the 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: Letter to DDJ

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));
  return result;



From: Andrew Haley <>
Subject: Optimization Techniques
X-Mailer: VM 7.14 under Emacs

Dear DDJ,

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"
		    : "=A"(result));
  return result;

(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
          significant bits.

The "volatile" is essential because it tells gcc that the result might
change; without this, gcc may only invoke readTSC() once.


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