Page 117 - ARM 64 Bit Assembly Language
P. 117

Data processing and other instructions 103

                       Name     Effect                               Description
                       csetm    if < cond > then                     Fill Rn with ones or zeroes.
                                 Rd ← 0xffffffffffffffff
                                else
                                 Rd ← 0x0000000000000000
                                end if
                       ccmp     if < cond > then                     Test and set PSTATE flags to a com-
                                 PSTATE ← flags(Rn − Rm|imm5)        parison or immediate.
                                else
                                 PSTATE ← nzcv
                                end if
                       ccmn     if < cond > then                     Test and set PSTATE flags to a negated
                                 PSTATE ← flags(Rn + Rm|imm5)        comparison or immediate.
                                else
                                 PSTATE ← nzcv
                                end if


                     4.2.10.3 Examples

                    1     csel    x0, x1, x2, ge        // x0 = (ge ? x1 : x2)
                    2     csneg   x2, x3, x4, ne        // x2 = (ne ? x3: -x4)
                    3     cinv    x9, x7, hi            // x9 = (hi ? ~x7 : x7)
                    4     cset    x1, eq                // x1 = (eq ? 1 : 0)
                    5     ccmp    x4, #31, 0b0100, mi   // nzcv = (mi ? flags(x4 - 32) : 4)

                     The following C function will flip the parity of a number and alternate between two values if
                     its output is re-used as the input:

                    1  int flipFlop(int num) {
                    2   if (num % 2 == 0) {  /* is even */
                    3     return num + 1;
                    4   } else {
                    5     return num - 1;
                    6   }
                    7  }

                     The AArch64 translation uses conditional selection instead of branching to implement the
                     if-else statement. The input num is in w0 and is also returned in w0 as the output:

                    1         .text
                    2         .type  flipFlop, %function
                    3  flipFlop:
                    4         // Tests for parity by setting the PSTATE flags to w0 AND 1
   112   113   114   115   116   117   118   119   120   121   122