## Intriniscs

Intrinsics are functions whose precise implementation is known to a compiler. Intrinsics let you use Neon or SVE without having to write assembly code. This is because intrinsics themselves contain short assembly kernels, which are inlined into the calling code. Also, register allocation and pipeline optimization are handled by the compiler. This avoids many of the difficulties that often arise when developing assembly code.

Using intrinsics has several benefits:

- Powerful: With intrinsics, you have direct access to the Neon and SVE instruction sets during development. You do not need to hand-write assembly code.
- Portable: You might need to rewrite hand-written Neon or SVE assembly instructions for different target processors. You can compile C and C++ code containing Neon intrinsics for a new AArch64 target, or a new Execution state, with minimal or no code changes. However, C and C++ code containing SVE intrinsics only runs on SVE-enabled hardware.
- Flexible: You can exploit Neon or SVE only when needed. This allows you to avoid many low-level engineering concerns.

However, intrinsics might not be the right choice in all situations:

- You need more learning to use intrinsics than you need to import a library, or to rely on a compiler.
- Hand-optimized assembly code might offer the greatest scope for performance improvement, even if it is more difficult to write.

For a list of all the Neon intrinsics, see the Neon intrinsics interactive reference. The Neon intrinsics engineering specification is contained in the Arm C Language Extensions (ACLE) specification.

The SVE intrinsics engineering specification is contained in the Arm C Language Extensions for SVE specification.

### Example: Simple matrix multiplication with intrinsics

This example implements some C functions using Neon intrinsics and using SVE intrinsics. The example does not demonstrate the full complexity of the application, but illustrates the use of intrinsics, and is a starting point for more complex code.

Matrix multiplication is an operation that is performed in many data intensive applications. Matrix multiplication consists of groups of arithmetic operations which are repeated in a simple way, as you can see in the following diagram:

Here is the matrix multiplication process:

- Take a row in matrix A.
- Perform a dot product of this row with a column from matrix B.
- Store the result in the corresponding row and column of the new matrix C.

For matrices of 32-bit floats, the multiplication could be written as you can see in this code:

void matrix_multiply_c(float32_t *A, float32_t *B, float32_t *C, uint32_t n, uint32_t m, uint32_t k) { for (int i_idx=0; i_idx < n; i_idx++) { for (int j_idx=0; j_idx < m; j_idx++) { C[n*j_idx + i_idx] = 0; for (int k_idx=0; k_idx < k; k_idx++) { C[n*j_idx + i_idx] += A[n*k_idx + i_idx]*B[k*j_idx + k_idx]; } } } }

Assume a column-major layout of the matrices in memory. That is, an `n`

x `m`

matrix `M`

, is represented as an array `M_array`

, where `Mij = M_array[n*j + i]`

.

This code is suboptimal because it does not make full use of Neon. Intrinsics can be used to improve performance.

The following code uses intrinsics to multiply two 4x4 matrices. The loops can be completely unrolled because there is a small, fixed number of values to process. All of the values can fit into the Neon registers of the processor at the same time:

void matrix_multiply_4x4_neon(const float32_t *A, const float32_t *B, float32_t *C) { // these are the columns A float32x4_t A0; float32x4_t A1; float32x4_t A2; float32x4_t A3; // these are the columns B float32x4_t B0; float32x4_t B1; float32x4_t B2; float32x4_t B3; // these are the columns C float32x4_t C0; float32x4_t C1; float32x4_t C2; float32x4_t C3; A0 = vld1q_f32(A); A1 = vld1q_f32(A+4); A2 = vld1q_f32(A+8); A3 = vld1q_f32(A+12); // Zero accumulators for C values C0 = vmovq_n_f32(0); C1 = vmovq_n_f32(0); C2 = vmovq_n_f32(0); C3 = vmovq_n_f32(0); // Multiply accumulate in 4x1 blocks, that is each column in C B0 = vld1q_f32(B); C0 = vfmaq_laneq_f32(C0, A0, B0, 0); C0 = vfmaq_laneq_f32(C0, A1, B0, 1); C0 = vfmaq_laneq_f32(C0, A2, B0, 2); C0 = vfmaq_laneq_f32(C0, A3, B0, 3); vst1q_f32(C, C0); B1 = vld1q_f32(B+4); C1 = vfmaq_laneq_f32(C1, A0, B1, 0); C1 = vfmaq_laneq_f32(C1, A1, B1, 1); C1 = vfmaq_laneq_f32(C1, A2, B1, 2); C1 = vfmaq_laneq_f32(C1, A3, B1, 3); vst1q_f32(C+4, C1); B2 = vld1q_f32(B+8); C2 = vfmaq_laneq_f32(C2, A0, B2, 0); C2 = vfmaq_laneq_f32(C2, A1, B2, 1); C2 = vfmaq_laneq_f32(C2, A2, B2, 2); C2 = vfmaq_laneq_f32(C2, A3, B2, 3); vst1q_f32(C+8, C2); B3 = vld1q_f32(B+12); C3 = vfmaq_laneq_f32(C3, A0, B3, 0); C3 = vfmaq_laneq_f32(C3, A1, B3, 1); C3 = vfmaq_laneq_f32(C3, A2, B3, 2); C3 = vfmaq_laneq_f32(C3, A3, B3, 3); vst1q_f32(C+12, C3); }

Fixed size 4x4 matrices are chosen because:

- Some applications need 4x4 matrices specifically, for example graphics or relativistic physics.
- The Neon vector registers hold four 32-bit values. Matching the program to the architecture makes it easier to optimize.
- This 4x4 kernel can be used in a more general kernel.

The following table summarizes the Neon intrinsics that are used in this example:

Code element | What is it? | Why is it used? |
---|---|---|

`float32x4_t` |
An array of four 32-bit floats. | One `uint32x4_t` fits into a 128-bit register and ensures that there are no wasted register bits, even in C code. |

`vld1q_f32()` |
A function which loads four 32-bit floats into `float32x4_t` . |
To get the matrix values needed from A and B. |

`vfmaq_lane_f32()` |
A function which uses the fused multiply accumulate instruction. Multiplies a `float32x4_t` value by a single element of another `float32x4_t` then adds the result to a third float32x4_t before returning the result. |
Since the matrix row-on-column dot products are a set of multiplications and additions, this operation fits naturally. |

`vst1q_f32()` |
A function which stores `float32x4_t` at a given address. |
To store the results after they are calculated. |

Optimizing a similar case for SVE gives the following code:

void matrix_multiply_nx4_neon(const float32_t *A, const float32_t *B, float32_t *C, uint32_t n) { // these are the columns A svfloat32_t A0; svfloat32_t A1; svfloat32_t A2; svfloat32_t A3; // these are the columns B svfloat32_t B0; svfloat32_t B1; svfloat32_t B2; svfloat32_t B3; // these are the columns C svfloat32_t C0; svfloat32_t C1; svfloat32_t C2; svfloat32_t C3; svbool_t pred = svwhilelt_b32_u32(0, n); A0 = svld1_f32(pred, A); A1 = svld1_f32(pred, A+n); A2 = svld1_f32(pred, A+2*n); A3 = svld1_f32(pred, A+3*n); // Zero accumulators for C values C0 = svdup_n_f32(0); C1 = svdup_n_f32(0); C2 = svdup_n_f32(0); C3 = svdup_n_f32(0); // Multiply accumulate in 4x1 blocks, that is each column in C B0 = svld1rq_f32(svptrue_b32(), B); C0 = svmla_lane_f32(C0, A0, B0, 0); C0 = svmla_lane_f32(C0, A1, B0, 1); C0 = svmla_lane_f32(C0, A2, B0, 2); C0 = svmla_lane_f32(C0, A3, B0, 3); svst1_f32(pred, C, C0); B1 = svld1rq_f32(svptrue_b32(), B+4); C1 = svmla_lane_f32(C1, A0, B1, 0); C1 = svmla_lane_f32(C1, A1, B1, 1); C1 = svmla_lane_f32(C1, A2, B1, 2); C1 = svmla_lane_f32(C1, A3, B1, 3); svst1_f32(pred, C+4, C1); B2 = svld1rq_f32(svptrue_b32(), B+8); C2 = svmla_lane_f32(C2, A0, B2, 0); C2 = svmla_lane_f32(C2, A1, B2, 1); C2 = svmla_lane_f32(C2, A2, B2, 2); C2 = svmla_lane_f32(C2, A3, B2, 3); svst1_f32(pred, C+8, C2); B3 = svld1rq_f32(svptrue_b32(), B+12); C3 = svmla_lane_f32(C3, A0, B3, 0); C3 = svmla_lane_f32(C3, A1, B3, 1); C3 = svmla_lane_f32(C3, A2, B3, 2); C3 = svmla_lane_f32(C3, A3, B3, 3); svst1_f32(pred, C+12, C3); }

The following table summarizes the SVE intrinsics that are used in this example:

Code element | What is it? | Why is it used? |
---|---|---|

`svfloat32_t` |
An array of 32-bit floats, where the exact number is defined at runtime based on the SVE vector length. | `svfloat32_t` enables you to use SVE vectors and predicates directly, without relying on the compiler for autovectorization. |

`svwhilelt_b32_u32()` |
A function which computes a predicate from two `uint32_t` integers |
When loading from A and storing to C, `svwhilelt_b32_u32()` ensures that you do not read or write past the end of each column. |

`svld1_f32()` |
A function which loads 32-bit `svfloat32_t` floats into an SVE vector |
To get the matrix values needed from A. This also takes a predicate to make sure that we do not load off the end of the matrix. Unpredicated elements are set to zero. |

`svptrue_b32()` |
A function which sets a predicate for 32-bit values to all-true | When loading from B, `svptrue_b32()` ensures that the vector fills completely. This is because the precondition of calling this function is that the matrix has a dimension which is a multiple of four. |

`svld1rq_f32()` |
A function which loads an SVE vector with copies of the same 128-bits (four 32-bit values) | To get the matrix values needed from B. Only loads four replicated values because the `svmla_lane_f32` instruction only indexes in 128-bit segments. |

`svmla_lane_f32()` |
A function which uses the fused multiply accumulate instruction. The function multiplies each 128-bit segment of an `svfloat32_t` value by the corresponding single element of each 128-bit segment of another `svfloat32_t` . The `svmla_lane_f32()` function then adds the result to a third `svfloat32_t` before returning the result. |
This operation naturally fits the row-on-column dot products because they are a set of multiplications and additions. |

`svst1_f32()` |
A function which stores `svfloat32_t` at a given address |
To store the results after they are calculated. The predicate ensures we do not store results past the end of each column. |

The important difference with the SVE code is the ability to ignore one of the dimensions of the matrix because of the variable-length vectors. Instead, you can explicitly pass the length of the n dimension and use predication to ensure that the maximum length is not exceeded.

### Large matrix multiplication with intrinsics

To multiply larger matrices, you can treat them as blocks of 4x4 matrices. However, this approach only works with matrix sizes which are a multiple of four in both dimensions. To use this method for other matrix sizes, you can pad the matrix with zeroes.

The following code block shows the Neon code for a more general matrix multiplication. The structure of the kernel has changed. The most important change is the addition of loops and address calculations. Like in the 4x4 kernel, unique variable names are used for the B columns. The alternative is to use one variable and reload it. This acts as a hint to the compiler to assign different registers to these variables. Assigning different registers enables the processor to complete the arithmetic instructions for one column, while waiting on the loads for another.

Here is the code:

void matrix_multiply_neon(const float32_t *A, const float32_t *B, float32_t *C, uint32_t n, uint32_t m, uint32_t k) { /* * Multiply matrices A and B, store the result in C. * It is the user's responsibility to make sure the matrices are compatible. */ int a_idx; int b_idx; int c_idx; // these are the columns of a 4x4 sub matrix of A float32x4_t A0; float32x4_t A1; float32x4_t A2; float32x4_t A3; // these are the columns of a 4x4 sub matrix of B float32x4_t B0; float32x4_t B1; float32x4_t B2; float32x4_t B3; // these are the columns of a 4x4 sub matrix of C float32x4_t C0; float32x4_t C1; float32x4_t C2; float32x4_t C3; for (int i_idx=0; i_idx<n; i_idx+=4) { for (int j_idx=0; j_idx<m; j_idx+=4) { // zero accumulators before matrix op C0 = vmovq_n_f32(0); C1 = vmovq_n_f32(0); C2 = vmovq_n_f32(0); C3 = vmovq_n_f32(0); for (int k_idx=0; k_idx<k; k_idx+=4){ // compute base index to 4x4 block a_idx = i_idx + n*k_idx; b_idx = k*j_idx + k_idx; // load most current a values in row A0 = vld1q_f32(A+a_idx); A1 = vld1q_f32(A+a_idx+n); A2 = vld1q_f32(A+a_idx+2*n); A3 = vld1q_f32(A+a_idx+3*n); // multiply accumulate 4x1 blocks, that is each column C B0 = vld1q_f32(B+b_idx); C0 = vfmaq_laneq_f32(C0,A0,B0,0); C0 = vfmaq_laneq_f32(C0,A1,B0,1); C0 = vfmaq_laneq_f32(C0,A2,B0,2); C0 = vfmaq_laneq_f32(C0,A3,B0,3); B1 = v1d1q_f32(B+b_idx+k); C1 = vfmaq_laneq_f32(C1,A0,B1,0); C1 = vfmaq_laneq_f32(C1,A1,B1,1); C1 = vfmaq_laneq_f32(C1,A2,B1,2); C1 = vfmaq_laneq_f32(C1,A3,B1,3); B2 = vld1q_f32(B+b_idx+2*k); C2 = vfmaq_laneq_f32(C2,A0,B2,0); C2 = vfmaq_laneq_f32(C2,A1,B2,1); C2 = vfmaq_laneq_f32(C2,A2,B2,2); C2 = vfmaq_laneq_f32(C2,A3,B3,3); B3 = vld1q_f32(B+b_idx+3*k); C3 = vfmaq_laneq_f32(C3,A0,B3,0); C3 = vfmaq_laneq_f32(C3,A1,B3,1); C3 = vfmaq_laneq_f32(C3,A2,B3,2); C3 = vfmaq_laneq_f32(C3,A3,B3,3); } // compute base index for stores c_idx = n*j_idx + i_idx; vstlq_f32(C+c_idx, C0); vstlq_f32(C+c_idx+n,C1); vstlq_f32(C+c_idx+2*n,C2); vstlq_f32(C+c_idx+3*n,C3); } } }

Compiling and disassembling this function, and comparing it with the C function, shows the following differences:

- Fewer arithmetic instructions for a given matrix multiplication. This is because the function utilizes the Advanced SIMD technology with full register packing. Typical C code, generally, does not do this.
`FMLA`

instructions instead of`FMUL`

instructions, as specified by the intrinsics.- Fewer loop iterations. When used properly, intrinsics allow loops to be unrolled easily.

However, the disassembled code also reveals unnecessary loads and stores because of memory allocation and initialization of data types, for example, `float32x4_t`

. These data types are not used in the non-intrinsics C code.

Optimizing this code for SVE produces the following code:

void matrix_multiply_sve(const float32_t *A, const float32_t *B, float32_t *C, uint32_t n, uint32_t m, uint32_t k) { /* * Multiply matrices A and B, store the result in C. * It is the user's responsibility to make sure the matrices are compatible. */ int a_idx; int b_idx; int c_idx; // these are the columns of a nx4 sub matrix of A svfloat32_t A0; svfloat32_t A1; svfloat32_t A2; svfloat32_t A3; // these are the columns of a 4x4 sub matrix of B svfloat32_t B0; svfloat32_t B1; svfloat32_t B2; svfloat32_t B3; // these are the columns of a nx4 sub matrix of C svfloat32_t C0; svfloat32_t C1; svfloat32_t C2; svfloat32_t C3; for (int i_idx=0; i_idx<n; i_idx+=svcntw()) { // calculate predicate for this i_idx svbool_t pred = svwhilelt_b32_u32(i_idx, n); for (int j_idx=0; j_idx<m; j_idx+=4) { // zero accumulators before matrix op C0 = svdup_n_f32(0); C1 = svdup_n_f32(0); C2 = svdup_n_f32(0); C3 = svdup_n_f32(0); for (int k_idx=0; k_idx<k; k_idx+=4){ // compute base index to 4x4 block a_idx = i_idx + n*k_idx; b_idx = k*j_idx + k_idx; // load most current a values in row A0 = svld1_f32(pred, A+a_idx); A1 = svld1_f32(pred, A+a_idx+n); A2 = svld1_f32(pred, A+a_idx+2*n); A3 = svld1_f32(pred, A+a_idx+3*n); // multiply accumulate 4x1 blocks, that is each column C B0 = svld1rq_f32(svptrue_b32(), B+b_idx); C0 = svmla_lane_f32(C0,A0,B0,0); C0 = svmla_lane_f32(C0,A1,B0,1); C0 = svmla_lane_f32(C0,A2,B0,2); C0 = svmla_lane_f32(C0,A3,B0,3); B1 = svld1rq_f32(svptrue_b32(), B+b_idx+k); C1 = svmla_lane_f32(C1,A0,B1,0); C1 = svmla_lane_f32(C1,A1,B1,1); C1 = svmla_lane_f32(C1,A2,B1,2); C1 = svmla_lane_f32(C1,A3,B1,3); B2 = svld1rq_f32(svptrue_b32(), B+b_idx+2*k); C2 = svmla_lane_f32(C2,A0,B2,0); C2 = svmla_lane_f32(C2,A1,B2,1); C2 = svmla_lane_f32(C2,A2,B2,2); C2 = svmla_lane_f32(C2,A3,B3,3); B3 = svld1rq_f32(svptrue_b32(), B+b_idx+3*k); C3 = svmla_lane_f32(C3,A0,B3,0); C3 = svmla_lane_f32(C3,A1,B3,1); C3 = svmla_lane_f32(C3,A2,B3,2); C3 = svmla_lane_f32(C3,A3,B3,3); } // compute base index for stores c_idx = n*j_idx + i_idx; svst1_f32(pred, C+c_idx, C0); svst1_f32(pred, C+c_idx+n,C1); svst1_f32(pred, C+c_idx+2*n,C2); svst1_f32(pred, C+c_idx+3*n,C3); } } }

This code is almost identical to the earlier Neon code, except for the differing intrinsics. In addition, thanks to predication, there is no longer a constraint on the number of rows of A. However, you must ensure that the number of columns of A and C, and both dimensions of B, are multiples of four. This is because the predication used in this code does not account for this. Adding further predication is possible, but would reduce the clarity of this example.

Comparing this example with the C function and Neon functions reveals that the SVE example:

- Uses
`WHILELT`

to determine the predicate for doing each iteration of the outer loop. This guarantees that there is always at least one element for the loop condition to process. - Increments
`i_idx`

by`CNTW`

(the number of 32-bit elements in a vector) to avoid hard-coding the number of elements that are processed in one iteration of the outer loop.