From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 4866841B9D; Wed, 1 Feb 2023 10:05:48 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id DA27B42D4A; Wed, 1 Feb 2023 10:05:11 +0100 (CET) Received: from mx0b-0016f401.pphosted.com (mx0b-0016f401.pphosted.com [67.231.156.173]) by mails.dpdk.org (Postfix) with ESMTP id 555AC42D3A for ; Wed, 1 Feb 2023 10:05:09 +0100 (CET) Received: from pps.filterd (m0045851.ppops.net [127.0.0.1]) by mx0b-0016f401.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 3116M2gi008963; Wed, 1 Feb 2023 01:05:06 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=marvell.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-type; s=pfpt0220; bh=3BWwykqrvGuH4rBmzirLZp31R60ZVK9fLQq/NuATRbY=; b=CTYoT7LLVY7sSWLGiqatj6DThoQpvSvI4abX8Zvvn2R/ZHxG7ogm7kBAV3q1m6y8XTHA T2vfxJLxJRPDQsHnHr2clwLGWfuW0oDn1S6HUsXtKwXEKtOJuXM6KMCkMklVxYA+PR8U QlpoV4xr0qfdeCph/h9VOFajF8bAfyYQ9rlOhtUOvtASQhWHIUtwzn0NDH3e9HxcKjfd zoY+08/0FesMIQy/3hc41E1P4sZeD8VRTrsSDJgLTOUL2KTz7IPxQ6vmXJT61cmwB7yM DX2r0tYHVzBQRhSHC61MDFnwE0kBgVBaUkBHIrI7W6Ac4aswKtOLK5clg4o7VTdH0P0N sg== Received: from dc5-exch02.marvell.com ([199.233.59.182]) by mx0b-0016f401.pphosted.com (PPS) with ESMTPS id 3nfjrj0p5v-5 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT); Wed, 01 Feb 2023 01:05:06 -0800 Received: from DC5-EXCH01.marvell.com (10.69.176.38) by DC5-EXCH02.marvell.com (10.69.176.39) with Microsoft SMTP Server (TLS) id 15.0.1497.42; Wed, 1 Feb 2023 01:05:03 -0800 Received: from maili.marvell.com (10.69.176.80) by DC5-EXCH01.marvell.com (10.69.176.38) with Microsoft SMTP Server id 15.0.1497.42 via Frontend Transport; Wed, 1 Feb 2023 01:05:03 -0800 Received: from ml-host-33.caveonetworks.com (unknown [10.110.143.233]) by maili.marvell.com (Postfix) with ESMTP id BFF3C3F70E8; Wed, 1 Feb 2023 01:05:02 -0800 (PST) From: Srikanth Yalavarthi To: Srikanth Yalavarthi , Ruifeng Wang CC: , , , Subject: [PATCH v4 4/4] mldev: add Arm NEON type conversion routines Date: Wed, 1 Feb 2023 01:04:53 -0800 Message-ID: <20230201090454.14649-5-syalavarthi@marvell.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230201090454.14649-1-syalavarthi@marvell.com> References: <20221208193532.16718-1-syalavarthi@marvell.com> <20230201090454.14649-1-syalavarthi@marvell.com> MIME-Version: 1.0 Content-Type: text/plain X-Proofpoint-ORIG-GUID: Z7SkSFNDbOEaiTPhi4LULu-JkqqhCXSl X-Proofpoint-GUID: Z7SkSFNDbOEaiTPhi4LULu-JkqqhCXSl X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.930,Hydra:6.0.562,FMLib:17.11.122.1 definitions=2023-02-01_03,2023-01-31_01,2022-06-22_01 X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Added ARM NEON intrinsic based implementations to support conversion of data types. Support is enabled to handle int8, uint8, int16, uint16, float16, float32 and bfloat16 types. Signed-off-by: Srikanth Yalavarthi --- lib/mldev/meson.build | 4 + lib/mldev/mldev_utils_neon.c | 873 +++++++++++++++++++++++++++++++++++ 2 files changed, 877 insertions(+) create mode 100644 lib/mldev/mldev_utils_neon.c diff --git a/lib/mldev/meson.build b/lib/mldev/meson.build index fce9c0ebee..05694b0839 100644 --- a/lib/mldev/meson.build +++ b/lib/mldev/meson.build @@ -8,6 +8,10 @@ sources = files( 'mldev_utils_scalar.c', ) +if arch_subdir == 'arm' + sources += files('mldev_utils_neon.c') +endif + headers = files( 'rte_mldev.h', ) diff --git a/lib/mldev/mldev_utils_neon.c b/lib/mldev/mldev_utils_neon.c new file mode 100644 index 0000000000..32b620db20 --- /dev/null +++ b/lib/mldev/mldev_utils_neon.c @@ -0,0 +1,873 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright (c) 2022 Marvell. + */ + +#include +#include +#include + +#include "mldev_utils.h" + +#include + +/* Description: + * This file implements vector versions of Machine Learning utility functions used to convert data + * types from higher precision to lower precision and vice-versa. Implementation is based on Arm + * Neon intrinsics. + */ + +static inline void +__float32_to_int8_neon_s8x8(float scale, float *input, int8_t *output) +{ + int16x4_t s16x4_l; + int16x4_t s16x4_h; + float32x4_t f32x4; + int16x8_t s16x8; + int32x4_t s32x4; + int8x8_t s8x8; + + /* load 4 float32 elements, scale, convert, saturate narrow to int16. + * Use round to nearest with ties away rounding mode. + */ + f32x4 = vld1q_f32(input); + f32x4 = vmulq_n_f32(f32x4, scale); + s32x4 = vcvtaq_s32_f32(f32x4); + s16x4_l = vqmovn_s32(s32x4); + + /* load next 4 float32 elements, scale, convert, saturate narrow to int16. + * Use round to nearest with ties away rounding mode. + */ + f32x4 = vld1q_f32(input + 4); + f32x4 = vmulq_n_f32(f32x4, scale); + s32x4 = vcvtaq_s32_f32(f32x4); + s16x4_h = vqmovn_s32(s32x4); + + /* combine lower and higher int16x4_t to int16x8_t */ + s16x8 = vcombine_s16(s16x4_l, s16x4_h); + + /* narrow to int8_t */ + s8x8 = vqmovn_s16(s16x8); + + /* store 8 elements */ + vst1_s8(output, s8x8); +} + +static inline void +__float32_to_int8_neon_s8x1(float scale, float *input, int8_t *output) +{ + int32_t s32; + int16_t s16; + + /* scale and convert, round to nearest with ties away rounding mode */ + s32 = vcvtas_s32_f32(scale * (*input)); + + /* saturate narrow */ + s16 = vqmovns_s32(s32); + + /* convert to int8_t */ + *output = vqmovnh_s16(s16); +} + +int +rte_ml_io_float32_to_int8(float scale, uint64_t nb_elements, void *input, void *output) +{ + float *input_buffer; + int8_t *output_buffer; + uint64_t nb_iterations; + uint32_t vlen; + uint64_t i; + + if ((scale == 0) || (nb_elements == 0) || (input == NULL) || (output == NULL)) + return -EINVAL; + + input_buffer = (float *)input; + output_buffer = (int8_t *)output; + vlen = 2 * sizeof(float) / sizeof(int8_t); + nb_iterations = nb_elements / vlen; + + /* convert vlen elements in each iteration */ + for (i = 0; i < nb_iterations; i++) { + __float32_to_int8_neon_s8x8(scale, input_buffer, output_buffer); + input_buffer += vlen; + output_buffer += vlen; + } + + /* convert leftover elements */ + i = i * vlen; + for (; i < nb_elements; i++) { + __float32_to_int8_neon_s8x1(scale, input_buffer, output_buffer); + input_buffer++; + output_buffer++; + } + + return 0; +} + +static inline void +__int8_to_float32_neon_f32x8(float scale, int8_t *input, float *output) +{ + float32x4_t f32x4; + int16x8_t s16x8; + int16x4_t s16x4; + int32x4_t s32x4; + int8x8_t s8x8; + + /* load 8 x int8_t elements */ + s8x8 = vld1_s8(input); + + /* widen int8_t to int16_t */ + s16x8 = vmovl_s8(s8x8); + + /* convert lower 4 elements: widen to int32_t, convert to float, scale and store */ + s16x4 = vget_low_s16(s16x8); + s32x4 = vmovl_s16(s16x4); + f32x4 = vcvtq_f32_s32(s32x4); + f32x4 = vmulq_n_f32(f32x4, scale); + vst1q_f32(output, f32x4); + + /* convert higher 4 elements: widen to int32_t, convert to float, scale and store */ + s16x4 = vget_high_s16(s16x8); + s32x4 = vmovl_s16(s16x4); + f32x4 = vcvtq_f32_s32(s32x4); + f32x4 = vmulq_n_f32(f32x4, scale); + vst1q_f32(output + 4, f32x4); +} + +static inline void +__int8_to_float32_neon_f32x1(float scale, int8_t *input, float *output) +{ + *output = scale * vcvts_f32_s32((int32_t)*input); +} + +int +rte_ml_io_int8_to_float32(float scale, uint64_t nb_elements, void *input, void *output) +{ + int8_t *input_buffer; + float *output_buffer; + uint64_t nb_iterations; + uint32_t vlen; + uint64_t i; + + if ((scale == 0) || (nb_elements == 0) || (input == NULL) || (output == NULL)) + return -EINVAL; + + input_buffer = (int8_t *)input; + output_buffer = (float *)output; + vlen = 2 * sizeof(float) / sizeof(int8_t); + nb_iterations = nb_elements / vlen; + + /* convert vlen elements in each iteration */ + for (i = 0; i < nb_iterations; i++) { + __int8_to_float32_neon_f32x8(scale, input_buffer, output_buffer); + input_buffer += vlen; + output_buffer += vlen; + } + + /* convert leftover elements */ + i = i * vlen; + for (; i < nb_elements; i++) { + __int8_to_float32_neon_f32x1(scale, input_buffer, output_buffer); + input_buffer++; + output_buffer++; + } + + return 0; +} + +static inline void +__float32_to_uint8_neon_u8x8(float scale, float *input, uint8_t *output) +{ + uint16x4_t u16x4_l; + uint16x4_t u16x4_h; + float32x4_t f32x4; + uint32x4_t u32x4; + uint16x8_t u16x8; + uint8x8_t u8x8; + + /* load 4 float elements, scale, convert, saturate narrow to uint16_t. + * use round to nearest with ties away rounding mode. + */ + f32x4 = vld1q_f32(input); + f32x4 = vmulq_n_f32(f32x4, scale); + u32x4 = vcvtaq_u32_f32(f32x4); + u16x4_l = vqmovn_u32(u32x4); + + /* load next 4 float elements, scale, convert, saturate narrow to uint16_t + * use round to nearest with ties away rounding mode. + */ + f32x4 = vld1q_f32(input + 4); + f32x4 = vmulq_n_f32(f32x4, scale); + u32x4 = vcvtaq_u32_f32(f32x4); + u16x4_h = vqmovn_u32(u32x4); + + /* combine lower and higher uint16x4_t */ + u16x8 = vcombine_u16(u16x4_l, u16x4_h); + + /* narrow to uint8x8_t */ + u8x8 = vqmovn_u16(u16x8); + + /* store 8 elements */ + vst1_u8(output, u8x8); +} + +static inline void +__float32_to_uint8_neon_u8x1(float scale, float *input, uint8_t *output) +{ + uint32_t u32; + uint16_t u16; + + /* scale and convert, round to nearest with ties away rounding mode */ + u32 = vcvtas_u32_f32(scale * (*input)); + + /* saturate narrow */ + u16 = vqmovns_u32(u32); + + /* convert to uint8_t */ + *output = vqmovnh_u16(u16); +} + +int +rte_ml_io_float32_to_uint8(float scale, uint64_t nb_elements, void *input, void *output) +{ + float *input_buffer; + uint8_t *output_buffer; + uint64_t nb_iterations; + uint32_t vlen; + uint64_t i; + + if ((scale == 0) || (nb_elements == 0) || (input == NULL) || (output == NULL)) + return -EINVAL; + + input_buffer = (float *)input; + output_buffer = (uint8_t *)output; + vlen = 2 * sizeof(float) / sizeof(uint8_t); + nb_iterations = nb_elements / vlen; + + /* convert vlen elements in each iteration */ + for (i = 0; i < nb_iterations; i++) { + __float32_to_uint8_neon_u8x8(scale, input_buffer, output_buffer); + input_buffer += vlen; + output_buffer += vlen; + } + + /* convert leftover elements */ + i = i * vlen; + for (; i < nb_elements; i++) { + __float32_to_uint8_neon_u8x1(scale, input_buffer, output_buffer); + input_buffer++; + output_buffer++; + } + + return 0; +} + +static inline void +__uint8_to_float32_neon_f32x8(float scale, uint8_t *input, float *output) +{ + float32x4_t f32x4; + uint16x8_t u16x8; + uint16x4_t u16x4; + uint32x4_t u32x4; + uint8x8_t u8x8; + + /* load 8 x uint8_t elements */ + u8x8 = vld1_u8(input); + + /* widen uint8_t to uint16_t */ + u16x8 = vmovl_u8(u8x8); + + /* convert lower 4 elements: widen to uint32_t, convert to float, scale and store */ + u16x4 = vget_low_u16(u16x8); + u32x4 = vmovl_u16(u16x4); + f32x4 = vcvtq_f32_u32(u32x4); + f32x4 = vmulq_n_f32(f32x4, scale); + vst1q_f32(output, f32x4); + + /* convert higher 4 elements: widen to uint32_t, convert to float, scale and store */ + u16x4 = vget_high_u16(u16x8); + u32x4 = vmovl_u16(u16x4); + f32x4 = vcvtq_f32_u32(u32x4); + f32x4 = vmulq_n_f32(f32x4, scale); + vst1q_f32(output + 4, f32x4); +} + +static inline void +__uint8_to_float32_neon_f32x1(float scale, uint8_t *input, float *output) +{ + *output = scale * vcvts_f32_u32((uint32_t)*input); +} + +int +rte_ml_io_uint8_to_float32(float scale, uint64_t nb_elements, void *input, void *output) +{ + uint8_t *input_buffer; + float *output_buffer; + uint64_t nb_iterations; + uint64_t vlen; + uint64_t i; + + if ((scale == 0) || (nb_elements == 0) || (input == NULL) || (output == NULL)) + return -EINVAL; + + input_buffer = (uint8_t *)input; + output_buffer = (float *)output; + vlen = 2 * sizeof(float) / sizeof(uint8_t); + nb_iterations = nb_elements / vlen; + + /* convert vlen elements in each iteration */ + for (i = 0; i < nb_iterations; i++) { + __uint8_to_float32_neon_f32x8(scale, input_buffer, output_buffer); + input_buffer += vlen; + output_buffer += vlen; + } + + /* convert leftover elements */ + i = i * vlen; + for (; i < nb_elements; i++) { + __uint8_to_float32_neon_f32x1(scale, input_buffer, output_buffer); + input_buffer++; + output_buffer++; + } + + return 0; +} + +static inline void +__float32_to_int16_neon_s16x4(float scale, float *input, int16_t *output) +{ + float32x4_t f32x4; + int16x4_t s16x4; + int32x4_t s32x4; + + /* load 4 x float elements */ + f32x4 = vld1q_f32(input); + + /* scale */ + f32x4 = vmulq_n_f32(f32x4, scale); + + /* convert to int32x4_t using round to nearest with ties away rounding mode */ + s32x4 = vcvtaq_s32_f32(f32x4); + + /* saturate narrow to int16x4_t */ + s16x4 = vqmovn_s32(s32x4); + + /* store 4 elements */ + vst1_s16(output, s16x4); +} + +static inline void +__float32_to_int16_neon_s16x1(float scale, float *input, int16_t *output) +{ + int32_t s32; + + /* scale and convert, round to nearest with ties away rounding mode */ + s32 = vcvtas_s32_f32(scale * (*input)); + + /* saturate narrow */ + *output = vqmovns_s32(s32); +} + +int +rte_ml_io_float32_to_int16(float scale, uint64_t nb_elements, void *input, void *output) +{ + float *input_buffer; + int16_t *output_buffer; + uint64_t nb_iterations; + uint32_t vlen; + uint64_t i; + + if ((scale == 0) || (nb_elements == 0) || (input == NULL) || (output == NULL)) + return -EINVAL; + + input_buffer = (float *)input; + output_buffer = (int16_t *)output; + vlen = 2 * sizeof(float) / sizeof(int16_t); + nb_iterations = nb_elements / vlen; + + /* convert vlen elements in each iteration */ + for (i = 0; i < nb_iterations; i++) { + __float32_to_int16_neon_s16x4(scale, input_buffer, output_buffer); + input_buffer += vlen; + output_buffer += vlen; + } + + /* convert leftover elements */ + i = i * vlen; + for (; i < nb_elements; i++) { + __float32_to_int16_neon_s16x1(scale, input_buffer, output_buffer); + input_buffer++; + output_buffer++; + } + + return 0; +} + +static inline void +__int16_to_float32_neon_f32x4(float scale, int16_t *input, float *output) +{ + float32x4_t f32x4; + int16x4_t s16x4; + int32x4_t s32x4; + + /* load 4 x int16_t elements */ + s16x4 = vld1_s16(input); + + /* widen int16_t to int32_t */ + s32x4 = vmovl_s16(s16x4); + + /* convert int32_t to float */ + f32x4 = vcvtq_f32_s32(s32x4); + + /* scale */ + f32x4 = vmulq_n_f32(f32x4, scale); + + /* store float32x4_t */ + vst1q_f32(output, f32x4); +} + +static inline void +__int16_to_float32_neon_f32x1(float scale, int16_t *input, float *output) +{ + *output = scale * vcvts_f32_s32((int32_t)*input); +} + +int +rte_ml_io_int16_to_float32(float scale, uint64_t nb_elements, void *input, void *output) +{ + int16_t *input_buffer; + float *output_buffer; + uint64_t nb_iterations; + uint32_t vlen; + uint64_t i; + + if ((scale == 0) || (nb_elements == 0) || (input == NULL) || (output == NULL)) + return -EINVAL; + + input_buffer = (int16_t *)input; + output_buffer = (float *)output; + vlen = 2 * sizeof(float) / sizeof(int16_t); + nb_iterations = nb_elements / vlen; + + /* convert vlen elements in each iteration */ + for (i = 0; i < nb_iterations; i++) { + __int16_to_float32_neon_f32x4(scale, input_buffer, output_buffer); + input_buffer += vlen; + output_buffer += vlen; + } + + /* convert leftover elements */ + i = i * vlen; + for (; i < nb_elements; i++) { + __int16_to_float32_neon_f32x1(scale, input_buffer, output_buffer); + input_buffer++; + output_buffer++; + } + + return 0; +} + +static inline void +__float32_to_uint16_neon_u16x4(float scale, float *input, uint16_t *output) +{ + float32x4_t f32x4; + uint16x4_t u16x4; + uint32x4_t u32x4; + + /* load 4 float elements */ + f32x4 = vld1q_f32(input); + + /* scale */ + f32x4 = vmulq_n_f32(f32x4, scale); + + /* convert using round to nearest with ties to away rounding mode */ + u32x4 = vcvtaq_u32_f32(f32x4); + + /* saturate narrow */ + u16x4 = vqmovn_u32(u32x4); + + /* store 4 elements */ + vst1_u16(output, u16x4); +} + +static inline void +__float32_to_uint16_neon_u16x1(float scale, float *input, uint16_t *output) +{ + uint32_t u32; + + /* scale and convert, round to nearest with ties away rounding mode */ + u32 = vcvtas_u32_f32(scale * (*input)); + + /* saturate narrow */ + *output = vqmovns_u32(u32); +} + +int +rte_ml_io_float32_to_uint16(float scale, uint64_t nb_elements, void *input, void *output) +{ + float *input_buffer; + uint16_t *output_buffer; + uint64_t nb_iterations; + uint64_t vlen; + uint64_t i; + + if ((scale == 0) || (nb_elements == 0) || (input == NULL) || (output == NULL)) + return -EINVAL; + + input_buffer = (float *)input; + output_buffer = (uint16_t *)output; + vlen = 2 * sizeof(float) / sizeof(uint16_t); + nb_iterations = nb_elements / vlen; + + /* convert vlen elements in each iteration */ + for (i = 0; i < nb_iterations; i++) { + __float32_to_uint16_neon_u16x4(scale, input_buffer, output_buffer); + input_buffer += vlen; + output_buffer += vlen; + } + + /* convert leftover elements */ + i = i * vlen; + for (; i < nb_elements; i++) { + __float32_to_uint16_neon_u16x1(scale, input_buffer, output_buffer); + input_buffer++; + output_buffer++; + } + + return 0; +} + +static inline void +__uint16_to_float32_neon_f32x4(float scale, uint16_t *input, float *output) +{ + float32x4_t f32x4; + uint16x4_t u16x4; + uint32x4_t u32x4; + + /* load 4 x uint16_t elements */ + u16x4 = vld1_u16(input); + + /* widen uint16_t to uint32_t */ + u32x4 = vmovl_u16(u16x4); + + /* convert uint32_t to float */ + f32x4 = vcvtq_f32_u32(u32x4); + + /* scale */ + f32x4 = vmulq_n_f32(f32x4, scale); + + /* store float32x4_t */ + vst1q_f32(output, f32x4); +} + +static inline void +__uint16_to_float32_neon_f32x1(float scale, uint16_t *input, float *output) +{ + *output = scale * vcvts_f32_u32((uint32_t)*input); +} + +int +rte_ml_io_uint16_to_float32(float scale, uint64_t nb_elements, void *input, void *output) +{ + uint16_t *input_buffer; + float *output_buffer; + uint64_t nb_iterations; + uint32_t vlen; + uint64_t i; + + if ((scale == 0) || (nb_elements == 0) || (input == NULL) || (output == NULL)) + return -EINVAL; + + input_buffer = (uint16_t *)input; + output_buffer = (float *)output; + vlen = 2 * sizeof(float) / sizeof(uint16_t); + nb_iterations = nb_elements / vlen; + + /* convert vlen elements in each iteration */ + for (i = 0; i < nb_iterations; i++) { + __uint16_to_float32_neon_f32x4(scale, input_buffer, output_buffer); + input_buffer += vlen; + output_buffer += vlen; + } + + /* convert leftover elements */ + i = i * vlen; + for (; i < nb_elements; i++) { + __uint16_to_float32_neon_f32x1(scale, input_buffer, output_buffer); + input_buffer++; + output_buffer++; + } + + return 0; +} + +static inline void +__float32_to_float16_neon_f16x4(float32_t *input, float16_t *output) +{ + float32x4_t f32x4; + float16x4_t f16x4; + + /* load 4 x float32_t elements */ + f32x4 = vld1q_f32(input); + + /* convert to float16x4_t */ + f16x4 = vcvt_f16_f32(f32x4); + + /* store float16x4_t */ + vst1_f16(output, f16x4); +} + +static inline void +__float32_to_float16_neon_f16x1(float32_t *input, float16_t *output) +{ + float32x4_t f32x4; + float16x4_t f16x4; + + /* load element to 4 lanes */ + f32x4 = vld1q_dup_f32(input); + + /* convert float32_t to float16_t */ + f16x4 = vcvt_f16_f32(f32x4); + + /* store lane 0 / 1 element */ + vst1_lane_f16(output, f16x4, 0); +} + +int +rte_ml_io_float32_to_float16(uint64_t nb_elements, void *input, void *output) +{ + float32_t *input_buffer; + float16_t *output_buffer; + uint64_t nb_iterations; + uint32_t vlen; + uint64_t i; + + if ((nb_elements == 0) || (input == NULL) || (output == NULL)) + return -EINVAL; + + input_buffer = (float32_t *)input; + output_buffer = (float16_t *)output; + vlen = 2 * sizeof(float32_t) / sizeof(float16_t); + nb_iterations = nb_elements / vlen; + + /* convert vlen elements in each iteration */ + for (i = 0; i < nb_iterations; i++) { + __float32_to_float16_neon_f16x4(input_buffer, output_buffer); + input_buffer += vlen; + output_buffer += vlen; + } + + /* convert leftover elements */ + i = i * vlen; + for (; i < nb_elements; i++) { + __float32_to_float16_neon_f16x1(input_buffer, output_buffer); + input_buffer++; + output_buffer++; + } + + return 0; +} + +static inline void +__float16_to_float32_neon_f32x4(float16_t *input, float32_t *output) +{ + float16x4_t f16x4; + float32x4_t f32x4; + + /* load 4 x float16_t elements */ + f16x4 = vld1_f16(input); + + /* convert float16x4_t to float32x4_t */ + f32x4 = vcvt_f32_f16(f16x4); + + /* store float32x4_t */ + vst1q_f32(output, f32x4); +} + +static inline void +__float16_to_float32_neon_f32x1(float16_t *input, float32_t *output) +{ + float16x4_t f16x4; + float32x4_t f32x4; + + /* load element to 4 lanes */ + f16x4 = vld1_dup_f16(input); + + /* convert float16_t to float32_t */ + f32x4 = vcvt_f32_f16(f16x4); + + /* store 1 element */ + vst1q_lane_f32(output, f32x4, 0); +} + +int +rte_ml_io_float16_to_float32(uint64_t nb_elements, void *input, void *output) +{ + float16_t *input_buffer; + float32_t *output_buffer; + uint64_t nb_iterations; + uint32_t vlen; + uint64_t i; + + if ((nb_elements == 0) || (input == NULL) || (output == NULL)) + return -EINVAL; + + input_buffer = (float16_t *)input; + output_buffer = (float32_t *)output; + vlen = 2 * sizeof(float32_t) / sizeof(float16_t); + nb_iterations = nb_elements / vlen; + + /* convert vlen elements in each iteration */ + for (i = 0; i < nb_iterations; i++) { + __float16_to_float32_neon_f32x4(input_buffer, output_buffer); + input_buffer += vlen; + output_buffer += vlen; + } + + /* convert leftover elements */ + i = i * vlen; + for (; i < nb_elements; i++) { + __float16_to_float32_neon_f32x1(input_buffer, output_buffer); + input_buffer++; + output_buffer++; + } + + return 0; +} + +#ifdef __ARM_FEATURE_BF16 + +static inline void +__float32_to_bfloat16_neon_f16x4(float32_t *input, bfloat16_t *output) +{ + float32x4_t f32x4; + bfloat16x4_t bf16x4; + + /* load 4 x float32_t elements */ + f32x4 = vld1q_f32(input); + + /* convert float32x4_t to bfloat16x4_t */ + bf16x4 = vcvt_bf16_f32(f32x4); + + /* store bfloat16x4_t */ + vst1_bf16(output, bf16x4); +} + +static inline void +__float32_to_bfloat16_neon_f16x1(float32_t *input, bfloat16_t *output) +{ + float32x4_t f32x4; + bfloat16x4_t bf16x4; + + /* load element to 4 lanes */ + f32x4 = vld1q_dup_f32(input); + + /* convert float32_t to bfloat16_t */ + bf16x4 = vcvt_bf16_f32(f32x4); + + /* store lane 0 / 1 element */ + vst1_lane_bf16(output, bf16x4, 0); +} + +int +rte_ml_io_float32_to_bfloat16(uint64_t nb_elements, void *input, void *output) +{ + float32_t *input_buffer; + bfloat16_t *output_buffer; + uint64_t nb_iterations; + uint32_t vlen; + uint64_t i; + + if ((nb_elements == 0) || (input == NULL) || (output == NULL)) + return -EINVAL; + + input_buffer = (float32_t *)input; + output_buffer = (bfloat16_t *)output; + vlen = 2 * sizeof(float32_t) / sizeof(bfloat16_t); + nb_iterations = nb_elements / vlen; + + /* convert vlen elements in each iteration */ + for (i = 0; i < nb_iterations; i++) { + __float32_to_bfloat16_neon_f16x4(input_buffer, output_buffer); + input_buffer += vlen; + output_buffer += vlen; + } + + /* convert leftover elements */ + i = i * vlen; + for (; i < nb_elements; i++) { + __float32_to_bfloat16_neon_f16x1(input_buffer, output_buffer); + input_buffer++; + output_buffer++; + } + + return 0; +} + +static inline void +__bfloat16_to_float32_neon_f32x4(bfloat16_t *input, float32_t *output) +{ + bfloat16x4_t bf16x4; + float32x4_t f32x4; + + /* load 4 x bfloat16_t elements */ + bf16x4 = vld1_bf16(input); + + /* convert bfloat16x4_t to float32x4_t */ + f32x4 = vcvt_f32_bf16(bf16x4); + + /* store float32x4_t */ + vst1q_f32(output, f32x4); +} + +static inline void +__bfloat16_to_float32_neon_f32x1(bfloat16_t *input, float32_t *output) +{ + bfloat16x4_t bf16x4; + float32x4_t f32x4; + + /* load element to 4 lanes */ + bf16x4 = vld1_dup_bf16(input); + + /* convert bfloat16_t to float32_t */ + f32x4 = vcvt_f32_bf16(bf16x4); + + /* store lane 0 / 1 element */ + vst1q_lane_f32(output, f32x4, 0); +} + +int +rte_ml_io_bfloat16_to_float32(uint64_t nb_elements, void *input, void *output) +{ + bfloat16_t *input_buffer; + float32_t *output_buffer; + uint64_t nb_iterations; + uint32_t vlen; + uint64_t i; + + if ((nb_elements == 0) || (input == NULL) || (output == NULL)) + return -EINVAL; + + input_buffer = (bfloat16_t *)input; + output_buffer = (float32_t *)output; + vlen = 2 * sizeof(float32_t) / sizeof(bfloat16_t); + nb_iterations = nb_elements / vlen; + + /* convert vlen elements in each iteration */ + for (i = 0; i < nb_iterations; i++) { + __bfloat16_to_float32_neon_f32x4(input_buffer, output_buffer); + input_buffer += vlen; + output_buffer += vlen; + } + + /* convert leftover elements */ + i = i * vlen; + for (; i < nb_elements; i++) { + __bfloat16_to_float32_neon_f32x1(input_buffer, output_buffer); + input_buffer++; + output_buffer++; + } + + return 0; +} + +#endif /* __ARM_FEATURE_BF16 */ -- 2.17.1