For my project I must use inline assembly instructions such as rdtsc to calculate the execution time of an Android 4.3 C++ instruction in the stack. I found similar problem in stackoverflow such as 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 but non of them solve the problem that I have.
I used the following code:
{unsigned a, d;asm volatile("rdtsc" : "=a" (a), "=d" (d)); t0 = ((unsigned long)a) | (((unsigned long)d) << 32);}
//The C++ statement to measure its execution time
{unsigned a, d;asm volatile("rdtsc" : "=a" (a), "=d" (d)); t1 = ((unsigned long)a) | (((unsigned long)d) << 32);}
time = (t1-t0)/2-20;
But I'm getting the error message:
error: impossible constraint in 'asm'
My build environment are:
- Ubuntu 14.04.5 LTS
- Android 4.3
- GCC 4.8.5
- G++ 4.8.5
- Target: x86_64-linux-gnu
I have tried the above code in a standalone C program (in the same environment) and it is working fine with no problem but once I embed the above code in the Android source code, I get the error message.
My target:
I'm building an image for Android emulator qemu on x86_64-linux-gnu platform.
rdtscdoes not exist in ARM assembly, it's anx86orx86_64instruction.rdtscis a valid instruction for that hw. The compiler/assembler will be able to recognize this code, and emit the appropriate machine code instructions. But it simply isn't a valid ARM instruction, and the fact that you are running an emulator on an i386 host doesn't allow you to 'cheat' and still use it. Also, strictly speaking, the "constraints" error comes from using"=a"and"=d", which are i386-only machine constraints, not available on ARM. Not like that changes anything.ndk-buildscript from NDK, so I set up some things injni/Application.mk, but recently I think there's push to switch to CMake and/or gradle, I'm didn't use those yet. By the "constraint" error I think it may be you already do have correct target, and the inline asm is wrong, I don't know, sorry, I never mix C with asm, I write asm in asm files and C++ in cpp files. I see zero reasons a) why you don't use theclock_gettimeinstead, it's the same thing. b) why to clock something in emulator -> useless.