Luca Foschiani | ee939fb | 2020-01-28 10:38:07 +0000 | [diff] [blame] | 1 | /* |
Pablo Marquez Tello | 6bcdc57 | 2023-01-11 09:54:00 +0000 | [diff] [blame] | 2 | * Copyright (c) 2020, 2022-2023 Arm Limited. |
Luca Foschiani | ee939fb | 2020-01-28 10:38:07 +0000 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #ifndef ARM_COMPUTE_WRAPPER_CVT_H |
| 25 | #define ARM_COMPUTE_WRAPPER_CVT_H |
| 26 | |
| 27 | #include <arm_neon.h> |
| 28 | |
| 29 | namespace arm_compute |
| 30 | { |
| 31 | namespace wrapper |
| 32 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 33 | #define VCVT_TO_F32_IMPL(ptype, vtype, prefix, postfix1, postfix2) \ |
| 34 | template <typename T> \ |
| 35 | inline typename std::enable_if<std::is_same<T, float>::value, float32x4_t>::type vcvt(const vtype &a) \ |
| 36 | { \ |
| 37 | return prefix##_##postfix1##_##postfix2(a); \ |
Luca Foschiani | ee939fb | 2020-01-28 10:38:07 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | VCVT_TO_F32_IMPL(float32x4_t, uint32x4_t, vcvtq, f32, u32) |
| 41 | VCVT_TO_F32_IMPL(float32x4_t, int32x4_t, vcvtq, f32, s32) |
Sang-Hoon Park | 3351f2a | 2020-07-16 14:26:16 +0100 | [diff] [blame] | 42 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 43 | VCVT_TO_F32_IMPL(float32x4_t, float16x4_t, vcvt, f32, f16) |
| 44 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Luca Foschiani | ee939fb | 2020-01-28 10:38:07 +0000 | [diff] [blame] | 45 | #undef VCVT_TO_F32_IMPL |
| 46 | |
Sang-Hoon Park | 3351f2a | 2020-07-16 14:26:16 +0100 | [diff] [blame] | 47 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 48 | #define VCVT_TO_F16_IMPL(ptype, vtype, prefix, postfix1, postfix2) \ |
| 49 | template <typename T> \ |
| 50 | inline typename std::enable_if<std::is_same<T, float16_t>::value, float16x4_t>::type vcvt(const vtype &a) \ |
| 51 | { \ |
| 52 | return prefix##_##postfix1##_##postfix2(a); \ |
Sang-Hoon Park | 3351f2a | 2020-07-16 14:26:16 +0100 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | VCVT_TO_F16_IMPL(float16x4_t, float32x4_t, vcvt, f16, f32) |
| 56 | #undef VCVT_TO_F16_IMPL |
| 57 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 58 | |
Luca Foschiani | ee939fb | 2020-01-28 10:38:07 +0000 | [diff] [blame] | 59 | template <typename T> |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 60 | inline typename std::enable_if<std::is_same<T, uint8_t>::value || std::is_same<T, uint32_t>::value, uint32x4_t>::type |
Luca Foschiani | ee939fb | 2020-01-28 10:38:07 +0000 | [diff] [blame] | 61 | vcvt(const float32x4_t &a) |
| 62 | { |
| 63 | return vcvtq_u32_f32(a); |
| 64 | } |
| 65 | |
| 66 | template <typename T> |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 67 | inline typename std::enable_if<std::is_same<T, int8_t>::value || std::is_same<T, int32_t>::value, int32x4_t>::type |
Luca Foschiani | ee939fb | 2020-01-28 10:38:07 +0000 | [diff] [blame] | 68 | vcvt(const float32x4_t &a) |
| 69 | { |
| 70 | return vcvtq_s32_f32(a); |
| 71 | } |
| 72 | |
Gunes Bayir | c4f2743 | 2022-09-11 15:59:19 +0100 | [diff] [blame] | 73 | #ifdef __aarch64__ |
| 74 | template <typename T> |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 75 | inline typename std::enable_if<std::is_same<T, uint32_t>::value, uint32x4_t>::type vcvta(const float32x4_t &a) |
Gunes Bayir | c4f2743 | 2022-09-11 15:59:19 +0100 | [diff] [blame] | 76 | { |
| 77 | return vcvtaq_u32_f32(a); |
| 78 | } |
| 79 | |
| 80 | template <typename T> |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 81 | inline typename std::enable_if<std::is_same<T, int32_t>::value, int32x4_t>::type vcvta(const float32x4_t &a) |
Gunes Bayir | c4f2743 | 2022-09-11 15:59:19 +0100 | [diff] [blame] | 82 | { |
| 83 | return vcvtaq_s32_f32(a); |
| 84 | } |
| 85 | #endif //__aarch64__ |
Pablo Marquez Tello | 6bcdc57 | 2023-01-11 09:54:00 +0000 | [diff] [blame] | 86 | |
| 87 | #if defined(ARM_COMPUTE_ENABLE_BF16) |
| 88 | /** Convert 2x128-bit floating point vectors into 1x128-bit bfloat16 vector |
| 89 | * |
| 90 | * @param[in] inptr Pointer to the input memory to load values from |
| 91 | * @param[in,out] outptr Pointer to the output memory to store values to |
| 92 | */ |
| 93 | inline void vcvt_bf16_f32(const float *inptr, uint16_t *outptr) |
| 94 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 95 | __asm __volatile("ldp q0, q1, [%[inptr]]\n" |
| 96 | ".inst 0xea16800\n" // BFCVTN v0, v0 |
| 97 | ".inst 0x4ea16820\n" // BFCVTN2 v0, v1 |
| 98 | "str q0, [%[outptr]]\n" |
| 99 | : [inptr] "+r"(inptr) |
| 100 | : [outptr] "r"(outptr) |
| 101 | : "v0", "v1", "memory"); |
Pablo Marquez Tello | 6bcdc57 | 2023-01-11 09:54:00 +0000 | [diff] [blame] | 102 | } |
| 103 | #endif /* defined(ARM_COMPUTE_ENABLE_BF16) */ |
| 104 | |
Luca Foschiani | ee939fb | 2020-01-28 10:38:07 +0000 | [diff] [blame] | 105 | } // namespace wrapper |
| 106 | } // namespace arm_compute |
| 107 | #endif /* ARM_COMPUTE_WRAPPER_CVT_H */ |