a = x + y;
reveals a profound misunderstanding of what an adder is inside a CPU.
a = x + y maps to an Aristolecian Natural integer ideal addition while CPU works in an anneal but with 2ce the size you think.
An adder is a series of bit level adder. The sum of x + y for any bit is the result of bit level adder piped in together through the propagation of a carry bit.
Hence for an addition at the bit level 1 + 1 has a sum of 0 and a carry of 1 is propagated.
1 + 1 = 0 .... + 1
We copy paste them in a pipeline of propagation and know we have a full adder with one extra carry flag being set. Flag C ignores.
It just that you have an exact result on n+1 bits but it would ruin the compiler shortcuts if you took that into account. Memory is padded on n bits. So if you intend to to do the calculus AND store in a a set of atomic instructions then having to extend the available memory on the fly is a little tricky.
C wisely has decided that resources are bounded thus x + y should always fit in something of the size of the operands.
After all a one cycle ASM addition can be seen exactly as :
- exact integer adder telling you when you exceeded your capacity;
- an exact addition that has an extra bit of information that waits for treatment.
Yes, addition can be even considered synchronuously or asynchronuously.
Since the carry indicates the event (unsigned context) and since the carry indicates and extention why not have :
promotion, a = x + y
Dyslexic rant (goto end of rant for the impatient)
or
x + y => a, promotion
I am dyslexic and latin reader.
The a = x + y has always pissed me off.
I read from left ro right, right?
a is set AFTER the addition. So causality is from right to left, right?
What is left? I have to swap directions of causality all the time.
Plus => is the mathematical notation for implication.
So as an ASM 68K lovers (source destination convention), an aristocrat of coding, a western centric writer I propose that we always favour left to right notation.
It has worked for math let's do it with computer.
I guarantee that if the flow of the syntax follow the flow of the causality compiler will have less bug, parsers will be easier to make, and software more efficient. A concept in a flow should appear after the necessary context for its creation.
A = new Object( data )
is also stupid.
data Object.new ) => A
would proove smarter. You would read a stack being filled from left to right and some reserved words map to operator like call ")" or MOV "=>" and for once the flow of reading would map the control flow inside the CPU
End of Rant
There is still a problem. In ASM you cannot test the carry flag and set it atomically. But you can do a branch on whatever the good status flag (goto after on promote if no carry). That can be translated in more intuitive meaning like:
x + y => a
on promotion {
1 => b
}
;
Or with dyslexic syntax and more correct variable names:
int lower = 0;
int higher = 0;
lower = x + y on promote {
higher = 1;
};
This way overflow in higher language may be not only more correct in terms of meaning and less buggy but also mapping more efficiently to the CPU paradigms.
With compiler we can turn the various meaning of the condition tests in way more convenient meanings according to the context (unsigned/signed ...)... and use more efficiently the out of bound signaling systems left by our wise ancestor electronical engineers in the status flags.
Programmation is stateful. Those who ignore it in their abstractions are bound to do have nasty bugs.
No comments:
Post a Comment