Domanda

Sorry i was not specific with the problem. i am trying to use intrinsic bit-parallelism of a system .a small part of the code is as follows-

int d;
char ch1;
char ch2;

cin>>ch1>>ch2;
if((d&1) == 0) {                            
        //heavy computation
}
    if(ch1 == ch2){
//heavy computation
}

first if condition execute if lsb of d is set. how many clock cycles the two 'if' conditions require to execute? include the clock cycles required to convert the variable values in binary form.

È stato utile?

Soluzione

On a i386 architecture and with gcc the assembly code produced for the abode conditions would be, for condition 1:

subl    $16, %esp
movb    $97, -2(%ebp)
movb    $98, -1(%ebp)
movl    -12(%ebp), %eax
andl    $1, %eax
testl   %eax, %eax
jne     .L2

for condition 2:

movzbl  -2(%ebp), %eax
cmpb    -1(%ebp), %al
jne     .L4

So for simplicity we consider the i386 is a MIPS with RISC core and it fallows the fallowing table: enter image description here number of clock cycles for the above statements would be 18.

Actually when you compile with "gcc -S file.c" the assembly for the 2 conditions is not produced as the compiler might go for the optimization of the null conditions(ineffective conditions or the dead code), so try to include some useful statements inside the conditions and compile the code you would get the above stated instructions.

Altri suggerimenti

With any good compiler, the if statements shown in this question would not consume any processor cycles in an executing program. This is because the compiler would recognize that neither of the if statements does anything, regardless of whether the condition is true or false, so they would be removed during optimization.

In general, optimization can dramatically transform a program. Even if the if statements had statements in their then-clauses, the compiler could determine at compile-time that ch1 does not equal ch2, so there is no need to perform the comparison during program execution.

Beyond that, if a condition is tested during program execution, there is often not a clear correlation between evaluating the test and how many processor cycles it takes. Modern processors are quite complicated, and a test and branch might be executed speculatively in advance while other instructions are also executing, so that the if statement does not cost the program any time at all. On the other hand, executing a branch might cause the processor to discard many instructions it had been preparing to execute and to reload new instructions from the new branch destination, thus costing the program many cycles.

In fact, both of these effects might occur for the same if statement in the same program. When the if statement is used in a loop with many executions, the processor may cache information about the branch decision and use that to speed up execution. At another time, when the if statement happens to be executed just once (because the loop conditions are different), the cached information may mislead the processor and cost cycles.

Probably you can compile your complete code and disassemble it using GDB. Once disassembled find out number and type (Load (5 cycles) Store (4 cycles) Branch (3 cycles) Jump (3 cycles) etc.,) of instructions your mentioned statements took. Sum of such cycles result to clock cycles consumed. However this depends on what processor you are on.

By looking at your question, i think you need to calculate number of instruction executed for your statement and then calculate cycles for every instruction in your if else

Code:

 if(x == 0)
 {
  x = 1;
 }
 x++;

This will consume following number of instructions

mov eax, $x
cmp eax, 0
jne end
mov eax, 1
end:
inc eax
mov $x, eax

so first if statement will consume 2cpu cycles

Adding to your particular code

cin>>ch1>>ch2;
if((d&1) == 0) {                            
    //heavy computation
}
if(ch1 == ch2){
//heavy computation
}

you need to get instruction required in those two if else operations from which you can calculate cycles. Also you need to add something inside ( if(){body} ) in body of if statements else modern compilers are intelligent remove your code considering it is dead code.

It depends on your "IF".

Take this to the simplest case that you want to compare two bytes, you probably only need 2 clock cycles in an instruction, ie. 1111 0001 which means (1st) activating ALU-CMP and setting data from R0 to TMP; (2nd) carrying R1 onto the bus and setting the output to ACC.

Otherwise, you will need at least other 3 clocks for fetching, 1 clock for checking I/O interrupt, and 1 final clock to reset the instruction register.

Therefore, on the circuit scale, you only need 7 clock cycles to execute an "IF" for 2 bytes. However, you would never write an "IF" just to compare two numbers (represented by two bytes), wouldn't you? 😅

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top