Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many primitive steps in y=x++? [closed]

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 ?

like image 488
Omkant Avatar asked Oct 30 '12 10:10

Omkant


People also ask

What is primitive recursive function show that F x/y xy is primitive recursive function?

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.

How do you know if a function is primitive recursive?

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.

What is a primitive operation?

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.

How do you prove a relation is primitive recursive?

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).


5 Answers

C++ follows the as-if principle, so the answer really is "n" steps.

  • it could be 2: 1) assign x to y. 2) increment x.
  • it could be 1 if y is never used
  • it could be undefined behavior, is y is declared as int& y = x;
  • it could be 100 assignments and reassignments that in the end yield the same result.
like image 68
Luchian Grigore Avatar answered Oct 06 '22 17:10

Luchian Grigore


  1. a temp variable is created with value == x
  2. x is incremented
  3. the old value of x (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.

like image 35
Kiril Kirov Avatar answered Oct 06 '22 16:10

Kiril Kirov


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.

like image 44
unwind Avatar answered Oct 06 '22 18:10

unwind


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.

like image 38
binblee Avatar answered Oct 06 '22 17:10

binblee


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.

like image 1
Azodious Avatar answered Oct 06 '22 16:10

Azodious