Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Useless jp / jnp assembly instruction on x86_64

I'm trying to figure out what purpose jp/jnp instructions serve in LLVM-generated C code. Sample:

int main(int argc, const char * argv[]) {
    double value = 1.5;

    if (value == 1.5) {
        value = 3.0;
    }

    return 0;
}

Assembly output:

Ltmp4:
    movsd   LCPI0_0(%rip), %xmm0
    movl    $0, -4(%rbp)
    movl    %edi, -8(%rbp)
    movq    %rsi, -16(%rbp)
Ltmp5:
    movsd   %xmm0, -24(%rbp)
Ltmp6:
    movsd   -24(%rbp), %xmm1
    ucomisd %xmm0, %xmm1
    jne LBB0_2
    jp  LBB0_2
## BB#1:
    movabsq $3, %rax
    cvtsi2sdq   %rax, %xmm0
Ltmp7:
    movsd   %xmm0, -24(%rbp)
Ltmp8:
LBB0_2:
    movl    $0, %eax
    popq    %rbp
    retq

The jne is checking if value != 1.5 and jumping over the assignment, but what is the jp doing in this context?

like image 687
user2994359 Avatar asked Jan 28 '15 00:01

user2994359


People also ask

What is JP in Assembly?

@JackZhang it's the same reason... the "JPE" reads "jump when parity is even", while the "JP" is "jump when parity is set" ... if you don't write tons of x86 assembly, you may find easier to recall "JPE" than recall if the PF is 1 or 0 when the low 8 bits of result had even parity (also if you expect the HW ...

What does JB do in assembly?

The JB instruction branches to the address specified in the second operand if the value of the bit specified in the first operand is 1. The bit that is tested is not modified. No flags are affected by this instruction.

What is the difference between JBE and JLE?

JBE, Jump if Below or Equal, should be used when comparing unsigned numbers. JLE, Jump if Less Than or Equal, should be used when comparing signed numbers.

What is JA in assembly?

The carry and zero flags are also used by the unsigned comparison instructions: "jb" (jump if unsigned below), "jbe" (jump if unsigned below or equal), "ja" (jump if unsigned above), and "jae" (jump if unsigned above or equal) in the usual way.


1 Answers

jne is jump if not equal, i.e. jump if the zero flag is not set. jp is jump if parity.

ucomisd is defined to compare two doubles. It will indicate that they are one of four things: unordered, equal, greater than or less than.

The zero flag is set if the numbers are unordered or equal. So the jne avoids the remaining cases of greater than or less than.

Parity is set only if the result is unordered. The jp catches that.

So the two together avoid: unordered, greater than, less than. Leaving only the fourth possibility, of equal.

like image 197
3 revs Avatar answered Oct 05 '22 23:10

3 revs