-1

I have following code which runs perfectly in x86/linux. I want to convert this code into ALPHA using gcc cross compiler. It is generating errors like the following:

unknown register name 'eax' in 'asm'

I couldnt find proper sources to do on my own.

 inline uint64_t timestamp(void)
    {
        unsigned long a;
        unsigned long d;
        asm volatile("xorl %%eax,%%eax\n cpuid \n" ::: "%eax", "%ebx", "%ecx", "%edx"); // flush pipeline
        asm volatile("rdtsc\n" : "=a" (a), "=d" (d) );                           // read rdtsc
        asm volatile("xorl %%eax,%%eax\n cpuid \n" ::: "%eax", "%ebx", "%ecx", "%edx"); // flush pipeline again    
        return a | ((uint64_t)d << 32);
    }

I hardly found that rpcc is equivalent instruction for rdts. cpuid and xorl are the same i guess. But eax, ebx are the registers which are specific to x86, not in ALPHA. Alpha ISA registers are numbered from 0 to 31 like here

Can some one please do convert the above code into ALPHA or can suggest me how to do it, by providing some links with reasonable information?

It will be good enough if I know how to convert following line at least into ALPHA inline:

 asm volatile ("rdtsc" : "=a" (a), "=d" (d) : : "ebx", "ecx");

Thank you

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
ANTHONY
  • 333
  • 5
  • 18
  • 3
    It is not as simple as changing the register's name. `cpuid` and `rdtsc` are x86-specific instructions. You will have to determine what a suitable Alpha equivalent is for those instructions, and there may not even be one. A better idea would be to just use a portable system API to obtain this information. Do you *really* need the precision of a real-time clock? And if you do, does your target operating system *really* not provide a portable way to query it? – Cody Gray - on strike Jun 27 '16 at 12:08
  • While I don't speak alpha, I see a `__builtin_alpha_rpcc` in the gcc [docs](https://gcc.gnu.org/onlinedocs/gcc/Alpha-Built-in-Functions.html). Builtins are usually a better choice than inline asm. – David Wohlferd Jun 27 '16 at 22:58
  • @CodyGray: yeah. , i need precision. – ANTHONY Jun 28 '16 at 05:45
  • @DavidWohlferd I want that entire specified code to be converted. Is it possible using __buildit_alpha_rpcc!!? – ANTHONY Jun 28 '16 at 05:46
  • Do you understand what that "entire specified code" does? `rdtsc` reads the TimeStampCounter. However, as the intel docs say: *The RDTSC instruction is not a serializing instruction. It does not necessarily wait until all previous instructions have been executed before reading the counter.* That's why the `cpuid` instruction is added (it IS serializing). As I am not an alpha programmer, I don't know if this is even an issue on that platform. I assume the alpha docs describe the requirements for using rpcc. – David Wohlferd Jun 28 '16 at 06:09
  • @DavidWohlferd I am aware of those. yes, you are right. CPUID kind of instruction not required for ALPHA. "The RPCC instruction is not issued until all previous instructions that generate a result in Rb have completed" But still i want to get the result into a register from RPCC right? – ANTHONY Jun 28 '16 at 08:55
  • 1
    Into a register? Or just into a c variable? What's wrong with `long x = __builtin_alpha_rpcc()`? Let the compiler handle the 'registers' stuff. – David Wohlferd Jun 28 '16 at 09:16
  • Linux kernel uses `__builtin_alpha_rpcc();` too http://lxr.free-electrons.com/source/arch/alpha/kernel/time.c#L76 – osgx Jun 28 '16 at 19:52

1 Answers1

2

I'm going to go ahead and propose this as an answer. I'm not on alpha, so I haven't tested it, but it seems to match the OP's requirements, and may be of value to future SO users.

I usually try to avoid using inline asm. It's tricky to get right and easy to get wrong. If I need something that C itself doesn't provide, my first thought is to use a builtin. In this case gcc docs an alternative:

long x = __builtin_alpha_rpcc();
David Wohlferd
  • 7,110
  • 2
  • 29
  • 56