Page 109 - ARM 64 Bit Assembly Language
P. 109

Data processing and other instructions 95

                     smulh   Signed multiply high and
                     umulh   Unsigned multiply high.

                     4.2.7.1 Syntax

                          <op>      Xd, Xn, Xm


                     •   The <op> is either smulh or umulh.


                     4.2.7.2 Operations

                       Name     Effect                               Description
                       smulh    Xd ← (signExtend(Xn) ×               Signed multiply high.
                                signExtend(Xm))[127 : 64]
                       umulh    Xd ← (Xn × Xm)[127 : 64]             Unsigned multiply high.

                     4.2.7.3 Examples

                    1     mul     x0, x1, x2     // x0 = x1 * x2
                    2     smulh   x1, x1, x2     // x1 = (signExtend(x1) * signExtend(x2))[127:64]

                     The following example shows how a C program and an AArch64 program could implement
                     a simple command-line tool for multiplying 64-bit unsigned numbers, without rounding or
                     truncating the result. To change the program to work with signed numbers, the only change
                     needed for the AArch64 version would be to replace umulh with smulh.


                    1  #include <stdio.h>
                    2  #include <stdint.h>
                    3
                    4  /* longMul takes two unsigned longs as input x and y and returns a
                    5    128-bit product. */
                    6  __uint128_t longMul(uint64_t x, uint64_t y)
                    7  {
                    8   return (__uint128_t)x*(__uint128_t) y;
                    9  }
                   10
                   11  /* Reads two unsigned longs from stdin and prints the resulting
                   12    product on stdout. */
                   13  int main(void)
                   14  {
                   15   long x, y, lo, hi;
                   16   printf("Enter a number in hex: ");
                   17   scanf("%lx", &x);
                   18   printf("Enter a number in hex: ");
   104   105   106   107   108   109   110   111   112   113   114