0

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.

22
  • 3
    If you're building for an ARM Android target (as most Androids are), this cannot be fixed - rdtsc does not exist in ARM assembly, it's an x86 or x86_64 instruction. Commented Nov 5, 2016 at 22:53
  • And there are even other platforms that runs Android which are even more remote from x86. Commented Nov 5, 2016 at 23:12
  • 2
    The reason this works in the host OS is that rdtsc is 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. Commented Nov 6, 2016 at 1:05
  • 1
    I don't know what your project build method is. I build my C++ sources for android with ndk-build script from NDK, so I set up some things in jni/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 the clock_gettime instead, it's the same thing. b) why to clock something in emulator -> useless. Commented Nov 7, 2016 at 5:11
  • 1
    @Ped7g: I’m building/compiling the Android source code (with some added inline assembly code) itself (not an app) for x86_64 and ARM Android ROM images. I must build/compile these ROM images on my Ubuntu PC (my build environment) first before I can test the images on an emulator or a real phone, however,I’m getting error messages as explained before during the compiling process and I could not pass the compilation phase yet. Commented Nov 7, 2016 at 22:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.