问题
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
回答1:
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
orQDADD
instruction - Saturation of the subtraction result in a
QSUB
orQDSUB
instruction - Saturation of the doubling intermediate result in a
QDADD
orQDSUB
instruction - Signed overflow during an
SMLA<x><y>
orSMLAW<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:
- Use an
MSR CPSR_f,#0
instruction to clear the Q flag (this also clears the condition code flags). - Peform a sequence of calculations.
- Use an
MRS Rn,CPSR
instruction to read theCPSR
, 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.
来源:https://stackoverflow.com/questions/19557338/importance-of-qsaturation-flag-in-arm