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.
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:
QADD
or QDADD
instructionQSUB
or QDSUB
instructionQDADD
or QDSUB
instructionSMLA<x><y>
or SMLAW<y>
instructionThe 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:
MSR CPSR_f,#0
instruction to clear the Q flag (this also clears the condition code flags).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.