How can I get an ARM MULL instruction to output my result in a uint64_t file in gcc?

I would like to introduce some assembly code into the c99 code base. I want to use the UMULL instruction from the ARM CPU to multiply 2 uint32_t and immediately get the result in uint64_t.

Now uint64_t needs 2 registers, so how can I specify the output and limitations of the asm block?

+4
source share
2 answers

Good question!

The following code prints out that you want to use GCC -O or higher without resorting to assembler:

  uint32_t a, b;
 uint64_t c;
 ...
 c = (uint64_t) a * (uint64_t) b;
or, if you think you need to use asm for a specific machine, you can go:
  uint32_t a, b;
 uint64_t c;

asm ("umull% Q0,% R0,% 1,% 2": "= r" (c): "r" (a), "r" (b));

c the register name is the first of the pair of registers, and% Q and% R select the lower and upper 32-bit registers of the pair. For an example, see Gcc / config / arm / arm.md β†’ umulsidi3.

However, if you can stay in C, this gives the optimizer a chance to do more and better for readers of your program.

+2
source

The umull command outputs its results to two 32-bit registers. I suggest explicitly reassembling a 64 bit value with something like this:

 /* assuming the 64-bit result was stored in "hi" (upper half) and "lo" (lower half) */ uint64_t v = ((uint64_t)hi << 32) | (uint64_t)lo; 

The compiler optimizer should notice that a left shift is pure data routing, and the resulting code must be accurate. To make sure, just use -S to check the compiler output.

+1
source

Source: https://habr.com/ru/post/1300195/


All Articles