ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-hexagon / htp / hvx-base.h
| 1 | #ifndef HVX_BASE_H |
| 2 | #define HVX_BASE_H |
| 3 | |
| 4 | #include <stdbool.h> |
| 5 | #include <stdint.h> |
| 6 | #include <math.h> |
| 7 | #include <assert.h> |
| 8 | |
| 9 | #include "hex-utils.h" |
| 10 | #include "hvx-types.h" |
| 11 | |
| 12 | #define hvx_vmem(A) *((HVX_Vector *)(A)) |
| 13 | #define hvx_vmemu(A) *((HVX_UVector *)(A)) |
| 14 | |
| 15 | static inline void hvx_vec_store_u(void * restrict dst, uint32_t n, HVX_Vector v) { |
| 16 | // Rotate as needed. |
| 17 | v = Q6_V_vlalign_VVR(v, v, (size_t) dst); |
| 18 | |
| 19 | uint32_t left_off = (size_t) dst & 127; |
| 20 | uint32_t right_off = left_off + n; |
| 21 | |
| 22 | HVX_VectorPred ql_not = Q6_Q_vsetq_R((size_t) dst); |
| 23 | HVX_VectorPred qr = Q6_Q_vsetq2_R(right_off); |
| 24 | |
| 25 | if (right_off > 128) { |
| 26 | Q6_vmem_QRIV(qr, (HVX_Vector *) dst + 1, v); |
| 27 | // all 1's |
| 28 | qr = Q6_Q_vcmp_eq_VbVb(v, v); |
| 29 | } |
| 30 | |
| 31 | ql_not = Q6_Q_or_QQn(ql_not, qr); |
| 32 | Q6_vmem_QnRIV(ql_not, (HVX_Vector *) dst, v); |
| 33 | } |
| 34 | |
| 35 | static inline void hvx_vec_store_a(void * restrict dst, uint32_t n, HVX_Vector v) { |
| 36 | assert((unsigned long) dst % 128 == 0); |
| 37 | HVX_VectorPred m = Q6_Q_or_QQn(Q6_Q_vsetq_R((unsigned long) dst), Q6_Q_vsetq2_R(n)); |
| 38 | Q6_vmem_QnRIV(m, (HVX_Vector *) dst, v); |
| 39 | } |
| 40 | |
| 41 | static inline HVX_Vector hvx_vec_splat_f32(float v) { |
| 42 | union { float f; uint32_t i; } u = { .f = v }; |
| 43 | return Q6_V_vsplat_R(u.i); |
| 44 | } |
| 45 | |
| 46 | static inline HVX_Vector hvx_vec_splat_f16(_Float16 v) { |
| 47 | union { __fp16 f; uint16_t i; } u = { .f = v }; |
| 48 | return Q6_Vh_vsplat_R(u.i); |
| 49 | } |
| 50 | |
| 51 | static inline HVX_Vector hvx_vec_repl4(HVX_Vector v) { |
| 52 | // vdelta control to replicate first 4 bytes across all elements |
| 53 | static const uint8_t __attribute__((aligned(128))) repl[128] = { |
| 54 | 0x00, 0x00, 0x00, 0x00, 0x04, 0x04, 0x04, 0x04, 0x08, 0x08, 0x08, 0x08, 0x04, 0x04, 0x04, 0x04, |
| 55 | 0x10, 0x10, 0x10, 0x10, 0x04, 0x04, 0x04, 0x04, 0x08, 0x08, 0x08, 0x08, 0x04, 0x04, 0x04, 0x04, |
| 56 | 0x20, 0x20, 0x20, 0x20, 0x04, 0x04, 0x04, 0x04, 0x08, 0x08, 0x08, 0x08, 0x04, 0x04, 0x04, 0x04, |
| 57 | 0x10, 0x10, 0x10, 0x10, 0x04, 0x04, 0x04, 0x04, 0x08, 0x08, 0x08, 0x08, 0x04, 0x04, 0x04, 0x04, |
| 58 | 0x40, 0x40, 0x40, 0x40, 0x04, 0x04, 0x04, 0x04, 0x08, 0x08, 0x08, 0x08, 0x04, 0x04, 0x04, 0x04, |
| 59 | 0x10, 0x10, 0x10, 0x10, 0x04, 0x04, 0x04, 0x04, 0x08, 0x08, 0x08, 0x08, 0x04, 0x04, 0x04, 0x04, |
| 60 | 0x20, 0x20, 0x20, 0x20, 0x04, 0x04, 0x04, 0x04, 0x08, 0x08, 0x08, 0x08, 0x04, 0x04, 0x04, 0x04, |
| 61 | 0x10, 0x10, 0x10, 0x10, 0x04, 0x04, 0x04, 0x04, 0x08, 0x08, 0x08, 0x08, 0x04, 0x04, 0x04, 0x04, |
| 62 | }; |
| 63 | |
| 64 | HVX_Vector ctrl = *(HVX_Vector *) repl; |
| 65 | return Q6_V_vdelta_VV(v, ctrl); |
| 66 | } |
| 67 | |
| 68 | static inline float hvx_vec_get_f32(HVX_Vector v) { |
| 69 | float __attribute__((aligned(128))) x; |
| 70 | hvx_vec_store_a(&x, 4, v); |
| 71 | return x; |
| 72 | } |
| 73 | |
| 74 | static inline int32_t hvx_vec_get_i32(HVX_Vector v) { |
| 75 | int32_t __attribute__((aligned(128))) x; |
| 76 | hvx_vec_store_a(&x, 4, v); |
| 77 | return x; |
| 78 | } |
| 79 | |
| 80 | static inline _Float16 hvx_vec_get_f16(HVX_Vector v) { |
| 81 | _Float16 __attribute__((aligned(128))) x; |
| 82 | hvx_vec_store_a(&x, 2, v); |
| 83 | return x; |
| 84 | } |
| 85 | |
| 86 | static inline HVX_Vector hvx_vec_abs_f16(HVX_Vector v) { |
| 87 | // abs by clearing the fp16 sign bit |
| 88 | HVX_Vector mask = Q6_Vh_vsplat_R(0x7fff); |
| 89 | return Q6_V_vand_VV(v, mask); |
| 90 | } |
| 91 | |
| 92 | static inline HVX_Vector hvx_vec_neg_f16(HVX_Vector v) { |
| 93 | // neg by setting the fp16 sign bit |
| 94 | HVX_Vector mask = Q6_Vh_vsplat_R(0x8000); |
| 95 | return Q6_V_vxor_VV(v, mask); |
| 96 | } |
| 97 | |
| 98 | static inline HVX_Vector hvx_vec_abs_f32(HVX_Vector v) { |
| 99 | // abs by clearing the fp32 sign bit |
| 100 | HVX_Vector mask = Q6_V_vsplat_R(0x7fffffff); |
| 101 | return Q6_V_vand_VV(v, mask); |
| 102 | } |
| 103 | |
| 104 | static inline HVX_Vector hvx_vec_neg_f32(HVX_Vector v) { |
| 105 | #if __HVX_ARCH__ > 75 |
| 106 | return Q6_Vsf_vfneg_Vsf(v); |
| 107 | #else |
| 108 | // neg by setting the fp32 sign bit |
| 109 | HVX_Vector mask = Q6_V_vsplat_R(0x80000000); |
| 110 | return Q6_V_vxor_VV(v, mask); |
| 111 | #endif // __HVX_ARCH__ > 75 |
| 112 | } |
| 113 | |
| 114 | static inline HVX_Vector hvx_vec_step_f32(HVX_Vector v) { |
| 115 | const HVX_Vector zero = Q6_V_vzero(); |
| 116 | const HVX_Vector one = hvx_vec_splat_f32(1.0f); |
| 117 | HVX_VectorPred q = Q6_Q_vcmp_gt_VsfVsf(v, zero); |
| 118 | return Q6_V_vmux_QVV(q, one, zero); |
| 119 | } |
| 120 | |
| 121 | static inline HVX_Vector hvx_vec_sgn_f32(HVX_Vector v) { |
| 122 | const HVX_Vector zero = Q6_V_vzero(); |
| 123 | const HVX_Vector one = hvx_vec_splat_f32(1.0f); |
| 124 | const HVX_Vector sign_mask = Q6_V_vsplat_R(0x80000000); |
| 125 | const HVX_Vector mag = hvx_vec_abs_f32(v); |
| 126 | const HVX_Vector signed_one = Q6_V_vor_VV(one, Q6_V_vand_VV(v, sign_mask)); |
| 127 | // The ordered magnitude comparison maps NaN and both signed zeros to zero. |
| 128 | const HVX_VectorPred nonzero = Q6_Q_vcmp_gt_VsfVsf(mag, zero); |
| 129 | return Q6_V_vmux_QVV(nonzero, signed_one, zero); |
| 130 | } |
| 131 | |
| 132 | static inline HVX_Vector hvx_vec_step_f16(HVX_Vector v) { |
| 133 | const HVX_Vector zero = Q6_V_vzero(); |
| 134 | const HVX_Vector one = hvx_vec_splat_f16((_Float16) 1.0f); |
| 135 | HVX_VectorPred q = Q6_Q_vcmp_gt_VhfVhf(v, zero); |
| 136 | return Q6_V_vmux_QVV(q, one, zero); |
| 137 | } |
| 138 | |
| 139 | static inline HVX_VectorPred hvx_vec_is_nan_f16(HVX_Vector v) { |
| 140 | const HVX_Vector vnan_exp = Q6_Vh_vsplat_R(0x7C00); |
| 141 | const HVX_Vector vnan_frac = Q6_Vh_vsplat_R(0x7FFF); |
| 142 | |
| 143 | // get pred of which are NaN, i.e., exponent bits all 1s and fraction bits non 0s |
| 144 | HVX_VectorPred p_exp = Q6_Q_vcmp_eq_VhVh(Q6_V_vand_VV(v, vnan_exp), vnan_exp); |
| 145 | HVX_VectorPred p_frac = Q6_Q_not_Q(Q6_Q_vcmp_eq_VhVh(Q6_V_vand_VV(v, vnan_frac), vnan_exp)); |
| 146 | return Q6_Q_and_QQ(p_exp, p_frac); |
| 147 | } |
| 148 | |
| 149 | static inline HVX_Vector hvx_vec_f32_to_f16_shuff(HVX_Vector v0, HVX_Vector v1) { |
| 150 | #if __HVX_ARCH__ >= 81 |
| 151 | HVX_Vector q0 = Q6_Vqf32_equals_Vsf(v0); |
| 152 | HVX_Vector q1 = Q6_Vqf32_equals_Vsf(v1); |
| 153 | #else |
| 154 | const HVX_Vector zero = Q6_V_vzero(); |
| 155 | HVX_Vector q0 = Q6_Vqf32_vadd_VsfVsf(v0, zero); |
| 156 | HVX_Vector q1 = Q6_Vqf32_vadd_VsfVsf(v1, zero); |
| 157 | #endif |
| 158 | return Q6_Vhf_equals_Wqf32(Q6_W_vcombine_VV(q1, q0)); |
| 159 | } |
| 160 | |
| 161 | static inline HVX_Vector hvx_vec_f32_to_f16(HVX_Vector v0, HVX_Vector v1) { |
| 162 | return Q6_Vh_vdeal_Vh(hvx_vec_f32_to_f16_shuff(v0, v1)); |
| 163 | } |
| 164 | |
| 165 | #if __HVX_ARCH__ >= 79 |
| 166 | static inline HVX_VectorPair hvx_vec_f16_to_f32_shuff(HVX_Vector v) { |
| 167 | const HVX_Vector one = hvx_vec_splat_f16(1.0); |
| 168 | HVX_VectorPair p = Q6_Wsf_vmpy_VhfVhf(v, one); |
| 169 | return Q6_W_vcombine_VV(Q6_V_hi_W(p), Q6_V_lo_W(p)); |
| 170 | } |
| 171 | static inline HVX_VectorPair hvx_vec_f16_to_f32(HVX_Vector v) { |
| 172 | const HVX_Vector one = hvx_vec_splat_f16(1.0); |
| 173 | HVX_VectorPair p = Q6_Wsf_vmpy_VhfVhf(Q6_Vh_vshuff_Vh(v), one); |
| 174 | return Q6_W_vcombine_VV(Q6_V_hi_W(p), Q6_V_lo_W(p)); |
| 175 | } |
| 176 | #else |
| 177 | static inline HVX_VectorPair hvx_vec_f16_to_f32_shuff(HVX_Vector v) { |
| 178 | const HVX_Vector one = hvx_vec_splat_f16(1.0); |
| 179 | HVX_VectorPair p = Q6_Wqf32_vmpy_VhfVhf(v, one); |
| 180 | return Q6_W_vcombine_VV(Q6_Vsf_equals_Vqf32(Q6_V_hi_W(p)), Q6_Vsf_equals_Vqf32(Q6_V_lo_W(p))); |
| 181 | } |
| 182 | static inline HVX_VectorPair hvx_vec_f16_to_f32(HVX_Vector v) { |
| 183 | const HVX_Vector one = hvx_vec_splat_f16(1.0); |
| 184 | HVX_VectorPair p = Q6_Wqf32_vmpy_VhfVhf(Q6_Vh_vshuff_Vh(v), one); |
| 185 | return Q6_W_vcombine_VV(Q6_Vsf_equals_Vqf32(Q6_V_hi_W(p)), Q6_Vsf_equals_Vqf32(Q6_V_lo_W(p))); |
| 186 | } |
| 187 | #endif |
| 188 | |
| 189 | static inline HVX_Vector hvx_vec_i16_from_hf_rnd_sat(HVX_Vector vin) { |
| 190 | // This looks complicated. |
| 191 | // Ideally should just be Q6_Vh_equals_Vhf(vin) |
| 192 | // but that instruction does not do proper rounding. |
| 193 | |
| 194 | // convert to qf32, multiplying by 1.0 in the process. |
| 195 | HVX_VectorPair v32 = Q6_Wqf32_vmpy_VhfVhf(vin, Q6_Vh_vsplat_R(0x3C00)); |
| 196 | |
| 197 | // 'in-range' values are +/32752. |
| 198 | // add 192K to it, convert to sf |
| 199 | HVX_Vector v192K = Q6_V_vsplat_R(0x48400000); |
| 200 | HVX_Vector vsf_0 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vsf(Q6_V_lo_W(v32), v192K)); |
| 201 | HVX_Vector vsf_1 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vsf(Q6_V_hi_W(v32), v192K)); |
| 202 | |
| 203 | // for in-range cases, result is {163858... 229360} so the exponent is always 144. |
| 204 | // if we extract bits 21..0 as a signed quantity, and round 6 bits off, that will be the answer. |
| 205 | // Start by <<10 to get the final 'sign' bit in bit 15... |
| 206 | vsf_0 = Q6_Vw_vasl_VwR(vsf_0, 10); |
| 207 | vsf_1 = Q6_Vw_vasl_VwR(vsf_1, 10); |
| 208 | |
| 209 | // now round down to 16 |
| 210 | return Q6_Vh_vround_VwVw_sat(vsf_1, vsf_0); |
| 211 | } |
| 212 | |
| 213 | #if __HVX_ARCH__ < 79 |
| 214 | |
| 215 | static inline HVX_VectorPair hvx_vec_mpyacc_f32_f16(HVX_VectorPair acc, HVX_Vector x, HVX_Vector y) |
| 216 | { |
| 217 | HVX_VectorPair m = Q6_Wqf32_vmpy_VhfVhf(x, y); |
| 218 | HVX_Vector a0 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vsf(Q6_V_lo_W(m), Q6_V_lo_W(acc))); |
| 219 | HVX_Vector a1 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vsf(Q6_V_hi_W(m), Q6_V_hi_W(acc))); |
| 220 | return Q6_W_vcombine_VV(a1, a0); |
| 221 | } |
| 222 | |
| 223 | #else |
| 224 | |
| 225 | static inline HVX_VectorPair hvx_vec_mpyacc_f32_f16(HVX_VectorPair acc, HVX_Vector x, HVX_Vector y) |
| 226 | { |
| 227 | return Q6_Wsf_vmpyacc_WsfVhfVhf(acc, x, y); |
| 228 | } |
| 229 | |
| 230 | #endif |
| 231 | |
| 232 | #if __HVX_ARCH__ < 79 |
| 233 | |
| 234 | static inline HVX_Vector hvx_vec_add_f16_f16(HVX_Vector a, HVX_Vector b) |
| 235 | { |
| 236 | const HVX_Vector negone = Q6_Vh_vsplat_R(0xBC00); // -1.0 in IEEE FP16 |
| 237 | const HVX_Vector one = Q6_Vh_vsplat_R(0x3C00); // 1.0 in IEEE FP16 |
| 238 | HVX_VectorPair a_p = Q6_Wqf32_vmpy_VhfVhf(a, one); |
| 239 | HVX_VectorPair b_p = Q6_Wqf32_vmpy_VhfVhf(b, negone); |
| 240 | HVX_Vector a0 = Q6_Vqf32_vsub_Vqf32Vqf32(Q6_V_lo_W(a_p), Q6_V_lo_W(b_p)); |
| 241 | HVX_Vector a1 = Q6_Vqf32_vsub_Vqf32Vqf32(Q6_V_hi_W(a_p), Q6_V_hi_W(b_p)); |
| 242 | return Q6_Vhf_equals_Wqf32(Q6_W_vcombine_VV(a1, a0)); |
| 243 | } |
| 244 | |
| 245 | static inline HVX_Vector hvx_vec_sub_f16_f16(HVX_Vector a, HVX_Vector b) |
| 246 | { |
| 247 | const HVX_Vector negone = Q6_Vh_vsplat_R(0xBC00); // -1.0 in IEEE FP16 |
| 248 | const HVX_Vector one = Q6_Vh_vsplat_R(0x3C00); // 1.0 in IEEE FP16 |
| 249 | HVX_VectorPair a_p = Q6_Wqf32_vmpy_VhfVhf(a, one); |
| 250 | HVX_VectorPair b_p = Q6_Wqf32_vmpy_VhfVhf(b, negone); |
| 251 | HVX_Vector a0 = Q6_Vqf32_vadd_Vqf32Vqf32(Q6_V_lo_W(a_p), Q6_V_lo_W(b_p)); |
| 252 | HVX_Vector a1 = Q6_Vqf32_vadd_Vqf32Vqf32(Q6_V_hi_W(a_p), Q6_V_hi_W(b_p)); |
| 253 | return Q6_Vhf_equals_Wqf32(Q6_W_vcombine_VV(a1, a0)); |
| 254 | } |
| 255 | |
| 256 | static inline HVX_Vector hvx_vec_mul_f16_f16(HVX_Vector a, HVX_Vector b) |
| 257 | { |
| 258 | return Q6_Vhf_equals_Wqf32(Q6_Wqf32_vmpy_VhfVhf(a, b)); |
| 259 | } |
| 260 | |
| 261 | static inline HVX_Vector hvx_vec_add_f32_f32(HVX_Vector a, HVX_Vector b) { |
| 262 | return Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_VsfVsf(a, b)); |
| 263 | } |
| 264 | |
| 265 | static inline HVX_Vector hvx_vec_sub_f32_f32(HVX_Vector a, HVX_Vector b) { |
| 266 | return Q6_Vsf_equals_Vqf32(Q6_Vqf32_vsub_VsfVsf(a, b)); |
| 267 | } |
| 268 | |
| 269 | static inline HVX_Vector hvx_vec_mul_f32_f32(HVX_Vector a, HVX_Vector b) { |
| 270 | return Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(a, b)); |
| 271 | } |
| 272 | |
| 273 | #else |
| 274 | |
| 275 | static inline HVX_Vector hvx_vec_add_f16_f16(HVX_Vector a, HVX_Vector b) |
| 276 | { |
| 277 | return Q6_Vhf_vadd_VhfVhf(a, b); |
| 278 | } |
| 279 | |
| 280 | static inline HVX_Vector hvx_vec_sub_f16_f16(HVX_Vector a, HVX_Vector b) |
| 281 | { |
| 282 | return Q6_Vhf_vsub_VhfVhf(a, b); |
| 283 | } |
| 284 | |
| 285 | static inline HVX_Vector hvx_vec_mul_f16_f16(HVX_Vector a, HVX_Vector b) |
| 286 | { |
| 287 | return Q6_Vhf_vmpy_VhfVhf(a, b); |
| 288 | } |
| 289 | |
| 290 | static inline HVX_Vector hvx_vec_add_f32_f32(HVX_Vector a, HVX_Vector b) { |
| 291 | return Q6_Vsf_vadd_VsfVsf(a, b); |
| 292 | } |
| 293 | |
| 294 | static inline HVX_Vector hvx_vec_sub_f32_f32(HVX_Vector a, HVX_Vector b) { |
| 295 | return Q6_Vsf_vsub_VsfVsf(a, b); |
| 296 | } |
| 297 | |
| 298 | static inline HVX_Vector hvx_vec_mul_f32_f32(HVX_Vector a, HVX_Vector b) { |
| 299 | return Q6_Vsf_vmpy_VsfVsf(a, b); |
| 300 | } |
| 301 | |
| 302 | #endif // __HVX_ARCH__ < 79 |
| 303 | |
| 304 | static inline HVX_Vector hvx_vec_load_act_tile(const uint8_t * y_q, uint32_t kt, HVX_Vector * v_act_all) { |
| 305 | if (kt % 4 == 0) { |
| 306 | *v_act_all = hvx_vmem(y_q + kt * 32); |
| 307 | return *v_act_all; |
| 308 | } else if (kt % 4 == 1) { |
| 309 | return Q6_V_vror_VR(*v_act_all, 32); |
| 310 | } else if (kt % 4 == 2) { |
| 311 | return Q6_V_vror_VR(*v_act_all, 64); |
| 312 | } else { |
| 313 | return Q6_V_vror_VR(*v_act_all, 96); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | // Full tile, in place. tmp: 32 vectors scratch. |
| 318 | // Use when result must stay in m. Slowest (spills). |
| 319 | static inline void hvx_transpose_32x32_w_shuff_inplace(HVX_Vector * restrict m, HVX_Vector * restrict tmp) { |
| 320 | for (int i = 0; i < 16; ++i) { |
| 321 | HVX_VectorPair p = Q6_W_vshuff_VVR(m[2*i + 1], m[2*i], -4); |
| 322 | tmp[2*i + 0] = Q6_V_lo_W(p); |
| 323 | tmp[2*i + 1] = Q6_V_hi_W(p); |
| 324 | } |
| 325 | |
| 326 | for (int b = 0; b < 32; b += 4) { |
| 327 | HVX_VectorPair p0 = Q6_W_vshuff_VVR(tmp[b + 2], tmp[b + 0], -8); |
| 328 | HVX_VectorPair p1 = Q6_W_vshuff_VVR(tmp[b + 3], tmp[b + 1], -8); |
| 329 | m[b + 0] = Q6_V_lo_W(p0); m[b + 1] = Q6_V_hi_W(p0); |
| 330 | m[b + 2] = Q6_V_lo_W(p1); m[b + 3] = Q6_V_hi_W(p1); |
| 331 | } |
| 332 | |
| 333 | for (int b = 0; b < 32; b += 8) { |
| 334 | for (int i = 0; i < 4; ++i) { |
| 335 | HVX_VectorPair p = Q6_W_vshuff_VVR(m[b + i + 4], m[b + i], -16); |
| 336 | tmp[b + 2*i + 0] = Q6_V_lo_W(p); |
| 337 | tmp[b + 2*i + 1] = Q6_V_hi_W(p); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | for (int b = 0; b < 32; b += 16) { |
| 342 | for (int i = 0; i < 8; ++i) { |
| 343 | HVX_VectorPair p = Q6_W_vshuff_VVR(tmp[b + i + 8], tmp[b + i], -32); |
| 344 | m[b + 2*i + 0] = Q6_V_lo_W(p); |
| 345 | m[b + 2*i + 1] = Q6_V_hi_W(p); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | for (int i = 0; i < 16; ++i) { |
| 350 | HVX_VectorPair p = Q6_W_vshuff_VVR(m[i + 16], m[i], -64); |
| 351 | tmp[2 * i + 0] = Q6_V_lo_W(p); |
| 352 | tmp[2 * i + 1] = Q6_V_hi_W(p); |
| 353 | } |
| 354 | |
| 355 | for (int i = 0; i < 32; ++i) { |
| 356 | m[i] = tmp[i]; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | // Rows [0, nrows) of the transpose of src[0, ncols) to dst. src is clobbered, must not alias dst. |
| 361 | // Use when not in VTCM or for full tiles. |
| 362 | static inline void hvx_transpose_32x32_w_shuff(uint8_t * restrict dst, uint32_t dst_stride, HVX_Vector * restrict src, |
| 363 | uint32_t nrows, uint32_t ncols) { |
| 364 | for (uint32_t g = 0; g < ncols; g += 8) { |
| 365 | HVX_Vector * s = src + g; |
| 366 | |
| 367 | HVX_VectorPair a0 = Q6_W_vshuff_VVR(s[1], s[0], -4); |
| 368 | HVX_VectorPair a1 = Q6_W_vshuff_VVR(s[3], s[2], -4); |
| 369 | HVX_VectorPair a2 = Q6_W_vshuff_VVR(s[5], s[4], -4); |
| 370 | HVX_VectorPair a3 = Q6_W_vshuff_VVR(s[7], s[6], -4); |
| 371 | |
| 372 | HVX_VectorPair b0 = Q6_W_vshuff_VVR(Q6_V_lo_W(a1), Q6_V_lo_W(a0), -8); |
| 373 | HVX_VectorPair b1 = Q6_W_vshuff_VVR(Q6_V_hi_W(a1), Q6_V_hi_W(a0), -8); |
| 374 | HVX_VectorPair b2 = Q6_W_vshuff_VVR(Q6_V_lo_W(a3), Q6_V_lo_W(a2), -8); |
| 375 | HVX_VectorPair b3 = Q6_W_vshuff_VVR(Q6_V_hi_W(a3), Q6_V_hi_W(a2), -8); |
| 376 | |
| 377 | HVX_VectorPair c0 = Q6_W_vshuff_VVR(Q6_V_lo_W(b2), Q6_V_lo_W(b0), -16); |
| 378 | HVX_VectorPair c1 = Q6_W_vshuff_VVR(Q6_V_hi_W(b2), Q6_V_hi_W(b0), -16); |
| 379 | HVX_VectorPair c2 = Q6_W_vshuff_VVR(Q6_V_lo_W(b3), Q6_V_lo_W(b1), -16); |
| 380 | HVX_VectorPair c3 = Q6_W_vshuff_VVR(Q6_V_hi_W(b3), Q6_V_hi_W(b1), -16); |
| 381 | |
| 382 | s[0] = Q6_V_lo_W(c0); s[1] = Q6_V_hi_W(c0); |
| 383 | s[2] = Q6_V_lo_W(c1); s[3] = Q6_V_hi_W(c1); |
| 384 | s[4] = Q6_V_lo_W(c2); s[5] = Q6_V_hi_W(c2); |
| 385 | s[6] = Q6_V_lo_W(c3); s[7] = Q6_V_hi_W(c3); |
| 386 | } |
| 387 | |
| 388 | for (uint32_t k = 0; 4 * k < nrows; k++) { |
| 389 | HVX_VectorPair d0 = Q6_W_vshuff_VVR(src[k + 8], src[k], -32); |
| 390 | HVX_VectorPair d1 = Q6_W_vshuff_VVR(src[k + 24], src[k + 16], -32); |
| 391 | |
| 392 | HVX_VectorPair e0 = Q6_W_vshuff_VVR(Q6_V_lo_W(d1), Q6_V_lo_W(d0), -64); |
| 393 | HVX_VectorPair e1 = Q6_W_vshuff_VVR(Q6_V_hi_W(d1), Q6_V_hi_W(d0), -64); |
| 394 | |
| 395 | uint8_t * d = dst + 4 * k * dst_stride; |
| 396 | *(HVX_Vector *) (d + 0 * dst_stride) = Q6_V_lo_W(e0); |
| 397 | *(HVX_Vector *) (d + 1 * dst_stride) = Q6_V_hi_W(e0); |
| 398 | *(HVX_Vector *) (d + 2 * dst_stride) = Q6_V_lo_W(e1); |
| 399 | *(HVX_Vector *) (d + 3 * dst_stride) = Q6_V_hi_W(e1); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | // Specialized store that will follow outstanding scatters or gathers to make sure they complete |
| 404 | static inline void hvx_gather_sync(void * vtcm_addr) { |
| 405 | asm volatile("vmem(%0+#0):scatter_release\n" |
| 406 | "v0 = vmem(%0+#0)\n" :: "r"(vtcm_addr) : "v0", "memory"); |
| 407 | } |
| 408 | |
| 409 | // Lane k = k * stride. Offsets for the _gather transposes. |
| 410 | static inline HVX_Vector hvx_vec_gather_offsets_w(uint32_t stride) { |
| 411 | int32_t offsets[32] __attribute__((aligned(128))); |
| 412 | for (int k = 0; k < 32; k++) { |
| 413 | offsets[k] = k * stride; |
| 414 | } |
| 415 | return *(HVX_Vector *) offsets; |
| 416 | } |
| 417 | |
| 418 | static inline HVX_Vector hvx_vec_gather_offsets_h(uint32_t stride) { |
| 419 | int16_t offsets[64] __attribute__((aligned(128))); |
| 420 | for (int k = 0; k < 64; k++) { |
| 421 | offsets[k] = k * stride; |
| 422 | } |
| 423 | return *(HVX_Vector *) offsets; |
| 424 | } |
| 425 | |
| 426 | // Rows [0, nrows) of the transpose of ncols src vectors (src_stride apart) to dst. |
| 427 | // src, dst in VTCM. Async: hvx_gather_sync() before use. |
| 428 | // Use for partial tiles (small ncols). Fewest packets. |
| 429 | static inline void hvx_transpose_32x32_w_gather(uint8_t * restrict dst, uint32_t dst_stride, const uint8_t * restrict src, |
| 430 | uint32_t src_stride, HVX_Vector offsets, uint32_t nrows, uint32_t ncols) { |
| 431 | const uint32_t mu = ncols * src_stride; |
| 432 | |
| 433 | #pragma unroll(4) |
| 434 | for (uint32_t r = 0; r < nrows; r++) { |
| 435 | Q6_vgather_ARMVw((HVX_Vector *) (dst + r * dst_stride), (size_t) (src + r * sizeof(int32_t)), mu, offsets); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | // 16-bit version of hvx_transpose_32x32_w_gather. |
| 440 | static inline void hvx_transpose_64x64_h_gather(uint8_t * restrict dst, uint32_t dst_stride, const uint8_t * restrict src, |
| 441 | uint32_t src_stride, HVX_Vector offsets, uint32_t nrows, uint32_t ncols) { |
| 442 | const uint32_t mu = ncols * src_stride; |
| 443 | |
| 444 | #pragma unroll(4) |
| 445 | for (uint32_t r = 0; r < nrows; r++) { |
| 446 | Q6_vgather_ARMVh((HVX_Vector *) (dst + r * dst_stride), (size_t) (src + r * sizeof(int16_t)), mu, offsets); |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | #endif /* HVX_BASE_H */ |