Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
/*++
 
Copyright (c) Microsoft Corporation. All rights reserved.
 
Licensed under the MIT License.
 
Module Name:
 
    cast_kernel_neon.cpp
 
Abstract:
 
    This module implements the common kernels for ARM NEON specific to float16.
 
--*/
 
#include "mlasi.h"
 
#include "arm_neon.h"
 
// This file is enabled in cmake only if ARM64 is defined and not on Apple platforms
// The cmake condition is equivalent to MLAS_F16VEC_INTRINSICS_SUPPORTED && MLAS_TARGET_ARM64.
// Therefore omit the MLAS_F16VEC_INTRINSICS_SUPPORTED && MLAS_TARGET_ARM64 macro in this file.
 
MLAS_FORCEINLINE
size_t
StoreFp32Lane(float* dest, float32x4_t src, size_t count)
{
    if (count == 3) {
        vst1q_lane_f32(dest + 0, src, 0);
        vst1q_lane_f32(dest + 1, src, 1);
        vst1q_lane_f32(dest + 2, src, 2);
        return 3;
    } else if (count == 2) {
        vst1q_lane_f32(dest + 0, src, 0);
        vst1q_lane_f32(dest + 1, src, 1);
        return 2;
    } else if (count == 1) {
        vst1q_lane_f32(dest + 0, src, 0);
        return 1;
    }
 
    return 0;
}
 
void
MlasCastF16ToF32KernelNeon(const unsigned short* src, float* dest, size_t count)
{
    // 4 float16 alignment
    auto* src_aligned = reinterpret_cast<const unsigned short*>((reinterpret_cast<uintptr_t>(src) + 7) & ~7);
    auto pre_count = std::min(static_cast<size_t>(src_aligned - src), count);
    size_t i = 0;
 
    // Handle leading unaligned src
    if (pre_count > 0) {
        float16x4_t fp16v4;
        std::memcpy(&fp16v4, src, pre_count * sizeof(unsigned short));
        float32x4_t fp32v4 = vcvt_f32_f16(fp16v4);
 
        i = StoreFp32Lane(dest, fp32v4, pre_count);
    }
 
    // aligned src
    for (; i + 7 < count; i += 8)
    {
        float16x4_t fp16v4_0 = vreinterpret_f16_u16(vld1_u16(src + i));
        float32x4_t fp32v4_0 = vcvt_f32_f16(fp16v4_0);
        vst1q_f32(dest + i, fp32v4_0);
 
        float16x4_t fp16v4_1 = vreinterpret_f16_u16(vld1_u16(src + i + 4));
        float32x4_t fp32v4_1 = vcvt_f32_f16(fp16v4_1);
        vst1q_f32(dest + i + 4, fp32v4_1);
    }
 
    if (i + 3 < count)
    {
        float16x4_t fp16v4_0 = vreinterpret_f16_u16(vld1_u16(src + i));
        float32x4_t fp32v4_0 = vcvt_f32_f16(fp16v4_0);
        vst1q_f32(dest + i, fp32v4_0);
        i += 4;
    }
 
    // Handle trailing unaligned src
    auto post_count = count - i;
    if (post_count > 0)
    {
        float16x4_t fp16v4;
        std::memcpy(&fp16v4, src + i, post_count * sizeof(unsigned short));
        float32x4_t fp32v4 = vcvt_f32_f16(fp16v4);
 
        StoreFp32Lane(dest + i, fp32v4, post_count);
    }
}
 
MLAS_FORCEINLINE
size_t
StoreU16Lane(unsigned short* dest, uint16x4_t src, size_t count)
{
    if (count == 3) {
        vst1_lane_u16(dest + 0, src, 0);
        vst1_lane_u16(dest + 1, src, 1);
        vst1_lane_u16(dest + 2, src, 2);
        return 3;
    } else if (count == 2) {
        vst1_lane_u16(dest + 0, src, 0);
        vst1_lane_u16(dest + 1, src, 1);
        return 2;
    } else if (count == 1) {
        vst1_lane_u16(dest + 0, src, 0);
        return 1;
    }
 
    return 0;
}
 
void
MlasCastF32ToF16KernelNeon(const float* src, unsigned short* dest, size_t count)
{
    // 4 float32 alignment
    auto* src_aligned = reinterpret_cast<const float*>((reinterpret_cast<uintptr_t>(src) + 15) & ~15);
    auto pre_count = std::min(static_cast<size_t>(src_aligned - src), count);
    size_t i = 0;
 
    // Handle leading unaligned src
    if (pre_count > 0)
    {
        float32x4_t fp32v4;
        std::memcpy(&fp32v4, src, pre_count * sizeof(float));
        uint16x4_t u16v4 = vreinterpret_u16_f16(vcvt_f16_f32(fp32v4));
 
        i = StoreU16Lane(dest, u16v4, pre_count);
    }
 
    // aligned src
    for (; i + 7 < count; i += 8)
    {
        float32x4_t fp32v4_0 = vld1q_f32(src + i);
        float16x4_t fp16v4_0 = vcvt_f16_f32(fp32v4_0);
        vst1_u16(dest + i, vreinterpret_u16_f16(fp16v4_0));
 
        float32x4_t fp32v4_1 = vld1q_f32(src + i + 4);
        float16x4_t fp16v4_1 = vcvt_f16_f32(fp32v4_1);
        vst1_u16(dest + i + 4, vreinterpret_u16_f16(fp16v4_1));
    }
 
    if (i + 3 < count)
    {
        float32x4_t fp32v4_0 = vld1q_f32(src + i);
        float16x4_t fp16v4_0 = vcvt_f16_f32(fp32v4_0);
        vst1_u16(dest + i, vreinterpret_u16_f16(fp16v4_0));
        i += 4;
    }
 
    // Handle trailing unaligned src
    auto post_count = count - i;
    if (post_count > 0)
    {
        float32x4_t fp32v4;
        std::memcpy(&fp32v4, src + i, post_count * sizeof(float));
        uint16x4_t u16v4 = vreinterpret_u16_f16(vcvt_f16_f32(fp32v4));
 
        StoreU16Lane(dest + i, u16v4, post_count);
    }
}