Thread overview
RDTSCP from dlang
Aug 30, 2016
kookman
Aug 30, 2016
rikki cattermole
Aug 30, 2016
Basile B.
Aug 31, 2016
kookman
Aug 31, 2016
rikki cattermole
Aug 31, 2016
kookman
Aug 31, 2016
rikki cattermole
Aug 31, 2016
Basile B.
Aug 31, 2016
kookman
August 30, 2016
I need to access the x86_64 RDTSCP assembly instruction from D.

I found this for C++:
http://stackoverflow.com/questions/14783782/which-inline-assembly-code-is-correct-for-rdtscp

Does anyone here know how (if?) I can do this from D?
August 30, 2016
On 30/08/2016 2:04 PM, kookman wrote:
> I need to access the x86_64 RDTSCP assembly instruction from D.
>
> I found this for C++:
> http://stackoverflow.com/questions/14783782/which-inline-assembly-code-is-correct-for-rdtscp
>
>
> Does anyone here know how (if?) I can do this from D?

It appears dmd's inline assembler does not support it.

So a workaround:

asm {
	// rdtscp
	db 0x0F;
	db 0x01;
	db 0xF9;
}

http://dlang.org/spec/iasm.html
August 30, 2016
On Tuesday, 30 August 2016 at 02:04:55 UTC, kookman wrote:
> I need to access the x86_64 RDTSCP assembly instruction from D.
>
> I found this for C++:
> http://stackoverflow.com/questions/14783782/which-inline-assembly-code-is-correct-for-rdtscp
>
> Does anyone here know how (if?) I can do this from D?

ALternatively to Rikki K's solution, you can do this to mimic the rdtscp behavior:

asm
{
  cpuid;
  rdtsc;
  // store time in locals
}
// bench
{
  rdtsc;
  // store time in locals
}
// compute delta


explanations here:
- http://www.intel.com/content/dam/www/public/us/en/documents/white-papers/ia-32-ia-64-benchmark-code-execution-paper.pdf
- http://stackoverflow.com/a/14214220

But according to the first link, this solution, while better than rdtsc alone, is not as good as rdtscp.
August 31, 2016
On Tuesday, 30 August 2016 at 09:04:41 UTC, Basile B. wrote:
> ALternatively to Rikki K's solution, you can do this to mimic the rdtscp behavior:
>
> asm
> {
>   cpuid;
>   rdtsc;
>   // store time in locals
> }
> // bench
> {
>   rdtsc;
>   // store time in locals
> }
> // compute delta
>
>
> explanations here:
> - http://www.intel.com/content/dam/www/public/us/en/documents/white-papers/ia-32-ia-64-benchmark-code-execution-paper.pdf
> - http://stackoverflow.com/a/14214220
>
> But according to the first link, this solution, while better than rdtsc alone, is not as good as rdtscp.

Indeed, I want to use rdtscp to get access to the core ID it makes available (to at least know when tsc is from different cores/packages). I guess in the meantime I can use Rikki's solution.

Sorry I'm a DMD asm newbie: DO i need to worry about which registers I clobber here? I couldn't find any info about this on the Inline Assembly doco page on dlang.org.
August 31, 2016
On 31/08/2016 7:34 PM, kookman wrote:
> On Tuesday, 30 August 2016 at 09:04:41 UTC, Basile B. wrote:
>> ALternatively to Rikki K's solution, you can do this to mimic the
>> rdtscp behavior:
>>
>> asm
>> {
>>   cpuid;
>>   rdtsc;
>>   // store time in locals
>> }
>> // bench
>> {
>>   rdtsc;
>>   // store time in locals
>> }
>> // compute delta
>>
>>
>> explanations here:
>> -
>> http://www.intel.com/content/dam/www/public/us/en/documents/white-papers/ia-32-ia-64-benchmark-code-execution-paper.pdf
>>
>> - http://stackoverflow.com/a/14214220
>>
>> But according to the first link, this solution, while better than
>> rdtsc alone, is not as good as rdtscp.
>
> Indeed, I want to use rdtscp to get access to the core ID it makes
> available (to at least know when tsc is from different cores/packages).
> I guess in the meantime I can use Rikki's solution.
>
> Sorry I'm a DMD asm newbie: DO i need to worry about which registers I
> clobber here? I couldn't find any info about this on the Inline Assembly
> doco page on dlang.org.

http://dlang.org/spec/abi.html#register_conventions
August 31, 2016
On Wednesday, 31 August 2016 at 07:36:16 UTC, rikki cattermole wrote:

> http://dlang.org/spec/abi.html#register_conventions

That link talks about for functions defined extern(C) and extern(D), and gives specific info for win32.

I'm using linux x86_64, does that mean I can assume standard x86_64 ABI? ie even if the function is not extern?
August 31, 2016
On 31/08/2016 7:49 PM, kookman wrote:
> On Wednesday, 31 August 2016 at 07:36:16 UTC, rikki cattermole wrote:
>
>> http://dlang.org/spec/abi.html#register_conventions
>
> That link talks about for functions defined extern(C) and extern(D), and
> gives specific info for win32.
>
> I'm using linux x86_64, does that mean I can assume standard x86_64 ABI?
> ie even if the function is not extern?

That link describes extern(D) for x86(_64) for all platforms. Keep in mind if a function body does not have an extern(X) supplied, the default is extern(D).

C is listed there as we have first class C interaction via extern(C).

The listing of DLL's on that page really should be changed to dynamic libraries. Since that covers linuxes.
August 31, 2016
On Wednesday, 31 August 2016 at 07:34:14 UTC, kookman wrote:
> On Tuesday, 30 August 2016 at 09:04:41 UTC, Basile B. wrote:
> Indeed, I want to use rdtscp to get access to the core ID it makes available (to at least know when tsc is from different cores/packages). I guess in the meantime I can use Rikki's solution.
>
> Sorry [...]

Never mind, I know Rikki's solution is better but since I've read a bit of doc and found this alternative I've just posted the alternative.

By the way maybe someone could post an ER in bugzilla to get RDTSCP available in iasm w/o using the byte code trick.

August 31, 2016
On Wednesday, 31 August 2016 at 08:23:57 UTC, Basile B. wrote:

> By the way maybe someone could post an ER in bugzilla to get RDTSCP available in iasm w/o using the byte code trick.

Someone beat me to it, but see here:
https://issues.dlang.org/show_bug.cgi?id=16449