blob: e52e3dd0c428524fc850f33b8d8ffb9849d1b00e [file] [log] [blame]
Luca Foschianiee939fb2020-01-28 10:38:07 +00001/*
Pablo Marquez Tellod208f4f2022-07-19 12:19:46 +01002 * Copyright (c) 2020, 2022 Arm Limited.
Luca Foschianiee939fb2020-01-28 10:38:07 +00003 *
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
29namespace arm_compute
30{
31namespace wrapper
32{
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 \
36 vcvt(const vtype &a) \
37 { \
38 return prefix##_##postfix1##_##postfix2(a); \
39 }
40
41VCVT_TO_F32_IMPL(float32x4_t, uint32x4_t, vcvtq, f32, u32)
42VCVT_TO_F32_IMPL(float32x4_t, int32x4_t, vcvtq, f32, s32)
Sang-Hoon Park3351f2a2020-07-16 14:26:16 +010043#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
44VCVT_TO_F32_IMPL(float32x4_t, float16x4_t, vcvt, f32, f16)
45#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Luca Foschianiee939fb2020-01-28 10:38:07 +000046#undef VCVT_TO_F32_IMPL
47
Sang-Hoon Park3351f2a2020-07-16 14:26:16 +010048#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
49#define VCVT_TO_F16_IMPL(ptype, vtype, prefix, postfix1, postfix2) \
50 template <typename T> \
51 inline typename std::enable_if<std::is_same<T, float16_t>::value, float16x4_t>::type \
52 vcvt(const vtype &a) \
53 { \
54 return prefix##_##postfix1##_##postfix2(a); \
55 }
56
57VCVT_TO_F16_IMPL(float16x4_t, float32x4_t, vcvt, f16, f32)
58#undef VCVT_TO_F16_IMPL
59#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
60
Luca Foschianiee939fb2020-01-28 10:38:07 +000061template <typename T>
62inline typename std::enable_if<std::is_same<T, uint8_t>::value, uint32x4_t>::type
63vcvt(const float32x4_t &a)
64{
65 return vcvtq_u32_f32(a);
66}
67
68template <typename T>
69inline typename std::enable_if<std::is_same<T, int8_t>::value, int32x4_t>::type
70vcvt(const float32x4_t &a)
71{
72 return vcvtq_s32_f32(a);
73}
74
Pablo Marquez Tellod208f4f2022-07-19 12:19:46 +010075#if defined(ARM_COMPUTE_ENABLE_BF16)
Georgios Pinitase8291ac2020-02-26 09:58:13 +000076/** Convert 2x128-bit floating point vectors into 1x128-bit bfloat16 vector
77 *
78 * @param[in] inptr Pointer to the input memory to load values from
79 * @param[in,out] outptr Pointer to the output memory to store values to
80 */
81inline void vcvt_bf16_f32(const float *inptr, uint16_t *outptr)
82{
83 __asm __volatile(
84 "ldp q0, q1, [%[inptr]]\n"
85 ".inst 0xea16800\n" // BFCVTN v0, v0
86 ".inst 0x4ea16820\n" // BFCVTN2 v0, v1
87 "str q0, [%[outptr]]\n"
88 : [inptr] "+r"(inptr)
89 : [outptr] "r"(outptr)
90 : "v0", "v1", "memory");
91}
Pablo Marquez Tellod208f4f2022-07-19 12:19:46 +010092#endif /* defined(ARM_COMPUTE_ENABLE_BF16) */
Georgios Pinitase8291ac2020-02-26 09:58:13 +000093
Luca Foschianiee939fb2020-01-28 10:38:07 +000094} // namespace wrapper
95} // namespace arm_compute
96#endif /* ARM_COMPUTE_WRAPPER_CVT_H */