gi_float.h 58.5 KB
Newer Older
1 2 3 4 5
#pragma once

#include "gi_common.h"

GI_FORCEINLINE
6
GI_INT32_t GiReinterpretAsInt32(GI_FLOAT32_t In) {
7 8 9 10
#if defined(GI_NEON_INTRINSICS)
    return vreinterpretq_s32_f32(In);
#elif defined(GI_SSE2_INTRINSICS)
    return _mm_castps_si128(In);
11 12
#elif defined(GI_RVV_INTRINSICS)
    return vreinterpret_v_f32m1_i32m1(In);
13
#else
14
    return (GI_INT32_t)In;
15 16 17 18
#endif
}

GI_FORCEINLINE
19 20 21 22 23
GI_UINT32_t GiReinterpretAsUint32(GI_FLOAT32_t In) {
#if defined(GI_NEON_INTRINSICS)
    return vreinterpretq_u32_f32(In);
#elif defined(GI_SSE2_INTRINSICS)
    return _mm_castps_si128(In);
24 25
#elif defined(GI_RVV_INTRINSICS)
    return vreinterpret_v_f32m1_u32m1(In);
26
#else
27
    return (GI_UINT32_t)In;
28 29 30 31 32 33 34 35 36
#endif
}

GI_FORCEINLINE
GI_FLOAT32_t GiReintInt32ToFloat32(GI_INT32_t Vector) {
#if defined(GI_NEON_INTRINSICS)
    return vreinterpretq_f32_s32(Vector);
#elif defined(GI_SSE2_INTRINSICS)
    return _mm_castsi128_ps(Vector);
37 38
#elif defined(GI_RVV_INTRINSICS)
    return vreinterpret_v_i32m1_f32m1(Vector);
39
#else
40
    return (GI_FLOAT32_t)Vector;
41 42 43 44 45 46 47 48 49
#endif
}

GI_FORCEINLINE
GI_FLOAT32_t GiReintUint32ToFloat32(GI_UINT32_t Vector) {
#if defined(GI_NEON_INTRINSICS)
    return vreinterpretq_f32_u32(Vector);
#elif defined(GI_SSE2_INTRINSICS)
    return _mm_castsi128_ps(Vector);
50 51
#elif defined(GI_RVV_INTRINSICS)
    return vreinterpret_v_u32m1_f32m1(Vector);
52
#else
53
    return (GI_FLOAT32_t)Vector;
54 55 56 57 58
#endif
}

GI_FORCEINLINE
GI_INT32_t GiRoundAsInt32(GI_FLOAT32_t Vector) {
59 60 61 62
#if defined(GI_NEON_INTRINSICS)
#if __ARM_ARCH >= 8
    return vcvtaq_s32_f32(Vector);
#else
63 64 65
    float32x4_t vinc0 = vbslq_f32(
            vcgeq_f32(Vector, GiBroadcastFloat32(0.0f)), GiBroadcastFloat32(0.5f),
            GiBroadcastFloat32(-0.5f));
66 67
    return vcvtq_s32_f32(vaddq_f32(Vector, vinc0));
#endif
68
#elif defined(GI_SSE42_INTRINSICS)
69 70 71
    __m128 vinc0 = _mm_blendv_ps(
            GiBroadcastFloat32(-0.5f), GiBroadcastFloat32(0.5f),
            _mm_cmpge_ps(Vector, GiBroadcastFloat32(0.0f)));
72
    return _mm_cvttps_epi32(_mm_add_ps(Vector, vinc0));
73 74
#elif defined(GI_RVV_INTRINSICS)
    return vfcvt_x_f_v_i32m1(Vector, GI_SIMD_LEN_BYTE / sizeof(float));
75
#else
76
    GI_INT32_t ret;
77 78 79
    GI_INT32_NAIVE_t tmp_ret;
    GI_FLOAT32_NAIVE_t s0;
    memcpy(&s0, &Vector, sizeof(GI_FLOAT32_NAIVE_t));
80
    for (size_t i = 0; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) {
81
        tmp_ret[i] = (int32_t)round(s0[i]);
82
    }
83
    memcpy(&ret, &tmp_ret, sizeof(GI_INT32_t));
84 85 86 87 88
    return ret;
#endif
}

GI_FORCEINLINE
89
GI_INT32_t GiCastToInt32(GI_FLOAT32_t Vector) {
90
#if defined(GI_NEON_INTRINSICS)
91
    return vcvtq_s32_f32(Vector);
92
#elif defined(GI_SSE2_INTRINSICS)
93
    return _mm_cvttps_epi32(Vector);
94 95 96 97 98 99 100 101 102 103
#elif defined(GI_RVV_INTRINSICS)
    //! TODO: vfcvt_rtz_x_f_v_i32m1 is RVV 1.0 api, now xuantie D1 only support 0p7
    //! as a workaround, we imp this API by naive
    //! return vfcvt_rtz_x_f_v_i32m1(Vector, GI_SIMD_LEN_BYTE / sizeof(float));
    GI_FLOAT32_FIXLEN_t src = GiFloat32Type2FixLenType(Vector);
    GI_INT32_FIXLEN_t ret;
    for (size_t i = 0; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) {
        ret[i] = (int32_t)(src[i]);
    }
    return GiFixLenType2GiInt32Type(ret);
104
#else
105 106 107
    GI_INT32_t ret;
    for (size_t i = 0; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) {
        ret[i] = (int32_t)(Vector[i]);
108 109 110 111 112 113
    }
    return ret;
#endif
}

GI_FORCEINLINE
114
GI_FLOAT32_t GiCastToFloat32(GI_INT32_t Vector) {
115
#if defined(GI_NEON_INTRINSICS)
116
    return vcvtq_f32_s32(Vector);
117
#elif defined(GI_SSE2_INTRINSICS)
118
    return _mm_cvtepi32_ps(Vector);
119 120
#elif defined(GI_RVV_INTRINSICS)
    return vfcvt_f_x_v_f32m1(Vector, GI_SIMD_LEN_BYTE / sizeof(float));
121
#else
122 123
    GI_FLOAT32_t ret;
    for (size_t i = 0; i < GI_SIMD_LEN_BYTE / sizeof(int32_t); i++) {
124
        ret[i] = (float)Vector[i];
125 126 127 128 129 130
    }
    return ret;
#endif
}

GI_FORCEINLINE
131
GI_FLOAT32_t GiLoadBroadcastFloat32(const float* Value) {
132 133 134 135
#if defined(GI_NEON_INTRINSICS)
    return vld1q_dup_f32(Value);
#elif defined(GI_SSE2_INTRINSICS)
    return _mm_load_ps1(Value);
136 137
#elif defined(GI_RVV_INTRINSICS)
    return GiBroadcastFloat32(*Value);
138
#else
139
    GI_FLOAT32_t ret;
140 141 142 143 144 145 146 147
    for (size_t i = 0; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) {
        ret[i] = *Value;
    }
    return ret;
#endif
}

GI_FORCEINLINE
148
GI_FLOAT32_t GiZeroFloat32(void) {
149 150 151 152 153 154 155 156 157 158
#if defined(GI_NEON_INTRINSICS)
    return vdupq_n_f32(0.0f);
#elif defined(GI_SSE2_INTRINSICS)
    return _mm_setzero_ps();
#else
    return GiBroadcastFloat32(0.0f);
#endif
}

GI_FORCEINLINE
159
GI_FLOAT32_t GiLoadFloat32(const float* Buffer) {
160 161 162
#if defined(GI_NEON_INTRINSICS)
    return vld1q_f32(Buffer);
#elif defined(GI_SSE2_INTRINSICS)
163 164 165 166
    if ((((uintptr_t)(Buffer)) & 15) == 0)
        return _mm_load_ps(Buffer);
    else
        return _mm_loadu_ps(Buffer);
167 168
#elif defined(GI_RVV_INTRINSICS)
    return vle32_v_f32m1(Buffer, GI_SIMD_LEN_BYTE / sizeof(float));
169
#else
170
    GI_FLOAT32_t ret;
171 172 173 174 175 176 177
    for (size_t i = 0; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) {
        ret[i] = Buffer[i];
    }
    return ret;
#endif
}

178 179 180 181 182 183
GI_FORCEINLINE
GI_FLOAT32_V2_t GiLoadFloat32V2(const float* Buffer) {
#if defined(GI_NEON_INTRINSICS)
    return vld1q_f32_x2(Buffer);
#else
    GI_FLOAT32_V2_t v;
184 185 186
    GiSetSubVectorFloat32V2(v, 0, GiLoadFloat32(Buffer));
    GiSetSubVectorFloat32V2(
            v, 1, GiLoadFloat32(Buffer + GI_SIMD_LEN_BYTE / sizeof(float)));
187 188 189 190 191

    return v;
#endif
}

192 193 194 195 196 197 198 199 200 201 202 203 204
GI_FORCEINLINE
GI_FLOAT32_t GiLoadFloat32LowHalf(const float* Buffer) {
#if defined(GI_NEON_INTRINSICS)
    return vcombine_f32(vld1_f32(Buffer), vdup_n_f32(0.f));
#elif defined(GI_SSE2_INTRINSICS)
    typedef __m64_128 float32x2_t;
    float32x2_t low, high;
    low.m64_f32[0] = Buffer[0];
    low.m64_f32[1] = Buffer[1];
    high.m64_f32[0] = 0;
    high.m64_f32[1] = 0;
    __m128i res = _mm_unpacklo_epi64(_pM128i(low), _pM128i(high));
    return _M128(res);
205 206
#elif defined(GI_RVV_INTRINSICS)
    return vle32_v_f32m1(Buffer, GI_SIMD_LEN_BYTE / sizeof(float) / 2);
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
#else
    GI_FLOAT32_t ret;
    memset(&ret, 0, sizeof(GI_FLOAT32_t));
    for (size_t i = 0; i < GI_SIMD_LEN_BYTE / sizeof(float) / 2; i++) {
        ret[i] = Buffer[i];
    }
    return ret;
#endif
}

GI_FORCEINLINE
GI_FLOAT32_t GiMlaqFloat32(GI_FLOAT32_t a, GI_FLOAT32_t b, GI_FLOAT32_t c) {
#if defined(GI_NEON_INTRINSICS)
#if defined(__ARM_FEATURE_FMA)
    return vfmaq_f32(a, b, c);
#else
    return vmlaq_f32(a, b, c);
#endif
#elif defined(GI_SSE2_INTRINSICS)
    // fma is coming soon, but right now:
    __m128 res;
    res = _mm_mul_ps(c, b);
    return _mm_add_ps(a, res);
230 231
#elif defined(GI_RVV_INTRINSICS)
    return vfmadd_vv_f32m1(b, c, a, GI_SIMD_LEN_BYTE / sizeof(float));
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
#else
    GI_FLOAT32_t ret;
    for (size_t i = 0; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) {
        ret[i] = a[i] + (b[i] * c[i]);
    }
    return ret;
#endif
}

GI_FORCEINLINE GI_FLOAT32_V2_t GiUzpqFloat32(GI_FLOAT32_t a, GI_FLOAT32_t b) {
#if defined(GI_NEON_INTRINSICS)
    return vuzpq_f32(a, b);
#elif defined(GI_SSE2_INTRINSICS)
    GI_FLOAT32_V2_t v32x4;
    v32x4.val[0] = _mm_shuffle_ps(a, b, _MM_SHUFFLE(2, 0, 2, 0));
    v32x4.val[1] = _mm_shuffle_ps(a, b, _MM_SHUFFLE(3, 1, 3, 1));
    return v32x4;
249 250 251 252 253 254 255 256
#elif defined(GI_RVV_INTRINSICS)
    //! may need optimize
    float tmp[GI_SIMD_LEN_BYTE / sizeof(float) * 2] = {0};
    vse32_v_f32m1(tmp, a, GI_SIMD_LEN_BYTE / sizeof(float));
    vse32_v_f32m1(
            tmp + GI_SIMD_LEN_BYTE / sizeof(float), b,
            GI_SIMD_LEN_BYTE / sizeof(float));
    return vlseg2e32_v_f32m1x2(tmp, GI_SIMD_LEN_BYTE / sizeof(float));
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
#else
    GI_FLOAT32_V2_t ret;
    ret.val[0][0] = a[0];
    ret.val[0][1] = a[2];
    ret.val[0][2] = b[0];
    ret.val[0][3] = b[2];
    ret.val[1][0] = a[1];
    ret.val[1][1] = a[3];
    ret.val[1][2] = b[1];
    ret.val[1][3] = b[3];
    return ret;
#endif
}

GI_FORCEINLINE float32x2_t GiDupFloat32(float a) {
#if defined(GI_NEON_INTRINSICS)
    return vdup_n_f32(a);
#elif defined(GI_SSE2_INTRINSICS)
    float32x2_t res;
    res.m64_f32[0] = a;
    res.m64_f32[1] = a;
    return res;
279 280
#elif defined(GI_RVV_INTRINSICS)
    return GiBroadcastFloat32(a);
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
#else
    float32x2_t res;
    res[0] = a;
    res[1] = a;
    return res;
#endif
}

GI_FORCEINLINE float32x2_t GiLdFloat32(float const* ptr) {
#if defined(GI_NEON_INTRINSICS)
    return vld1_f32(ptr);
#elif defined(GI_SSE2_INTRINSICS)
    float32x2_t res;
    res.m64_f32[0] = *(ptr);
    res.m64_f32[1] = *(ptr + 1);
    return res;
297 298
#elif defined(GI_RVV_INTRINSICS)
    return vle32_v_f32m1(ptr, 2);
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
#else
    float32x2_t res;
    res[0] = *(ptr);
    res[1] = *(ptr + 1);
    return res;
#endif
}

GI_FORCEINLINE float32x2_t GiAddDFloat32(float32x2_t a, float32x2_t b) {
#if defined(GI_NEON_INTRINSICS)
    return vadd_f32(a, b);
#elif defined(GI_SSE2_INTRINSICS)
    __m128 res;
    __m64_128 res64;
    res = _mm_add_ps(_pM128(a), _pM128(b));  // SSE, use only low 64 bits
    _M64f(res64, res);
    return res64;
316 317
#elif defined(GI_RVV_INTRINSICS)
    return vfadd_vv_f32m1(a, b, 2);
318 319 320 321 322 323 324 325 326 327 328 329 330 331
#else
    float32x2_t res;
    res[0] = a[0] + b[0];
    res[1] = a[1] + b[1];
    return res;
#endif
}

#if defined(GI_NEON_INTRINSICS)
#define GiGetLaneFloat32(v, lane) vget_lane_f32(v, lane)
#else
GI_FORCEINLINE float __gi_vget_lane_f32(float32x2_t v, const int lane) {
#if defined(GI_SSE2_INTRINSICS)
    return _sse_vget_lane_f32(v, lane);
332 333 334 335
#elif defined(GI_RVV_INTRINSICS)
    float ret[2];
    vse32_v_f32m1(ret, v, 2);
    return ret[lane];
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
#else
    return v[lane];
#endif
}
#define GiGetLaneFloat32(v, lane) __gi_vget_lane_f32(v, lane)
#endif

#if defined(GI_NEON_INTRINSICS)
#define GiSetLaneFloat32(value, vec, lane) vset_lane_f32(value, vec, lane)
#else
GI_FORCEINLINE float32x2_t
__gi_vset_lane_f32(float32_t value, float32x2_t vec, int lane) {
#if defined(GI_SSE2_INTRINSICS)
    float32x2_t res;
    res = vec;
    res.m64_f32[lane] = value;
    return res;
353 354 355 356 357
#elif defined(GI_RVV_INTRINSICS)
    float tmp[2];
    vse32_v_f32m1(tmp, vec, 2);
    tmp[lane] = value;
    return vle32_v_f32m1(tmp, 2);
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
#else
    float32x2_t res;
    res = vec;
    res[lane] = value;
    return res;
#endif
}
#define GiSetLaneFloat32(value, vec, lane) __gi_vset_lane_f32(value, vec, lane)
#endif

GI_FORCEINLINE void GiSt1Float32(float* ptr, float32x2_t val) {
#if defined(GI_NEON_INTRINSICS)
    return vst1_f32(ptr, val);
#elif defined(GI_SSE2_INTRINSICS)
    *(ptr) = val.m64_f32[0];
    *(ptr + 1) = val.m64_f32[1];
    return;
375 376
#elif defined(GI_RVV_INTRINSICS)
    return vse32_v_f32m1(ptr, val, 2);
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
#else
    *(ptr) = val[0];
    *(ptr + 1) = val[1];
    return;
#endif
}

GI_FORCEINLINE GI_FLOAT32_V2_t GiLd2qFloat32(const float* Buffer) {
#if defined(GI_NEON_INTRINSICS)
    return vld2q_f32(Buffer);
#elif defined(GI_SSE2_INTRINSICS)
    GI_FLOAT32_V2_t v;
    v.val[0] = GiLoadFloat32(Buffer);
    v.val[1] = GiLoadFloat32((Buffer + 4));
    v = GiUzpqFloat32(v.val[0], v.val[1]);
    return v;
393 394
#elif defined(GI_RVV_INTRINSICS)
    return vlseg2e32_v_f32m1x2(Buffer, GI_SIMD_LEN_BYTE / sizeof(float));
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
#else
    GI_FLOAT32_V2_t ret;
    ret.val[0][0] = Buffer[0];
    ret.val[0][1] = Buffer[2];
    ret.val[0][2] = Buffer[4];
    ret.val[0][3] = Buffer[6];
    ret.val[1][0] = Buffer[1];
    ret.val[1][1] = Buffer[3];
    ret.val[1][2] = Buffer[5];
    ret.val[1][3] = Buffer[7];
    return ret;
#endif
}

#if defined(GI_NEON_INTRINSICS)
#define GiExtqFloat32(a, b, n) vextq_f32(a, b, n)
#elif defined(GI_SSE2_INTRINSICS)
#define GiExtqFloat32(a, b, n) _M128(_sse_vextq_s32(_M128i(a), _M128i(b), n));
#else
GI_FORCEINLINE GI_FLOAT32_t
__naive_gi_vextq_f32(GI_FLOAT32_t a, GI_FLOAT32_t b, const int n) {
416 417 418 419 420 421 422 423 424 425
#if defined(GI_RVV_INTRINSICS)
    int t_count = GI_SIMD_LEN_BYTE / sizeof(float);
    int a_count = t_count - n;
    float tmp[GI_SIMD_LEN_BYTE / sizeof(float)];
    float tmp_a[GI_SIMD_LEN_BYTE / sizeof(float)];
    vse32_v_f32m1(tmp_a, a, GI_SIMD_LEN_BYTE / sizeof(float));
    memcpy(tmp, tmp_a + n, a_count * sizeof(float));
    vse32_v_f32m1(tmp + a_count, b, n);
    return vle32_v_f32m1(tmp, GI_SIMD_LEN_BYTE / sizeof(float));
#else
426 427 428 429 430 431 432 433 434 435
    GI_FLOAT32_t ret;
    int t_count = GI_SIMD_LEN_BYTE / sizeof(float);
    int a_count = t_count - n;
    for (int i = 0; i < a_count; i++) {
        ret[i] = a[i + n];
    }
    for (int i = 0; i < n; i++) {
        ret[i + a_count] = b[i];
    }
    return ret;
436
#endif
437 438 439 440 441 442 443 444 445 446 447
}
#define GiExtqFloat32(a, b, n) __naive_gi_vextq_f32(a, b, n)
#endif

GI_FORCEINLINE
GI_FLOAT32_t GiMultiplySubFloat32(
        GI_FLOAT32_t VectorSum, GI_FLOAT32_t Vector1, GI_FLOAT32_t Vector2) {
#if defined(GI_NEON_INTRINSICS)
    return vmlsq_f32(VectorSum, Vector1, Vector2);
#elif defined(GI_SSE2_INTRINSICS)
    return _mm_sub_ps(VectorSum, _mm_mul_ps(Vector1, Vector2));
448 449 450
#elif defined(GI_RVV_INTRINSICS)
    return vfnmsub_vv_f32m1(
            Vector1, Vector2, VectorSum, GI_SIMD_LEN_BYTE / sizeof(float));
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
#else
    GI_FLOAT32_t ret;
    for (size_t i = 0; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) {
        ret[i] = VectorSum[i] - Vector1[i] * Vector2[i];
    }

    return ret;
#endif
}

#if defined(GI_SSE2_INTRINSICS)
GI_FORCEINLINE GI_FLOAT32_t
_MM_INSERT_PS(GI_FLOAT32_t vec, GI_FLOAT32_t p, const int LANE) {
    _GI_ALIGN_16 uint32_t mask[4] = {0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff};
    __m128 tmp, vec_masked, p_masked;
    mask[LANE >> 4] = 0x0;
    vec_masked = _mm_and_ps(*(__m128*)mask, vec);
    p_masked = _mm_andnot_ps(*(__m128*)mask, p);
    tmp = _mm_or_ps(vec_masked, p_masked);
    return tmp;
}

GI_FORCEINLINE float32x2_t sse_vget_high_f32(GI_FLOAT32_t a) {
    __m128i res;
    __m64_128 res64;
    res = _mm_unpackhi_epi64(_M128i(a), _M128i(a));
    return64(res);
}

GI_FORCEINLINE float32x2_t sse_vget_low_f32(GI_FLOAT32_t a) {
    float32x2_t res64;
    _M64f(res64, a);
    return res64;
}

GI_FORCEINLINE GI_FLOAT32_t
sse_vmlaq_lane_f32(GI_FLOAT32_t a, GI_FLOAT32_t b, float32x2_t v, int l) {
    float32_t vlane;
    GI_FLOAT32_t c;
    vlane = _sse_vget_lane_f32(v, l);
    c = _mm_set1_ps(vlane);
    return GiMlaqFloat32(a, b, c);
}

GI_FORCEINLINE int _MM_EXTRACT_PS(__m128 vec, const int LANE) {
    _GI_ALIGN_16 int32_t tmp[4];
    _mm_store_si128((__m128i*)tmp, _M128i(vec));
    return tmp[LANE];
}

GI_FORCEINLINE float32_t sse_vgetq_lane_f32(GI_FLOAT32_t vec, int lane) {
    float32_t floatVal;
    char* const floatVal_c = (char*)&floatVal;
    *((int32_t*)floatVal_c) = _MM_EXTRACT_PS(vec, lane);
    return floatVal;
}

GI_FORCEINLINE GI_FLOAT32_t
sse_vmlsq_lane_f32(GI_FLOAT32_t a, GI_FLOAT32_t b, float32x2_t v, int l) {
    float32_t vlane;
    GI_FLOAT32_t c;
    vlane = (float)GiGetLaneFloat32(v, l);
    c = GiBroadcastFloat32(vlane);
    return GiMultiplySubFloat32(a, b, c);
}

#endif

#if defined(GI_NEON_INTRINSICS)
#define GiLd1qLaneFloat32(Buffer, src, n) vld1q_lane_f32(Buffer, src, n)
#else
GI_FORCEINLINE GI_FLOAT32_t
__gi_vld1q_lane_f32(const float* Buffer, GI_FLOAT32_t src, const int n) {
#if defined(GI_SSE2_INTRINSICS)
    GI_FLOAT32_t p;
    p = _mm_set1_ps(*(Buffer));
    return _MM_INSERT_PS(src, p, _INSERTPS_NDX(0, n));
528 529 530 531 532 533
#elif defined(GI_RVV_INTRINSICS)
    //! mask will use more instruct
    float tmp[GI_SIMD_LEN_BYTE / sizeof(float)];
    vse32_v_f32m1(tmp, src, GI_SIMD_LEN_BYTE / sizeof(float));
    tmp[n] = *Buffer;
    return vle32_v_f32m1(tmp, GI_SIMD_LEN_BYTE / sizeof(float));
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
#else
    GI_FLOAT32_t ret;
    memcpy(&ret, &src, sizeof(GI_FLOAT32_t));
    ret[n] = *Buffer;
    return ret;
#endif
}
#define GiLd1qLaneFloat32(Buffer, src, n) __gi_vld1q_lane_f32(Buffer, src, n)
#endif

#if defined(GI_NEON_INTRINSICS)
#define GiSetqLaneFloat32(value, vec, lane) vsetq_lane_f32(value, vec, lane)
#else
GI_FORCEINLINE GI_FLOAT32_t
__gi_vsetq_lane_f32(float value, GI_FLOAT32_t vec, const int lane) {
    float val = value;
    return GiLd1qLaneFloat32(&val, vec, lane);
}
#define GiSetqLaneFloat32(value, vec, lane) __gi_vsetq_lane_f32(value, vec, lane)
#endif

#if defined(GI_NEON_INTRINSICS)
#define GiMlaqLaneFloat32HighHalf(a, b, v, lane) \
    vmlaq_lane_f32(a, b, vget_high_f32(v), lane)
#elif defined(GI_SSE2_INTRINSICS)
#define GiMlaqLaneFloat32HighHalf(a, b, v, lane) \
    sse_vmlaq_lane_f32(a, b, sse_vget_high_f32(v), lane)
#else
GI_FORCEINLINE GI_FLOAT32_t __naive_gi_vmlaq_lane_f32_high_half(
        GI_FLOAT32_t a, GI_FLOAT32_t b, GI_FLOAT32_t v, const int lane) {
564 565 566 567 568 569 570 571
#if defined(GI_RVV_INTRINSICS)
    float tmp[GI_SIMD_LEN_BYTE / sizeof(float)];
    vse32_v_f32m1(tmp, v, GI_SIMD_LEN_BYTE / sizeof(float));

    return vfmadd_vf_f32m1(
            b, tmp[lane + GI_SIMD_LEN_BYTE / sizeof(float) / 2], a,
            GI_SIMD_LEN_BYTE / sizeof(float));
#else
572 573
    GI_FLOAT32_t ret;
    for (size_t i = 0; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) {
574
        ret[i] = a[i] + (b[i] * v[lane + GI_SIMD_LEN_BYTE / sizeof(float) / 2]);
575 576
    }
    return ret;
577
#endif
578 579 580 581 582 583 584 585 586 587 588 589 590 591
}
#define GiMlaqLaneFloat32HighHalf(a, b, v, lane) \
    __naive_gi_vmlaq_lane_f32_high_half(a, b, v, lane)
#endif

#if defined(GI_NEON_INTRINSICS)
#define GiVmlaqLaneFloat32LowHalf(a, b, v, lane) \
    vmlaq_lane_f32(a, b, vget_low_f32(v), lane)
#elif defined(GI_SSE2_INTRINSICS)
#define GiVmlaqLaneFloat32LowHalf(a, b, v, lane) \
    sse_vmlaq_lane_f32(a, b, sse_vget_low_f32(v), lane)
#else
GI_FORCEINLINE GI_FLOAT32_t __naive_gi_vmlaq_lane_f32_low_half(
        GI_FLOAT32_t a, GI_FLOAT32_t b, GI_FLOAT32_t v, const int lane) {
592 593 594 595 596 597
#if defined(GI_RVV_INTRINSICS)
    float tmp[GI_SIMD_LEN_BYTE / sizeof(float) / 2];
    vse32_v_f32m1(tmp, v, GI_SIMD_LEN_BYTE / sizeof(float) / 2);

    return vfmadd_vf_f32m1(b, tmp[lane], a, GI_SIMD_LEN_BYTE / sizeof(float));
#else
598 599 600 601 602
    GI_FLOAT32_t ret;
    for (size_t i = 0; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) {
        ret[i] = a[i] + (b[i] * v[lane]);
    }
    return ret;
603
#endif
604 605 606 607 608
}
#define GiVmlaqLaneFloat32LowHalf(a, b, v, lane) \
    __naive_gi_vmlaq_lane_f32_low_half(a, b, v, lane)
#endif

609
GI_FORCEINLINE
610
void GiStoreFloat32(float* Buffer, GI_FLOAT32_t Vector) {
611 612 613 614
#if defined(GI_NEON_INTRINSICS)
    vst1q_f32(Buffer, Vector);
#elif defined(GI_SSE2_INTRINSICS)
    _mm_storeu_ps(Buffer, Vector);
615 616
#elif defined(GI_RVV_INTRINSICS)
    vse32_v_f32m1(Buffer, Vector, GI_SIMD_LEN_BYTE / sizeof(float));
617 618 619 620 621 622 623
#else
    for (size_t i = 0; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) {
        Buffer[i] = Vector[i];
    }
#endif
}

624 625 626 627 628
GI_FORCEINLINE
void GiStoreFloat32V2(float* Buffer, GI_FLOAT32_V2_t Vector) {
#if defined(GI_NEON_INTRINSICS)
    vst1q_f32_x2(Buffer, Vector);
#else
629 630 631 632
    GiStoreFloat32(Buffer, GiGetSubVectorFloat32V2(Vector, 0));
    GiStoreFloat32(
            Buffer + GI_SIMD_LEN_BYTE / sizeof(float),
            GiGetSubVectorFloat32V2(Vector, 1));
633 634 635
#endif
}

636
#if defined(GI_NEON_INTRINSICS)
637 638 639
#define GISTORELANEFLOAT32(i)                                                         \
    GI_FORCEINLINE void GiStoreLane##i##Float32(float* Buffer, GI_FLOAT32_t Vector) { \
        vst1q_lane_f32(Buffer, Vector, i);                                            \
640 641 642 643 644
    }

#elif defined(GI_SSE2_INTRINSICS)

#define GISTORELANEFLOAT32(i)                                                          \
645
    GI_FORCEINLINE void GiStoreLane##i##Float32(float* Buffer, GI_FLOAT32_t Vector) {  \
646 647
        _mm_store_ss(Buffer, _mm_shuffle_ps(Vector, Vector, _MM_SHUFFLE(i, i, i, i))); \
    }
648 649 650 651 652 653 654 655
#elif defined(GI_RVV_INTRINSICS)

#define GISTORELANEFLOAT32(i)                                                         \
    GI_FORCEINLINE void GiStoreLane##i##Float32(float* Buffer, GI_FLOAT32_t Vector) { \
        float tmp[GI_SIMD_LEN_BYTE / sizeof(float)];                                  \
        vse32_v_f32m1(tmp, Vector, GI_SIMD_LEN_BYTE / sizeof(float));                 \
        *Buffer = tmp[i];                                                             \
    }
656
#else
657 658 659
#define GISTORELANEFLOAT32(i)                                                         \
    GI_FORCEINLINE void GiStoreLane##i##Float32(float* Buffer, GI_FLOAT32_t Vector) { \
        *Buffer = Vector[i];                                                          \
660 661 662 663 664 665 666 667 668 669 670
    }
#endif

GISTORELANEFLOAT32(0)
GISTORELANEFLOAT32(1)
GISTORELANEFLOAT32(2)
GISTORELANEFLOAT32(3)

#undef GISTORELANEFLOAT32

#if defined(GI_NEON_INTRINSICS)
671 672 673
#define GIEXTRACTLANEFLOAT32(i)                                           \
    GI_FORCEINLINE float GiExtractLane##i##Float32(GI_FLOAT32_t Vector) { \
        return vgetq_lane_f32(Vector, i);                                 \
674 675 676 677
    }
#elif defined(GI_SSE2_INTRINSICS)

#define GIEXTRACTLANEFLOAT32(i)                                                        \
678
    GI_FORCEINLINE float GiExtractLane##i##Float32(GI_FLOAT32_t Vector) {              \
679 680
        return _mm_cvtss_f32(_mm_shuffle_ps(Vector, Vector, _MM_SHUFFLE(i, i, i, i))); \
    }
681 682 683 684 685 686 687 688
#elif defined(GI_RVV_INTRINSICS)

#define GIEXTRACTLANEFLOAT32(i)                                           \
    GI_FORCEINLINE float GiExtractLane##i##Float32(GI_FLOAT32_t Vector) { \
        float tmp[GI_SIMD_LEN_BYTE / sizeof(float)];                      \
        vse32_v_f32m1(tmp, Vector, GI_SIMD_LEN_BYTE / sizeof(float));     \
        return tmp[i];                                                    \
    }
689
#else
690 691 692
#define GIEXTRACTLANEFLOAT32(i)                                           \
    GI_FORCEINLINE float GiExtractLane##i##Float32(GI_FLOAT32_t Vector) { \
        return Vector[i];                                                 \
693 694 695 696 697 698 699 700 701
    }
#endif

GIEXTRACTLANEFLOAT32(0)
GIEXTRACTLANEFLOAT32(1)
GIEXTRACTLANEFLOAT32(2)
GIEXTRACTLANEFLOAT32(3)
#undef GIEXTRACTLANEFLOAT32

702 703 704 705 706 707 708 709 710
GI_FORCEINLINE
GI_FLOAT32_V2_t GiZipqFloat32(GI_FLOAT32_t Vector1, GI_FLOAT32_t Vector2) {
#if defined(GI_NEON_INTRINSICS)
    return vzipq_f32(Vector1, Vector2);
#elif defined(GI_SSE2_INTRINSICS)
    GI_FLOAT32_V2_t f32x4;
    f32x4.val[0] = _mm_unpacklo_ps(Vector1, Vector2);
    f32x4.val[1] = _mm_unpackhi_ps(Vector1, Vector2);
    return f32x4;
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
#elif defined(GI_RVV_INTRINSICS)
    vfloat32m2_t d = vundefined_f32m2();
    d = vset_v_f32m1_f32m2(d, 0, Vector1);
    d = vset_v_f32m1_f32m2(d, 1, Vector2);
    vuint32m2_t index;
#if GI_SIMD_LEN_BYTE == 16
    uint32_t index_128[8] = {0, 4, 1, 5, 2, 6, 3, 7};
    index = vle32_v_u32m2(index_128, 8);
#else
    uint32_t* index_p = (uint32_t*)&index;
    for (size_t i = 0; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) {
        index_p[2 * i] = i;
        index_p[2 * i + 1] = i + GI_SIMD_LEN_BYTE / sizeof(float);
    }
#endif
    vfloat32m2_t g_d =
            vrgather_vv_f32m2(d, index, GI_SIMD_LEN_BYTE / sizeof(float) * 2);
    vfloat32m1_t v0 = vget_v_f32m2_f32m1(g_d, 0);
    vfloat32m1_t v1 = vget_v_f32m2_f32m1(g_d, 1);
    return vcreate_f32m1x2(v0, v1);
731 732 733 734 735 736 737 738 739 740 741 742 743 744
#else
    GI_FLOAT32_V2_t ret;
    ret.val[0][0] = Vector1[0];
    ret.val[0][1] = Vector2[0];
    ret.val[0][2] = Vector1[1];
    ret.val[0][3] = Vector2[1];
    ret.val[1][0] = Vector1[2];
    ret.val[1][1] = Vector2[2];
    ret.val[1][2] = Vector1[3];
    ret.val[1][3] = Vector2[3];
    return ret;
#endif
}

745 746 747 748 749 750
GI_FORCEINLINE
void GiStoreZipFloat32V2(float* Buffer, GI_FLOAT32_V2_t Vector) {
#if defined(GI_NEON_INTRINSICS)
    vst2q_f32(Buffer, Vector);
#else
    GI_FLOAT32_V2_t tmp;
751 752 753 754 755
    tmp = GiZipqFloat32(
            GiGetSubVectorFloat32V2(Vector, 0), GiGetSubVectorFloat32V2(Vector, 1));
    GiStoreFloat32(Buffer, GiGetSubVectorFloat32V2(tmp, 0));
    GiStoreFloat32(
            Buffer + GI_SIMD_LEN_BYTE / sizeof(float), GiGetSubVectorFloat32V2(tmp, 1));
756 757 758
#endif
}

759
GI_FORCEINLINE
760
GI_FLOAT32_t GiInterleaveLowFloat32(GI_FLOAT32_t Vector1, GI_FLOAT32_t Vector2) {
761 762 763
#if defined(GI_NEON64_INTRINSICS)
    return vzip1q_f32(Vector1, Vector2);
#elif defined(GI_NEON32_INTRINSICS)
764
    float32x4x2_t zipped = vzipq_f32(Vector1, Vector2);
765 766 767
    return zipped.val[0];
#elif defined(GI_SSE2_INTRINSICS)
    return _mm_unpacklo_ps(Vector1, Vector2);
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
#elif defined(GI_RVV_INTRINSICS)
    vfloat32m2_t d = vundefined_f32m2();
    d = vset_v_f32m1_f32m2(d, 0, Vector1);
    d = vset_v_f32m1_f32m2(d, 1, Vector2);
    vuint32m2_t index;
#if GI_SIMD_LEN_BYTE == 16
    uint32_t index_128[4] = {0, 4, 1, 5};
    index = vle32_v_u32m2(index_128, 4);
#else
    uint32_t* index_p = (uint32_t*)&index;
    for (size_t i = 0; i < GI_SIMD_LEN_BYTE / sizeof(float) / 2; i++) {
        index_p[2 * i] = i;
        index_p[2 * i + 1] = i + GI_SIMD_LEN_BYTE / sizeof(float);
    }
#endif
    vfloat32m2_t g_d =
            vrgather_vv_f32m2(d, index, GI_SIMD_LEN_BYTE / sizeof(float) * 2);
    return vget_v_f32m2_f32m1(g_d, 0);
786
#else
787
    GI_FLOAT32_t ret;
788 789 790 791 792 793 794 795 796
    for (size_t i = 0; i < GI_SIMD_LEN_BYTE / 2 / sizeof(float); i++) {
        ret[2 * i] = Vector1[i];
        ret[2 * i + 1] = Vector2[i];
    }
    return ret;
#endif
}

GI_FORCEINLINE
797
GI_FLOAT32_t GiInterleaveHighFloat32(GI_FLOAT32_t Vector1, GI_FLOAT32_t Vector2) {
798 799 800
#if defined(GI_NEON64_INTRINSICS)
    return vzip2q_f32(Vector1, Vector2);
#elif defined(GI_NEON32_INTRINSICS)
801
    float32x4x2_t zipped = vzipq_f32(Vector1, Vector2);
802 803 804
    return zipped.val[1];
#elif defined(GI_SSE2_INTRINSICS)
    return _mm_unpackhi_ps(Vector1, Vector2);
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
#elif defined(GI_RVV_INTRINSICS)
    vfloat32m2_t d = vundefined_f32m2();
    d = vset_v_f32m1_f32m2(d, 0, Vector1);
    d = vset_v_f32m1_f32m2(d, 1, Vector2);
    vuint32m2_t index;
#if GI_SIMD_LEN_BYTE == 16
    uint32_t index_128[8] = {0, 4, 1, 5, 2, 6, 3, 7};
    index = vle32_v_u32m2(index_128, 8);
#else
    uint32_t* index_p = (uint32_t*)&index;
    for (size_t i = 0; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) {
        index_p[2 * i] = i;
        index_p[2 * i + 1] = i + GI_SIMD_LEN_BYTE / sizeof(float);
    }
#endif
    vfloat32m2_t g_d =
            vrgather_vv_f32m2(d, index, GI_SIMD_LEN_BYTE / sizeof(float) * 2);
    return vget_v_f32m2_f32m1(g_d, 1);
823
#else
824
    GI_FLOAT32_t ret;
825
    for (size_t i = 0; i < GI_SIMD_LEN_BYTE / 2 / sizeof(float); i++) {
826 827
        ret[2 * i] = Vector1[GI_SIMD_LEN_BYTE / 2 / sizeof(float) + i];
        ret[2 * i + 1] = Vector2[GI_SIMD_LEN_BYTE / 2 / sizeof(float) + i];
828 829 830 831 832 833
    }
    return ret;
#endif
}

GI_FORCEINLINE
834
GI_FLOAT32_t GiAddFloat32(GI_FLOAT32_t Vector1, GI_FLOAT32_t Vector2) {
835 836 837 838
#if defined(GI_NEON_INTRINSICS)
    return vaddq_f32(Vector1, Vector2);
#elif defined(GI_SSE2_INTRINSICS)
    return _mm_add_ps(Vector1, Vector2);
839 840
#elif defined(GI_RVV_INTRINSICS)
    return vfadd_vv_f32m1(Vector1, Vector2, GI_SIMD_LEN_BYTE / sizeof(float));
841 842 843 844 845 846
#else
    return Vector1 + Vector2;
#endif
}

GI_FORCEINLINE
847
GI_FLOAT32_t GiSubtractFloat32(GI_FLOAT32_t Vector1, GI_FLOAT32_t Vector2) {
848 849 850 851
#if defined(GI_NEON_INTRINSICS)
    return vsubq_f32(Vector1, Vector2);
#elif defined(GI_SSE2_INTRINSICS)
    return _mm_sub_ps(Vector1, Vector2);
852 853
#elif defined(GI_RVV_INTRINSICS)
    return vfsub_vv_f32m1(Vector1, Vector2, GI_SIMD_LEN_BYTE / sizeof(float));
854 855 856 857 858 859
#else
    return Vector1 - Vector2;
#endif
}

GI_FORCEINLINE
860
GI_FLOAT32_t GiMultiplyFloat32(GI_FLOAT32_t Vector1, GI_FLOAT32_t Vector2) {
861 862 863 864
#if defined(GI_NEON_INTRINSICS)
    return vmulq_f32(Vector1, Vector2);
#elif defined(GI_SSE2_INTRINSICS)
    return _mm_mul_ps(Vector1, Vector2);
865 866
#elif defined(GI_RVV_INTRINSICS)
    return vfmul_vv_f32m1(Vector1, Vector2, GI_SIMD_LEN_BYTE / sizeof(float));
867 868 869 870 871 872
#else
    return Vector1 * Vector2;
#endif
}

GI_FORCEINLINE
873
GI_FLOAT32_t GiMultiplyScalerFloat32(GI_FLOAT32_t Vector1, float Scaler) {
874 875 876
#if defined(GI_NEON_INTRINSICS)
    return vmulq_n_f32(Vector1, Scaler);
#elif defined(GI_SSE2_INTRINSICS)
877
    GI_FLOAT32_t Vector2 = _mm_set1_ps(Scaler);
878
    return _mm_mul_ps(Vector1, Vector2);
879 880
#elif defined(GI_RVV_INTRINSICS)
    return vfmul_vf_f32m1(Vector1, Scaler, GI_SIMD_LEN_BYTE / sizeof(float));
881 882 883 884 885 886
#else
    return Vector1 * Scaler;
#endif
}

GI_FORCEINLINE
887 888
GI_FLOAT32_t GiMultiplyAddFloat32(
        GI_FLOAT32_t VectorSum, GI_FLOAT32_t Vector1, GI_FLOAT32_t Vector2) {
889
#if defined(GI_NEON_INTRINSICS)
890
    return v_fma_ps_f32(VectorSum, Vector1, Vector2);
891 892 893 894
#elif defined(GI_FMA3_INTRINSICS)
    return _mm_fmadd_ps(Vector1, Vector2, VectorSum);
#elif defined(GI_SSE2_INTRINSICS)
    return _mm_add_ps(_mm_mul_ps(Vector1, Vector2), VectorSum);
895 896 897
#elif defined(GI_RVV_INTRINSICS)
    return vfmadd_vv_f32m1(
            Vector1, Vector2, VectorSum, GI_SIMD_LEN_BYTE / sizeof(float));
898 899 900 901 902
#else
    return Vector1 * Vector2 + VectorSum;
#endif
}

903 904 905 906
GI_FORCEINLINE
GI_FLOAT32_t GiMultiplyAddScalarFloat32(
        GI_FLOAT32_t VectorSum, GI_FLOAT32_t Vector, float Scalar) {
#if defined(GI_NEON_INTRINSICS)
907
    return v_fma_n_f32(VectorSum, Vector, Scalar);
908
#elif defined(GI_SSE2_INTRINSICS)
909
    return GiMultiplyAddFloat32(VectorSum, GiBroadcastFloat32(Scalar), Vector);
910 911
#elif defined(GI_RVV_INTRINSICS)
    return vfmadd_vf_f32m1(Vector, Scalar, VectorSum, GI_SIMD_LEN_BYTE / sizeof(float));
912 913 914 915 916 917
#else
    return VectorSum + Vector * Scalar;
#endif
}

#if defined(GI_NEON_INTRINSICS)
918 919 920
#define GIMULTIPLYADDLANFLOAT32(i)                                                \
    GI_FORCEINLINE GI_FLOAT32_t GiMultiplyAddLan##i##Float32(                     \
            GI_FLOAT32_t VectorSum, GI_FLOAT32_t Vector1, GI_FLOAT32_t Vector2) { \
921
        return v_fma_lane_f32(VectorSum, Vector1, vget_low_f32(Vector2), i);      \
922 923 924 925 926
    }
GIMULTIPLYADDLANFLOAT32(0)
GIMULTIPLYADDLANFLOAT32(1)
#undef GIMULTIPLYADDLANFLOAT32
#define GIMULTIPLYADDLANFLOAT32(i)                                                \
927 928
    GI_FORCEINLINE GI_FLOAT32_t GiMultiplyAddLan##i##Float32(                     \
            GI_FLOAT32_t VectorSum, GI_FLOAT32_t Vector1, GI_FLOAT32_t Vector2) { \
929
        return v_fma_lane_f32(VectorSum, Vector1, vget_high_f32(Vector2), i - 2); \
930 931 932
    }
GIMULTIPLYADDLANFLOAT32(2)
GIMULTIPLYADDLANFLOAT32(3)
933
#undef GIMULTIPLYADDLANFLOAT32
934
#else
935

936 937 938 939 940
#define GIMULTIPLYADDLANFLOAT32(i)                                                \
    GI_FORCEINLINE GI_FLOAT32_t GiMultiplyAddLan##i##Float32(                     \
            GI_FLOAT32_t VectorSum, GI_FLOAT32_t Vector1, GI_FLOAT32_t Vector2) { \
        return GiMultiplyAddScalarFloat32(                                        \
                VectorSum, Vector1, GiExtractLane##i##Float32(Vector2));          \
941 942 943 944 945 946 947 948 949
    }
GIMULTIPLYADDLANFLOAT32(0)
GIMULTIPLYADDLANFLOAT32(1)
GIMULTIPLYADDLANFLOAT32(2)
GIMULTIPLYADDLANFLOAT32(3)
#undef GIMULTIPLYADDLANFLOAT32
#endif

GI_FORCEINLINE
950
GI_FLOAT32_t GiDivideFloat32(GI_FLOAT32_t Vector1, GI_FLOAT32_t Vector2) {
951 952 953 954 955 956 957 958
#if defined(GI_NEON64_INTRINSICS)
    return vdivq_f32(Vector1, Vector2);
#elif defined(GI_NEON32_INTRINSICS)
    float32x4_t recp = vrecpeq_f32(Vector2);
    recp = vmulq_f32(vrecpsq_f32(Vector2, recp), recp);
    return vmulq_f32(Vector1, recp);
#elif defined(GI_SSE2_INTRINSICS)
    return _mm_div_ps(Vector1, Vector2);
959 960
#elif defined(GI_RVV_INTRINSICS)
    return vfdiv_vv_f32m1(Vector1, Vector2, GI_SIMD_LEN_BYTE / sizeof(float));
961 962 963 964 965 966
#else
    return Vector1 / Vector2;
#endif
}

GI_FORCEINLINE
967 968 969 970 971 972
GI_FLOAT32_t GiRecpeSFloat32(GI_FLOAT32_t Vector1, GI_FLOAT32_t Vector2) {
#if defined(GI_NEON64_INTRINSICS)
    return vrecpsq_f32(Vector1, Vector2);
#elif defined(GI_SSE2_INTRINSICS)
    GI_FLOAT32_t two = _mm_set1_ps(2.0f);
    return _mm_sub_ps(two, _mm_mul_ps(Vector1, Vector2));
973 974 975
#elif defined(GI_RVV_INTRINSICS)
    GI_FLOAT32_t two = GiBroadcastFloat32(2.0f);
    return vfnmsub_vv_f32m1(Vector1, Vector2, two, GI_SIMD_LEN_BYTE / sizeof(float));
976 977 978 979 980 981 982 983 984 985 986 987
#else
    return (2.0f - Vector1 * Vector2);
#endif
}

GI_FORCEINLINE
GI_FLOAT32_t GiRecpeFloat32(GI_FLOAT32_t Vector) {
#if defined(GI_NEON32_INTRINSICS)
    return vrecpeq_f32(Vector);
#elif defined(GI_SSE2_INTRINSICS)
    GI_FLOAT32_t ones = _mm_set1_ps(1.0f);
    return _mm_div_ps(ones, Vector);
988 989 990
#elif defined(GI_RVV_INTRINSICS)
    GI_FLOAT32_t ones = GiBroadcastFloat32(1.0f);
    return vfdiv_vv_f32m1(ones, Vector, GI_SIMD_LEN_BYTE / sizeof(float));
991
#else
992
    //! FIXME: neon or sse always have low accuracy than 1/x
993 994 995 996 997 998 999 1000 1001 1002 1003
    return 1 / Vector;
#endif
}

GI_FORCEINLINE
GI_FLOAT32_t GiNegFloat32(GI_FLOAT32_t Vector) {
#if defined(GI_NEON32_INTRINSICS)
    return vnegq_f32(Vector);
#elif defined(GI_SSE2_INTRINSICS)
    GI_FLOAT32_t zero = _mm_set1_ps(0.0f);
    return _mm_sub_ps(zero, Vector);
1004 1005
#elif defined(GI_RVV_INTRINSICS)
    return vfneg_v_f32m1(Vector, GI_SIMD_LEN_BYTE / sizeof(float));
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
#else
    return -Vector;
#endif
}

GI_FORCEINLINE
GI_UINT32_t GiGreaterThanFloat32(GI_FLOAT32_t Vector1, GI_FLOAT32_t Vector2) {
#if defined(GI_NEON_INTRINSICS)
    return vcgtq_f32(Vector1, Vector2);
#elif defined(GI_SSE2_INTRINSICS)
    return _mm_castps_si128(_mm_cmpgt_ps(Vector1, Vector2));
1017 1018 1019 1020 1021 1022
#elif defined(GI_RVV_INTRINSICS)
    vbool32_t b =
            vmfgt_vv_f32m1_b32(Vector1, Vector2, GI_SIMD_LEN_BYTE / sizeof(float));
    GI_UINT32_t ret;
    memcpy(&ret, &b, GI_SIMD_LEN_BYTE);
    return vneg_v_u32m1(ret, GI_SIMD_LEN_BYTE / sizeof(float));
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
#else
    GI_UINT32_t ret;
    for (size_t i = 0; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) {
        ret[i] = Vector1[i] > Vector2[i] ? 0xFFFFFFFF : 0;
    }
    return ret;
#endif
}

GI_FORCEINLINE
GI_UINT32_t GiLessThanEqFloat32(GI_FLOAT32_t Vector1, GI_FLOAT32_t Vector2) {
1034
#if defined(GI_NEON_INTRINSICS)
1035
    return vcleq_f32(Vector1, Vector2);
1036
#elif defined(GI_SSE2_INTRINSICS)
1037
    return _mm_castps_si128(_mm_cmple_ps(Vector1, Vector2));
1038 1039 1040 1041 1042 1043
#elif defined(GI_RVV_INTRINSICS)
    vbool32_t b =
            vmfle_vv_f32m1_b32(Vector1, Vector2, GI_SIMD_LEN_BYTE / sizeof(float));
    GI_UINT32_t ret;
    memcpy(&ret, &b, GI_SIMD_LEN_BYTE);
    return vneg_v_u32m1(ret, GI_SIMD_LEN_BYTE / sizeof(float));
1044
#else
1045 1046 1047 1048 1049
    GI_UINT32_t ret;
    for (size_t i = 0; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) {
        ret[i] = Vector1[i] <= Vector2[i] ? 0xFFFFFFFF : 0;
    }
    return ret;
1050 1051 1052 1053
#endif
}

GI_FORCEINLINE
1054 1055 1056 1057 1058
GI_UINT32_t GiLessThanFloat32(GI_FLOAT32_t Vector1, GI_FLOAT32_t Vector2) {
#if defined(GI_NEON_INTRINSICS)
    return vcltq_f32(Vector1, Vector2);
#elif defined(GI_SSE2_INTRINSICS)
    return _mm_castps_si128(_mm_cmplt_ps(Vector1, Vector2));
1059 1060 1061 1062 1063 1064
#elif defined(GI_RVV_INTRINSICS)
    vbool32_t b =
            vmflt_vv_f32m1_b32(Vector1, Vector2, GI_SIMD_LEN_BYTE / sizeof(float));
    GI_UINT32_t ret;
    memcpy(&ret, &b, GI_SIMD_LEN_BYTE);
    return vneg_v_u32m1(ret, GI_SIMD_LEN_BYTE / sizeof(float));
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
#else
    GI_UINT32_t ret;
    for (size_t i = 0; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) {
        ret[i] = Vector1[i] < Vector2[i] ? 0xFFFFFFFF : 0;
    }
    return ret;
#endif
}

GI_FORCEINLINE
GI_FLOAT32_t GiAndFloat32(GI_FLOAT32_t Vector1, GI_FLOAT32_t Vector2) {
1076 1077 1078
#if defined(GI_SSE2_INTRINSICS)
    return _mm_and_ps(Vector1, Vector2);
#else
1079
    return GiReintInt32ToFloat32(
1080 1081 1082 1083 1084
            GiAndInt32(GiReinterpretAsInt32(Vector1), GiReinterpretAsInt32(Vector2)));
#endif
}

GI_FORCEINLINE
1085
GI_FLOAT32_t GiOrFloat32(GI_FLOAT32_t Vector1, GI_FLOAT32_t Vector2) {
1086 1087 1088
#if defined(GI_SSE2_INTRINSICS)
    return _mm_or_ps(Vector1, Vector2);
#else
1089
    return GiReintInt32ToFloat32(
1090 1091 1092 1093 1094
            GiOrInt32(GiReinterpretAsInt32(Vector1), GiReinterpretAsInt32(Vector2)));
#endif
}

GI_FORCEINLINE
1095
GI_FLOAT32_t GiAndNotFloat32(GI_FLOAT32_t VectorNot, GI_FLOAT32_t Vector) {
1096 1097 1098
#if defined(GI_SSE2_INTRINSICS)
    return _mm_andnot_ps(VectorNot, Vector);
#else
1099
    return GiReintInt32ToFloat32(GiAndNotInt32(
1100 1101 1102 1103 1104
            GiReinterpretAsInt32(VectorNot), GiReinterpretAsInt32(Vector)));
#endif
}

GI_FORCEINLINE
1105
GI_FLOAT32_t GiXorFloat32(GI_FLOAT32_t Vector1, GI_FLOAT32_t Vector2) {
1106 1107 1108
#if defined(GI_SSE2_INTRINSICS)
    return _mm_xor_ps(Vector1, Vector2);
#else
1109
    return GiReintInt32ToFloat32(
1110 1111 1112 1113 1114
            GiXorInt32(GiReinterpretAsInt32(Vector1), GiReinterpretAsInt32(Vector2)));
#endif
}

GI_FORCEINLINE
1115 1116
GI_FLOAT32_t GiBlendFloat32(
        GI_FLOAT32_t Vector1, GI_FLOAT32_t Vector2, GI_FLOAT32_t Selection) {
1117
    return GiOrFloat32(
1118
            GiAndFloat32(Vector1, Selection), GiAndNotFloat32(Selection, Vector2));
1119 1120
}

1121 1122 1123
#define MIN_NAN(a, b) (isnan(a) || (a) < (b)) ? (a) : (b);
#define MAX_NAN(a, b) (isnan(a) || (a) > (b)) ? (a) : (b);

1124
GI_FORCEINLINE
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
GI_FLOAT32_t GiBSLFloat32(
        GI_UINT32_t Selection, GI_FLOAT32_t Vector1, GI_FLOAT32_t Vector2) {
#if defined(GI_NEON_INTRINSICS)
    return vbslq_f32(Selection, Vector1, Vector2);
#else
    return GiBlendFloat32(Vector1, Vector2, GiReintUint32ToFloat32(Selection));
#endif
}

GI_FORCEINLINE
GI_FLOAT32_t GiMaximumFloat32(GI_FLOAT32_t Vector1, GI_FLOAT32_t Vector2) {
#if defined(GI_NEON_INTRINSICS)
    return vmaxq_f32(Vector1, Vector2);
#elif defined(GI_NEON32_INTRINSICS)
    return _mm_max_ps(Vector1, Vector2);
1140 1141
#elif defined(GI_RVV_INTRINSICS)
    return vfmax_vv_f32m1(Vector1, Vector2, GI_SIMD_LEN_BYTE / sizeof(float));
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
#else
    GI_FLOAT32_t max;
    for (size_t i = 0; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) {
        max[i] = Max(Vector1[i], Vector2[i]);
    }
    return max;
#endif
}

GI_FORCEINLINE
GI_FLOAT32_t GiMinimumFloat32(GI_FLOAT32_t Vector1, GI_FLOAT32_t Vector2) {
#if defined(GI_NEON_INTRINSICS)
    return vminq_f32(Vector1, Vector2);
#elif defined(GI_NEON32_INTRINSICS)
    return _mm_min_ps(Vector1, Vector2);
1157 1158
#elif defined(GI_RVV_INTRINSICS)
    return vfmin_vv_f32m1(Vector1, Vector2, GI_SIMD_LEN_BYTE / sizeof(float));
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
#else
    GI_FLOAT32_t min;
    for (size_t i = 0; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) {
        min[i] = Min(Vector1[i], Vector2[i]);
    }
    return min;
#endif
}

GI_FORCEINLINE
GI_FLOAT32_t GiMaxNanFloat32(GI_FLOAT32_t Vector1, GI_FLOAT32_t Vector2) {
1170 1171
#if defined(GI_NEON_INTRINSICS)
    return vmaxq_f32(Vector1, Vector2);
1172 1173 1174 1175 1176 1177 1178 1179 1180
#elif defined(GI_RVV_INTRINSICS)
    //! vfmax_vv_f32m1 NAN logic is not same with NEON, imp with naive
    GI_FLOAT32_FIXLEN_t a, b, ret;
    a = GiFloat32Type2FixLenType(Vector1);
    b = GiFloat32Type2FixLenType(Vector2);
    for (size_t i = 0; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) {
        ret[i] = MAX_NAN(a[i], b[i]);
    }
    return GiFixLenType2GiFloat32Type(ret);
1181
#else
1182 1183
    //! _mm_max_ps does not fellow the IEEE standard when input is NAN, so
    //! implement by C code
1184
    GI_FLOAT32_t max;
1185 1186 1187 1188
    for (size_t i = 0; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) {
        max[i] = MAX_NAN(Vector1[i], Vector2[i]);
    }
    return max;
1189 1190 1191 1192
#endif
}

GI_FORCEINLINE
1193
GI_FLOAT32_t GiMinNanFloat32(GI_FLOAT32_t Vector1, GI_FLOAT32_t Vector2) {
1194 1195
#if defined(GI_NEON_INTRINSICS)
    return vminq_f32(Vector1, Vector2);
1196 1197 1198 1199 1200 1201 1202 1203 1204
#elif defined(GI_RVV_INTRINSICS)
    //! vfmin_vv_f32m1 NAN logic is not same with NEON, imp with naive
    GI_FLOAT32_FIXLEN_t a, b, ret;
    a = GiFloat32Type2FixLenType(Vector1);
    b = GiFloat32Type2FixLenType(Vector2);
    for (size_t i = 0; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) {
        ret[i] = MIN_NAN(a[i], b[i]);
    }
    return GiFixLenType2GiFloat32Type(ret);
1205
#else
1206 1207
    //! _mm_min_ps does not fellow the IEEE standard when input is NAN, so
    //! implement by C code
1208
    GI_FLOAT32_t min;
1209 1210 1211 1212
    for (size_t i = 0; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) {
        min[i] = MIN_NAN(Vector1[i], Vector2[i]);
    }
    return min;
1213 1214 1215 1216
#endif
}

GI_FORCEINLINE
1217
GI_FLOAT32_t GiClampFloat32(GI_FLOAT32_t Value, float LowerRange, float UpperRange) {
1218 1219 1220 1221 1222 1223
    Value = GiMaximumFloat32(GiBroadcastFloat32(LowerRange), Value);
    Value = GiMinimumFloat32(GiBroadcastFloat32(UpperRange), Value);
    return Value;
}

GI_FORCEINLINE
1224
float GiReduceAddFloat32(GI_FLOAT32_t Vector) {
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
#if defined(GI_NEON64_INTRINSICS)
    Vector = vpaddq_f32(Vector, Vector);
    Vector = vpaddq_f32(Vector, Vector);
    return vgetq_lane_f32(Vector, 0);
#elif defined(GI_NEON32_INTRINSICS)
    float32x2_t VectorLow = vget_low_f32(Vector);
    float32x2_t VectorHigh = vget_high_f32(Vector);
    VectorLow = vpadd_f32(VectorLow, VectorHigh);
    VectorLow = vpadd_f32(VectorLow, VectorHigh);
    return vget_lane_f32(VectorLow, 0);
#elif defined(GI_SSE2_INTRINSICS)
    Vector = GiAddFloat32(
            Vector, _mm_shuffle_ps(Vector, Vector, _MM_SHUFFLE(2, 3, 2, 3)));
    Vector = GiAddFloat32(
            Vector, _mm_shuffle_ps(Vector, Vector, _MM_SHUFFLE(1, 1, 1, 1)));
    return GiExtractLane0Float32(Vector);
1241 1242 1243 1244 1245 1246
#elif defined(GI_RVV_INTRINSICS)
    vfloat32m1_t redsum = vundefined_f32m1();
    //! use Ordered sum, may Unordered sum more fast with vfredusum_vs_f32m1_f32m1
    redsum = vfredosum_vs_f32m1_f32m1(
            redsum, Vector, GiBroadcastFloat32(0.0f), GI_SIMD_LEN_BYTE / sizeof(float));
    return GiExtractLane0Float32(redsum);
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
#else
    float ret = 0;
    for (size_t i = 0; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) {
        ret += Vector[i];
    }
    return ret;
#endif
}

GI_FORCEINLINE
1257
float GiReduceMultiplyFloat32(GI_FLOAT32_t Vector) {
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
#if defined(GI_NEON64_INTRINSICS)
    float32x2_t low = vget_low_f32(Vector);
    float32x2_t high = vget_high_f32(Vector);
    float32x2_t res = vmul_f32(low, high);
    return vget_lane_f32(res, 0) * vget_lane_f32(res, 1);
#elif defined(GI_SSE2_INTRINSICS)
    Vector = GiMultiplyFloat32(
            Vector, _mm_shuffle_ps(Vector, Vector, _MM_SHUFFLE(2, 3, 2, 3)));
    Vector = GiMultiplyFloat32(
            Vector, _mm_shuffle_ps(Vector, Vector, _MM_SHUFFLE(1, 1, 1, 1)));
    return GiExtractLane0Float32(Vector);
1269 1270 1271 1272 1273 1274 1275 1276
#elif defined(GI_RVV_INTRINSICS)
    //! RVV do not have reduce mul, imp with naive
    float ret = 1;
    GI_FLOAT32_FIXLEN_t v = GiFloat32Type2FixLenType(Vector);
    for (size_t i = 0; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) {
        ret *= v[i];
    }
    return ret;
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
#else
    float ret = 1;
    for (size_t i = 0; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) {
        ret *= Vector[i];
    }
    return ret;
#endif
}

#define Max(a, b) (a) > (b) ? (a) : (b)
#define Min(a, b) (a) < (b) ? (a) : (b)

GI_FORCEINLINE
1290
float GiReduceMaxNanFloat32(GI_FLOAT32_t Vector) {
1291 1292 1293 1294 1295 1296 1297 1298 1299
#if defined(GI_NEON64_INTRINSICS)
    return vmaxvq_f32(Vector);
#elif defined(GI_NEON32_INTRINSICS)
    float32x2_t VectorLow = vget_low_f32(Vector);
    float32x2_t VectorHigh = vget_high_f32(Vector);
    VectorLow = vpmax_f32(VectorLow, VectorHigh);
    VectorLow = vpmax_f32(VectorLow, VectorHigh);
    return vget_lane_f32(VectorLow, 0);
#elif defined(GI_SSE2_INTRINSICS)
1300
    Vector = GiMaxNanFloat32(
1301
            Vector, _mm_shuffle_ps(Vector, Vector, _MM_SHUFFLE(2, 3, 2, 3)));
1302
    Vector = GiMaxNanFloat32(
1303 1304
            Vector, _mm_shuffle_ps(Vector, Vector, _MM_SHUFFLE(1, 1, 1, 1)));
    return GiExtractLane0Float32(Vector);
1305 1306 1307 1308 1309 1310 1311 1312
#elif defined(GI_RVV_INTRINSICS)
    //! vfredmax_vs_f32m1_f32m1 can not handle NAN case, imp with naive
    GI_FLOAT32_FIXLEN_t v = GiFloat32Type2FixLenType(Vector);
    float ret = v[0];
    for (size_t i = 1; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) {
        ret = MAX_NAN(ret, v[i]);
    }
    return ret;
1313 1314 1315
#else
    float ret = Vector[0];
    for (size_t i = 1; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) {
1316
        ret = MAX_NAN(ret, Vector[i]);
1317 1318 1319 1320 1321 1322
    }
    return ret;
#endif
}

GI_FORCEINLINE
1323
float GiReduceMinNanFloat32(GI_FLOAT32_t Vector) {
1324 1325 1326 1327 1328 1329 1330 1331 1332
#if defined(GI_NEON64_INTRINSICS)
    return vminvq_f32(Vector);
#elif defined(GI_NEON32_INTRINSICS)
    float32x2_t VectorLow = vget_low_f32(Vector);
    float32x2_t VectorHigh = vget_high_f32(Vector);
    VectorLow = vpmin_f32(VectorLow, VectorHigh);
    VectorLow = vpmin_f32(VectorLow, VectorHigh);
    return vget_lane_f32(VectorLow, 0);
#elif defined(GI_SSE2_INTRINSICS)
1333
    Vector = GiMinNanFloat32(
1334
            Vector, _mm_shuffle_ps(Vector, Vector, _MM_SHUFFLE(2, 3, 2, 3)));
1335
    Vector = GiMinNanFloat32(
1336 1337
            Vector, _mm_shuffle_ps(Vector, Vector, _MM_SHUFFLE(1, 1, 1, 1)));
    return GiExtractLane0Float32(Vector);
1338 1339 1340 1341 1342 1343 1344 1345
#elif defined(GI_RVV_INTRINSICS)
    //! vfredmin_vs_f32m1_f32m1 can not handle NAN case, imp with naive
    GI_FLOAT32_FIXLEN_t v = GiFloat32Type2FixLenType(Vector);
    float ret = v[0];
    for (size_t i = 1; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) {
        ret = MIN_NAN(ret, v[i]);
    }
    return ret;
1346 1347 1348
#else
    float ret = Vector[0];
    for (size_t i = 1; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) {
1349
        ret = MIN_NAN(ret, Vector[i]);
1350 1351 1352 1353 1354
    }
    return ret;
#endif
}

1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
GI_FORCEINLINE
GI_FLOAT32_t GiAbsFloat32(GI_FLOAT32_t Vector1) {
#if defined(GI_NEON64_INTRINSICS)
    return vabsq_f32(Vector1);
#elif defined(GI_SSE2_INTRINSICS)
    union {
        unsigned int int_val;
        float float_val;
    } value;
    value.int_val = 0x7fffffff;
    return _mm_and_ps(Vector1, _mm_set_ps1(value.float_val));
1366 1367
#elif defined(GI_RVV_INTRINSICS)
    return vfabs_v_f32m1(Vector1, GI_SIMD_LEN_BYTE / sizeof(float));
1368 1369 1370 1371 1372 1373 1374 1375 1376
#else
    GI_FLOAT32_t ret;
    for (size_t i = 0; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) {
        ret[i] = Vector1[i] > 0 ? Vector1[i] : -Vector1[i];
    }
    return ret;
#endif
}

1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429
#if defined(GI_SSE2_INTRINSICS)
typedef __m128i int8x16_t;
typedef __m64_128 int8x8_t;
GI_FORCEINLINE int8x16_t vcombine_s8(int8x8_t low, int8x8_t high) {
    return _mm_unpacklo_epi64(_pM128i(low), _pM128i(high));
}

typedef __m64_128 int64x1_t;
GI_FORCEINLINE int64x1_t vget_low_s64(GI_INT64_t a) {
    int64x1_t res64;
    return64(a);
}
GI_FORCEINLINE int64x1_t vget_high_s64(GI_INT64_t a) {
    int64x1_t res64;
    __m128i res;
    res = _mm_unpackhi_epi64(a, a);
    return64(res);
}
#endif

GI_FORCEINLINE GI_INT64_t GiZip1qS64(GI_INT64_t __p0, GI_INT64_t __p1) {
#if defined(GI_NEON_INTRINSICS)
    return vzip1q_s64(__p0, __p1);
#elif defined(GI_SSE2_INTRINSICS)
#define vcombine_s64 vcombine_s8
    return vcombine_s64(vget_low_s64(__p0), vget_low_s64(__p1));
#else
    GI_INT64_t ret;
    ret[0] = __p0[0];
    ret[1] = __p1[0];
    return ret;
#endif
}

GI_FORCEINLINE GI_INT64_t GiZip2qS64(GI_INT64_t __p0, GI_INT64_t __p1) {
#if defined(GI_NEON_INTRINSICS)
    return vzip2q_s64(__p0, __p1);
#elif defined(GI_SSE2_INTRINSICS)
#define vcombine_s64 vcombine_s8
    return vcombine_s64(vget_high_s64(__p0), vget_high_s64(__p1));
#else
    GI_INT64_t ret;
    ret[0] = __p0[1];
    ret[1] = __p1[1];
    return ret;
#endif
}

GI_FORCEINLINE GI_FLOAT32_t GiReinterpretqS64ToFloat32(GI_INT64_t a) {
#if defined(GI_NEON_INTRINSICS)
    return vreinterpretq_f32_s64(a);
#elif defined(GI_SSE2_INTRINSICS)
    return _M128(a);
1430 1431
#elif defined(GI_RVV_INTRINSICS)
    return vle32_v_f32m1((float*)&a, GI_SIMD_LEN_BYTE / sizeof(float));
1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443
#else
    GI_FLOAT32_t ret;
    memcpy(&ret, &a, sizeof(GI_FLOAT32_t));
    return ret;
#endif
}

GI_FORCEINLINE GI_INT64_t GiReinterpretqFloat32ToS64(GI_FLOAT32_t a) {
#if defined(GI_NEON_INTRINSICS)
    return vreinterpretq_s64_f32(a);
#elif defined(GI_SSE2_INTRINSICS)
    return _M128i(a);
1444 1445 1446 1447
#elif defined(GI_RVV_INTRINSICS)
    GI_INT64_t ret;
    vse32_v_f32m1((float*)&ret, a, GI_SIMD_LEN_BYTE / sizeof(float));
    return ret;
1448 1449 1450 1451 1452 1453 1454 1455 1456
#else
    GI_INT64_t ret;
    memcpy(&ret, &a, sizeof(GI_INT64_t));
    return ret;
#endif
}

#if defined(GI_NEON_INTRINSICS)
#define GiSimdFmaLane(a, b, c, d) vfmaq_laneq_f32(a, b, c, d)
1457 1458 1459 1460 1461 1462 1463 1464 1465 1466
#elif defined(GI_RVV_INTRINSICS)
#define __rvv_fmaq_laneq_f32(__a, __b, __c, __lane)                     \
    __extension__({                                                     \
        float t[GI_SIMD_LEN_BYTE / sizeof(float)];                      \
        vse32_v_f32m1(t, __c, GI_SIMD_LEN_BYTE / sizeof(float));        \
        GI_FLOAT32_t __ret = vfmadd_vf_f32m1(                           \
                __b, t[__lane], __a, GI_SIMD_LEN_BYTE / sizeof(float)); \
        __ret;                                                          \
    })
#define GiSimdFmaLane(a, b, c, d) __rvv_fmaq_laneq_f32(a, b, c, d)
1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551
#else
GI_FORCEINLINE GI_FLOAT32_t
___gi_vmlaq_lane_f32(GI_FLOAT32_t a, GI_FLOAT32_t b, float32x2_t v, int l) {
    float vlane;
    GI_FLOAT32_t c;
    vlane = (float)GiGetLaneFloat32(v, l);
    c = GiBroadcastFloat32(vlane);
    return GiMlaqFloat32(a, b, c);
}
GI_FORCEINLINE float32x2_t ___gi_vget_low_f32(GI_FLOAT32_t a) {
#if defined(GI_SSE2_INTRINSICS)
    float32x2_t res64;
    _M64f(res64, a);
    return res64;
#else
    float32x2_t ret;
    ret[0] = a[0];
    ret[1] = a[1];
    return ret;
#endif
}
GI_FORCEINLINE float32x2_t ___gi_vget_high_f32(GI_FLOAT32_t a) {
#if defined(GI_SSE2_INTRINSICS)
    __m128i res;
    __m64_128 res64;
    res = _mm_unpackhi_epi64(_M128i(a), _M128i(a));
    return64(res);
#else
    float32x2_t ret;
    ret[0] = a[2];
    ret[1] = a[3];
    return ret;
#endif
}
GI_FORCEINLINE GI_FLOAT32_t
___gi_vfmaq_laneq_f32(GI_FLOAT32_t a, GI_FLOAT32_t b, GI_FLOAT32_t v, int l) {
    if (l < 2) {
        return ___gi_vmlaq_lane_f32(a, b, ___gi_vget_low_f32(v), l);
    } else {
        return ___gi_vmlaq_lane_f32(a, b, ___gi_vget_high_f32(v), l - 2);
    }
}
#define GiSimdFmaLane(a, b, c, d) ___gi_vfmaq_laneq_f32(a, b, c, d)
#endif

#if defined(GI_NEON_INTRINSICS)
#if MEGDNN_AARCH64
#define GiMlaqLowLaneFloat32(__a, __b, __v, __lane) \
    vmlaq_laneq_f32(__a, __b, __v, __lane)

#define GiMlaqHighLaneFloat32(__a, __b, __v, __lane) \
    vmlaq_laneq_f32(__a, __b, __v, __lane)

#else
#define GiMlaqLowLaneFloat32(__a, __b, __v, __lane)               \
    __extension__({                                               \
        float32x2_t c = vget_low_f32(__v);                        \
        GI_FLOAT32_t __ret = vmlaq_lane_f32(__a, __b, c, __lane); \
        __ret;                                                    \
    })

#define GiMlaqHighLaneFloat32(__a, __b, __v, __lane)                    \
    __extension__({                                                     \
        float32x2_t c = vget_high_f32(__v);                             \
        GI_FLOAT32_t __ret = vmlaq_lane_f32(__a, __b, c, (__lane - 2)); \
        __ret;                                                          \
    })

#endif

#elif defined(GI_SSE2_INTRINSICS)
#define GiMlaqLowLaneFloat32(__a, __b, __v, __lane)                   \
    __extension__({                                                   \
        float32x2_t c = sse_vget_low_f32(__v);                        \
        GI_FLOAT32_t __ret = sse_vmlaq_lane_f32(__a, __b, c, __lane); \
        __ret;                                                        \
    })

#define GiMlaqHighLaneFloat32(__a, __b, __v, __lane)                        \
    __extension__({                                                         \
        float32x2_t c = sse_vget_high_f32(__v);                             \
        GI_FLOAT32_t __ret = sse_vmlaq_lane_f32(__a, __b, c, (__lane - 2)); \
        __ret;                                                              \
    })

1552 1553 1554
#elif defined(GI_RVV_INTRINSICS)
#define GiMlaqLowLaneFloat32(a, b, c, d)  __rvv_fmaq_laneq_f32(a, b, c, d)
#define GiMlaqHighLaneFloat32(a, b, c, d) __rvv_fmaq_laneq_f32(a, b, c, d)
1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595
#else
//! naive
#define GiMlaqLowLaneFloat32(__a, __b, __v, __lane)                     \
    __extension__({                                                     \
        GI_FLOAT32_t __ret;                                             \
        for (size_t i = 0; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) { \
            __ret[i] = __a[i] + (__b[i] * __v[__lane]);                 \
        }                                                               \
        __ret;                                                          \
    })

#define GiMlaqHighLaneFloat32(__a, __b, __v, __lane)                    \
    __extension__({                                                     \
        GI_FLOAT32_t __ret;                                             \
        for (size_t i = 0; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) { \
            __ret[i] = __a[i] + (__b[i] * __v[__lane]);                 \
        }                                                               \
        __ret;                                                          \
    })
#endif

#if defined(GI_NEON_INTRINSICS)
#define GiFmsqLaneQFloat32(a, b, v, lane) vfmsq_laneq_f32(a, b, v, lane)
#elif defined(GI_SSE2_INTRINSICS)
#define SSE_VFMSQ_LANEQ_F32(lane)                                   \
    GI_FORCEINLINE GI_FLOAT32_t sse_vfmsq_lane_##lane##_q_f32(      \
            GI_FLOAT32_t a, GI_FLOAT32_t b, GI_FLOAT32_t v) {       \
        return sse_vmlsq_lane_f32(a, b, sse_vget_low_f32(v), lane); \
    }
SSE_VFMSQ_LANEQ_F32(0)
SSE_VFMSQ_LANEQ_F32(1)
#undef SSE_VFMSQ_LANEQ_F32
#define SSE_VFMSQ_LANEQ_F32(lane)                                        \
    GI_FORCEINLINE GI_FLOAT32_t sse_vfmsq_lane_##lane##_q_f32(           \
            GI_FLOAT32_t a, GI_FLOAT32_t b, GI_FLOAT32_t v) {            \
        return sse_vmlsq_lane_f32(a, b, sse_vget_high_f32(v), lane - 2); \
    }
SSE_VFMSQ_LANEQ_F32(2)
SSE_VFMSQ_LANEQ_F32(3)
#undef SSE_VFMSQ_LANEQ_F32
#define GiFmsqLaneQFloat32(a, b, v, lane) sse_vfmsq_lane_##lane##_q_f32(a, b, v)
1596 1597 1598 1599 1600 1601 1602 1603 1604 1605
#elif defined(GI_RVV_INTRINSICS)
#define __rvv_fmsq_lane_float32(__a, __b, __c, __lane)                  \
    __extension__({                                                     \
        float t[GI_SIMD_LEN_BYTE / sizeof(float)];                      \
        vse32_v_f32m1(t, __c, GI_SIMD_LEN_BYTE / sizeof(float));        \
        GI_FLOAT32_t __ret = vfnmsub_vf_f32m1(                          \
                __b, t[__lane], __a, GI_SIMD_LEN_BYTE / sizeof(float)); \
        __ret;                                                          \
    })
#define GiFmsqLaneQFloat32(a, b, c, d) __rvv_fmsq_lane_float32(a, b, c, d)
1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626
#else
//! naive
GI_FORCEINLINE GI_FLOAT32_t __naive_GiFmsqLaneQFloat32(
        GI_FLOAT32_t a, GI_FLOAT32_t b, GI_FLOAT32_t v, const int lane) {
    GI_FLOAT32_t ret;
    for (size_t i = 0; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) {
        ret[i] = a[i] - (b[i] * v[lane]);
    }

    return ret;
}
#define GiFmsqLaneQFloat32(a, b, v, lane) __naive_GiFmsqLaneQFloat32(a, b, v, lane)
#endif

GI_FORCEINLINE GI_FLOAT32_t GiCombineFloat32(float32x2_t a, float32x2_t b) {
#if defined(GI_NEON_INTRINSICS)
    return vcombine_f32(a, b);
#elif defined(GI_SSE2_INTRINSICS)
    __m128i res;
    res = _mm_unpacklo_epi64(_pM128i(a), _pM128i(b));
    return _M128(res);
1627 1628 1629 1630 1631
#elif defined(GI_RVV_INTRINSICS)
    float t[GI_SIMD_LEN_BYTE / sizeof(float)];
    vse32_v_f32m1(t, a, 2);
    vse32_v_f32m1(t + 2, b, 2);
    return vle32_v_f32m1(t, GI_SIMD_LEN_BYTE / sizeof(float));
1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644
#else
    GI_FLOAT32_t res;
    res[0] = a[0];
    res[1] = a[1];
    res[2] = b[0];
    res[3] = b[1];
    return res;
#endif
}

GI_FORCEINLINE float32x2_t GiGetLowFloat32(GI_FLOAT32_t a) {
#if defined(GI_NEON_INTRINSICS)
    return vget_low_f32(a);
1645 1646
#elif defined(GI_RVV_INTRINSICS)
    return vmv_v_v_f32m1(a, 2);
1647 1648 1649 1650 1651 1652 1653 1654
#else
    return ___gi_vget_low_f32(a);
#endif
}

GI_FORCEINLINE float32x2_t GiGetHighFloat32(GI_FLOAT32_t a) {
#if defined(GI_NEON_INTRINSICS)
    return vget_high_f32(a);
1655 1656 1657 1658 1659 1660
#elif defined(GI_RVV_INTRINSICS)
    float t[GI_SIMD_LEN_BYTE / sizeof(float)];
    vse32_v_f32m1(t, a, GI_SIMD_LEN_BYTE / sizeof(float));
    return vle32_v_f32m1(
            t + GI_SIMD_LEN_BYTE / sizeof(float) / 2,
            GI_SIMD_LEN_BYTE / sizeof(float) / 2);
1661 1662 1663 1664
#else
    return ___gi_vget_high_f32(a);
#endif
}
1665 1666 1667 1668 1669 1670 1671 1672 1673

GI_FORCEINLINE float32x2_t GiPaddFloat32(float32x2_t a, float32x2_t b) {
#if defined(GI_NEON_INTRINSICS)
    return vpadd_f32(a, b);
#elif defined(GI_SSE2_INTRINSICS)
    float32x2_t res;
    res.m64_f32[0] = a.m64_f32[0] + a.m64_f32[1];
    res.m64_f32[1] = b.m64_f32[0] + b.m64_f32[1];
    return res;
1674 1675 1676 1677 1678 1679 1680
#elif defined(GI_RVV_INTRINSICS)
    float t[GI_SIMD_LEN_BYTE / sizeof(float)];
    vse32_v_f32m1(t, a, 2);
    vse32_v_f32m1(t + 2, b, 2);
    t[0] = t[0] + t[1];
    t[1] = t[2] + t[3];
    return vle32_v_f32m1(t, 2);
1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696
#else
    float32x2_t res;
    res[0] = a[0] + a[1];
    res[1] = b[0] + b[1];
    return res;
#endif
}

GI_FORCEINLINE float32x2_t GiPmaxFloat32(float32x2_t a, float32x2_t b) {
#if defined(GI_NEON_INTRINSICS)
    return vpmax_f32(a, b);
#elif defined(GI_SSE2_INTRINSICS)
    float32x2_t res;
    res.m64_f32[0] = MAX_NAN(a.m64_f32[0], a.m64_f32[1]);
    res.m64_f32[1] = MAX_NAN(b.m64_f32[0], b.m64_f32[1]);
    return res;
1697 1698 1699 1700 1701 1702 1703
#elif defined(GI_RVV_INTRINSICS)
    float t[GI_SIMD_LEN_BYTE / sizeof(float)];
    vse32_v_f32m1(t, a, 2);
    vse32_v_f32m1(t + 2, b, 2);
    t[0] = MAX_NAN(t[0], t[1]);
    t[1] = MAX_NAN(t[2], t[3]);
    return vle32_v_f32m1(t, 2);
1704 1705 1706 1707 1708 1709 1710
#else
    float32x2_t res;
    res[0] = MAX_NAN(a[0], a[1]);
    res[1] = MAX_NAN(b[0], b[1]);
    return res;
#endif
}
1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737

GI_FORCEINLINE
GI_FLOAT32_V3_t GiLoadUzipFloat32V3(const float* ptr) {
#if defined(GI_NEON_INTRINSICS)
    return vld3q_f32(ptr);
#elif defined(GI_SSE2_INTRINSICS)
    GI_FLOAT32_V3_t v;
    __m128 tmp0, tmp1, tmp2, tmp3;
    v.val[0] = GiLoadFloat32(ptr);
    v.val[1] = GiLoadFloat32((ptr + 4));
    v.val[2] = GiLoadFloat32((ptr + 8));

    tmp0 = _mm_castsi128_ps(_mm_shuffle_epi32(
            _mm_castps_si128(v.val[0]), 0 | (3 << 2) | (1 << 4) | (2 << 6)));
    tmp1 = _mm_castsi128_ps(
            _mm_shuffle_epi32(_mm_castps_si128(v.val[1]), _SWAP_HI_LOW32));
    tmp2 = _mm_castsi128_ps(_mm_shuffle_epi32(
            _mm_castps_si128(v.val[2]), 1 | (2 << 2) | (0 << 4) | (3 << 6)));
    tmp3 = _mm_unpacklo_ps(tmp1, tmp2);

    v.val[0] = _mm_movelh_ps(tmp0, tmp3);
    tmp0 = _mm_unpackhi_ps(tmp0, tmp1);
    v.val[1] =
            _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(tmp0), _SWAP_HI_LOW32));
    v.val[1] = _mm_movehl_ps(tmp3, v.val[1]);
    v.val[2] = _mm_movehl_ps(tmp2, tmp0);
    return v;
1738 1739
#elif defined(GI_RVV_INTRINSICS)
    return vlseg3e32_v_f32m1x3(ptr, GI_SIMD_LEN_BYTE / sizeof(float));
1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771
#else
    GI_FLOAT32_V3_t ret;
    for (size_t i = 0; i < 3; i++) {
        ret.val[i][0] = ptr[0 + i];
        ret.val[i][1] = ptr[3 + i];
        ret.val[i][2] = ptr[6 + i];
        ret.val[i][3] = ptr[9 + i];
    }

    return ret;
#endif
}

GI_FORCEINLINE
void GiStoreZipFloat32V3(float* ptr, GI_FLOAT32_V3_t val) {
#if defined(GI_NEON_INTRINSICS)
    vst3q_f32(ptr, val);
#elif defined(GI_SSE2_INTRINSICS)
    GI_FLOAT32_V3_t v;
    __m128 tmp0, tmp1, tmp2;
    tmp0 = _mm_unpacklo_ps(val.val[0], val.val[1]);
    tmp1 = _mm_unpackhi_ps(val.val[0], val.val[1]);
    tmp2 = _mm_unpacklo_ps(val.val[1], val.val[2]);
    v.val[1] = _mm_shuffle_ps(tmp2, tmp1, _MM_SHUFFLE(1, 0, 3, 2));
    v.val[2] = _mm_movehl_ps(val.val[2], tmp1);
    v.val[2] = _mm_shuffle_ps(v.val[2], v.val[2], _MM_SHUFFLE(3, 1, 0, 2));
    tmp1 = _mm_unpacklo_ps(tmp2, val.val[0]);
    v.val[0] = _mm_shuffle_ps(tmp0, tmp1, _MM_SHUFFLE(3, 2, 1, 0));

    GiStoreFloat32(ptr, v.val[0]);
    GiStoreFloat32((ptr + 4), v.val[1]);
    GiStoreFloat32((ptr + 8), v.val[2]);
1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800
#elif defined(GI_RVV_INTRINSICS)
    vfloat32m4_t d = vundefined_f32m4();
    d = vset_v_f32m1_f32m4(d, 0, GiGetSubVectorFloat32V3(val, 0));
    d = vset_v_f32m1_f32m4(d, 1, GiGetSubVectorFloat32V3(val, 1));
    d = vset_v_f32m1_f32m4(d, 2, GiGetSubVectorFloat32V3(val, 2));
    vuint32m4_t index;
#if GI_SIMD_LEN_BYTE == 16
    uint32_t index_128[16] = {0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11, 0, 0, 0, 0};
    index = vle32_v_u32m4(index_128, 16);
#else
    uint32_t* index_p = (uint32_t*)&index;
    for (size_t i = 0; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) {
        index_p[3 * i] = i;
        index_p[3 * i + 1] = i + GI_SIMD_LEN_BYTE / sizeof(float);
        index_p[3 * i + 2] = i + GI_SIMD_LEN_BYTE / sizeof(float) * 2;
    }
#endif
    vfloat32m4_t g_d =
            vrgather_vv_f32m4(d, index, GI_SIMD_LEN_BYTE / sizeof(float) * 3);
    vfloat32m1_t v0 = vget_v_f32m4_f32m1(g_d, 0);
    vfloat32m1_t v1 = vget_v_f32m4_f32m1(g_d, 1);
    vfloat32m1_t v2 = vget_v_f32m4_f32m1(g_d, 2);
    GI_FLOAT32_V3_t tmp = vcreate_f32m1x3(v0, v1, v2);
    GiStoreFloat32(ptr, GiGetSubVectorFloat32V3(tmp, 0));
    GiStoreFloat32(
            ptr + GI_SIMD_LEN_BYTE / sizeof(float), GiGetSubVectorFloat32V3(tmp, 1));
    GiStoreFloat32(
            ptr + GI_SIMD_LEN_BYTE / sizeof(float) * 2,
            GiGetSubVectorFloat32V3(tmp, 2));
1801 1802 1803 1804 1805 1806 1807 1808
#else
    for (size_t i = 0; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) {
        *ptr++ = val.val[0][i];
        *ptr++ = val.val[1][i];
        *ptr++ = val.val[2][i];
    }
#endif
}
1809 1810 1811 1812 1813 1814 1815 1816 1817 1818

GI_FORCEINLINE
GI_FLOAT32_t GiDivFloat32(GI_FLOAT32_t Vector1, GI_FLOAT32_t Vector2) {
#if defined(GI_RVV_INTRINSICS)
    return vfdiv_vv_f32m1(Vector1, Vector2, GI_SIMD_LEN_BYTE / sizeof(float));
#else
    //! neon, ssex and naive can auto call builtin function
    return Vector1 / Vector2;
#endif
}