y=x++;
In how many steps does it break at compiler level without optimization not at CPU level or instructions ? Does any temporary is created to assign x to y or, it happens directly ?
One can easily show that the following functions are primitive recursive: f(x, y) = x + y f(x, y) = x · y f(x, y) = xy f(x, y) = x! At this point we introduce the notation 1=0/ and 2=1/ = 0//, and so on. We can then use the primitive recursion equations to calculate that 2+2=4.
Def 1.1 A function f(x1,...,xn) is primitive recursive if either: 1. f is the function that is always 0, i.e. f(x1,...,xn) = 0; This is denoted by Z when the number of arguments is understood. This rule for deriving a primitive recursive function is called the Zero rule.
Primitive operations are basic computations performed by an algorithm. Examples are evaluating an expression, assigning a value to a variable, indexing into an array, calling a method, returning from a method, etc. They are easily identifiable in pseudocode and largely independent from the programming language.
A relation R(x) is primitive recursive just in case its characteristic function χR is primitive recursive: χR(x) = 1 if R(x), χR(x) = 0 if ¬R(x). We will simplify notation by letting the relation stand for its own character- istic function when no confusion results. χR(x) = R(x).
C++ follows the as-if principle, so the answer really is "n" steps.
x
to y
. 2) increment x
.y
is never used y
is declared as int& y = x;
== x
x
is incrementedx
(kept in that temp variable) is assigned to the y
These will be the basic steps.
Depends on what you mean by "primitive" - if you mean CPU instructions, then the answer will be different.
You may want to read more about postfix and prefix increment/decrement operators.
It depends on the architecture for which you compile this code.
C++ doesn't specify things like these, or even what the possible "primitive steps" are.
One possible answer, for the venerable M68000 CPU, might be:
move.l x, d0
addq.l #1, x
move.l d0, y
So, this uses three instructions (primitive steps), and one temporary in the form of the register d0
.
Note: it's been ... a while since I wrote M68k assembly, I do hope the above code is valid but seem to be having issues reaching relevant reference pages at the moment. As an illustration, I'm pretty sure it's valid.
Depends on what do you mean primitive step, if primitive step is CPU instructions, I did a quick test like below:
(linux environment, g++ 4.6.3) use g++ -S main.cpp to show what CPU instructions are generated by g++, I got below output:
movl -8(%ebp), %eax<br>
movl %eax, -4(%ebp)<br>
addl $1, -8(%ebp)
So that means g++ will copy y to x, then increase x. In this case, 3 CPU instructions (if you consider them as primitive steps) are generated.
Please be noted that with different optimization level of compiler and different CPU architecture, the result will be different.
See Operators Precedence Table
Steps happen as follows:
A temporary variable is assigned with x
's value.
x
is incremented by 1.
y
is assigned with temporary variable value.
So, 3 primitive steps at abstract level.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With