Given a number x, how can we tell that it can be represented as 32-bit immediate. Does any kind of formula exists? If yes, is that formula exhaustive, i.e., covers all the possible cases? ARM decomposes the rightmost 12 bits in the instruction set as 4 bit rotate-bit and 8 bit value bit.
Asked
Active
Viewed 590 times
3
-
I would think that you can just test whether the population count (number of 1 bits) is <= 8 ? – Paul R Jan 29 '16 at 11:42
-
1Which instruction do you mean? http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0473e/Chdcegci.html -- "using LDR Rd, =const", so if it does not "fit", the assembler should place it in a literal pool, so I don't see restrictions there. – Matthias W. Jan 29 '16 at 11:42
-
Count the distance between the first and last '1' bit (with wrapping allowed). If it's less than 8, then it'll work. – Michael Jan 29 '16 at 11:42
-
@PaulR: That would allow `0xF000F000`, which can't be represented as `imm8 ROR n`. – Michael Jan 29 '16 at 11:45
-
1@Michael: yes, just realised that - thanks - useful tool here: http://alisdair.mcdiarmid.org/arm-immediate-value-encoding/ (scroll down to bottom of page) – Paul R Jan 29 '16 at 11:47
-
3[Related question](http://stackoverflow.com/q/27963312/3156750) - although we only really discuss brute-force checking there rather than cleverer algorithms. – Notlikethat Jan 29 '16 at 11:47
-
how you know is read the arm documentation infocenter.arm.com – old_timer Jan 29 '16 at 13:07
1 Answers
2
I would look into open source compilers and how they do it...
/* Return TRUE if int I is a valid immediate ARM constant. */
int
const_ok_for_arm (HOST_WIDE_INT i)
{
...

Peter Cordes
- 328,167
- 45
- 605
- 847

auselen
- 27,577
- 7
- 73
- 114
-
2Note that the gcc source is also checking for more modern ARM `mov` immediate encodings which allow for '0xAB00AB00' and '0x00AB00AB' type constants. There is also a great [ARM blog on constants](https://community.arm.com/groups/processors/blog/2010/07/27/how-to-load-constants-in-assembly-for-arm-architecture) which might be helpful to readers. The 'gas' assembler might be better as it needs to decide whether `ldr rX,=xxx` will be a `mov` or an `ldr rX,[pc,#offset]`. – artless noise Feb 01 '16 at 15:33