Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the equivalent of rdtsc opcode for PPC?

I have an assembly program that has the following code. This code compiles fine for a intel processor. But, when I use a PPC (cross)compiler, I get an error that the opcode is not recognized. I am trying to find if there is an equivalent opcode for PPC architecture.

.file   "assembly.s"
.text
.globl func64
.type   func64,@function
func64:
    rdtsc
    ret

.size   func64,.Lfe1-func64
.globl func
.type   func,@function
func:
    rdtsc
    ret
like image 624
Rob Avatar asked May 04 '11 12:05

Rob


2 Answers

When you need a 64-bit value on a 32-bit architecture (not sure how it works on 64-bit) and you read the TB register you can run into the problem of the lower half going from 0xffffffff to 0 - granted this doesn't happen often but you can be sure it will happen when it will do the most damage ;)

I recommend you read the upper half first, then the lower and finally the upper again. Compare the two uppers and if they are equal, no problemo. If they differ (the first should be one less than the last) you have to look at the lower to see which upper it should be paired with: if its highest bit is set it should be paired with the first, otherwise with the last.

like image 20
Olof Forshell Avatar answered Sep 20 '22 15:09

Olof Forshell


PowerPC includes a "time base" register which is incremented regularly (although perhaps not at each clock -- it depends on the actual hardware and the operating system). The TB register is a 64-bit value, read as two 32-bit halves with mftb (low half) and mftbu (high half). The four least significant bits of TB are somewhat unreliable (they increment monotonically, but not necessarily with a fixed rate).

Some of the older PowerPC processors do not have the TB register (but the OS might emulate it, probably with questionable accuracy); however, the 603e already has it, so it is a fair bet that most if not all PowerPC systems actually in production have it. There is also an "aternate time base register".

For details, see the Power ISA specification, available from the power.org Web site. At the time of writing that answer, the current version was 2.06B, and the TB register and opcodes were documented at pages 703 to 706.

like image 193
Thomas Pornin Avatar answered Sep 21 '22 15:09

Thomas Pornin