gi_float.h 42.8 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 11
#if defined(GI_NEON_INTRINSICS)
    return vreinterpretq_s32_f32(In);
#elif defined(GI_SSE2_INTRINSICS)
    return _mm_castps_si128(In);
#else
12
    return (GI_INT32_t)In;
13 14 15 16
#endif
}

GI_FORCEINLINE
17 18 19 20 21 22
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);
#else
23
    return (GI_UINT32_t)In;
24 25 26 27 28 29 30 31 32 33
#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);
#else
34
    return (GI_FLOAT32_t)Vector;
35 36 37 38 39 40 41 42 43 44
#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);
#else
45
    return (GI_FLOAT32_t)Vector;
46 47 48 49 50
#endif
}

GI_FORCEINLINE
GI_INT32_t GiRoundAsInt32(GI_FLOAT32_t Vector) {
51 52 53 54
#if defined(GI_NEON_INTRINSICS)
#if __ARM_ARCH >= 8
    return vcvtaq_s32_f32(Vector);
#else
55
    float32x4_t vinc0 = vbslq_f32(vcgeq_f32(Vector, vfzero), vfhalf, vfneg_half);
56 57
    return vcvtq_s32_f32(vaddq_f32(Vector, vinc0));
#endif
58
#elif defined(GI_SSE42_INTRINSICS)
59
    __m128 vinc0 = _mm_blendv_ps(vfneg_half, vfhalf, _mm_cmpge_ps(Vector, vfzero));
60
    return _mm_cvttps_epi32(_mm_add_ps(Vector, vinc0));
61
#else
62
    GI_INT32_t ret;
63 64 65
    GI_INT32_NAIVE_t tmp_ret;
    GI_FLOAT32_NAIVE_t s0;
    memcpy(&s0, &Vector, sizeof(GI_FLOAT32_NAIVE_t));
66
    for (size_t i = 0; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) {
67
        tmp_ret[i] = (int32_t)round(s0[i]);
68
    }
69
    memcpy(&ret, &tmp_ret, sizeof(GI_INT32_t));
70 71 72 73 74
    return ret;
#endif
}

GI_FORCEINLINE
75
GI_INT32_t GiCastToInt32(GI_FLOAT32_t Vector) {
76
#if defined(GI_NEON_INTRINSICS)
77
    return vcvtq_s32_f32(Vector);
78
#elif defined(GI_SSE2_INTRINSICS)
79
    return _mm_cvttps_epi32(Vector);
80
#else
81 82 83
    GI_INT32_t ret;
    for (size_t i = 0; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) {
        ret[i] = (int32_t)(Vector[i]);
84 85 86 87 88 89
    }
    return ret;
#endif
}

GI_FORCEINLINE
90
GI_FLOAT32_t GiCastToFloat32(GI_INT32_t Vector) {
91
#if defined(GI_NEON_INTRINSICS)
92
    return vcvtq_f32_s32(Vector);
93
#elif defined(GI_SSE2_INTRINSICS)
94
    return _mm_cvtepi32_ps(Vector);
95
#else
96 97
    GI_FLOAT32_t ret;
    for (size_t i = 0; i < GI_SIMD_LEN_BYTE / sizeof(int32_t); i++) {
98
        ret[i] = (float)Vector[i];
99 100 101 102 103 104
    }
    return ret;
#endif
}

GI_FORCEINLINE
105
GI_FLOAT32_t GiLoadBroadcastFloat32(const float* Value) {
106 107 108 109 110
#if defined(GI_NEON_INTRINSICS)
    return vld1q_dup_f32(Value);
#elif defined(GI_SSE2_INTRINSICS)
    return _mm_load_ps1(Value);
#else
111
    GI_FLOAT32_t ret;
112 113 114 115 116 117 118 119
    for (size_t i = 0; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) {
        ret[i] = *Value;
    }
    return ret;
#endif
}

GI_FORCEINLINE
120
GI_FLOAT32_t GiZeroFloat32(void) {
121 122 123 124 125 126 127 128 129 130
#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
131
GI_FLOAT32_t GiLoadFloat32(const float* Buffer) {
132 133 134
#if defined(GI_NEON_INTRINSICS)
    return vld1q_f32(Buffer);
#elif defined(GI_SSE2_INTRINSICS)
135 136 137 138
    if ((((uintptr_t)(Buffer)) & 15) == 0)
        return _mm_load_ps(Buffer);
    else
        return _mm_loadu_ps(Buffer);
139
#else
140
    GI_FLOAT32_t ret;
141 142 143 144 145 146 147
    for (size_t i = 0; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) {
        ret[i] = Buffer[i];
    }
    return ret;
#endif
}

148 149 150 151 152 153 154 155 156 157 158 159 160
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;
    v.val[0] = GiLoadFloat32(Buffer);
    v.val[1] = GiLoadFloat32(Buffer + GI_SIMD_LEN_BYTE / sizeof(float));

    return v;
#endif
}

161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 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
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);
#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);
#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;
#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;
#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;
#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;
#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);
#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;
#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;
#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;
#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) {
    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;
}
#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));
#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));
#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) {
    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 + 2]);
    }
    return ret;
}
#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) {
    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 GiVmlaqLaneFloat32LowHalf(a, b, v, lane) \
    __naive_gi_vmlaq_lane_f32_low_half(a, b, v, lane)
#endif

511
GI_FORCEINLINE
512
void GiStoreFloat32(float* Buffer, GI_FLOAT32_t Vector) {
513 514 515 516 517 518 519 520 521 522 523
#if defined(GI_NEON_INTRINSICS)
    vst1q_f32(Buffer, Vector);
#elif defined(GI_SSE2_INTRINSICS)
    _mm_storeu_ps(Buffer, Vector);
#else
    for (size_t i = 0; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) {
        Buffer[i] = Vector[i];
    }
#endif
}

524 525 526 527 528 529 530 531 532 533
GI_FORCEINLINE
void GiStoreFloat32V2(float* Buffer, GI_FLOAT32_V2_t Vector) {
#if defined(GI_NEON_INTRINSICS)
    vst1q_f32_x2(Buffer, Vector);
#else
    GiStoreFloat32(Buffer, Vector.val[0]);
    GiStoreFloat32(Buffer + GI_SIMD_LEN_BYTE / sizeof(float), Vector.val[1]);
#endif
}

534
#if defined(GI_NEON_INTRINSICS)
535 536 537
#define GISTORELANEFLOAT32(i)                                                         \
    GI_FORCEINLINE void GiStoreLane##i##Float32(float* Buffer, GI_FLOAT32_t Vector) { \
        vst1q_lane_f32(Buffer, Vector, i);                                            \
538 539 540 541 542
    }

#elif defined(GI_SSE2_INTRINSICS)

#define GISTORELANEFLOAT32(i)                                                          \
543
    GI_FORCEINLINE void GiStoreLane##i##Float32(float* Buffer, GI_FLOAT32_t Vector) {  \
544 545 546
        _mm_store_ss(Buffer, _mm_shuffle_ps(Vector, Vector, _MM_SHUFFLE(i, i, i, i))); \
    }
#else
547 548 549
#define GISTORELANEFLOAT32(i)                                                         \
    GI_FORCEINLINE void GiStoreLane##i##Float32(float* Buffer, GI_FLOAT32_t Vector) { \
        *Buffer = Vector[i];                                                          \
550 551 552 553 554 555 556 557 558 559 560
    }
#endif

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

#undef GISTORELANEFLOAT32

#if defined(GI_NEON_INTRINSICS)
561 562 563
#define GIEXTRACTLANEFLOAT32(i)                                           \
    GI_FORCEINLINE float GiExtractLane##i##Float32(GI_FLOAT32_t Vector) { \
        return vgetq_lane_f32(Vector, i);                                 \
564 565 566 567
    }
#elif defined(GI_SSE2_INTRINSICS)

#define GIEXTRACTLANEFLOAT32(i)                                                        \
568
    GI_FORCEINLINE float GiExtractLane##i##Float32(GI_FLOAT32_t Vector) {              \
569 570 571
        return _mm_cvtss_f32(_mm_shuffle_ps(Vector, Vector, _MM_SHUFFLE(i, i, i, i))); \
    }
#else
572 573 574
#define GIEXTRACTLANEFLOAT32(i)                                           \
    GI_FORCEINLINE float GiExtractLane##i##Float32(GI_FLOAT32_t Vector) { \
        return Vector[i];                                                 \
575 576 577 578 579 580 581 582 583
    }
#endif

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

584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
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;
#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
}

607 608 609 610 611 612 613 614 615 616 617 618
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;
    tmp = GiZipqFloat32(Vector.val[0], Vector.val[1]);
    GiStoreFloat32(Buffer, tmp.val[0]);
    GiStoreFloat32(Buffer + GI_SIMD_LEN_BYTE / sizeof(float), tmp.val[1]);
#endif
}

619
GI_FORCEINLINE
620
GI_FLOAT32_t GiInterleaveLowFloat32(GI_FLOAT32_t Vector1, GI_FLOAT32_t Vector2) {
621 622 623
#if defined(GI_NEON64_INTRINSICS)
    return vzip1q_f32(Vector1, Vector2);
#elif defined(GI_NEON32_INTRINSICS)
624
    float32x4x2_t zipped = vzipq_f32(Vector1, Vector2);
625 626 627 628
    return zipped.val[0];
#elif defined(GI_SSE2_INTRINSICS)
    return _mm_unpacklo_ps(Vector1, Vector2);
#else
629
    GI_FLOAT32_t ret;
630 631 632 633 634 635 636 637 638
    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
639
GI_FLOAT32_t GiInterleaveHighFloat32(GI_FLOAT32_t Vector1, GI_FLOAT32_t Vector2) {
640 641 642
#if defined(GI_NEON64_INTRINSICS)
    return vzip2q_f32(Vector1, Vector2);
#elif defined(GI_NEON32_INTRINSICS)
643
    float32x4x2_t zipped = vzipq_f32(Vector1, Vector2);
644 645 646 647
    return zipped.val[1];
#elif defined(GI_SSE2_INTRINSICS)
    return _mm_unpackhi_ps(Vector1, Vector2);
#else
648
    GI_FLOAT32_t ret;
649
    for (size_t i = 0; i < GI_SIMD_LEN_BYTE / 2 / sizeof(float); i++) {
650 651
        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];
652 653 654 655 656 657
    }
    return ret;
#endif
}

GI_FORCEINLINE
658
GI_FLOAT32_t GiAddFloat32(GI_FLOAT32_t Vector1, GI_FLOAT32_t Vector2) {
659 660 661 662 663 664 665 666 667 668
#if defined(GI_NEON_INTRINSICS)
    return vaddq_f32(Vector1, Vector2);
#elif defined(GI_SSE2_INTRINSICS)
    return _mm_add_ps(Vector1, Vector2);
#else
    return Vector1 + Vector2;
#endif
}

GI_FORCEINLINE
669
GI_FLOAT32_t GiSubtractFloat32(GI_FLOAT32_t Vector1, GI_FLOAT32_t Vector2) {
670 671 672 673 674 675 676 677 678 679
#if defined(GI_NEON_INTRINSICS)
    return vsubq_f32(Vector1, Vector2);
#elif defined(GI_SSE2_INTRINSICS)
    return _mm_sub_ps(Vector1, Vector2);
#else
    return Vector1 - Vector2;
#endif
}

GI_FORCEINLINE
680
GI_FLOAT32_t GiMultiplyFloat32(GI_FLOAT32_t Vector1, GI_FLOAT32_t Vector2) {
681 682 683 684 685 686 687 688 689 690
#if defined(GI_NEON_INTRINSICS)
    return vmulq_f32(Vector1, Vector2);
#elif defined(GI_SSE2_INTRINSICS)
    return _mm_mul_ps(Vector1, Vector2);
#else
    return Vector1 * Vector2;
#endif
}

GI_FORCEINLINE
691
GI_FLOAT32_t GiMultiplyScalerFloat32(GI_FLOAT32_t Vector1, float Scaler) {
692 693 694
#if defined(GI_NEON_INTRINSICS)
    return vmulq_n_f32(Vector1, Scaler);
#elif defined(GI_SSE2_INTRINSICS)
695
    GI_FLOAT32_t Vector2 = _mm_set1_ps(Scaler);
696 697 698 699 700 701 702
    return _mm_mul_ps(Vector1, Vector2);
#else
    return Vector1 * Scaler;
#endif
}

GI_FORCEINLINE
703 704
GI_FLOAT32_t GiMultiplyAddFloat32(
        GI_FLOAT32_t VectorSum, GI_FLOAT32_t Vector1, GI_FLOAT32_t Vector2) {
705
#if defined(GI_NEON_INTRINSICS)
706
    return v_fma_ps_f32(VectorSum, Vector1, Vector2);
707 708 709 710 711 712 713 714 715
#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);
#else
    return Vector1 * Vector2 + VectorSum;
#endif
}

716 717 718 719
GI_FORCEINLINE
GI_FLOAT32_t GiMultiplyAddScalarFloat32(
        GI_FLOAT32_t VectorSum, GI_FLOAT32_t Vector, float Scalar) {
#if defined(GI_NEON_INTRINSICS)
720
    return v_fma_n_f32(VectorSum, Vector, Scalar);
721
#elif defined(GI_SSE2_INTRINSICS)
722
    return GiMultiplyAddFloat32(VectorSum, GiBroadcastFloat32(Scalar), Vector);
723 724 725 726 727 728
#else
    return VectorSum + Vector * Scalar;
#endif
}

#if defined(GI_NEON_INTRINSICS)
729 730 731
#define GIMULTIPLYADDLANFLOAT32(i)                                                \
    GI_FORCEINLINE GI_FLOAT32_t GiMultiplyAddLan##i##Float32(                     \
            GI_FLOAT32_t VectorSum, GI_FLOAT32_t Vector1, GI_FLOAT32_t Vector2) { \
732
        return v_fma_lane_f32(VectorSum, Vector1, vget_low_f32(Vector2), i);      \
733 734 735 736 737
    }
GIMULTIPLYADDLANFLOAT32(0)
GIMULTIPLYADDLANFLOAT32(1)
#undef GIMULTIPLYADDLANFLOAT32
#define GIMULTIPLYADDLANFLOAT32(i)                                                \
738 739
    GI_FORCEINLINE GI_FLOAT32_t GiMultiplyAddLan##i##Float32(                     \
            GI_FLOAT32_t VectorSum, GI_FLOAT32_t Vector1, GI_FLOAT32_t Vector2) { \
740
        return v_fma_lane_f32(VectorSum, Vector1, vget_high_f32(Vector2), i - 2); \
741 742 743
    }
GIMULTIPLYADDLANFLOAT32(2)
GIMULTIPLYADDLANFLOAT32(3)
744
#undef GIMULTIPLYADDLANFLOAT32
745
#else
746

747 748 749 750 751
#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));          \
752 753 754 755 756 757 758 759 760
    }
GIMULTIPLYADDLANFLOAT32(0)
GIMULTIPLYADDLANFLOAT32(1)
GIMULTIPLYADDLANFLOAT32(2)
GIMULTIPLYADDLANFLOAT32(3)
#undef GIMULTIPLYADDLANFLOAT32
#endif

GI_FORCEINLINE
761
GI_FLOAT32_t GiDivideFloat32(GI_FLOAT32_t Vector1, GI_FLOAT32_t Vector2) {
762 763 764 765 766 767 768 769 770 771 772 773 774 775
#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);
#else
    return Vector1 / Vector2;
#endif
}

GI_FORCEINLINE
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
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));
#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);
#else
795
    //! FIXME: neon or sse always have low accuracy than 1/x
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
    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);
#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));
#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) {
829
#if defined(GI_NEON_INTRINSICS)
830
    return vcleq_f32(Vector1, Vector2);
831
#elif defined(GI_SSE2_INTRINSICS)
832
    return _mm_castps_si128(_mm_cmple_ps(Vector1, Vector2));
833
#else
834 835 836 837 838
    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;
839 840 841 842
#endif
}

GI_FORCEINLINE
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
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));
#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) {
859 860 861
#if defined(GI_SSE2_INTRINSICS)
    return _mm_and_ps(Vector1, Vector2);
#else
862
    return GiReintInt32ToFloat32(
863 864 865 866 867
            GiAndInt32(GiReinterpretAsInt32(Vector1), GiReinterpretAsInt32(Vector2)));
#endif
}

GI_FORCEINLINE
868
GI_FLOAT32_t GiOrFloat32(GI_FLOAT32_t Vector1, GI_FLOAT32_t Vector2) {
869 870 871
#if defined(GI_SSE2_INTRINSICS)
    return _mm_or_ps(Vector1, Vector2);
#else
872
    return GiReintInt32ToFloat32(
873 874 875 876 877
            GiOrInt32(GiReinterpretAsInt32(Vector1), GiReinterpretAsInt32(Vector2)));
#endif
}

GI_FORCEINLINE
878
GI_FLOAT32_t GiAndNotFloat32(GI_FLOAT32_t VectorNot, GI_FLOAT32_t Vector) {
879 880 881
#if defined(GI_SSE2_INTRINSICS)
    return _mm_andnot_ps(VectorNot, Vector);
#else
882
    return GiReintInt32ToFloat32(GiAndNotInt32(
883 884 885 886 887
            GiReinterpretAsInt32(VectorNot), GiReinterpretAsInt32(Vector)));
#endif
}

GI_FORCEINLINE
888
GI_FLOAT32_t GiXorFloat32(GI_FLOAT32_t Vector1, GI_FLOAT32_t Vector2) {
889 890 891
#if defined(GI_SSE2_INTRINSICS)
    return _mm_xor_ps(Vector1, Vector2);
#else
892
    return GiReintInt32ToFloat32(
893 894 895 896 897
            GiXorInt32(GiReinterpretAsInt32(Vector1), GiReinterpretAsInt32(Vector2)));
#endif
}

GI_FORCEINLINE
898 899
GI_FLOAT32_t GiBlendFloat32(
        GI_FLOAT32_t Vector1, GI_FLOAT32_t Vector2, GI_FLOAT32_t Selection) {
900
    return GiOrFloat32(
901
            GiAndFloat32(Vector1, Selection), GiAndNotFloat32(Selection, Vector2));
902 903
}

904 905 906
#define MIN_NAN(a, b) (isnan(a) || (a) < (b)) ? (a) : (b);
#define MAX_NAN(a, b) (isnan(a) || (a) > (b)) ? (a) : (b);

907
GI_FORCEINLINE
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
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);
#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);
#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) {
949 950
#if defined(GI_NEON_INTRINSICS)
    return vmaxq_f32(Vector1, Vector2);
951
#else
952 953
    //! _mm_max_ps does not fellow the IEEE standard when input is NAN, so
    //! implement by C code
954
    GI_FLOAT32_t max;
955 956 957 958
    for (size_t i = 0; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) {
        max[i] = MAX_NAN(Vector1[i], Vector2[i]);
    }
    return max;
959 960 961 962
#endif
}

GI_FORCEINLINE
963
GI_FLOAT32_t GiMinNanFloat32(GI_FLOAT32_t Vector1, GI_FLOAT32_t Vector2) {
964 965
#if defined(GI_NEON_INTRINSICS)
    return vminq_f32(Vector1, Vector2);
966
#else
967 968
    //! _mm_min_ps does not fellow the IEEE standard when input is NAN, so
    //! implement by C code
969
    GI_FLOAT32_t min;
970 971 972 973
    for (size_t i = 0; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) {
        min[i] = MIN_NAN(Vector1[i], Vector2[i]);
    }
    return min;
974 975 976 977
#endif
}

GI_FORCEINLINE
978
GI_FLOAT32_t GiClampFloat32(GI_FLOAT32_t Value, float LowerRange, float UpperRange) {
979 980 981 982 983 984
    Value = GiMaximumFloat32(GiBroadcastFloat32(LowerRange), Value);
    Value = GiMinimumFloat32(GiBroadcastFloat32(UpperRange), Value);
    return Value;
}

GI_FORCEINLINE
985
float GiReduceAddFloat32(GI_FLOAT32_t Vector) {
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
#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);
#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
1012
float GiReduceMultiplyFloat32(GI_FLOAT32_t Vector) {
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
#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);
#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
1037
float GiReduceMaxNanFloat32(GI_FLOAT32_t Vector) {
1038 1039 1040 1041 1042 1043 1044 1045 1046
#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)
1047
    Vector = GiMaxNanFloat32(
1048
            Vector, _mm_shuffle_ps(Vector, Vector, _MM_SHUFFLE(2, 3, 2, 3)));
1049
    Vector = GiMaxNanFloat32(
1050 1051 1052 1053 1054
            Vector, _mm_shuffle_ps(Vector, Vector, _MM_SHUFFLE(1, 1, 1, 1)));
    return GiExtractLane0Float32(Vector);
#else
    float ret = Vector[0];
    for (size_t i = 1; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) {
1055
        ret = MAX_NAN(ret, Vector[i]);
1056 1057 1058 1059 1060 1061
    }
    return ret;
#endif
}

GI_FORCEINLINE
1062
float GiReduceMinNanFloat32(GI_FLOAT32_t Vector) {
1063 1064 1065 1066 1067 1068 1069 1070 1071
#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)
1072
    Vector = GiMinNanFloat32(
1073
            Vector, _mm_shuffle_ps(Vector, Vector, _MM_SHUFFLE(2, 3, 2, 3)));
1074
    Vector = GiMinNanFloat32(
1075 1076 1077 1078 1079
            Vector, _mm_shuffle_ps(Vector, Vector, _MM_SHUFFLE(1, 1, 1, 1)));
    return GiExtractLane0Float32(Vector);
#else
    float ret = Vector[0];
    for (size_t i = 1; i < GI_SIMD_LEN_BYTE / sizeof(float); i++) {
1080
        ret = MIN_NAN(ret, Vector[i]);
1081 1082 1083 1084 1085
    }
    return ret;
#endif
}

1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
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));
#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
}

1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
#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);
#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);
#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)
#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;                                                              \
    })

#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)
#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);
#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);
#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);
#else
    return ___gi_vget_high_f32(a);
#endif
}
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383

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;
#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;
#else
    float32x2_t res;
    res[0] = MAX_NAN(a[0], a[1]);
    res[1] = MAX_NAN(b[0], b[1]);
    return res;
#endif
}
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 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450

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;
#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]);
#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
}