Question

I want to understand the importance of Q flag in ARM Processor. I know there are certain instructions like QADD,QSUB etc.

But I need to understand this with some examples which will clarify the concept.

Please explain me.

Thank you

Was it helpful?

Solution

This is explained in the "ARM Architecture Reference Manual" (ARM DDI 0100E):

Bit[27] of the CPSR is a sticky overflow flag, also known as the Q flag. This flag is set to 1 if any of the following occurs:

  • Saturation of the addition result in a QADD or QDADD instruction
  • Saturation of the subtraction result in a QSUB or QDSUB instruction
  • Saturation of the doubling intermediate result in a QDADD or QDSUB instruction
  • Signed overflow during an SMLA<x><y> or SMLAW<y> instruction

The Q flag is sticky in that once it has been set to 1, it is not affected by whether subsequent calculations saturate and/or overflow. Its intended usage is:

  1. Use an MSR CPSR_f,#0 instruction to clear the Q flag (this also clears the condition code flags).
  2. Peform a sequence of calculations.
  3. Use an MRS Rn,CPSR instruction to read the CPSR, then test the value of the Q flag. If it is still 0, none of the above types of saturation or overflow occured during step 2. Otherwise, at least one instance of stauration or overflow occured.

An example:

mov     r2,#0x70000000
qadd    r3,r2,r2

0x70000000 + 0x70000000 would become 0xE0000000, but since qadd is saturating, the result is saturated to 0x7FFFFFFF (the largest positive 32-bit integer) and the Q flag is set.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top