interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / src / ggml-hexagon / htp / hvx-arith.h

953 lines · 50313 bytes Raw Blame History
1#ifndef HVX_ARITH_H
2#define HVX_ARITH_H
3
4#include <assert.h>
5#include <stddef.h>
6#include <stdint.h>
7#include <math.h>
8
9#include "hvx-base.h"
10#include "hvx-floor.h"
11#include "hex-utils.h"
12
13//
14// Binary operations (add, mul, sub)
15//
16
17#define UNUSED(x) (void)(x)
18
19#define hvx_arith_loop_body(dst_type, src0_type, src1_type, elem_size, vec_store, vec_op) \
20 do { \
21 dst_type * vdst = (dst_type *) dst; \
22 src0_type * vsrc0 = (src0_type *) src0; \
23 src1_type * vsrc1 = (src1_type *) src1; \
24 \
25 const uint32_t epv = 128 / (elem_size); \
26 const uint32_t nvec = n / epv; \
27 const uint32_t nloe = n % epv; \
28 \
29 uint32_t i = 0; \
30 \
31 _Pragma("unroll(4)") \
32 for (; i < nvec; i++) { \
33 vdst[i] = vec_op(vsrc0[i], vsrc1[i]); \
34 } \
35 if (nloe) { \
36 HVX_Vector v = vec_op(vsrc0[i], vsrc1[i]); \
37 vec_store((void *) &vdst[i], nloe * (elem_size), v); \
38 } \
39 } while(0)
40
41#if __HVX_ARCH__ < 79
42
43#define HVX_OP_ADD_F32(a, b) Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_VsfVsf(a, b))
44#define HVX_OP_SUB_F32(a, b) Q6_Vsf_equals_Vqf32(Q6_Vqf32_vsub_VsfVsf(a, b))
45#define HVX_OP_MUL_F32(a, b) Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(a, b))
46
47#else
48
49#define HVX_OP_ADD_F32(a, b) Q6_Vsf_vadd_VsfVsf(a, b)
50#define HVX_OP_SUB_F32(a, b) Q6_Vsf_vsub_VsfVsf(a, b)
51#define HVX_OP_MUL_F32(a, b) Q6_Vsf_vmpy_VsfVsf(a, b)
52
53#endif
54
55#define HVX_OP_ADD_F16(a, b) hvx_vec_add_f16_f16(a, b)
56#define HVX_OP_SUB_F16(a, b) hvx_vec_sub_f16_f16(a, b)
57#define HVX_OP_MUL_F16(a, b) hvx_vec_mul_f16_f16(a, b)
58
59// Generic macro to define alignment permutations for an op
60#define DEFINE_HVX_BINARY_OP_VARIANTS(OP_NAME, OP_MACRO, ELEM_TYPE) \
61static inline void OP_NAME##_aaa(uint8_t * dst, const uint8_t * src0, const uint8_t * src1, uint32_t n) { \
62 assert((uintptr_t) dst % 128 == 0); \
63 assert((uintptr_t) src0 % 128 == 0); \
64 assert((uintptr_t) src1 % 128 == 0); \
65 hvx_arith_loop_body(HVX_Vector, HVX_Vector, HVX_Vector, sizeof(ELEM_TYPE), hvx_vec_store_a, OP_MACRO); \
66} \
67static inline void OP_NAME##_aau(uint8_t * dst, const uint8_t * src0, const uint8_t * src1, uint32_t n) { \
68 assert((uintptr_t) dst % 128 == 0); \
69 assert((uintptr_t) src0 % 128 == 0); \
70 hvx_arith_loop_body(HVX_Vector, HVX_Vector, HVX_UVector, sizeof(ELEM_TYPE), hvx_vec_store_a, OP_MACRO); \
71} \
72static inline void OP_NAME##_aua(uint8_t * dst, const uint8_t * src0, const uint8_t * src1, uint32_t n) { \
73 assert((uintptr_t) dst % 128 == 0); \
74 assert((uintptr_t) src1 % 128 == 0); \
75 hvx_arith_loop_body(HVX_Vector, HVX_UVector, HVX_Vector, sizeof(ELEM_TYPE), hvx_vec_store_a, OP_MACRO); \
76} \
77static inline void OP_NAME##_auu(uint8_t * dst, const uint8_t * src0, const uint8_t * src1, uint32_t n) { \
78 assert((uintptr_t) dst % 128 == 0); \
79 hvx_arith_loop_body(HVX_Vector, HVX_UVector, HVX_UVector, sizeof(ELEM_TYPE), hvx_vec_store_a, OP_MACRO); \
80} \
81static inline void OP_NAME##_uaa(uint8_t * dst, const uint8_t * src0, const uint8_t * src1, uint32_t n) { \
82 assert((uintptr_t) src0 % 128 == 0); \
83 assert((uintptr_t) src1 % 128 == 0); \
84 hvx_arith_loop_body(HVX_UVector, HVX_Vector, HVX_Vector, sizeof(ELEM_TYPE), hvx_vec_store_u, OP_MACRO); \
85} \
86static inline void OP_NAME##_uau(uint8_t * dst, const uint8_t * src0, const uint8_t * src1, uint32_t n) { \
87 assert((uintptr_t) src0 % 128 == 0); \
88 hvx_arith_loop_body(HVX_UVector, HVX_Vector, HVX_UVector, sizeof(ELEM_TYPE), hvx_vec_store_u, OP_MACRO); \
89} \
90static inline void OP_NAME##_uua(uint8_t * dst, const uint8_t * src0, const uint8_t * src1, uint32_t n) { \
91 assert((uintptr_t) src1 % 128 == 0); \
92 hvx_arith_loop_body(HVX_UVector, HVX_UVector, HVX_Vector, sizeof(ELEM_TYPE), hvx_vec_store_u, OP_MACRO); \
93} \
94static inline void OP_NAME##_uuu(uint8_t * dst, const uint8_t * src0, const uint8_t * src1, uint32_t n) { \
95 hvx_arith_loop_body(HVX_UVector, HVX_UVector, HVX_UVector, sizeof(ELEM_TYPE), hvx_vec_store_u, OP_MACRO); \
96} \
97
98DEFINE_HVX_BINARY_OP_VARIANTS(hvx_add_f32, HVX_OP_ADD_F32, float)
99DEFINE_HVX_BINARY_OP_VARIANTS(hvx_sub_f32, HVX_OP_SUB_F32, float)
100DEFINE_HVX_BINARY_OP_VARIANTS(hvx_mul_f32, HVX_OP_MUL_F32, float)
101
102DEFINE_HVX_BINARY_OP_VARIANTS(hvx_add_f16, HVX_OP_ADD_F16, _Float16)
103DEFINE_HVX_BINARY_OP_VARIANTS(hvx_sub_f16, HVX_OP_SUB_F16, _Float16)
104DEFINE_HVX_BINARY_OP_VARIANTS(hvx_mul_f16, HVX_OP_MUL_F16, _Float16)
105
106// Dispatcher logic
107#define HVX_BINARY_DISPATCHER(OP_NAME) \
108static inline void OP_NAME(uint8_t * restrict dst, const uint8_t * restrict src0, const uint8_t * restrict src1, const uint32_t num_elems) { \
109 if (hex_is_aligned((void *) dst, 128)) { \
110 if (hex_is_aligned((void *) src0, 128)) { \
111 if (hex_is_aligned((void *) src1, 128)) OP_NAME##_aaa(dst, src0, src1, num_elems); \
112 else OP_NAME##_aau(dst, src0, src1, num_elems); \
113 } else { \
114 if (hex_is_aligned((void *) src1, 128)) OP_NAME##_aua(dst, src0, src1, num_elems); \
115 else OP_NAME##_auu(dst, src0, src1, num_elems); \
116 } \
117 } else { \
118 if (hex_is_aligned((void *) src0, 128)) { \
119 if (hex_is_aligned((void *) src1, 128)) OP_NAME##_uaa(dst, src0, src1, num_elems); \
120 else OP_NAME##_uau(dst, src0, src1, num_elems); \
121 } else { \
122 if (hex_is_aligned((void *) src1, 128)) OP_NAME##_uua(dst, src0, src1, num_elems); \
123 else OP_NAME##_uuu(dst, src0, src1, num_elems); \
124 } \
125 } \
126}
127
128HVX_BINARY_DISPATCHER(hvx_add_f32)
129HVX_BINARY_DISPATCHER(hvx_sub_f32)
130HVX_BINARY_DISPATCHER(hvx_mul_f32)
131
132HVX_BINARY_DISPATCHER(hvx_add_f16)
133HVX_BINARY_DISPATCHER(hvx_sub_f16)
134HVX_BINARY_DISPATCHER(hvx_mul_f16)
135
136// Mul-Mul Optimized
137static inline void hvx_mul_mul_f32_aa(uint8_t * restrict dst, const uint8_t * restrict src0, const uint8_t * restrict src1, const uint8_t * restrict src2, const uint32_t num_elems) {
138 assert((unsigned long) dst % 128 == 0);
139 assert((unsigned long) src0 % 128 == 0);
140 assert((unsigned long) src1 % 128 == 0);
141 assert((unsigned long) src2 % 128 == 0);
142
143 HVX_Vector * restrict vdst = (HVX_Vector *) dst;
144 HVX_Vector * restrict vsrc0 = (HVX_Vector *) src0;
145 HVX_Vector * restrict vsrc1 = (HVX_Vector *) src1;
146 HVX_Vector * restrict vsrc2 = (HVX_Vector *) src2;
147
148 const uint32_t elem_size = sizeof(float);
149 const uint32_t epv = 128 / elem_size;
150 const uint32_t nvec = num_elems / epv;
151 const uint32_t nloe = num_elems % epv;
152
153 uint32_t i = 0;
154
155 _Pragma("unroll(4)")
156 for (; i < nvec; i++) {
157 HVX_Vector v1 = HVX_OP_MUL_F32(vsrc0[i], vsrc1[i]);
158 vdst[i] = HVX_OP_MUL(v1, vsrc2[i]);
159 }
160
161 if (nloe) {
162 HVX_Vector v1 = HVX_OP_MUL_F32(vsrc0[i], vsrc1[i]);
163 HVX_Vector v2 = HVX_OP_MUL_F32(v1, vsrc2[i]);
164 hvx_vec_store_a((void *) &vdst[i], nloe * elem_size, v2);
165 }
166}
167
168static inline void hvx_mul_mul_f16_aa(uint8_t * restrict dst, const uint8_t * restrict src0, const uint8_t * restrict src1, const uint8_t * restrict src2, const uint32_t num_elems) {
169 assert((unsigned long) dst % 128 == 0);
170 assert((unsigned long) src0 % 128 == 0);
171 assert((unsigned long) src1 % 128 == 0);
172 assert((unsigned long) src2 % 128 == 0);
173
174 HVX_Vector * restrict vdst = (HVX_Vector *) dst;
175 HVX_Vector * restrict vsrc0 = (HVX_Vector *) src0;
176 HVX_Vector * restrict vsrc1 = (HVX_Vector *) src1;
177 HVX_Vector * restrict vsrc2 = (HVX_Vector *) src2;
178
179 const uint32_t elem_size = sizeof(_Float16);
180 const uint32_t epv = 128 / elem_size;
181 const uint32_t nvec = num_elems / epv;
182 const uint32_t nloe = num_elems % epv;
183
184 uint32_t i = 0;
185
186 _Pragma("unroll(4)")
187 for (; i < nvec; i++) {
188 HVX_Vector v1 = HVX_OP_MUL_F16(vsrc0[i], vsrc1[i]);
189 vdst[i] = HVX_OP_MUL_F16(v1, vsrc2[i]);
190 }
191
192 if (nloe) {
193 HVX_Vector v1 = HVX_OP_MUL_F16(vsrc0[i], vsrc1[i]);
194 HVX_Vector v2 = HVX_OP_MUL_F16(v1, vsrc2[i]);
195 hvx_vec_store_a((void *) &vdst[i], nloe * elem_size, v2);
196 }
197}
198
199// Scalar Operations
200
201#define hvx_scalar_loop_body(dst_type, src_type, elem_size, vec_store, scalar_op_macro) \
202 do { \
203 dst_type * restrict vdst = (dst_type *) dst; \
204 src_type * restrict vsrc = (src_type *) src; \
205 \
206 const uint32_t epv = 128 / (elem_size); \
207 const uint32_t nvec = n / epv; \
208 const uint32_t nloe = n % epv; \
209 \
210 uint32_t i = 0; \
211 \
212 _Pragma("unroll(4)") \
213 for (; i < nvec; i++) { \
214 HVX_Vector v = vsrc[i]; \
215 vdst[i] = scalar_op_macro(v); \
216 } \
217 if (nloe) { \
218 HVX_Vector v = vsrc[i]; \
219 v = scalar_op_macro(v); \
220 vec_store((void *) &vdst[i], nloe * (elem_size), v); \
221 } \
222 } while(0)
223
224#define HVX_OP_ADD_SCALAR_F32(v) \
225 ({ \
226 const HVX_VectorPred pred_inf = Q6_Q_vcmp_eq_VwVw(inf, v); \
227 HVX_Vector out = HVX_OP_ADD_F32(v, val_vec); \
228 Q6_V_vmux_QVV(pred_inf, inf, out); \
229 })
230
231#define HVX_OP_MUL_SCALAR_F32(v) HVX_OP_MUL_F32(v, val_vec)
232#define HVX_OP_SUB_SCALAR_F32(v) HVX_OP_SUB_F32(v, val_vec)
233
234#define HVX_OP_ADD_SCALAR_F16(v) \
235 ({ \
236 const HVX_VectorPred pred_inf = Q6_Q_vcmp_eq_VhVh(inf, v); \
237 HVX_Vector out = HVX_OP_ADD_F16(v, val_vec); \
238 Q6_V_vmux_QVV(pred_inf, inf, out); \
239 })
240
241#define HVX_OP_MUL_SCALAR_F16(v) HVX_OP_MUL_F16(v, val_vec)
242#define HVX_OP_SUB_SCALAR_F16(v) HVX_OP_SUB_F16(v, val_vec)
243
244// Scalar Variants
245
246// Generic macro to define alignment permutations for an op
247#define DEFINE_HVX_BINARY_SCALAR_OP_VARIANTS(OP_NAME, OP_MACRO, SPLAT_MACRO, ELEM_TYPE) \
248static inline void OP_NAME##_aa(uint8_t * restrict dst, const uint8_t * restrict src, const ELEM_TYPE val, uint32_t n) { \
249 const HVX_Vector val_vec = SPLAT_MACRO(val); \
250 const HVX_Vector inf = SPLAT_MACRO((ELEM_TYPE)INFINITY); UNUSED(inf); \
251 assert((uintptr_t) dst % 128 == 0); \
252 assert((uintptr_t) src % 128 == 0); \
253 hvx_scalar_loop_body(HVX_Vector, HVX_Vector, sizeof(ELEM_TYPE), hvx_vec_store_a, OP_MACRO); \
254} \
255static inline void OP_NAME##_au(uint8_t * restrict dst, const uint8_t * restrict src, const ELEM_TYPE val, uint32_t n) { \
256 const HVX_Vector val_vec = SPLAT_MACRO(val); \
257 const HVX_Vector inf = SPLAT_MACRO((ELEM_TYPE)INFINITY); UNUSED(inf); \
258 assert((uintptr_t) dst % 128 == 0); \
259 hvx_scalar_loop_body(HVX_Vector, HVX_UVector, sizeof(ELEM_TYPE), hvx_vec_store_a, OP_MACRO); \
260} \
261static inline void OP_NAME##_ua(uint8_t * restrict dst, const uint8_t * restrict src, const ELEM_TYPE val, uint32_t n) { \
262 const HVX_Vector val_vec = SPLAT_MACRO(val); \
263 const HVX_Vector inf = SPLAT_MACRO((ELEM_TYPE)INFINITY); UNUSED(inf); \
264 assert((uintptr_t) src % 128 == 0); \
265 hvx_scalar_loop_body(HVX_UVector, HVX_Vector, sizeof(ELEM_TYPE), hvx_vec_store_u, OP_MACRO); \
266} \
267static inline void OP_NAME##_uu(uint8_t * restrict dst, const uint8_t * restrict src, const ELEM_TYPE val, uint32_t n) { \
268 const HVX_Vector val_vec = SPLAT_MACRO(val); \
269 const HVX_Vector inf = SPLAT_MACRO((ELEM_TYPE)INFINITY); UNUSED(inf); \
270 hvx_scalar_loop_body(HVX_UVector, HVX_UVector, sizeof(ELEM_TYPE), hvx_vec_store_u, OP_MACRO); \
271} \
272
273DEFINE_HVX_BINARY_SCALAR_OP_VARIANTS(hvx_add_scalar_f32, HVX_OP_ADD_SCALAR_F32, hvx_vec_splat_f32, float)
274DEFINE_HVX_BINARY_SCALAR_OP_VARIANTS(hvx_sub_scalar_f32, HVX_OP_SUB_SCALAR_F32, hvx_vec_splat_f32, float)
275DEFINE_HVX_BINARY_SCALAR_OP_VARIANTS(hvx_mul_scalar_f32, HVX_OP_MUL_SCALAR_F32, hvx_vec_splat_f32, float)
276
277DEFINE_HVX_BINARY_SCALAR_OP_VARIANTS(hvx_add_scalar_f16, HVX_OP_ADD_SCALAR_F16, hvx_vec_splat_f16, _Float16)
278DEFINE_HVX_BINARY_SCALAR_OP_VARIANTS(hvx_sub_scalar_f16, HVX_OP_SUB_SCALAR_F16, hvx_vec_splat_f16, _Float16)
279DEFINE_HVX_BINARY_SCALAR_OP_VARIANTS(hvx_mul_scalar_f16, HVX_OP_MUL_SCALAR_F16, hvx_vec_splat_f16, _Float16)
280
281// Dispatcher logic
282#define HVX_BINARY_SCALAR_DISPATCHER(OP_NAME, ELEM_TYPE) \
283static inline void OP_NAME(uint8_t * restrict dst, const uint8_t * restrict src, const ELEM_TYPE val, const uint32_t num_elems) { \
284 if (hex_is_aligned((void *) dst, 128) && hex_is_aligned((void *) src, 128)) { \
285 OP_NAME##_aa(dst, src, val, num_elems); \
286 } else if (hex_is_aligned((void *) dst, 128)) { \
287 OP_NAME##_au(dst, src, val, num_elems); \
288 } else if (hex_is_aligned((void *) src, 128)) { \
289 OP_NAME##_ua(dst, src, val, num_elems); \
290 } else { \
291 OP_NAME##_uu(dst, src, val, num_elems); \
292 } \
293}
294
295HVX_BINARY_SCALAR_DISPATCHER(hvx_add_scalar_f32, float)
296HVX_BINARY_SCALAR_DISPATCHER(hvx_sub_scalar_f32, float)
297HVX_BINARY_SCALAR_DISPATCHER(hvx_mul_scalar_f32, float)
298
299HVX_BINARY_SCALAR_DISPATCHER(hvx_add_scalar_f16, _Float16)
300HVX_BINARY_SCALAR_DISPATCHER(hvx_sub_scalar_f16, _Float16)
301HVX_BINARY_SCALAR_DISPATCHER(hvx_mul_scalar_f16, _Float16)
302
303// MIN Scalar variants
304
305#define HVX_OP_MIN_SCALAR(v) Q6_Vsf_vmin_VsfVsf(val_vec, v)
306
307static inline void hvx_min_scalar_f32_aa(uint8_t * restrict dst, const uint8_t * restrict src, const float val, uint32_t n) {
308 const HVX_Vector val_vec = hvx_vec_splat_f32(val);
309 assert((unsigned long) dst % 128 == 0);
310 assert((unsigned long) src % 128 == 0);
311 hvx_scalar_loop_body(HVX_Vector, HVX_Vector, sizeof(float), hvx_vec_store_a, HVX_OP_MIN_SCALAR);
312}
313
314static inline void hvx_min_scalar_f32_au(uint8_t * restrict dst, const uint8_t * restrict src, const float val, uint32_t n) {
315 const HVX_Vector val_vec = hvx_vec_splat_f32(val);
316 assert((unsigned long) dst % 128 == 0);
317 hvx_scalar_loop_body(HVX_Vector, HVX_UVector, sizeof(float), hvx_vec_store_a, HVX_OP_MIN_SCALAR);
318}
319
320static inline void hvx_min_scalar_f32_ua(uint8_t * restrict dst, const uint8_t * restrict src, const float val, uint32_t n) {
321 const HVX_Vector val_vec = hvx_vec_splat_f32(val);
322 assert((unsigned long) src % 128 == 0);
323 hvx_scalar_loop_body(HVX_UVector, HVX_Vector, sizeof(float), hvx_vec_store_u, HVX_OP_MIN_SCALAR);
324}
325
326static inline void hvx_min_scalar_f32_uu(uint8_t * restrict dst, const uint8_t * restrict src, const float val, uint32_t n) {
327 const HVX_Vector val_vec = hvx_vec_splat_f32(val);
328 hvx_scalar_loop_body(HVX_UVector, HVX_UVector, sizeof(float), hvx_vec_store_u, HVX_OP_MIN_SCALAR);
329}
330
331static inline void hvx_min_scalar_f32(uint8_t * restrict dst, const uint8_t * restrict src, const float val, const int num_elems) {
332 if (hex_is_aligned((void *) dst, 128) && hex_is_aligned((void *) src, 128)) {
333 hvx_min_scalar_f32_aa(dst, src, val, num_elems);
334 } else if (hex_is_aligned((void *) dst, 128)) {
335 hvx_min_scalar_f32_au(dst, src, val, num_elems);
336 } else if (hex_is_aligned((void *) src, 128)) {
337 hvx_min_scalar_f32_ua(dst, src, val, num_elems);
338 } else {
339 hvx_min_scalar_f32_uu(dst, src, val, num_elems);
340 }
341}
342
343// MAX Scalar variants
344
345#define HVX_OP_MAX_SCALAR(v) Q6_Vsf_vmax_VsfVsf(val_vec, v)
346
347static inline void hvx_max_scalar_f32_aa(uint8_t * restrict dst, const uint8_t * restrict src, const float val, uint32_t n) {
348 const HVX_Vector val_vec = hvx_vec_splat_f32(val);
349 assert((unsigned long) dst % 128 == 0);
350 assert((unsigned long) src % 128 == 0);
351 hvx_scalar_loop_body(HVX_Vector, HVX_Vector, sizeof(float), hvx_vec_store_a, HVX_OP_MAX_SCALAR);
352}
353
354static inline void hvx_max_scalar_f32_au(uint8_t * restrict dst, const uint8_t * restrict src, const float val, uint32_t n) {
355 const HVX_Vector val_vec = hvx_vec_splat_f32(val);
356 assert((unsigned long) dst % 128 == 0);
357 hvx_scalar_loop_body(HVX_Vector, HVX_UVector, sizeof(float), hvx_vec_store_a, HVX_OP_MAX_SCALAR);
358}
359
360static inline void hvx_max_scalar_f32_ua(uint8_t * restrict dst, const uint8_t * restrict src, const float val, uint32_t n) {
361 const HVX_Vector val_vec = hvx_vec_splat_f32(val);
362 assert((unsigned long) src % 128 == 0);
363 hvx_scalar_loop_body(HVX_UVector, HVX_Vector, sizeof(float), hvx_vec_store_u, HVX_OP_MAX_SCALAR);
364}
365
366static inline void hvx_max_scalar_f32_uu(uint8_t * restrict dst, const uint8_t * restrict src, const float val, uint32_t n) {
367 const HVX_Vector val_vec = hvx_vec_splat_f32(val);
368 hvx_scalar_loop_body(HVX_UVector, HVX_UVector, sizeof(float), hvx_vec_store_u, HVX_OP_MAX_SCALAR);
369}
370
371static inline void hvx_max_scalar_f32(uint8_t * restrict dst, const uint8_t * restrict src, const float val, const int num_elems) {
372 if (hex_is_aligned((void *) dst, 128) && hex_is_aligned((void *) src, 128)) {
373 hvx_max_scalar_f32_aa(dst, src, val, num_elems);
374 } else if (hex_is_aligned((void *) dst, 128)) {
375 hvx_max_scalar_f32_au(dst, src, val, num_elems);
376 } else if (hex_is_aligned((void *) src, 128)) {
377 hvx_max_scalar_f32_ua(dst, src, val, num_elems);
378 } else {
379 hvx_max_scalar_f32_uu(dst, src, val, num_elems);
380 }
381}
382
383#define HVX_OP_MIN_SCALAR_F16(v) Q6_Vhf_vmin_VhfVhf(val_vec, v)
384
385static inline void hvx_min_scalar_f16_aa(uint8_t * restrict dst, const uint8_t * restrict src, const _Float16 val, uint32_t n) {
386 const HVX_Vector val_vec = hvx_vec_splat_f16(val);
387 assert((unsigned long) dst % 128 == 0);
388 assert((unsigned long) src % 128 == 0);
389 hvx_scalar_loop_body(HVX_Vector, HVX_Vector, sizeof(_Float16), hvx_vec_store_a, HVX_OP_MIN_SCALAR_F16);
390}
391
392static inline void hvx_min_scalar_f16_au(uint8_t * restrict dst, const uint8_t * restrict src, const _Float16 val, uint32_t n) {
393 const HVX_Vector val_vec = hvx_vec_splat_f16(val);
394 assert((unsigned long) dst % 128 == 0);
395 hvx_scalar_loop_body(HVX_Vector, HVX_UVector, sizeof(_Float16), hvx_vec_store_a, HVX_OP_MIN_SCALAR_F16);
396}
397
398static inline void hvx_min_scalar_f16_ua(uint8_t * restrict dst, const uint8_t * restrict src, const _Float16 val, uint32_t n) {
399 const HVX_Vector val_vec = hvx_vec_splat_f16(val);
400 assert((unsigned long) src % 128 == 0);
401 hvx_scalar_loop_body(HVX_UVector, HVX_Vector, sizeof(_Float16), hvx_vec_store_u, HVX_OP_MIN_SCALAR_F16);
402}
403
404static inline void hvx_min_scalar_f16_uu(uint8_t * restrict dst, const uint8_t * restrict src, const _Float16 val, uint32_t n) {
405 const HVX_Vector val_vec = hvx_vec_splat_f16(val);
406 hvx_scalar_loop_body(HVX_UVector, HVX_UVector, sizeof(_Float16), hvx_vec_store_u, HVX_OP_MIN_SCALAR_F16);
407}
408
409static inline void hvx_min_scalar_f16(uint8_t * restrict dst, const uint8_t * restrict src, const _Float16 val, const int num_elems) {
410 if (hex_is_aligned((void *) dst, 128) && hex_is_aligned((void *) src, 128)) {
411 hvx_min_scalar_f16_aa(dst, src, val, num_elems);
412 } else if (hex_is_aligned((void *) dst, 128)) {
413 hvx_min_scalar_f16_au(dst, src, val, num_elems);
414 } else if (hex_is_aligned((void *) src, 128)) {
415 hvx_min_scalar_f16_ua(dst, src, val, num_elems);
416 } else {
417 hvx_min_scalar_f16_uu(dst, src, val, num_elems);
418 }
419}
420
421// CLAMP Scalar variants
422
423#define HVX_OP_CLAMP_SCALAR(v) \
424 ({ \
425 HVX_VectorPred pred_cap_right = Q6_Q_vcmp_gt_VsfVsf(v, max_vec); \
426 HVX_VectorPred pred_cap_left = Q6_Q_vcmp_gt_VsfVsf(min_vec, v); \
427 HVX_Vector tmp = Q6_V_vmux_QVV(pred_cap_right, max_vec, v); \
428 Q6_V_vmux_QVV(pred_cap_left, min_vec, tmp); \
429 })
430
431static inline void hvx_clamp_scalar_f32_aa(uint8_t * restrict dst, const uint8_t * restrict src, const float min, const float max, uint32_t n) {
432 const HVX_Vector min_vec = hvx_vec_splat_f32(min);
433 const HVX_Vector max_vec = hvx_vec_splat_f32(max);
434 assert((unsigned long) dst % 128 == 0);
435 assert((unsigned long) src % 128 == 0);
436 hvx_scalar_loop_body(HVX_Vector, HVX_Vector, sizeof(float), hvx_vec_store_a, HVX_OP_CLAMP_SCALAR);
437}
438
439static inline void hvx_clamp_scalar_f32_au(uint8_t * restrict dst, const uint8_t * restrict src, const float min, const float max, uint32_t n) {
440 const HVX_Vector min_vec = hvx_vec_splat_f32(min);
441 const HVX_Vector max_vec = hvx_vec_splat_f32(max);
442 assert((unsigned long) dst % 128 == 0);
443 hvx_scalar_loop_body(HVX_Vector, HVX_UVector, sizeof(float), hvx_vec_store_a, HVX_OP_CLAMP_SCALAR);
444}
445
446static inline void hvx_clamp_scalar_f32_ua(uint8_t * restrict dst, const uint8_t * restrict src, const float min, const float max, uint32_t n) {
447 const HVX_Vector min_vec = hvx_vec_splat_f32(min);
448 const HVX_Vector max_vec = hvx_vec_splat_f32(max);
449 assert((unsigned long) src % 128 == 0);
450 hvx_scalar_loop_body(HVX_UVector, HVX_Vector, sizeof(float), hvx_vec_store_u, HVX_OP_CLAMP_SCALAR);
451}
452
453static inline void hvx_clamp_scalar_f32_uu(uint8_t * restrict dst, const uint8_t * restrict src, const float min, const float max, uint32_t n) {
454 const HVX_Vector min_vec = hvx_vec_splat_f32(min);
455 const HVX_Vector max_vec = hvx_vec_splat_f32(max);
456 hvx_scalar_loop_body(HVX_UVector, HVX_UVector, sizeof(float), hvx_vec_store_u, HVX_OP_CLAMP_SCALAR);
457}
458
459static inline void hvx_clamp_scalar_f32(uint8_t * restrict dst, const uint8_t * restrict src, const float min, const float max, const int num_elems) {
460 if (hex_is_aligned((void *) dst, 128) && hex_is_aligned((void *) src, 128)) {
461 hvx_clamp_scalar_f32_aa(dst, src, min, max, num_elems);
462 } else if (hex_is_aligned((void *) dst, 128)) {
463 hvx_clamp_scalar_f32_au(dst, src, min, max, num_elems);
464 } else if (hex_is_aligned((void *) src, 128)) {
465 hvx_clamp_scalar_f32_ua(dst, src, min, max, num_elems);
466 } else {
467 hvx_clamp_scalar_f32_uu(dst, src, min, max, num_elems);
468 }
469}
470
471#define HVX_OP_CLAMP_SCALAR_F16(v) \
472 ({ \
473 HVX_VectorPred pred_cap_right = Q6_Q_vcmp_gt_VhfVhf(v, max_vec); \
474 HVX_VectorPred pred_cap_left = Q6_Q_vcmp_gt_VhfVhf(min_vec, v); \
475 HVX_Vector tmp = Q6_V_vmux_QVV(pred_cap_right, max_vec, v); \
476 Q6_V_vmux_QVV(pred_cap_left, min_vec, tmp); \
477 })
478
479static inline void hvx_clamp_scalar_f16_aa(uint8_t * restrict dst, const uint8_t * restrict src, const _Float16 min, const _Float16 max, uint32_t n) {
480 const HVX_Vector min_vec = hvx_vec_splat_f16(min);
481 const HVX_Vector max_vec = hvx_vec_splat_f16(max);
482 assert((unsigned long) dst % 128 == 0);
483 assert((unsigned long) src % 128 == 0);
484 hvx_scalar_loop_body(HVX_Vector, HVX_Vector, sizeof(_Float16), hvx_vec_store_a, HVX_OP_CLAMP_SCALAR_F16);
485}
486
487static inline void hvx_clamp_scalar_f16_au(uint8_t * restrict dst, const uint8_t * restrict src, const _Float16 min, const _Float16 max, uint32_t n) {
488 const HVX_Vector min_vec = hvx_vec_splat_f16(min);
489 const HVX_Vector max_vec = hvx_vec_splat_f16(max);
490 assert((unsigned long) dst % 128 == 0);
491 hvx_scalar_loop_body(HVX_Vector, HVX_UVector, sizeof(_Float16), hvx_vec_store_a, HVX_OP_CLAMP_SCALAR_F16);
492}
493
494static inline void hvx_clamp_scalar_f16_ua(uint8_t * restrict dst, const uint8_t * restrict src, const _Float16 min, const _Float16 max, uint32_t n) {
495 const HVX_Vector min_vec = hvx_vec_splat_f16(min);
496 const HVX_Vector max_vec = hvx_vec_splat_f16(max);
497 assert((unsigned long) src % 128 == 0);
498 hvx_scalar_loop_body(HVX_UVector, HVX_Vector, sizeof(_Float16), hvx_vec_store_u, HVX_OP_CLAMP_SCALAR_F16);
499}
500
501static inline void hvx_clamp_scalar_f16_uu(uint8_t * restrict dst, const uint8_t * restrict src, const _Float16 min, const _Float16 max, uint32_t n) {
502 const HVX_Vector min_vec = hvx_vec_splat_f16(min);
503 const HVX_Vector max_vec = hvx_vec_splat_f16(max);
504 hvx_scalar_loop_body(HVX_UVector, HVX_UVector, sizeof(_Float16), hvx_vec_store_u, HVX_OP_CLAMP_SCALAR_F16);
505}
506
507static inline void hvx_clamp_scalar_f16(uint8_t * restrict dst, const uint8_t * restrict src, const _Float16 min, const _Float16 max, const int num_elems) {
508 if (hex_is_aligned((void *) dst, 128) && hex_is_aligned((void *) src, 128)) {
509 hvx_clamp_scalar_f16_aa(dst, src, min, max, num_elems);
510 } else if (hex_is_aligned((void *) dst, 128)) {
511 hvx_clamp_scalar_f16_au(dst, src, min, max, num_elems);
512 } else if (hex_is_aligned((void *) src, 128)) {
513 hvx_clamp_scalar_f16_ua(dst, src, min, max, num_elems);
514 } else {
515 hvx_clamp_scalar_f16_uu(dst, src, min, max, num_elems);
516 }
517}
518
519#define HVX_OP_LEAKY_RELU_SCALAR(v) \
520 ({ \
521 HVX_VectorPred pred_neg = Q6_Q_vcmp_gt_VsfVsf(zero_vec, v); \
522 HVX_Vector scaled = HVX_OP_MUL_F32(v, ns_vec); \
523 Q6_V_vmux_QVV(pred_neg, scaled, v); \
524 })
525
526static inline void hvx_leaky_relu_scalar_f32_aa(uint8_t * restrict dst, const uint8_t * restrict src, const float ns, uint32_t n) {
527 const HVX_Vector zero_vec = hvx_vec_splat_f32(0.0f);
528 const HVX_Vector ns_vec = hvx_vec_splat_f32(ns);
529 assert((unsigned long) dst % 128 == 0);
530 assert((unsigned long) src % 128 == 0);
531 hvx_scalar_loop_body(HVX_Vector, HVX_Vector, sizeof(float), hvx_vec_store_a, HVX_OP_LEAKY_RELU_SCALAR);
532}
533
534static inline void hvx_leaky_relu_scalar_f32_au(uint8_t * restrict dst, const uint8_t * restrict src, const float ns, uint32_t n) {
535 const HVX_Vector zero_vec = hvx_vec_splat_f32(0.0f);
536 const HVX_Vector ns_vec = hvx_vec_splat_f32(ns);
537 assert((unsigned long) dst % 128 == 0);
538 hvx_scalar_loop_body(HVX_Vector, HVX_UVector, sizeof(float), hvx_vec_store_a, HVX_OP_LEAKY_RELU_SCALAR);
539}
540
541static inline void hvx_leaky_relu_scalar_f32_ua(uint8_t * restrict dst, const uint8_t * restrict src, const float ns, uint32_t n) {
542 const HVX_Vector zero_vec = hvx_vec_splat_f32(0.0f);
543 const HVX_Vector ns_vec = hvx_vec_splat_f32(ns);
544 assert((unsigned long) src % 128 == 0);
545 hvx_scalar_loop_body(HVX_UVector, HVX_Vector, sizeof(float), hvx_vec_store_u, HVX_OP_LEAKY_RELU_SCALAR);
546}
547
548static inline void hvx_leaky_relu_scalar_f32_uu(uint8_t * restrict dst, const uint8_t * restrict src, const float ns, uint32_t n) {
549 const HVX_Vector zero_vec = hvx_vec_splat_f32(0.0f);
550 const HVX_Vector ns_vec = hvx_vec_splat_f32(ns);
551 hvx_scalar_loop_body(HVX_UVector, HVX_UVector, sizeof(float), hvx_vec_store_u, HVX_OP_LEAKY_RELU_SCALAR);
552}
553
554static inline void hvx_leaky_relu_scalar_f32(uint8_t * restrict dst, const uint8_t * restrict src, const float ns, const int num_elems) {
555 if (hex_is_aligned((void *) dst, 128) && hex_is_aligned((void *) src, 128)) {
556 hvx_leaky_relu_scalar_f32_aa(dst, src, ns, num_elems);
557 } else if (hex_is_aligned((void *) dst, 128)) {
558 hvx_leaky_relu_scalar_f32_au(dst, src, ns, num_elems);
559 } else if (hex_is_aligned((void *) src, 128)) {
560 hvx_leaky_relu_scalar_f32_ua(dst, src, ns, num_elems);
561 } else {
562 hvx_leaky_relu_scalar_f32_uu(dst, src, ns, num_elems);
563 }
564}
565
566//
567// Abs
568//
569
570static inline void hvx_abs_f32_aa(uint8_t * restrict dst, const uint8_t * restrict src, uint32_t n) {
571 assert((unsigned long) dst % 128 == 0);
572 assert((unsigned long) src % 128 == 0);
573
574 HVX_Vector * restrict vdst = (HVX_Vector *) dst;
575 HVX_Vector * restrict vsrc = (HVX_Vector *) src;
576
577 const uint32_t elem_size = sizeof(float);
578 const uint32_t epv = 128 / elem_size;
579 const uint32_t nvec = n / epv;
580 const uint32_t nloe = n % epv;
581
582 uint32_t i = 0;
583
584 _Pragma("unroll(4)")
585 for (; i < nvec; i++) {
586 vdst[i] = hvx_vec_abs_f32(vsrc[i]);
587 }
588 if (nloe) {
589 HVX_Vector v = hvx_vec_abs_f32(vsrc[i]);
590 hvx_vec_store_a((void *) &vdst[i], nloe * elem_size, v);
591 }
592}
593
594#define hvx_abs_f16_loop_body(dst_type, src_type, vec_store) \
595 do { \
596 dst_type * restrict vdst = (dst_type *) dst; \
597 src_type * restrict vsrc = (src_type *) src; \
598 \
599 const uint32_t elem_size = sizeof(_Float16); \
600 const uint32_t epv = 128 / elem_size; \
601 const uint32_t nvec = n / epv; \
602 const uint32_t nloe = n % epv; \
603 \
604 uint32_t i = 0; \
605 \
606 _Pragma("unroll(4)") \
607 for (; i < nvec; i++) { \
608 vdst[i] = hvx_vec_abs_f16(vsrc[i]); \
609 } \
610 if (nloe) { \
611 HVX_Vector v = hvx_vec_abs_f16(vsrc[i]); \
612 vec_store((void *) &vdst[i], nloe * elem_size, v); \
613 } \
614 } while(0)
615
616static inline void hvx_abs_f16_aa(uint8_t * restrict dst, const uint8_t * restrict src, uint32_t n) {
617 assert((unsigned long) dst % 128 == 0);
618 assert((unsigned long) src % 128 == 0);
619 hvx_abs_f16_loop_body(HVX_Vector, HVX_Vector, hvx_vec_store_a);
620}
621
622static inline void hvx_abs_f16_au(uint8_t * restrict dst, const uint8_t * restrict src, uint32_t n) {
623 assert((unsigned long) dst % 128 == 0);
624 hvx_abs_f16_loop_body(HVX_Vector, HVX_UVector, hvx_vec_store_a);
625}
626
627static inline void hvx_abs_f16_ua(uint8_t * restrict dst, const uint8_t * restrict src, uint32_t n) {
628 assert((unsigned long) src % 128 == 0);
629 hvx_abs_f16_loop_body(HVX_UVector, HVX_Vector, hvx_vec_store_u);
630}
631
632static inline void hvx_abs_f16_uu(uint8_t * restrict dst, const uint8_t * restrict src, uint32_t n) {
633 hvx_abs_f16_loop_body(HVX_UVector, HVX_UVector, hvx_vec_store_u);
634}
635
636static inline void hvx_abs_f16(uint8_t * restrict dst, const uint8_t * restrict src, const uint32_t num_elems) {
637 if (hex_is_aligned((void *) dst, 128)) {
638 if (hex_is_aligned((void *) src, 128)) {
639 hvx_abs_f16_aa(dst, src, num_elems);
640 } else {
641 hvx_abs_f16_au(dst, src, num_elems);
642 }
643 } else {
644 if (hex_is_aligned((void *) src, 128)) {
645 hvx_abs_f16_ua(dst, src, num_elems);
646 } else {
647 hvx_abs_f16_uu(dst, src, num_elems);
648 }
649 }
650}
651
652//
653// Step
654//
655
656static inline void hvx_step_f32_aa(uint8_t * restrict dst, const uint8_t * restrict src, uint32_t n) {
657 assert((unsigned long) dst % 128 == 0);
658 assert((unsigned long) src % 128 == 0);
659
660 HVX_Vector * restrict vdst = (HVX_Vector *) dst;
661 HVX_Vector * restrict vsrc = (HVX_Vector *) src;
662
663 const uint32_t elem_size = sizeof(float);
664 const uint32_t epv = 128 / elem_size;
665 const uint32_t nvec = n / epv;
666 const uint32_t nloe = n % epv;
667
668 uint32_t i = 0;
669
670 _Pragma("unroll(4)")
671 for (; i < nvec; i++) {
672 vdst[i] = hvx_vec_step_f32(vsrc[i]);
673 }
674 if (nloe) {
675 HVX_Vector v = hvx_vec_step_f32(vsrc[i]);
676 hvx_vec_store_a((void *) &vdst[i], nloe * elem_size, v);
677 }
678}
679
680static inline void hvx_sgn_f32_aa(uint8_t * restrict dst, const uint8_t * restrict src, uint32_t n) {
681 assert((unsigned long) dst % 128 == 0);
682 assert((unsigned long) src % 128 == 0);
683
684 HVX_Vector * restrict vdst = (HVX_Vector *) dst;
685 HVX_Vector * restrict vsrc = (HVX_Vector *) src;
686
687 const uint32_t elem_size = sizeof(float);
688 const uint32_t epv = 128 / elem_size;
689 const uint32_t nvec = n / epv;
690 const uint32_t nloe = n % epv;
691
692 uint32_t i = 0;
693
694 _Pragma("unroll(4)")
695 for (; i < nvec; i++) {
696 vdst[i] = hvx_vec_sgn_f32(vsrc[i]);
697 }
698 if (nloe) {
699 HVX_Vector v = hvx_vec_sgn_f32(vsrc[i]);
700 hvx_vec_store_a((void *) &vdst[i], nloe * elem_size, v);
701 }
702}
703
704static inline void hvx_sgn_f16_aa(uint8_t * restrict dst, const uint8_t * restrict src, uint32_t n) {
705 assert((unsigned long) dst % 128 == 0);
706 assert((unsigned long) src % 128 == 0);
707
708 HVX_Vector * restrict vdst = (HVX_Vector *) dst;
709 HVX_Vector * restrict vsrc = (HVX_Vector *) src;
710
711 const uint32_t elem_size = sizeof(_Float16);
712 const uint32_t epv = 128 / elem_size;
713 const uint32_t nvec = n / epv;
714 const uint32_t nloe = n % epv;
715
716 uint32_t i = 0;
717
718 _Pragma("unroll(4)")
719 for (; i < nvec; i++) {
720 HVX_VectorPair p = hvx_vec_f16_to_f32(vsrc[i]);
721 vdst[i] = hvx_vec_f32_to_f16(hvx_vec_sgn_f32(Q6_V_lo_W(p)), hvx_vec_sgn_f32(Q6_V_hi_W(p)));
722 }
723 if (nloe) {
724 HVX_VectorPair p = hvx_vec_f16_to_f32(vsrc[i]);
725 HVX_Vector v = hvx_vec_f32_to_f16(hvx_vec_sgn_f32(Q6_V_lo_W(p)), hvx_vec_sgn_f32(Q6_V_hi_W(p)));
726 hvx_vec_store_a((void *) &vdst[i], nloe * elem_size, v);
727 }
728}
729
730static inline void hvx_step_f16_aa(uint8_t * restrict dst, const uint8_t * restrict src, uint32_t n) {
731 assert((unsigned long) dst % 128 == 0);
732 assert((unsigned long) src % 128 == 0);
733
734 HVX_Vector * restrict vdst = (HVX_Vector *) dst;
735 HVX_Vector * restrict vsrc = (HVX_Vector *) src;
736
737 const uint32_t elem_size = sizeof(_Float16);
738 const uint32_t epv = 128 / elem_size;
739 const uint32_t nvec = n / epv;
740 const uint32_t nloe = n % epv;
741
742 uint32_t i = 0;
743
744 _Pragma("unroll(4)")
745 for (; i < nvec; i++) {
746 vdst[i] = hvx_vec_step_f16(vsrc[i]);
747 }
748 if (nloe) {
749 HVX_Vector v = hvx_vec_step_f16(vsrc[i]);
750 hvx_vec_store_a((void *) &vdst[i], nloe * elem_size, v);
751 }
752}
753
754//
755// Ceil
756//
757
758static inline void hvx_ceil_f32_aa(uint8_t * restrict dst, const uint8_t * restrict src, uint32_t n) {
759 assert((unsigned long) dst % 128 == 0);
760 assert((unsigned long) src % 128 == 0);
761
762 HVX_Vector * restrict vdst = (HVX_Vector *) dst;
763 HVX_Vector * restrict vsrc = (HVX_Vector *) src;
764
765 const uint32_t elem_size = sizeof(float);
766 const uint32_t epv = 128 / elem_size;
767 const uint32_t nvec = n / epv;
768 const uint32_t nloe = n % epv;
769
770 uint32_t i = 0;
771
772 _Pragma("unroll(4)")
773 for (; i < nvec; i++) {
774 vdst[i] = hvx_vec_ceil_f32(vsrc[i]);
775 }
776 if (nloe) {
777 HVX_Vector v = hvx_vec_ceil_f32(vsrc[i]);
778 hvx_vec_store_a((void *) &vdst[i], nloe * elem_size, v);
779 }
780}
781
782static inline void hvx_ceil_f16_aa(uint8_t * restrict dst, const uint8_t * restrict src, uint32_t n) {
783 assert((unsigned long) dst % 128 == 0);
784 assert((unsigned long) src % 128 == 0);
785
786 HVX_Vector * restrict vdst = (HVX_Vector *) dst;
787 HVX_Vector * restrict vsrc = (HVX_Vector *) src;
788
789 const uint32_t elem_size = sizeof(_Float16);
790 const uint32_t epv = 128 / elem_size;
791 const uint32_t nvec = n / epv;
792 const uint32_t nloe = n % epv;
793
794 uint32_t i = 0;
795
796 _Pragma("unroll(4)")
797 for (; i < nvec; i++) {
798 HVX_VectorPair p = hvx_vec_f16_to_f32(vsrc[i]);
799 vdst[i] = hvx_vec_f32_to_f16(hvx_vec_ceil_f32(Q6_V_lo_W(p)), hvx_vec_ceil_f32(Q6_V_hi_W(p)));
800 }
801 if (nloe) {
802 HVX_VectorPair p = hvx_vec_f16_to_f32(vsrc[i]);
803 HVX_Vector v = hvx_vec_f32_to_f16(hvx_vec_ceil_f32(Q6_V_lo_W(p)), hvx_vec_ceil_f32(Q6_V_hi_W(p)));
804 hvx_vec_store_a((void *) &vdst[i], nloe * elem_size, v);
805 }
806}
807
808//
809// Square
810//
811
812#define hvx_sqr_f32_loop_body(dst_type, src_type, vec_store) \
813 do { \
814 dst_type * restrict vdst = (dst_type *) dst; \
815 src_type * restrict vsrc = (src_type *) src; \
816 \
817 const uint32_t elem_size = sizeof(float); \
818 const uint32_t epv = 128 / elem_size; \
819 const uint32_t nvec = n / epv; \
820 const uint32_t nloe = n % epv; \
821 \
822 uint32_t i = 0; \
823 \
824 _Pragma("unroll(4)") \
825 for (; i < nvec; i++) { \
826 vdst[i] = HVX_OP_MUL_F32(vsrc[i], vsrc[i]); \
827 } \
828 if (nloe) { \
829 HVX_Vector v = HVX_OP_MUL_F32(vsrc[i], vsrc[i]); \
830 vec_store((void *) &vdst[i], nloe * elem_size, v); \
831 } \
832 } while(0)
833
834static inline void hvx_sqr_f32_aa(uint8_t * restrict dst, const uint8_t * restrict src, uint32_t n) {
835 assert((unsigned long) dst % 128 == 0);
836 assert((unsigned long) src % 128 == 0);
837 hvx_sqr_f32_loop_body(HVX_Vector, HVX_Vector, hvx_vec_store_a);
838}
839
840static inline void hvx_sqr_f32_au(uint8_t * restrict dst, const uint8_t * restrict src, uint32_t n) {
841 assert((unsigned long) dst % 128 == 0);
842 hvx_sqr_f32_loop_body(HVX_Vector, HVX_UVector, hvx_vec_store_a);
843}
844
845static inline void hvx_sqr_f32_ua(uint8_t * restrict dst, const uint8_t * restrict src, uint32_t n) {
846 assert((unsigned long) src % 128 == 0);
847 hvx_sqr_f32_loop_body(HVX_UVector, HVX_Vector, hvx_vec_store_u);
848}
849
850static inline void hvx_sqr_f32_uu(uint8_t * restrict dst, const uint8_t * restrict src, uint32_t n) {
851 hvx_sqr_f32_loop_body(HVX_UVector, HVX_UVector, hvx_vec_store_u);
852}
853
854static inline void hvx_sqr_f32(uint8_t * restrict dst, const uint8_t * restrict src, const uint32_t num_elems) {
855 if (hex_is_aligned((void *) dst, 128)) {
856 if (hex_is_aligned((void *) src, 128)) {
857 hvx_sqr_f32_aa(dst, src, num_elems);
858 } else {
859 hvx_sqr_f32_au(dst, src, num_elems);
860 }
861 } else {
862 if (hex_is_aligned((void *) src, 128)) {
863 hvx_sqr_f32_ua(dst, src, num_elems);
864 } else {
865 hvx_sqr_f32_uu(dst, src, num_elems);
866 }
867 }
868}
869
870#define hvx_sqr_f16_loop_body(dst_type, src_type, vec_store) \
871 do { \
872 dst_type * restrict vdst = (dst_type *) dst; \
873 src_type * restrict vsrc = (src_type *) src; \
874 \
875 const uint32_t elem_size = sizeof(_Float16); \
876 const uint32_t epv = 128 / elem_size; \
877 const uint32_t nvec = n / epv; \
878 const uint32_t nloe = n % epv; \
879 \
880 uint32_t i = 0; \
881 \
882 _Pragma("unroll(4)") \
883 for (; i < nvec; i++) { \
884 vdst[i] = HVX_OP_MUL_F16(vsrc[i], vsrc[i]); \
885 } \
886 if (nloe) { \
887 HVX_Vector v = HVX_OP_MUL_F16(vsrc[i], vsrc[i]); \
888 vec_store((void *) &vdst[i], nloe * elem_size, v); \
889 } \
890 } while(0)
891
892static inline void hvx_sqr_f16_aa(uint8_t * restrict dst, const uint8_t * restrict src, uint32_t n) {
893 assert((unsigned long) dst % 128 == 0);
894 assert((unsigned long) src % 128 == 0);
895 hvx_sqr_f16_loop_body(HVX_Vector, HVX_Vector, hvx_vec_store_a);
896}
897
898static inline void hvx_sqr_f16_au(uint8_t * restrict dst, const uint8_t * restrict src, uint32_t n) {
899 assert((unsigned long) dst % 128 == 0);
900 hvx_sqr_f16_loop_body(HVX_Vector, HVX_UVector, hvx_vec_store_a);
901}
902
903static inline void hvx_sqr_f16_ua(uint8_t * restrict dst, const uint8_t * restrict src, uint32_t n) {
904 assert((unsigned long) src % 128 == 0);
905 hvx_sqr_f16_loop_body(HVX_UVector, HVX_Vector, hvx_vec_store_u);
906}
907
908static inline void hvx_sqr_f16_uu(uint8_t * restrict dst, const uint8_t * restrict src, uint32_t n) {
909 hvx_sqr_f16_loop_body(HVX_UVector, HVX_UVector, hvx_vec_store_u);
910}
911
912static inline void hvx_sqr_f16(uint8_t * restrict dst, const uint8_t * restrict src, const uint32_t num_elems) {
913 if (hex_is_aligned((void *) dst, 128)) {
914 if (hex_is_aligned((void *) src, 128)) {
915 hvx_sqr_f16_aa(dst, src, num_elems);
916 } else {
917 hvx_sqr_f16_au(dst, src, num_elems);
918 }
919 } else {
920 if (hex_is_aligned((void *) src, 128)) {
921 hvx_sqr_f16_ua(dst, src, num_elems);
922 } else {
923 hvx_sqr_f16_uu(dst, src, num_elems);
924 }
925 }
926}
927
928#undef HVX_OP_ADD_F32
929#undef HVX_OP_SUB_F32
930#undef HVX_OP_MUL_F32
931#undef HVX_OP_ADD_F16
932#undef HVX_OP_SUB_F16
933#undef HVX_OP_MUL_F16
934#undef hvx_arith_loop_body
935#undef HVX_OP_ADD_SCALAR_F32
936#undef HVX_OP_SUB_SCALAR_F32
937#undef HVX_OP_MUL_SCALAR_F32
938#undef HVX_OP_ADD_SCALAR_F16
939#undef HVX_OP_SUB_SCALAR_F16
940#undef HVX_OP_MUL_SCALAR_F16
941#undef hvx_scalar_loop_body
942#undef HVX_OP_MIN_SCALAR
943#undef HVX_OP_MAX_SCALAR
944#undef HVX_OP_MIN_SCALAR_F16
945#undef HVX_OP_CLAMP_SCALAR
946#undef HVX_OP_CLAMP_SCALAR_F16
947#undef HVX_OP_LEAKY_RELU_SCALAR
948#undef DEFINE_HVX_BINARY_OP_VARIANTS
949#undef DEFINE_HVX_BINARY_SCALAR_OP_VARIANTS
950#undef HVX_BINARY_DISPATCHER
951#undef UNUSED
952
953#endif // HVX_ARITH_H