The vector computation function in DSP provides APIs for basic vector arithmetic operations, initialization and statistical reduction, complex number operations, signal processing, and linear algebra. When you need to perform arithmetic operations, statistical calculations, complex number analysis, convolution, matrix multiplication, or window function generation on sensor data, audio signals, or other numerical sequences, you can use the vector computation APIs.
Vector computation supports both single-precision (float) and double-precision (double) data types and is optimized for the Arm Neon instruction set, delivering significant performance gains in contiguous memory scenarios with a stride of 1. Note that to improve performance, some APIs have adjusted the computation order of floating-point numbers, which may affect the precision of the results.
This category covers all basic element-wise numerical operations, including scalar-vector operations, arithmetic operations between vectors, absolute value, square, threshold, and power operations.
Scalar-Vector Operations
| API | Description |
|---|---|
| void HMS_FAST_DSP_Vsmul (const float* input, size_t strideInput, const float scalar, float* output, size_t strideOutput, size_t length) | Multiplies each element of a vector by a scalar (single-precision). |
| void HMS_FAST_DSP_VsmulD (const double* input, size_t strideInput, const double scalar, double* output, size_t strideOutput, size_t length) | Multiplies each element of a vector by a scalar (double-precision). |
| void HMS_FAST_DSP_Vsdiv (const float* input, size_t strideInput, const float scalar, float* output, size_t strideOutput, size_t length) | Divides each element of a vector by a scalar (single-precision). |
| void HMS_FAST_DSP_VsdivD (const double* input, size_t strideInput, const double scalar, double* output, size_t strideOutput, size_t length) | Divides each element of a vector by a scalar (double-precision). |
| void HMS_FAST_DSP_Svdiv (const float scalar, const float* input, size_t strideInput, float* output, size_t strideOutput, size_t length) | Divides a scalar by each element of a vector (single-precision). |
| void HMS_FAST_DSP_SvdivD (const double scalar, const double* input, size_t strideInput, double* output, size_t strideOutput, size_t length) | Divides a scalar by each element of a vector (double-precision). |
| void HMS_FAST_DSP_Vsadd (const float* input, size_t strideInput, const float scalar, float* output, size_t strideOutput, size_t length) | Adds a scalar to each element of a vector (single-precision). |
| void HMS_FAST_DSP_VsaddD (const double* input, size_t strideInput, const double scalar, double* output, size_t strideOutput, size_t length) | Adds a scalar to each element of a vector (double-precision). |
Operations Between Vectors
| API | Description |
|---|---|
| void HMS_FAST_DSP_Vadd (const float* inputA, size_t strideA, const float* inputB, size_t strideB, float* outputC, size_t strideC, size_t length) | Performs element-wise vector addition (single-precision). |
| void HMS_FAST_DSP_VaddD (const double* inputA, size_t strideA, const double* inputB, size_t strideB, double* outputC, size_t strideC, size_t length) | Performs element-wise vector addition (double-precision). |
| void HMS_FAST_DSP_Vsub (const float* inputA, size_t strideA, const float* inputB, size_t strideB, float* outputC, size_t strideC, size_t length) | Performs element-wise vector subtraction (single-precision). |
| void HMS_FAST_DSP_VsubD (const double* inputA, size_t strideA, const double* inputB, size_t strideB, double* outputC, size_t strideC, size_t length) | Performs element-wise vector subtraction (double-precision). |
| void HMS_FAST_DSP_Vmul (const float* inputA, size_t strideA, const float* inputB, size_t strideB, float* outputC, size_t strideC, size_t length) | Performs element-wise vector multiplication (single-precision). |
| void HMS_FAST_DSP_VmulD (const double* inputA, size_t strideA, const double* inputB, size_t strideB, double* outputC, size_t strideC, size_t length) | Performs element-wise vector multiplication (double-precision). |
| void HMS_FAST_DSP_Vdiv (const float* inputA, size_t strideA, const float* inputB, size_t strideB, float* outputC, size_t strideC, size_t length) | Performs element-wise vector division (single-precision). |
| void HMS_FAST_DSP_VdivD (const double* inputA, size_t strideA, const double* inputB, size_t strideB, double* outputC, size_t strideC, size_t length) | Performs element-wise vector division (double-precision). |
| void HMS_FAST_DSP_Vsbsm (const float* inputA, size_t strideA, const float* inputB, size_t strideB, float scalar, float* outputC, size_t strideC, size_t length) | Performs vector subtraction and scaling (single-precision). |
| void HMS_FAST_DSP_VsbsmD (const double* inputA, size_t strideA, const double* inputB, size_t strideB, double scalar, double* outputC, size_t strideC, size_t length) | Performs vector subtraction and scaling (double-precision). |
| void HMS_FAST_DSP_Vdist (const float* inputA, size_t strideA, const float* inputB, size_t strideB, float* outputC, size_t strideC, size_t length) | Computes the Euclidean norm of corresponding elements in two vectors: C[i] equals the square root of the sum of the squares of A[i] and B[i] (single-precision). |
| void HMS_FAST_DSP_VdistD (const double* inputA, size_t strideA, const double* inputB, size_t strideB, double* outputC, size_t strideC, size_t length) | Computes the Euclidean norm of corresponding elements in two vectors: C[i] equals the square root of the sum of the squares of A[i] and B[i] (double-precision). |
Vector Transformations
| API | Description |
|---|---|
| void HMS_FAST_DSP_Vsq (const float* input, size_t strideInput, float* output, size_t strideOutput, size_t length) | Computes the square of each vector element (single-precision). |
| void HMS_FAST_DSP_VsqD (const double* input, size_t strideInput, double* output, size_t strideOutput, size_t length) | Computes the square of each vector element (double-precision). |
| void HMS_FAST_DSP_Vabs (const float* input, size_t strideInput, float* output, size_t strideOutput, size_t length) | Computes the absolute value of each vector element (single-precision). |
| void HMS_FAST_DSP_VabsD (const double* input, size_t strideInput, double* output, size_t strideOutput, size_t length) | Computes the absolute value of each vector element (double-precision). |
| void HMS_FAST_DSP_Vthr (const float* input, size_t strideInput, const float threshold, float* output, size_t strideOutput, size_t length) | Applies a threshold to the vector: If input[i] < threshold, the threshold is used; otherwise, the original value is used (single-precision). |
| void HMS_FAST_DSP_VthrD (const double* input, size_t strideInput, const double threshold, double* output, size_t strideOutput, size_t length) | Applies a threshold to the vector: If input[i] < threshold, the threshold is used; otherwise, the original value is used (double-precision). |
Power Operation
| API | Description |
|---|---|
| void HMS_FAST_DSP_Vvpow (const float* inputA, const float* inputB, float* outputC, size_t length) | Performs element-wise vector power operation: C[i] = A[i] raised to the power of B[i] (single-precision). |
| void HMS_FAST_DSP_VvpowD (const double* inputA, const double* inputB, double* outputC, size_t length) | Performs element-wise vector power operation: C[i] = A[i] raised to the power of B[i] (double-precision). |
These include operations that reduce vector data to scalars, data generation and filling, type conversion, and element order adjustment.
Initialization/Filling
| API | Description |
|---|---|
| void HMS_FAST_DSP_Vfill (float* vector, size_t stride, size_t length, const float scalar) | Fills a vector with a specified scalar value (single-precision). |
| void HMS_FAST_DSP_VfillD (double* vector, size_t stride, size_t length, const double scalar) | Fills a vector with a specified scalar value (double-precision). |
| void HMS_FAST_DSP_Vclr (float* vector, size_t stride, size_t length) | Clears all elements in a vector (single-precision). |
| void HMS_FAST_DSP_VclrD (double* vector, size_t stride, size_t length) | Clears all elements in a vector (double-precision). |
Type Conversion
| API | Description |
|---|---|
| void HMS_FAST_DSP_Vspdp (const float* input, size_t strideInput, double* output, size_t strideOutput, size_t length) | Converts a single-precision vector to a double-precision vector. |
| void HMS_FAST_DSP_Vdpsp (const double* input, size_t strideInput, float* output, size_t strideOutput, size_t length) | Converts a double-precision vector to a single-precision vector. |
Reduction Operations
| API | Description |
|---|---|
| float HMS_FAST_DSP_Maxmgv (const float* input, size_t stride, size_t length) | Computes the maximum magnitude of a strided real vector (single-precision). |
| double HMS_FAST_DSP_MaxmgvD (const double* input, size_t stride, size_t length) | Computes the maximum magnitude of a strided real vector (double-precision). |
| void HMS_FAST_DSP_Maxvi (const float* input, size_t stride, size_t length, float* value, size_t* index) | Finds the maximum value and its index in a strided real vector (single-precision). |
| void HMS_FAST_DSP_MaxviD (const double* input, size_t stride, size_t length, double* value, size_t* index) | Finds the maximum value and its index in a strided real vector (double-precision). |
| void HMS_FAST_DSP_Minvi (const float* input, size_t stride, size_t length, float* value, size_t* index) | Finds the minimum value and its index in a strided real vector (single-precision). |
| void HMS_FAST_DSP_MinviD (const double* input, size_t stride, size_t length, double* value, size_t* index) | Finds the minimum value and its index in a strided real vector (double-precision). |
| float HMS_FAST_DSP_Sve (const float* input, size_t stride, size_t length) | Computes the sum of a strided real vector (single-precision). |
| double HMS_FAST_DSP_SveD (const double* input, size_t stride, size_t length) | Computes the sum of a strided real vector (double-precision). |
| float HMS_FAST_DSP_Svemg (const float* input, size_t stride, size_t length) | Computes the sum of absolute values (L1 norm) of a strided vector (single-precision). |
| double HMS_FAST_DSP_SvemgD (const double* input, size_t stride, size_t length) | Computes the sum of absolute values (L1 norm) of a strided vector (double-precision). |
| float HMS_FAST_DSP_Meamgv (const float* input, size_t stride, size_t length) | Computes the mean of absolute values of a strided real vector (single-precision). |
| double HMS_FAST_DSP_MeamgvD (const double* input, size_t stride, size_t length) | Computes the mean of absolute values of a strided real vector (double-precision). |
| float HMS_FAST_DSP_Svesq (const float* input, size_t stride, size_t length) | Computes the sum of squares of vector elements (single-precision). |
| double HMS_FAST_DSP_SvesqD (const double* input, size_t stride, size_t length) | Computes the sum of squares of vector elements (double-precision). |
| float HMS_FAST_DSP_Dotpr (const float* inputA, size_t strideA, const float* inputB, size_t strideB, size_t length) | Computes the dot product of two strided real vectors (single-precision). |
| double HMS_FAST_DSP_DotprD (const double* inputA, size_t strideA, const double* inputB, size_t strideB, size_t length) | Computes the dot product of two strided real vectors (double-precision). |
Vector Element Operations
| API | Description |
|---|---|
| void HMS_FAST_DSP_Vrvrs (float* vector, size_t stride, size_t length) | Reverses the order of vector elements in place (single-precision). |
| void HMS_FAST_DSP_VrvrsD (double* vector, size_t stride, size_t length) | Reverses the order of vector elements in place (double-precision). |
| void HMS_FAST_DSP_Vsort (float* vector, size_t length, int order) | Sorts vectors in place (single-precision). |
| void HMS_FAST_DSP_VsortD (double* vector, size_t length, int order) | Sorts vectors in place (double-precision). |
The operations include the amplitude and phase calculation of complex vectors and the conversion between complex number formats.
Basic Complex Number Operations
| API | Description |
|---|---|
| void HMS_FAST_DSP_Zvabs (const FAST_SplitComplex* input, size_t strideInput, float* output, size_t strideOutput, size_t length) | Computes the magnitude of a complex vector (single-precision). |
| void HMS_FAST_DSP_ZvabsD (const FAST_SplitComplexD* input, size_t strideInput, double* output, size_t strideOutput, size_t length) | Computes the magnitude of a complex vector (double-precision). |
| void HMS_FAST_DSP_Zvmags (const FAST_SplitComplex* input, size_t strideInput, float* output, size_t strideOutput, size_t length) | Computes the squared magnitude of a complex vector (single-precision). |
| void HMS_FAST_DSP_ZvmagsD (const FAST_SplitComplexD* input, size_t strideInput, double* output, size_t strideOutput, size_t length) | Computes the squared magnitude of a complex vector (double-precision). |
| void HMS_FAST_DSP_Zvphas (const FAST_SplitComplex* input, size_t strideInput, float* output, size_t strideOutput, size_t length) | Computes the phase angles (in radians) of a complex vector (single-precision). |
| void HMS_FAST_DSP_ZvphasD (const FAST_SplitComplexD* input, size_t strideInput, double* output, size_t strideOutput, size_t length) | Computes the phase angles (in radians) of a complex vector (double-precision). |
Complex Number Format Conversion
| API | Description |
|---|---|
| void HMS_FAST_DSP_Ctoz (const float* input, size_t strideInput, FAST_SplitComplex* output, size_t strideOutput, size_t length) | Converts an interleaved complex array to the split format (single-precision). |
| void HMS_FAST_DSP_CtozD (const double* input, size_t strideInput, FAST_SplitComplexD* output, size_t strideOutput, size_t length) | Converts an interleaved complex array to the split format (double-precision). |
| void HMS_FAST_DSP_Ztoc (const FAST_SplitComplex* input, size_t strideInput, float* output, size_t strideOutput, size_t length) | Converts a split complex array to the interleaved format (single-precision). |
| void HMS_FAST_DSP_ZtocD (const FAST_SplitComplexD* input, size_t strideInput, double* output, size_t strideOutput, size_t length) | Converts a split complex array to the interleaved format (double-precision). |
These include convolution, window generation, and matrix operations.
Convolution
| API | Description |
|---|---|
| void HMS_FAST_DSP_Conv (const float* input, size_t strideInput, const float* filter, size_t strideFilter, float* output, size_t strideOutput, size_t outputLength, size_t filterLength) | Performs convolution on two vectors (single-precision). |
| void HMS_FAST_DSP_ConvD (const double* input, size_t strideInput, const double* filter, size_t strideFilter, double* output, size_t strideOutput, size_t outputLength, size_t filterLength) | Performs convolution on two vectors (double-precision). |
Window Generation
| API | Description |
|---|---|
| void HMS_FAST_DSP_HannWindow (float* output, size_t length, HMS_FAST_HannWindowType type) | Generates a Hanning window sequence (single-precision). |
| void HMS_FAST_DSP_HannWindowD (double* output, size_t length, HMS_FAST_HannWindowType type) | Generates a Hanning window sequence (double-precision). |
Matrix Computing
| API | Description |
|---|---|
| void HMS_FAST_DSP_Mmul (const float* matrixA, size_t strideA, const float* matrixB, size_t strideB, float* matrixC, size_t strideC, size_t rowsM, size_t colsN, size_t colsP) | Performs matrix multiplication (single-precision). |
| void HMS_FAST_DSP_MmulD (const double* matrixA, size_t strideA, const double* matrixB, size_t strideB, double* matrixC, size_t strideC, size_t rowsM, size_t colsN, size_t colsP) | Performs matrix multiplication (double-precision). |
Link the related dynamic library in the CMake script.
find_library(
lib_fast_dsp
NAMES fast_dsp
)
target_link_libraries(entry PRIVATE ${lib_fast_dsp}) Include the header file.
#include "FASTKit/fast_dsp_common.h"
Select the corresponding function based on the data type (no suffix for single-precision, and suffix D for double-precision).
Call the vector computation function, making sure to set the stride parameter correctly (set stride to 1 for contiguous memory).
Check the returned result.
#include <cstdio>
#include <cstdlib>
#include "FASTKit/fast_dsp_common.h"
FAST_ErrorCode max_value_demo() {
// Define the input vector.
float input[] = {1.0f, -2.0f, 3.0f, -4.0f, 5.0f};
size_t length = sizeof(input) / sizeof(float);
size_t stride = 1;
// Compute the maximum magnitude (maximum absolute value).
float max_magnitude = HMS_FAST_DSP_Maxmgv(input, stride, length);
printf("Max magnitude: %f\n", max_magnitude); // Output 5.0.
// Find the maximum value and its index.
float max_value = 0.0f;
size_t max_index = 0;
HMS_FAST_DSP_Maxvi(input, stride, length, &max_value, &max_index);
printf("Max value: %f at index %zu\n", max_value, max_index); // Output 5.0 at index 4.
return FAST_ERROR_CODE_SUCCESS;
} #include <cstdio>
#include <cstdlib>
#include "FASTKit/fast_dsp_common.h"
FAST_ErrorCode statistics_demo() {
// Define the input vector.
float input[] = {1.0f, -2.0f, 3.0f, -4.0f, 5.0f};
size_t length = sizeof(input) / sizeof(float);
size_t stride = 1;
// Compute the sum of the vector.
float sum = HMS_FAST_DSP_Sve(input, stride, length);
printf("Sum: %f\n", sum); // Output 3.0.
// Compute the sum of absolute values (L1 norm).
float sum_abs = HMS_FAST_DSP_Svemg(input, stride, length);
printf("Sum of absolute values: %f\n", sum_abs); // Output 15.0.
// Compute the mean of absolute values.
float mean_abs = HMS_FAST_DSP_Meamgv(input, stride, length);
printf("Mean of absolute values: %f\n", mean_abs); // Output 3.0.
return FAST_ERROR_CODE_SUCCESS;
} #include <cstdio>
#include <cstdlib>
#include "FASTKit/fast_dsp_common.h"
FAST_ErrorCode vector_operations_demo() {
// Define two input vectors.
float inputA[] = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f};
float inputB[] = {0.5f, 1.0f, 1.5f, 2.0f, 2.5f};
size_t length = 5;
size_t stride = 1;
// Compute the dot product.
float dot_product = HMS_FAST_DSP_Dotpr(inputA, stride, inputB, stride, length);
printf("Dot product: %f\n", dot_product); // Output 27.5.
// Perform vector subtraction: outputC[i] = (inputA[i] - inputB[i]) * 2.0.
float outputC[5];
float scalar = 2.0f;
HMS_FAST_DSP_Vsbsm(inputA, stride, inputB, stride, scalar, outputC, stride, length);
printf("Vector subtraction result:\n");
for (size_t i = 0; i < length; ++i) {
printf(" outputC[%zu] = %f\n", i, outputC[i]);
}
// Output 1.0, 2.0, 3.0, 4.0, 5.0.
return FAST_ERROR_CODE_SUCCESS;
} #include <cstdio>
#include <cstdlib>
#include "FASTKit/fast_dsp_common.h"
FAST_ErrorCode complex_conversion_demo() {
// Define the interleaved complex input (real, imag, real, imag...).
float interleaved[] = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f};
size_t length = 3; // 3 complex numbers.
size_t stride_input = 1;
// Prepare for output in split format.
float real_array[3];
float imag_array[3];
FAST_SplitComplex split_output = {
.real = real_array,
.imag = imag_array
};
size_t stride_output = 1;
// Convert to the split format.
HMS_FAST_DSP_Ctoz(interleaved, stride_input, &split_output, stride_output, length);
printf("Split format:\n");
for (size_t i = 0; i < length; ++i) {
printf(" Complex[%zu] = %f + %fi\n", i, real_array[i], imag_array[i]);
}
/* xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Split format:
Complex[0] = 1.000000 + 2.000000i
Complex[1] = 3.000000 + 4.000000i
Complex[2] = 5.000000 + 6.000000i
*/
// Convert to the interleaved format.
float interleaved_output[6];
HMS_FAST_DSP_Ztoc(&split_output, stride_output, interleaved_output, stride_input, length);
printf("Interleaved format:\n");
for (size_t i = 0; i < length; ++i) {
printf(" Complex[%zu] = %f + %fi\n", i, interleaved_output[i * 2], interleaved_output[i * 2 + 1]);
}
return FAST_ERROR_CODE_SUCCESS;
} #include <cstdio>
#include <cstdlib>
#include "FASTKit/fast_dsp_common.h"
FAST_ErrorCode strided_access_demo() {
// Define the interleaved complex data (real, imag, real, imag...).
float interleaved[] = {1.0f, 10.0f, 2.0f, 20.0f, 3.0f, 30.0f, 4.0f, 40.0f, 5.0f, 50.0f};
size_t length = 5; // 5 real values.
size_t stride = 2; // The stride is 2, skipping the imaginary part.
// Compute the maximum magnitude of the real part vector.
float max_magnitude = HMS_FAST_DSP_Maxmgv(interleaved, stride, length);
printf("Max magnitude of real parts: %f\n", max_magnitude); // Output 5.0.
return FAST_ERROR_CODE_SUCCESS;
}