By: ⚛ (0xe2.0x9a.0x9b.delete@this.gmail.com), March 1, 2013 10:23 am
Room: Moderated Discussions
Linus Torvalds (torvalds.delete@this.linux-foundation.org) on February 27, 2013 5:41 pm wrote:
> Michael S (already5chosen.delete@this.yahoo.com) on February 27, 2013 4:24 pm wrote:
> >
> > Now, do we really need 4 flag bits in order to implement 12 (or even all 14) fundamental
> > conditions? No, we don't
>
> [cut]
> So I do claim that the 4 bits are pretty fundamental,
You may be wrong about the fundamentality of the Z bit.
Obviously, a bit which would be the most expensive to re-compute by user code is the overflow bit. Signed and unsigned overflow needs to be separate, so this implies two overflow bits (x86: C and O bits). So, these two overflow bits are in the "fundamental flags" category. The unsigned overflow bit needs to be usable in a sequence like add+adc+adc+adc+...
Then, in addition to these two bits you need only 1 more bit in the flags register. This bit holds the results of boolean expressions. We can call it TF bit (True-or-False). Some instructions corresponding to a boolean expression are for example:
Then instead of x86's instructions JA, JBE, ..., the CPU architecture needs just two conditional jump instructions:
Similarly, there are just two conditional move instructions (and no need for setcc instructions):
In total there are 3 fundamental flag bits: 2 overflow bits, and 1 abstract boolean expression bit.
Having just 1 boolean expression bit makes it conceivable to put predication into the instruction set.
Zero flag isn't fundamental. Because the performance advantage it gives is small, zero flag isn't in the "fundamental flags" category. The two overflow bits are fundamental because the lack of these two bits can cause a large performance disadvantage.
In assembly code generated by compiling the C/C++ programs, the overflow bits are mostly unused. The overflow bits make sense from the viewpoint of debugging, and exist for programming languages safer than C/C++.
> the same way two's-complement is fundamental. Sure, you
> can encode numbers in base three too if you want to, but that doesn't make binary numbers any less fundamental
> - even if the "fundamental" nature of them ends up being at least partly about sanity of implementation.
>
> Linus
> Michael S (already5chosen.delete@this.yahoo.com) on February 27, 2013 4:24 pm wrote:
> >
> > Now, do we really need 4 flag bits in order to implement 12 (or even all 14) fundamental
> > conditions? No, we don't
>
> [cut]
> So I do claim that the 4 bits are pretty fundamental,
You may be wrong about the fundamentality of the Z bit.
Obviously, a bit which would be the most expensive to re-compute by user code is the overflow bit. Signed and unsigned overflow needs to be separate, so this implies two overflow bits (x86: C and O bits). So, these two overflow bits are in the "fundamental flags" category. The unsigned overflow bit needs to be usable in a sequence like add+adc+adc+adc+...
Then, in addition to these two bits you need only 1 more bit in the flags register. This bit holds the results of boolean expressions. We can call it TF bit (True-or-False). Some instructions corresponding to a boolean expression are for example:
cmple r1, r2 // TF=(r1<r2), signed
cmpbe r1, r2 // TF=(r1<r2), unsigned
cmpeq r1, r2 // TF=(r1==r2)
cmpz r1 // TF=(r1==0)
...
Then instead of x86's instructions JA, JBE, ..., the CPU architecture needs just two conditional jump instructions:
jt label // Jump if TF==true
jf label // Jump if TF==false
Similarly, there are just two conditional move instructions (and no need for setcc instructions):
cmovt r1, r2/imm
cmovf r1, r2/imm
In total there are 3 fundamental flag bits: 2 overflow bits, and 1 abstract boolean expression bit.
Having just 1 boolean expression bit makes it conceivable to put predication into the instruction set.
Zero flag isn't fundamental. Because the performance advantage it gives is small, zero flag isn't in the "fundamental flags" category. The two overflow bits are fundamental because the lack of these two bits can cause a large performance disadvantage.
In assembly code generated by compiling the C/C++ programs, the overflow bits are mostly unused. The overflow bits make sense from the viewpoint of debugging, and exist for programming languages safer than C/C++.
> the same way two's-complement is fundamental. Sure, you
> can encode numbers in base three too if you want to, but that doesn't make binary numbers any less fundamental
> - even if the "fundamental" nature of them ends up being at least partly about sanity of implementation.
>
> Linus