Advanced Assembly Programming

The function in listing 1 calculates result[i] = a[i] + b[i] for n single-precision floating-point elements. X0, X1 and X2 contain valid array pointers; X3 contains the element count. The output may equal either input exactly, but partially overlapping ranges are not supported. For n greater than zero, each array must provide at least n accessible elements.

Listing 1: Add two arrays of single-precision values
.section .text
.global vadd_f32
.type vadd_f32, %function
vadd_f32:
    LSR X8, X3, #4           // Number of 16-element blocks
    AND X9, X3, #15          // Remaining scalar elements
    CBZ X8, .Lvadd_tail
.Lvadd_loop:
    LD1 {V0.4S, V1.4S, V2.4S, V3.4S}, [X1], #64
    LD1 {V4.4S, V5.4S, V6.4S, V7.4S}, [X2], #64
    FADD V0.4S, V0.4S, V4.4S
    FADD V1.4S, V1.4S, V5.4S
    FADD V2.4S, V2.4S, V6.4S
    FADD V3.4S, V3.4S, V7.4S
    ST1 {V0.4S, V1.4S, V2.4S, V3.4S}, [X0], #64
    SUBS X8, X8, #1
    B.NE .Lvadd_loop
.Lvadd_tail:
    CBZ X9, .Lvadd_done
.Lvadd_scalar:
    LDR S0, [X1], #4
    LDR S1, [X2], #4
    FADD S0, S0, S1
    STR S0, [X0], #4
    SUBS X9, X9, #1
    B.NE .Lvadd_scalar
.Lvadd_done:
    RET
.size vadd_f32, . - vadd_f32
.section .note.GNU-stack,"",@progbits

The remainder is calculated before the block counter is consumed. For n = 17, the function processes one block of 16 elements and one scalar element. For n = 0, no memory access occurs. The function uses caller-saved registers and does not require a stack frame.

Each .L… symbol has a unique name within this source file. GNU assembler does not automatically make repeated definitions of .loop unique. Numeric labels such as 1: can be repeated and referenced with 1b or 1f.

Save the assembly as vadd.s. The following C program is a small boundary-length check, not a performance benchmark:

#include <stddef.h>
#include <stdio.h>
extern void vadd_f32(float *, const float *, const float *, size_t);
int main(void) {
    const size_t sizes[] = {0, 1, 15, 16, 17, 31, 32, 33};
    float a[80], b[80], out[82];
    for (size_t i = 0; i < 80; ++i) {
        a[i] = (float)i;
        b[i] = 2.0f * (float)i;
    }
    for (size_t t = 0; t < sizeof sizes / sizeof sizes[0]; ++t) {
        size_t n = sizes[t];
        for (size_t i = 0; i < 82; ++i) out[i] = -999.0f;
        vadd_f32(out + 1, a, b, n);
        if (out[0] != -999.0f) return 1;
        for (size_t i = 0; i < n; ++i)
            if (out[i + 1] != a[i] + b[i]) return 1;
        for (size_t i = n + 1; i < 82; ++i)
            if (out[i] != -999.0f) return 1;
    }
    puts("All boundary-length checks passed");
    return 0;
}

Save the C code as test_vadd.c, then build and run on the AArch64 Linux target:

gcc -O2 -Wall -Wextra test_vadd.c vadd.s -o test_vadd
./test_vadd

Expected output is All boundary-length checks passed. The larger buffers make the original overrun detectable without requiring that particular bug to cross the allocated arrays. A broader test suite should also cover in-place operation, protected boundaries and floating-point special values.

NEON (SIMD – Single Instruction Multiple Data)

NEON is a powerful extension to the ARM processors. It is capable of performing a single instruction with multiple variables or data. Like in the previous example, numerous floating-point variables were loaded into a single vector register “V0”. One NEON register can hold up to 16 bytes (128 bits). In the example, X1 holds the address of the next input element. LD1 loads four 128-bit vector registers, transferring 64 bytes. Its immediate post-index operand #64 then advances X1 by the number of bytes transferred. Other operand forms must follow their own addressing restrictions.

Float variable F0 is the one pointed to by the [X1] register. Other mappings are listed in table 1. Four vectors were loaded with one single “LD1” instruction.

Table 1: Variable Mapping
F0 F1 F2 F3 V0.4S = {F0, F1, F2, F3}
F4 F5 F6 F7 V1.4S = {F4, F5, F6, F7}
F8 F9 F10 F11 V2.4S = {F8, F9, F10, F11}
F12 F13 F14 F15 V3.4S = {F12, F13, F14, F15}

In AArch64, NEON and floating-point operations share the same set of 32 registers: V0..V31, where each is 128 bits wide. V0 register is graphically presented in figure 1. Note that in instruction LD1 {V0.4S, V1.4S, V2.4S, V3.4S}, [X1], #64 the postfix “S” identifies the type of variable. These are named as elements in the NEON architecture documentation.

  • V0.2D can hold two elements: two double-precision floating-point variables or two 64-bit integers, where the letter d indicates doubleword
  • V3.4S can hold four elements: four single-precision floating-point variables or four 32-bit integers, where the letter s indicates word
  • V16.8H can hold eight elements: eight 16-bit integers, where the letter h indicates a halfword
  • V31.16B can hold 16 elements: sixteen 8-bit integers, where the letter b indicates byte
Diagram of V0 register showing 128-bit width divided into lanes: 16x8-bit bytes (B lane), 8x16-bit halfwords (H lane), 4x32-bit words/floats (S lane), and 2x64-bit doublewords (D lane). Shows different element access patterns for same 128-bit register.
Figure 1: V0 Register

The NEON allows the CPU to perform operations on multiple data items with a single instruction. Standard scalar instructions can handle only one scalar value in one register, but NEON registers are specially designed so that they can hold multiple values. And this is how Single Instruction, Multiple Data (SIMD) operations work: one instruction operating on several elements in parallel. This technique is widely used for image, signal, and multimedia processing, as well as for accelerating math-heavy tasks such as machine learning, encryption, and other AI tasks. The processor used in the Raspberry Pi 5 supports the NEON and floating-point (FP) instruction sets.

This is important to specify the register and how to interpret it when working with NEON instructions. Elements in a vector are ordered from the least significant bit. That is, element 0 uses the least significant bits of the register.
FADD V0.4S, V1.4S, V2.4S // Add 4 single-precision floats from v1 and v2, store in v0.
ADD V0.8H, V1.8H, V2.8H // Add 8 halfwords (16-bit integers) elementwise.
MUL V0.16B, V1.16B, V2.16B // Multiply 16-byte elements elementwise.

Elementwise operations are performed by lane, where each element occupies its own lane. Like ADD V0.8H, V1.8H, V2.8H. The instruction specifies that there will be eight distinct 16-bit variables. The instruction is designed to operate on the integers, and the result is computed from eight 16-bit integers. In the picture below, it is visible which element from vector V1 is added to vector V2 and how the result is obtained in vector V0. The figure 2 illustrates the following instruction: ADD V0.8H, V1.8H, V2.8H.

Diagram showing 8-lane halfword addition where V0 = V1 + V2. Eight 16-bit elements from V1 are paired with eight matching elements from V2, each lane computing one independent addition: V0[0]=V1[0]+V2[0], V0[1]=V1[1]+V2[1], etc.
Figure 2: 8-Lane Halfword ADD Operation

Element Types, Widening and Narrowing

The lane suffix identifies element width and count; the instruction determines how the bits are interpreted. ADD performs integer addition. FADD performs floating-point addition.

ADD  V0.4S, V1.4S, V2.4S  // Four 32-bit integer additions
FADD V0.4S, V1.4S, V2.4S  // Four single-precision additions
UADDL V0.4S, V1.4H, V2.4H // Unsigned 16-bit inputs, 32-bit sums
SADDL V0.4S, V1.4H, V2.4H // Signed 16-bit inputs, 32-bit sums

Plain ADD does not accept arbitrarily different element widths. To add four 32-bit integers and keep the low 16 bits of each sum, use two instructions:

ADD V3.4S, V1.4S, V2.4S
XTN V0.4H, V3.4S

This sequence truncates; it does not saturate. Select a saturating narrowing instruction when the algorithm requires clamping instead of discarding high bits.

A scalar element can be used in each lane of a floating-point multiply-accumulate:

FMLA V0.4S, V1.4S, V2.S[1]

Each V0 lane is updated using the corresponding V1 lane and element 1 of V2. FMLA is a fused operation and can differ in rounding from separate multiply and add instructions.

The data preparation for NEON is also an essential part. Note that in the previous code example, the values into the vector registers were loaded by use of the LD1 instruction. NEON has special LDn and STn instructions, where n is 1 to 4 (i.e., LD1, ST4, ST2, LD3). Each load (or store) variant performs differently and must be chosen based on the data. Furthermore, the LOAD instructions will be explained, as the STORE instruction works similarly but stores data from a register into memory.

The LD1 instruction:

  • Loads a single-element structure to a vector register
  • Loads multiple single-element structures into a vector register
  • With postfix 'R', like LD1R, loads a single-element structure into all lanes of the vector register

To load one 32-bit element into lane 3 while preserving the other lanes:

LD1 {V0.S}[3], [X1]

To load one 32-bit value and replicate it into all four lanes:

LD1R {V0.4S}, [X1]

These instructions copy bit patterns. The bits may subsequently be interpreted as integers or floating-point values by the processing instruction.

The instruction LD1 {V0.4S, V1.4S}, [X1] loads eight 32-bit values into the lanes of first V0 and then on V1. All the data are loaded sequentially as x0, x1, x2, x3, and so on. Note that x here is meant for any chosen data type and that the data by itself are in sequential order. Similar operations are performed with LD2, LD3 and LD4 instructions; the data loaded into the vector register are interleaved. The LD2 instruction loads the structure of two elements into two vector registers. There must always be two vector registers identified. The structure in the memory may be in such order (figure 3): x0, y0, x1, y1, x2, y2, … and using the LD2 instruction, the x part of the structure is loaded into the first pointed register and the y part into the second vector register. This instruction is used, for example, to process audio data, where x may be the left audio channel and y the right:
LD2 {V0.4S, V1.4S}, [X1]

Diagram of 2D vector representation: memory layout shows interleaved x,y pairs (x0,y0,x1,y1,...), LD2 instruction deinterlaces into two registers where V0 contains all x values (x0,x1,x2,x3) and V1 contains all y values (y0,y1,y2,y3).
Figure 3: 2D Vector Representation in Memory and after Loading to the V Registers

The LD3 instruction loads memory data as a three-element structure, such as an image's RGB data. For this instruction, three vector registers must be identified. Assuming the data in memory: r0, g0, b0, r1, g1, b1, r2, g2, b2,…, the r (red) channel will be loaded in the first identified register, the 'g' in the second and 'b' in the third, the last identified register (figure 4).
LD3 {V0.4S, V1.4S, V2.4S}, [X1]

Diagram of 3D vector representation: memory contains interleaved RGB values (r0,g0,b0,r1,g1,b1,...), LD3 instruction separates into three registers: V0 red channel (r0,r1,r2,r3), V1 green channel (g0,g1,g2,g3), V2 blue channel (b0,b1,b2,b3).
Figure 4: 3D Vector Representation in Memory and after Loading to the V Registers

The LD4 instruction is also used to load interleaved data into four vector registers (figure 5).
LD4 {V0.4S, V1.4S, V2.4S, V3.4S}, [X1]

Diagram of 4D vector representation: memory contains interleaved 4-element structures (a0,b0,c0,d0,a1,b1,c1,d1,...), LD4 deinterlaces into 4 registers: V0 (a0,a1,a2,a3), V1 (b0,b1,b2,b3), V2 (c0,c1,c2,c3), V3 (d0,d1,d2,d3).
Figure 5: 4D Vector Representation in Memory and after Loading to the V Registers

The LD4R instruction will take four values from memory and fill the first vector with the first value, the second with the second, and so on. Such instructions can be used to perform arithmetical operations on the same value with multiple different scalar values. Similar operations are performed with LD3R and LD2R instructions. Note that the LDn, the 'n' identifies the number of vector registers loaded with the structure elements. Remember that the structure elements are the same as the number of vector registers used by an instruction.

en/multiasm/paarm/chapter_5_10.txt · Last modified: by karlisberkolds
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0