aboutsummaryrefslogtreecommitdiff
path: root/src/cpu/kernels/cast/generic/neon/fp16.cpp
blob: 2897f4b2420be76ecd776549940570d666a4bd4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
 * Copyright (c) 2016-2023 Arm Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS)

#include "arm_compute/core/CPP/CPPTypes.h"
#include "arm_compute/core/TensorInfo.h"

#include "src/cpu/kernels/cast/list.h"
#include "src/cpu/kernels/CpuCastKernel.h"
#include "support/SaturateCast.h"

#include "arm_neon.h"

namespace arm_compute
{
namespace cpu
{
void neon_qasymm8_signed_to_fp16_cast(
    const ITensor *_src, ITensor *_dst, const ThreadInfo &info, ConvertPolicy _policy, const Window &window)
{
    ARM_COMPUTE_UNUSED(info);
    ARM_COMPUTE_UNUSED(_policy);

    const auto window_start_x = static_cast<int>(window.x().start());
    const auto window_end_x   = static_cast<int>(window.x().end());
    const int  window_step_x  = 16;

    ARM_COMPUTE_ERROR_ON_NULLPTR(_src, _dst);
    ARM_COMPUTE_ERROR_ON(_src == _dst);

    ARM_COMPUTE_ERROR_ON_NULLPTR(_src, _dst);

    Window win{window};
    win.set(Window::DimX, Window::Dimension(0, 1, 1));

    Iterator src(_src, win);
    Iterator dst(_dst, win);
    execute_window_loop(
        win,
        [&](const Coordinates &)
        {
            const auto src_ptr = reinterpret_cast<const int8_t *>(src.ptr());
            const auto dst_ptr = reinterpret_cast<float16_t *>(dst.ptr());
            int        x       = window_start_x;

            for (; x <= (window_end_x - window_step_x); x += window_step_x)
            {
                const int8x16_t texels_s8 = vld1q_s8(src_ptr + x);

                const int16x8x2_t texels = {{vmovl_s8(vget_low_s8(texels_s8)), vmovl_s8(vget_high_s8(texels_s8))}};
                vst1q_f16(dst_ptr + x, vcvtq_f16_s16(texels.val[0]));
                vst1q_f16(dst_ptr + x + 8, vcvtq_f16_s16(texels.val[1]));
            }

            // Compute left-over elements
            for (; x < window_end_x; ++x)
            {
                *(dst_ptr + x) = static_cast<float16_t>(*(src_ptr + x));
            }
        },
        src, dst);
}

void neon_s32_to_fp16_cast(
    const ITensor *_src, ITensor *_dst, const ThreadInfo &info, ConvertPolicy _policy, const Window &window)
{
    ARM_COMPUTE_UNUSED(info);
    ARM_COMPUTE_UNUSED(_policy);

    const auto window_start_x = static_cast<int>(window.x().start());
    const auto window_end_x   = static_cast<int>(window.x().end());
    const int  window_step_x  = 16;

    ARM_COMPUTE_ERROR_ON_NULLPTR(_src, _dst);
    ARM_COMPUTE_ERROR_ON(_src == _dst);

    ARM_COMPUTE_ERROR_ON_NULLPTR(_src, _dst);

    Window win{window};
    win.set(Window::DimX, Window::Dimension(0, 1, 1));

    Iterator src(_src, win);
    Iterator dst(_dst, win);

    execute_window_loop(
        win,
        [&](const Coordinates &)
        {
            const auto src_ptr = reinterpret_cast<const int32_t *>(src.ptr());
            const auto dst_ptr = reinterpret_cast<float16_t *>(dst.ptr());

            int x = window_start_x;
            for (; x <= (window_end_x - window_step_x); x += window_step_x)
            {
                const float32x4x4_t texels = {
                    {vcvtq_f32_s32(vld1q_s32(src_ptr + x)), vcvtq_f32_s32(vld1q_s32(src_ptr + x + 4)),
                     vcvtq_f32_s32(vld1q_s32(src_ptr + x + 8)), vcvtq_f32_s32(vld1q_s32(src_ptr + x + 12))}};

                vst1q_f16(dst_ptr + x, vcombine_f16(vcvt_f16_f32(texels.val[0]), vcvt_f16_f32(texels.val[1])));
                vst1q_f16(dst_ptr + x + 8, vcombine_f16(vcvt_f16_f32(texels.val[2]), vcvt_f16_f32(texels.val[3])));
            }

            // Compute left-over elements
            for (; x < window_end_x; ++x)
            {
                *(dst_ptr + x) = static_cast<float16_t>(*(src_ptr + x));
            }
        },
        src, dst);
}

void neon_fp32_to_fp16_cast(
    const ITensor *_src, ITensor *_dst, const ThreadInfo &info, ConvertPolicy _policy, const Window &window)
{
    ARM_COMPUTE_UNUSED(info);
    ARM_COMPUTE_UNUSED(_policy);

    const auto window_start_x = static_cast<int>(window.x().start());
    const auto window_end_x   = static_cast<int>(window.x().end());
    const int  window_step_x  = 16;

    ARM_COMPUTE_ERROR_ON_NULLPTR(_src, _dst);
    ARM_COMPUTE_ERROR_ON(_src == _dst);

    ARM_COMPUTE_ERROR_ON_NULLPTR(_src, _dst);

    Window win{window};
    win.set(Window::DimX, Window::Dimension(0, 1, 1));

    Iterator src(_src, win);
    Iterator dst(_dst, win);

    execute_window_loop(
        win,
        [&](const Coordinates &)
        {
            const auto src_ptr = reinterpret_cast<const float *>(src.ptr());
            const auto dst_ptr = reinterpret_cast<float16_t *>(dst.ptr());

            int x = window_start_x;
            for (; x <= (window_end_x - window_step_x); x += window_step_x)
            {
                const float32x4x4_t texels = {{vld1q_f32(src_ptr + x), vld1q_f32(src_ptr + x + 4),
                                               vld1q_f32(src_ptr + x + 8), vld1q_f32(src_ptr + x + 12)}};

                vst1q_f16(dst_ptr + x, vcombine_f16(vcvt_f16_f32(texels.val[0]), vcvt_f16_f32(texels.val[1])));
                vst1q_f16(dst_ptr + x + 8, vcombine_f16(vcvt_f16_f32(texels.val[2]), vcvt_f16_f32(texels.val[3])));
            }

            // Compute left-over elements
            for (; x < window_end_x; ++x)
            {
                *(dst_ptr + x) = static_cast<float16_t>(*(src_ptr + x));
            }
        },
        src, dst);
}

void neon_fp16_to_other_dt_cast(
    const ITensor *_src, ITensor *_dst, const ThreadInfo &info, ConvertPolicy _policy, const Window &window)
{
    ARM_COMPUTE_UNUSED(info);
    ARM_COMPUTE_UNUSED(_policy);

    const auto window_start_x = static_cast<int>(window.x().start());
    const auto window_end_x   = static_cast<int>(window.x().end());
    const int  window_step_x  = 16;

    ARM_COMPUTE_ERROR_ON_NULLPTR(_src, _dst);
    ARM_COMPUTE_ERROR_ON(_src == _dst);

    ARM_COMPUTE_ERROR_ON_NULLPTR(_src, _dst);

    Window win{window};
    win.set(Window::DimX, Window::Dimension(0, 1, 1));

    Iterator src(_src, win);
    Iterator dst(_dst, win);
    switch (_dst->info()->data_type())
    {
        case DataType::QASYMM8_SIGNED:
        {
            /* Down-conversion F16 -> QASYMM8_SIGNED (Always saturating) */
            execute_window_loop(
                win,
                [&](const Coordinates &)
                {
                    const auto src_ptr = reinterpret_cast<const float16_t *>(src.ptr());
                    const auto dst_ptr = reinterpret_cast<int8_t *>(dst.ptr());

                    int x = window_start_x;
                    for (; x <= (window_end_x - window_step_x); x += window_step_x)
                    {
                        const float16x8x2_t texels = {{
                            vld1q_f16(src_ptr + x),
                            vld1q_f16(src_ptr + x + 8),
                        }};

                        vst1q_s8(dst_ptr + x, vcombine_s8(vqmovn_s16(vcvtq_s16_f16(texels.val[0])),
                                                          vqmovn_s16(vcvtq_s16_f16(texels.val[1]))));
                    }

                    // Compute left-over elements
                    for (; x < window_end_x; ++x)
                    {
                        *(dst_ptr + x) = utils::cast::saturate_cast<int8_t>(*(src_ptr + x));
                    }
                },
                src, dst);
            break;
        }
        case DataType::QASYMM8:
        case DataType::U8:
        {
            /* Down-conversion F16 -> QASYMM8/U8 (Always saturating) */
            execute_window_loop(
                win,
                [&](const Coordinates &)
                {
                    const auto src_ptr = reinterpret_cast<const float16_t *>(src.ptr());
                    const auto dst_ptr = reinterpret_cast<uint8_t *>(dst.ptr());

                    int x = window_start_x;
                    for (; x <= (window_end_x - window_step_x); x += window_step_x)
                    {
                        const float16x8x2_t texels = {{
                            vld1q_f16(src_ptr + x),
                            vld1q_f16(src_ptr + x + 8),
                        }};

                        vst1q_u8(dst_ptr + x, vcombine_u8(vqmovun_s16(vcvtq_s16_f16(texels.val[0])),
                                                          vqmovun_s16(vcvtq_s16_f16(texels.val[1]))));
                    }

                    // Compute left-over elements
                    for (; x < window_end_x; ++x)
                    {
                        *(dst_ptr + x) = utils::cast::saturate_cast<uint8_t>(*(src_ptr + x));
                    }
                },
                src, dst);
            break;
        }
        case DataType::F32:
        {
            /* Up-conversion F16 -> F32 */
            execute_window_loop(
                win,
                [&](const Coordinates &)
                {
                    const auto src_ptr = reinterpret_cast<const float16_t *>(src.ptr());
                    const auto dst_ptr = reinterpret_cast<float *>(dst.ptr());

                    int x = window_start_x;
                    for (; x <= (window_end_x - window_step_x); x += window_step_x)
                    {
                        const float16x8x2_t texels = {{vld1q_f16(src_ptr + x), vld1q_f16(src_ptr + x + 8)}};
                        vst1q_f32(dst_ptr + x, vcvt_f32_f16(vget_low_f16(texels.val[0])));
                        vst1q_f32(dst_ptr + x + 4, vcvt_f32_f16(vget_high_f16(texels.val[0])));
                        vst1q_f32(dst_ptr + x + 8, vcvt_f32_f16(vget_low_f16(texels.val[1])));
                        vst1q_f32(dst_ptr + x + 12, vcvt_f32_f16(vget_high_f16(texels.val[1])));
                    }

                    // Compute left-over elements
                    for (; x < window_end_x; ++x)
                    {
                        *(dst_ptr + x) = static_cast<float>(*(src_ptr + x));
                    }
                },
                src, dst);
            break;
        }
        case DataType::S32:
        {
            /* Up-conversion F16 -> S32 */
            execute_window_loop(
                win,
                [&](const Coordinates &)
                {
                    const auto src_ptr = reinterpret_cast<const float16_t *>(src.ptr());
                    const auto dst_ptr = reinterpret_cast<int32_t *>(dst.ptr());

                    int x = window_start_x;
                    for (; x <= (window_end_x - window_step_x); x += window_step_x)
                    {
                        const float16x8x2_t texels = {{vld1q_f16(src_ptr + x), vld1q_f16(src_ptr + x + 8)}};

                        vst1q_s32(dst_ptr + x, vcvtq_s32_f32(vcvt_f32_f16(vget_low_f16(texels.val[0]))));
                        vst1q_s32(dst_ptr + x + 4, vcvtq_s32_f32(vcvt_f32_f16(vget_high_f16(texels.val[0]))));
                        vst1q_s32(dst_ptr + x + 8, vcvtq_s32_f32(vcvt_f32_f16(vget_low_f16(texels.val[1]))));
                        vst1q_s32(dst_ptr + x + 12, vcvtq_s32_f32(vcvt_f32_f16(vget_high_f16(texels.val[1]))));
                    }

                    // Compute left-over elements
                    for (; x < window_end_x; ++x)
                    {
                        *(dst_ptr + x) = static_cast<int32_t>(*(src_ptr + x));
                    }
                },
                src, dst);
            break;
        }
        default:
            ARM_COMPUTE_ERROR("dst data type not supported");
    }
}

void neon_u8_to_fp16_cast(
    const ITensor *_src, ITensor *_dst, const ThreadInfo &info, ConvertPolicy _policy, const Window &window)
{
    ARM_COMPUTE_UNUSED(info);
    ARM_COMPUTE_UNUSED(_policy);

    const auto window_start_x = static_cast<int>(window.x().start());
    const auto window_end_x   = static_cast<int>(window.x().end());
    const int  window_step_x  = 16;

    ARM_COMPUTE_ERROR_ON_NULLPTR(_src, _dst);
    ARM_COMPUTE_ERROR_ON(_src == _dst);

    ARM_COMPUTE_ERROR_ON_NULLPTR(_src, _dst);

    Window win{window};
    win.set(Window::DimX, Window::Dimension(0, 1, 1));

    Iterator src(_src, win);
    Iterator dst(_dst, win);
    /* Up-conversion U8 -> F16 */
    execute_window_loop(
        win,
        [&](const Coordinates &)
        {
            const auto src_ptr = reinterpret_cast<const uint8_t *>(src.ptr());
            const auto dst_ptr = reinterpret_cast<float16_t *>(dst.ptr());

            int x = window_start_x;
            for (; x <= (window_end_x - window_step_x); x += window_step_x)
            {
                const uint8x16_t texels_u8 = vld1q_u8(src_ptr + x);

                const int16x8x2_t texels = {{vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(texels_u8))),
                                             vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(texels_u8)))}};
                vst1q_f16(dst_ptr + x, vcvtq_f16_s16(texels.val[0]));
                vst1q_f16(dst_ptr + x + 8, vcvtq_f16_s16(texels.val[1]));
            }

            // Compute left-over elements
            for (; x < window_end_x; ++x)
            {
                *(dst_ptr + x) = static_cast<float16_t>(*(src_ptr + x));
            }
        },
        src, dst);
    return;
}

} // namespace cpu
} // namespace arm_compute
#endif /* #if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS) */