1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384 |
- #pragma once
- #ifndef PSIMD_H
- #define PSIMD_H
- #if defined(__CUDA_ARCH__)
- /* CUDA compiler */
- #define PSIMD_INTRINSIC __forceinline__ __device__
- #elif defined(__OPENCL_VERSION__)
- /* OpenCL compiler */
- #define PSIMD_INTRINSIC inline static
- #elif defined(__INTEL_COMPILER)
- /* Intel compiler, even on Windows */
- #define PSIMD_INTRINSIC inline static __attribute__((__always_inline__))
- #elif defined(__GNUC__)
- /* GCC-compatible compiler (gcc/clang/icc) */
- #define PSIMD_INTRINSIC inline static __attribute__((__always_inline__))
- #elif defined(_MSC_VER)
- /* MSVC-compatible compiler (cl/icl/clang-cl) */
- #define PSIMD_INTRINSIC __forceinline static
- #elif defined(__cplusplus)
- /* Generic C++ compiler */
- #define PSIMD_INTRINSIC inline static
- #elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
- /* Generic C99 compiler */
- #define PSIMD_INTRINSIC inline static
- #else
- /* Generic C compiler */
- #define PSIMD_INTRINSIC static
- #endif
- #if defined(__GNUC__) || defined(__clang__)
- #if defined(__ARM_NEON__) || defined(__ARM_NEON)
- #include <arm_neon.h>
- #endif
- #if defined(__SSE2__)
- #include <emmintrin.h>
- #endif
- #if defined(__SSE3__)
- #include <pmmintrin.h>
- #endif
- #if defined(__SSSE3__)
- #include <tmmintrin.h>
- #endif
- #if defined(__SSE4_1__)
- #include <smmintrin.h>
- #endif
- #if defined(__SSE4_2__)
- #include <nmmintrin.h>
- #endif
- #if defined(__AVX__)
- #include <immintrin.h>
- #endif
- #elif defined(_MSC_VER)
- #include <intrin.h>
- #endif
- #if defined(__cplusplus)
- #define PSIMD_CXX_SYNTAX
- #elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
- #define PSIMD_C11_SYNTAX
- #elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
- #define PSIMD_C99_SYNTAX
- #else
- #define PSIMD_C89_SYNTAX
- #endif
- #if defined(__cplusplus) && (__cplusplus >= 201103L)
- #include <cstddef>
- #include <cstdint>
- #elif !defined(__OPENCL_VERSION__)
- #include <stddef.h>
- #include <stdint.h>
- #endif
- #if defined(__GNUC__) || defined(__clang__)
- #define PSIMD_HAVE_F64 0
- #define PSIMD_HAVE_F32 1
- #define PSIMD_HAVE_U8 1
- #define PSIMD_HAVE_S8 1
- #define PSIMD_HAVE_U16 1
- #define PSIMD_HAVE_S16 1
- #define PSIMD_HAVE_U32 1
- #define PSIMD_HAVE_S32 1
- #define PSIMD_HAVE_U64 0
- #define PSIMD_HAVE_S64 0
- typedef int8_t psimd_s8 __attribute__((vector_size(16), aligned(1)));
- typedef uint8_t psimd_u8 __attribute__((vector_size(16), aligned(1)));
- typedef int16_t psimd_s16 __attribute__((vector_size(16), aligned(2)));
- typedef uint16_t psimd_u16 __attribute__((vector_size(16), aligned(2)));
- typedef int32_t psimd_s32 __attribute__((vector_size(16), aligned(4)));
- typedef uint32_t psimd_u32 __attribute__((vector_size(16), aligned(4)));
- typedef float psimd_f32 __attribute__((vector_size(16), aligned(4)));
- typedef struct {
- psimd_s8 lo;
- psimd_s8 hi;
- } psimd_s8x2;
- typedef struct {
- psimd_u8 lo;
- psimd_u8 hi;
- } psimd_u8x2;
- typedef struct {
- psimd_s16 lo;
- psimd_s16 hi;
- } psimd_s16x2;
- typedef struct {
- psimd_u16 lo;
- psimd_u16 hi;
- } psimd_u16x2;
- typedef struct {
- psimd_s32 lo;
- psimd_s32 hi;
- } psimd_s32x2;
- typedef struct {
- psimd_u32 lo;
- psimd_u32 hi;
- } psimd_u32x2;
- typedef struct {
- psimd_f32 lo;
- psimd_f32 hi;
- } psimd_f32x2;
- /* Bit casts */
- PSIMD_INTRINSIC psimd_u32x2 psimd_cast_s32x2_u32x2(psimd_s32x2 v) {
- return (psimd_u32x2) { .lo = (psimd_u32) v.lo, .hi = (psimd_u32) v.hi };
- }
- PSIMD_INTRINSIC psimd_f32x2 psimd_cast_s32x2_f32x2(psimd_s32x2 v) {
- return (psimd_f32x2) { .lo = (psimd_f32) v.lo, .hi = (psimd_f32) v.hi };
- }
- PSIMD_INTRINSIC psimd_s32x2 psimd_cast_u32x2_s32x2(psimd_u32x2 v) {
- return (psimd_s32x2) { .lo = (psimd_s32) v.lo, .hi = (psimd_s32) v.hi };
- }
- PSIMD_INTRINSIC psimd_f32x2 psimd_cast_u32x2_f32x2(psimd_u32x2 v) {
- return (psimd_f32x2) { .lo = (psimd_f32) v.lo, .hi = (psimd_f32) v.hi };
- }
- PSIMD_INTRINSIC psimd_s32x2 psimd_cast_f32x2_s32x2(psimd_f32x2 v) {
- return (psimd_s32x2) { .lo = (psimd_s32) v.lo, .hi = (psimd_s32) v.hi };
- }
- PSIMD_INTRINSIC psimd_u32x2 psimd_cast_f32x2_u32x2(psimd_f32x2 v) {
- return (psimd_u32x2) { .lo = (psimd_u32) v.lo, .hi = (psimd_u32) v.hi };
- }
- /* Swap */
- PSIMD_INTRINSIC void psimd_swap_s8(psimd_s8 a[1], psimd_s8 b[1]) {
- const psimd_s8 new_a = *b;
- const psimd_s8 new_b = *a;
- *a = new_a;
- *b = new_b;
- }
- PSIMD_INTRINSIC void psimd_swap_u8(psimd_u8 a[1], psimd_u8 b[1]) {
- const psimd_u8 new_a = *b;
- const psimd_u8 new_b = *a;
- *a = new_a;
- *b = new_b;
- }
- PSIMD_INTRINSIC void psimd_swap_s16(psimd_s16 a[1], psimd_s16 b[1]) {
- const psimd_s16 new_a = *b;
- const psimd_s16 new_b = *a;
- *a = new_a;
- *b = new_b;
- }
- PSIMD_INTRINSIC void psimd_swap_u16(psimd_u16 a[1], psimd_u16 b[1]) {
- const psimd_u16 new_a = *b;
- const psimd_u16 new_b = *a;
- *a = new_a;
- *b = new_b;
- }
- PSIMD_INTRINSIC void psimd_swap_s32(psimd_s32 a[1], psimd_s32 b[1]) {
- const psimd_s32 new_a = *b;
- const psimd_s32 new_b = *a;
- *a = new_a;
- *b = new_b;
- }
- PSIMD_INTRINSIC void psimd_swap_u32(psimd_u32 a[1], psimd_u32 b[1]) {
- const psimd_u32 new_a = *b;
- const psimd_u32 new_b = *a;
- *a = new_a;
- *b = new_b;
- }
- PSIMD_INTRINSIC void psimd_swap_f32(psimd_f32 a[1], psimd_f32 b[1]) {
- const psimd_f32 new_a = *b;
- const psimd_f32 new_b = *a;
- *a = new_a;
- *b = new_b;
- }
- /* Zero-initialization */
- PSIMD_INTRINSIC psimd_s8 psimd_zero_s8(void) {
- return (psimd_s8) { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
- }
- PSIMD_INTRINSIC psimd_u8 psimd_zero_u8(void) {
- return (psimd_u8) { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
- }
- PSIMD_INTRINSIC psimd_s16 psimd_zero_s16(void) {
- return (psimd_s16) { 0, 0, 0, 0, 0, 0, 0, 0 };
- }
- PSIMD_INTRINSIC psimd_u16 psimd_zero_u16(void) {
- return (psimd_u16) { 0, 0, 0, 0, 0, 0, 0, 0 };
- }
- PSIMD_INTRINSIC psimd_s32 psimd_zero_s32(void) {
- return (psimd_s32) { 0, 0, 0, 0 };
- }
- PSIMD_INTRINSIC psimd_u32 psimd_zero_u32(void) {
- return (psimd_u32) { 0, 0, 0, 0 };
- }
- PSIMD_INTRINSIC psimd_f32 psimd_zero_f32(void) {
- return (psimd_f32) { 0.0f, 0.0f, 0.0f, 0.0f };
- }
- /* Initialization to the same constant */
- PSIMD_INTRINSIC psimd_s8 psimd_splat_s8(int8_t c) {
- return (psimd_s8) { c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c };
- }
- PSIMD_INTRINSIC psimd_u8 psimd_splat_u8(uint8_t c) {
- return (psimd_u8) { c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c };
- }
- PSIMD_INTRINSIC psimd_s16 psimd_splat_s16(int16_t c) {
- return (psimd_s16) { c, c, c, c, c, c, c, c };
- }
- PSIMD_INTRINSIC psimd_u16 psimd_splat_u16(uint16_t c) {
- return (psimd_u16) { c, c, c, c, c, c, c, c };
- }
- PSIMD_INTRINSIC psimd_s32 psimd_splat_s32(int32_t c) {
- return (psimd_s32) { c, c, c, c };
- }
- PSIMD_INTRINSIC psimd_u32 psimd_splat_u32(uint32_t c) {
- return (psimd_u32) { c, c, c, c };
- }
- PSIMD_INTRINSIC psimd_f32 psimd_splat_f32(float c) {
- return (psimd_f32) { c, c, c, c };
- }
- /* Load vector */
- PSIMD_INTRINSIC psimd_s8 psimd_load_s8(const void* address) {
- return *((const psimd_s8*) address);
- }
- PSIMD_INTRINSIC psimd_u8 psimd_load_u8(const void* address) {
- return *((const psimd_u8*) address);
- }
- PSIMD_INTRINSIC psimd_s16 psimd_load_s16(const void* address) {
- return *((const psimd_s16*) address);
- }
- PSIMD_INTRINSIC psimd_u16 psimd_load_u16(const void* address) {
- return *((const psimd_u16*) address);
- }
- PSIMD_INTRINSIC psimd_s32 psimd_load_s32(const void* address) {
- return *((const psimd_s32*) address);
- }
- PSIMD_INTRINSIC psimd_u32 psimd_load_u32(const void* address) {
- return *((const psimd_u32*) address);
- }
- PSIMD_INTRINSIC psimd_f32 psimd_load_f32(const void* address) {
- return *((const psimd_f32*) address);
- }
- PSIMD_INTRINSIC psimd_s8 psimd_load_splat_s8(const void* address) {
- return psimd_splat_s8(*((const int8_t*) address));
- }
- PSIMD_INTRINSIC psimd_u8 psimd_load_splat_u8(const void* address) {
- return psimd_splat_u8(*((const uint8_t*) address));
- }
- PSIMD_INTRINSIC psimd_s16 psimd_load_splat_s16(const void* address) {
- return psimd_splat_s16(*((const int16_t*) address));
- }
- PSIMD_INTRINSIC psimd_u16 psimd_load_splat_u16(const void* address) {
- return psimd_splat_u16(*((const uint16_t*) address));
- }
- PSIMD_INTRINSIC psimd_s32 psimd_load_splat_s32(const void* address) {
- return psimd_splat_s32(*((const int32_t*) address));
- }
- PSIMD_INTRINSIC psimd_u32 psimd_load_splat_u32(const void* address) {
- return psimd_splat_u32(*((const uint32_t*) address));
- }
- PSIMD_INTRINSIC psimd_f32 psimd_load_splat_f32(const void* address) {
- return psimd_splat_f32(*((const float*) address));
- }
- PSIMD_INTRINSIC psimd_s32 psimd_load1_s32(const void* address) {
- return (psimd_s32) { *((const int32_t*) address), 0, 0, 0 };
- }
- PSIMD_INTRINSIC psimd_u32 psimd_load1_u32(const void* address) {
- return (psimd_u32) { *((const uint32_t*) address), 0, 0, 0 };
- }
- PSIMD_INTRINSIC psimd_f32 psimd_load1_f32(const void* address) {
- return (psimd_f32) { *((const float*) address), 0.0f, 0.0f, 0.0f };
- }
- PSIMD_INTRINSIC psimd_s32 psimd_load2_s32(const void* address) {
- const int32_t* address_s32 = (const int32_t*) address;
- return (psimd_s32) { address_s32[0], address_s32[1], 0, 0 };
- }
- PSIMD_INTRINSIC psimd_u32 psimd_load2_u32(const void* address) {
- const uint32_t* address_u32 = (const uint32_t*) address;
- return (psimd_u32) { address_u32[0], address_u32[1], 0, 0 };
- }
- PSIMD_INTRINSIC psimd_f32 psimd_load2_f32(const void* address) {
- const float* address_f32 = (const float*) address;
- return (psimd_f32) { address_f32[0], address_f32[1], 0.0f, 0.0f };
- }
- PSIMD_INTRINSIC psimd_s32 psimd_load3_s32(const void* address) {
- const int32_t* address_s32 = (const int32_t*) address;
- return (psimd_s32) { address_s32[0], address_s32[1], address_s32[2], 0 };
- }
- PSIMD_INTRINSIC psimd_u32 psimd_load3_u32(const void* address) {
- const uint32_t* address_u32 = (const uint32_t*) address;
- return (psimd_u32) { address_u32[0], address_u32[1], address_u32[2], 0 };
- }
- PSIMD_INTRINSIC psimd_f32 psimd_load3_f32(const void* address) {
- const float* address_f32 = (const float*) address;
- return (psimd_f32) { address_f32[0], address_f32[1], address_f32[2], 0.0f };
- }
- PSIMD_INTRINSIC psimd_s32 psimd_load4_s32(const void* address) {
- return psimd_load_s32(address);
- }
- PSIMD_INTRINSIC psimd_u32 psimd_load4_u32(const void* address) {
- return psimd_load_u32(address);
- }
- PSIMD_INTRINSIC psimd_f32 psimd_load4_f32(const void* address) {
- return psimd_load_f32(address);
- }
- PSIMD_INTRINSIC psimd_f32 psimd_load_stride2_f32(const void* address) {
- const psimd_f32 v0x1x = psimd_load_f32(address);
- const psimd_f32 vx2x3 = psimd_load_f32((const float*) address + 3);
- #if defined(__clang__)
- return __builtin_shufflevector(v0x1x, vx2x3, 0, 2, 5, 7);
- #else
- return __builtin_shuffle(v0x1x, vx2x3, (psimd_s32) { 0, 2, 5, 7 });
- #endif
- }
- PSIMD_INTRINSIC psimd_f32 psimd_load1_stride2_f32(const void* address) {
- return psimd_load_f32(address);
- }
- PSIMD_INTRINSIC psimd_f32 psimd_load2_stride2_f32(const void* address) {
- const float* address_f32 = (const float*) address;
- return (psimd_f32) { address_f32[0], address_f32[2], 0.0f, 0.0f };
- }
- PSIMD_INTRINSIC psimd_f32 psimd_load3_stride2_f32(const void* address) {
- const psimd_f32 v0x1x = psimd_load_f32(address);
- const psimd_f32 v2zzz = psimd_load1_f32((const float*) address + 2);
- #if defined(__clang__)
- return __builtin_shufflevector(v0x1x, v2zzz, 0, 2, 4, 6);
- #else
- return __builtin_shuffle(v0x1x, v2zzz, (psimd_s32) { 0, 2, 4, 6 });
- #endif
- }
- PSIMD_INTRINSIC psimd_f32 psimd_load4_stride2_f32(const void* address) {
- return psimd_load_stride2_f32(address);
- }
- PSIMD_INTRINSIC psimd_f32 psimd_load_stride_f32(const void* address, size_t stride) {
- const float* address0_f32 = (const float*) address;
- const float* address1_f32 = address0_f32 + stride;
- const float* address2_f32 = address1_f32 + stride;
- const float* address3_f32 = address2_f32 + stride;
- return (psimd_f32) { *address0_f32, *address1_f32, *address2_f32, *address3_f32 };
- }
- PSIMD_INTRINSIC psimd_f32 psimd_load1_stride_f32(const void* address, size_t stride) {
- return psimd_load1_f32(address);
- }
- PSIMD_INTRINSIC psimd_f32 psimd_load2_stride_f32(const void* address, size_t stride) {
- const float* address_f32 = (const float*) address;
- return (psimd_f32) { address_f32[0], address_f32[stride], 0.0f, 0.0f };
- }
- PSIMD_INTRINSIC psimd_f32 psimd_load3_stride_f32(const void* address, size_t stride) {
- const float* address0_f32 = (const float*) address;
- const float* address1_f32 = address0_f32 + stride;
- const float* address2_f32 = address1_f32 + stride;
- return (psimd_f32) { *address0_f32, *address1_f32, *address2_f32, 0.0f };
- }
- PSIMD_INTRINSIC psimd_f32 psimd_load4_stride_f32(const void* address, size_t stride) {
- return psimd_load_stride_f32(address, stride);
- }
- /* Store vector */
- PSIMD_INTRINSIC void psimd_store_s8(void* address, psimd_s8 value) {
- *((psimd_s8*) address) = value;
- }
- PSIMD_INTRINSIC void psimd_store_u8(void* address, psimd_u8 value) {
- *((psimd_u8*) address) = value;
- }
- PSIMD_INTRINSIC void psimd_store_s16(void* address, psimd_s16 value) {
- *((psimd_s16*) address) = value;
- }
- PSIMD_INTRINSIC void psimd_store_u16(void* address, psimd_u16 value) {
- *((psimd_u16*) address) = value;
- }
- PSIMD_INTRINSIC void psimd_store_s32(void* address, psimd_s32 value) {
- *((psimd_s32*) address) = value;
- }
- PSIMD_INTRINSIC void psimd_store_u32(void* address, psimd_u32 value) {
- *((psimd_u32*) address) = value;
- }
- PSIMD_INTRINSIC void psimd_store_f32(void* address, psimd_f32 value) {
- *((psimd_f32*) address) = value;
- }
- PSIMD_INTRINSIC void psimd_store1_s32(void* address, psimd_s32 value) {
- *((int32_t*) address) = value[0];
- }
- PSIMD_INTRINSIC void psimd_store1_u32(void* address, psimd_u32 value) {
- *((uint32_t*) address) = value[0];
- }
- PSIMD_INTRINSIC void psimd_store1_f32(void* address, psimd_f32 value) {
- *((float*) address) = value[0];
- }
- PSIMD_INTRINSIC void psimd_store2_s32(void* address, psimd_s32 value) {
- int32_t* address_s32 = (int32_t*) address;
- address_s32[0] = value[0];
- address_s32[1] = value[1];
- }
- PSIMD_INTRINSIC void psimd_store2_u32(void* address, psimd_u32 value) {
- uint32_t* address_u32 = (uint32_t*) address;
- address_u32[0] = value[0];
- address_u32[1] = value[1];
- }
- PSIMD_INTRINSIC void psimd_store2_f32(void* address, psimd_f32 value) {
- float* address_f32 = (float*) address;
- address_f32[0] = value[0];
- address_f32[1] = value[1];
- }
- PSIMD_INTRINSIC void psimd_store3_s32(void* address, psimd_s32 value) {
- int32_t* address_s32 = (int32_t*) address;
- address_s32[0] = value[0];
- address_s32[1] = value[1];
- address_s32[2] = value[2];
- }
- PSIMD_INTRINSIC void psimd_store3_u32(void* address, psimd_u32 value) {
- uint32_t* address_u32 = (uint32_t*) address;
- address_u32[0] = value[0];
- address_u32[1] = value[1];
- address_u32[2] = value[2];
- }
- PSIMD_INTRINSIC void psimd_store3_f32(void* address, psimd_f32 value) {
- float* address_f32 = (float*) address;
- address_f32[0] = value[0];
- address_f32[1] = value[1];
- address_f32[2] = value[2];
- }
- PSIMD_INTRINSIC void psimd_store4_s32(void* address, psimd_s32 value) {
- psimd_store_s32(address, value);
- }
- PSIMD_INTRINSIC void psimd_store4_u32(void* address, psimd_u32 value) {
- psimd_store_u32(address, value);
- }
- PSIMD_INTRINSIC void psimd_store4_f32(void* address, psimd_f32 value) {
- psimd_store_f32(address, value);
- }
- PSIMD_INTRINSIC void psimd_store_stride_f32(void* address, size_t stride, psimd_f32 value) {
- float* address0_f32 = (float*) address;
- float* address1_f32 = address0_f32 + stride;
- float* address2_f32 = address1_f32 + stride;
- float* address3_f32 = address2_f32 + stride;
- *address0_f32 = value[0];
- *address1_f32 = value[1];
- *address2_f32 = value[2];
- *address3_f32 = value[3];
- }
- PSIMD_INTRINSIC void psimd_store1_stride_f32(void* address, size_t stride, psimd_f32 value) {
- psimd_store1_f32(address, value);
- }
- PSIMD_INTRINSIC void psimd_store2_stride_f32(void* address, size_t stride, psimd_f32 value) {
- float* address_f32 = (float*) address;
- address_f32[0] = value[0];
- address_f32[stride] = value[1];
- }
- PSIMD_INTRINSIC void psimd_store3_stride_f32(void* address, size_t stride, psimd_f32 value) {
- float* address0_f32 = (float*) address;
- float* address1_f32 = address0_f32 + stride;
- float* address2_f32 = address1_f32 + stride;
- *address0_f32 = value[0];
- *address1_f32 = value[1];
- *address2_f32 = value[2];
- }
- /* Vector addition */
- PSIMD_INTRINSIC psimd_s8 psimd_add_s8(psimd_s8 a, psimd_s8 b) {
- return a + b;
- }
- PSIMD_INTRINSIC psimd_u8 psimd_add_u8(psimd_u8 a, psimd_u8 b) {
- return a + b;
- }
- PSIMD_INTRINSIC psimd_s16 psimd_add_s16(psimd_s16 a, psimd_s16 b) {
- return a + b;
- }
- PSIMD_INTRINSIC psimd_u16 psimd_add_u16(psimd_u16 a, psimd_u16 b) {
- return a + b;
- }
- PSIMD_INTRINSIC psimd_s32 psimd_add_s32(psimd_s32 a, psimd_s32 b) {
- return a + b;
- }
- PSIMD_INTRINSIC psimd_u32 psimd_add_u32(psimd_u32 a, psimd_u32 b) {
- return a + b;
- }
- PSIMD_INTRINSIC psimd_f32 psimd_add_f32(psimd_f32 a, psimd_f32 b) {
- #if defined(__ARM_ARCH_7A__) && defined(__ARM_NEON__) && !defined(__FAST_MATH__)
- return (psimd_f32) vaddq_f32((float32x4_t) a, (float32x4_t) b);
- #else
- return a + b;
- #endif
- }
- /* Vector subtraction */
- PSIMD_INTRINSIC psimd_s8 psimd_sub_s8(psimd_s8 a, psimd_s8 b) {
- return a - b;
- }
- PSIMD_INTRINSIC psimd_u8 psimd_sub_u8(psimd_u8 a, psimd_u8 b) {
- return a - b;
- }
- PSIMD_INTRINSIC psimd_s16 psimd_sub_s16(psimd_s16 a, psimd_s16 b) {
- return a - b;
- }
- PSIMD_INTRINSIC psimd_u16 psimd_sub_u16(psimd_u16 a, psimd_u16 b) {
- return a - b;
- }
- PSIMD_INTRINSIC psimd_s32 psimd_sub_s32(psimd_s32 a, psimd_s32 b) {
- return a - b;
- }
- PSIMD_INTRINSIC psimd_u32 psimd_sub_u32(psimd_u32 a, psimd_u32 b) {
- return a - b;
- }
- PSIMD_INTRINSIC psimd_f32 psimd_sub_f32(psimd_f32 a, psimd_f32 b) {
- #if defined(__ARM_ARCH_7A__) && defined(__ARM_NEON__) && !defined(__FAST_MATH__)
- return (psimd_f32) vsubq_f32((float32x4_t) a, (float32x4_t) b);
- #else
- return a - b;
- #endif
- }
- /* Vector multiplication */
- PSIMD_INTRINSIC psimd_s8 psimd_mul_s8(psimd_s8 a, psimd_s8 b) {
- return a * b;
- }
- PSIMD_INTRINSIC psimd_u8 psimd_mul_u8(psimd_u8 a, psimd_u8 b) {
- return a * b;
- }
- PSIMD_INTRINSIC psimd_s16 psimd_mul_s16(psimd_s16 a, psimd_s16 b) {
- return a * b;
- }
- PSIMD_INTRINSIC psimd_u16 psimd_mul_u16(psimd_u16 a, psimd_u16 b) {
- return a * b;
- }
- PSIMD_INTRINSIC psimd_s32 psimd_mul_s32(psimd_s32 a, psimd_s32 b) {
- return a * b;
- }
- PSIMD_INTRINSIC psimd_u32 psimd_mul_u32(psimd_u32 a, psimd_u32 b) {
- return a * b;
- }
- PSIMD_INTRINSIC psimd_f32 psimd_mul_f32(psimd_f32 a, psimd_f32 b) {
- #if defined(__ARM_ARCH_7A__) && defined(__ARM_NEON__) && !defined(__FAST_MATH__)
- return (psimd_f32) vmulq_f32((float32x4_t) a, (float32x4_t) b);
- #else
- return a * b;
- #endif
- }
- /* Quasi-Fused Multiply-Add */
- PSIMD_INTRINSIC psimd_f32 psimd_qfma_f32(psimd_f32 a, psimd_f32 b, psimd_f32 c) {
- #if defined(__aarch64__) || defined(__ARM_NEON__) && defined(__ARM_FEATURE_FMA)
- return (psimd_f32) vfmaq_f32((float32x4_t) a, (float32x4_t) b, (float32x4_t) c);
- #elif (defined(__x86_64__) || defined(__i386__) || defined(__i686__)) && defined(__FMA__)
- return (psimd_f32) _mm_fmadd_ps((__m128) b, (__m128) c, (__m128) a);
- #elif (defined(__x86_64__) || defined(__i386__) || defined(__i686__)) && defined(__FMA4__)
- return (psimd_f32) _mm_macc_ps((__m128) b, (__m128) c, (__m128) a);
- #elif defined(__wasm__) && defined(__wasm_simd128__) && defined(__clang__) && PSIMD_ENABLE_WASM_QFMA
- return (psimd_f32) __builtin_wasm_qfma_f32x4(a, b, c);
- #else
- return a + b * c;
- #endif
- }
- PSIMD_INTRINSIC psimd_f32 psimd_div_f32(psimd_f32 a, psimd_f32 b) {
- return a / b;
- }
- /* Vector and */
- PSIMD_INTRINSIC psimd_f32 psimd_andmask_f32(psimd_s32 mask, psimd_f32 v) {
- return (psimd_f32) (mask & (psimd_s32) v);
- }
- /* Vector and-not */
- PSIMD_INTRINSIC psimd_f32 psimd_andnotmask_f32(psimd_s32 mask, psimd_f32 v) {
- return (psimd_f32) (~mask & (psimd_s32) v);
- }
- /* Vector blend */
- PSIMD_INTRINSIC psimd_s8 psimd_blend_s8(psimd_s8 mask, psimd_s8 a, psimd_s8 b) {
- #if defined(__ARM_NEON__) || defined(__ARM_NEON)
- return (psimd_s8) vbslq_s8((uint8x16_t) mask, (int8x16_t) a, (int8x16_t) b);
- #elif defined(__wasm__) && defined(__wasm_simd128__) && defined(__clang__)
- return (psimd_s8) __builtin_wasm_bitselect(a, b, mask);
- #else
- return (mask & a) | (~mask & b);
- #endif
- }
- PSIMD_INTRINSIC psimd_u8 psimd_blend_u8(psimd_s8 mask, psimd_u8 a, psimd_u8 b) {
- #if defined(__ARM_NEON__) || defined(__ARM_NEON)
- return (psimd_u8) vbslq_u8((uint8x16_t) mask, (uint8x16_t) a, (uint8x16_t) b);
- #elif defined(__wasm__) && defined(__wasm_simd128__) && defined(__clang__)
- return (psimd_u8) __builtin_wasm_bitselect(a, b, mask);
- #else
- return (psimd_u8) ((mask & (psimd_s8) a) | (~mask & (psimd_s8) b));
- #endif
- }
-
- PSIMD_INTRINSIC psimd_s16 psimd_blend_s16(psimd_s16 mask, psimd_s16 a, psimd_s16 b) {
- #if defined(__ARM_NEON__) || defined(__ARM_NEON)
- return (psimd_s16) vbslq_s16((uint16x8_t) mask, (int16x8_t) a, (int16x8_t) b);
- #elif defined(__wasm__) && defined(__wasm_simd128__) && defined(__clang__)
- return (psimd_s16) __builtin_wasm_bitselect(a, b, mask);
- #else
- return (mask & a) | (~mask & b);
- #endif
- }
- PSIMD_INTRINSIC psimd_u16 psimd_blend_u16(psimd_s16 mask, psimd_u16 a, psimd_u16 b) {
- #if defined(__ARM_NEON__) || defined(__ARM_NEON)
- return (psimd_u16) vbslq_u16((uint16x8_t) mask, (uint16x8_t) a, (uint16x8_t) b);
- #elif defined(__wasm__) && defined(__wasm_simd128__) && defined(__clang__)
- return (psimd_u16) __builtin_wasm_bitselect(a, b, mask);
- #else
- return (psimd_u16) ((mask & (psimd_s16) a) | (~mask & (psimd_s16) b));
- #endif
- }
-
- PSIMD_INTRINSIC psimd_s32 psimd_blend_s32(psimd_s32 mask, psimd_s32 a, psimd_s32 b) {
- #if defined(__ARM_NEON__) || defined(__ARM_NEON)
- return (psimd_s32) vbslq_s32((uint32x4_t) mask, (int32x4_t) a, (int32x4_t) b);
- #elif defined(__wasm__) && defined(__wasm_simd128__) && defined(__clang__)
- return (psimd_s32) __builtin_wasm_bitselect(a, b, mask);
- #else
- return (mask & a) | (~mask & b);
- #endif
- }
- PSIMD_INTRINSIC psimd_u32 psimd_blend_u32(psimd_s32 mask, psimd_u32 a, psimd_u32 b) {
- #if defined(__ARM_NEON__) || defined(__ARM_NEON)
- return (psimd_u32) vbslq_u32((uint32x4_t) mask, (uint32x4_t) a, (uint32x4_t) b);
- #elif defined(__wasm__) && defined(__wasm_simd128__) && defined(__clang__)
- return (psimd_u32) __builtin_wasm_bitselect(a, b, mask);
- #else
- return (psimd_u32) ((mask & (psimd_s32) a) | (~mask & (psimd_s32) b));
- #endif
- }
-
- PSIMD_INTRINSIC psimd_f32 psimd_blend_f32(psimd_s32 mask, psimd_f32 a, psimd_f32 b) {
- #if defined(__ARM_NEON__) || defined(__ARM_NEON)
- return (psimd_f32) vbslq_f32((uint32x4_t) mask, (float32x4_t) a, (float32x4_t) b);
- #elif defined(__wasm__) && defined(__wasm_simd128__) && defined(__clang__)
- return (psimd_f32) __builtin_wasm_bitselect(a, b, mask);
- #else
- return (psimd_f32) ((mask & (psimd_s32) a) | (~mask & (psimd_s32) b));
- #endif
- }
- /* Vector blend on sign */
- PSIMD_INTRINSIC psimd_s8 psimd_signblend_s8(psimd_s8 x, psimd_s8 a, psimd_s8 b) {
- return psimd_blend_s8(x >> psimd_splat_s8(7), a, b);
- }
- PSIMD_INTRINSIC psimd_u8 psimd_signblend_u8(psimd_s8 x, psimd_u8 a, psimd_u8 b) {
- return psimd_blend_u8((x >> psimd_splat_s8(7)), a, b);
- }
- PSIMD_INTRINSIC psimd_s16 psimd_signblend_s16(psimd_s16 x, psimd_s16 a, psimd_s16 b) {
- return psimd_blend_s16(x >> psimd_splat_s16(15), a, b);
- }
- PSIMD_INTRINSIC psimd_u16 psimd_signblend_u16(psimd_s16 x, psimd_u16 a, psimd_u16 b) {
- return psimd_blend_u16((x >> psimd_splat_s16(15)), a, b);
- }
- PSIMD_INTRINSIC psimd_s32 psimd_signblend_s32(psimd_s32 x, psimd_s32 a, psimd_s32 b) {
- return psimd_blend_s32(x >> psimd_splat_s32(31), a, b);
- }
- PSIMD_INTRINSIC psimd_u32 psimd_signblend_u32(psimd_s32 x, psimd_u32 a, psimd_u32 b) {
- return psimd_blend_u32((x >> psimd_splat_s32(31)), a, b);
- }
- PSIMD_INTRINSIC psimd_f32 psimd_signblend_f32(psimd_f32 x, psimd_f32 a, psimd_f32 b) {
- const psimd_s32 mask = (psimd_s32) x >> psimd_splat_s32(31);
- return psimd_blend_f32(mask, a, b);
- }
- /* Vector absolute value */
- PSIMD_INTRINSIC psimd_f32 psimd_abs_f32(psimd_f32 v) {
- const psimd_s32 mask = (psimd_s32) psimd_splat_f32(-0.0f);
- return (psimd_f32) ((psimd_s32) v & ~mask);
- }
- /* Vector negation */
- PSIMD_INTRINSIC psimd_f32 psimd_neg_f32(psimd_f32 v) {
- const psimd_s32 mask = (psimd_s32) psimd_splat_f32(-0.0f);
- return (psimd_f32) ((psimd_s32) v ^ mask);
- }
- /* Vector maximum */
- PSIMD_INTRINSIC psimd_s8 psimd_max_s8(psimd_s8 a, psimd_s8 b) {
- #if defined(__ARM_NEON__) || defined(__ARM_NEON)
- return (psimd_s8) vmaxq_s8((int8x16_t) a, (int8x16_t) b);
- #else
- return psimd_blend_s8(a > b, a, b);
- #endif
- }
- PSIMD_INTRINSIC psimd_u8 psimd_max_u8(psimd_u8 a, psimd_u8 b) {
- #if defined(__ARM_NEON__) || defined(__ARM_NEON)
- return (psimd_u8) vmaxq_u8((uint8x16_t) a, (uint8x16_t) b);
- #else
- return psimd_blend_u8(a > b, a, b);
- #endif
- }
- PSIMD_INTRINSIC psimd_s16 psimd_max_s16(psimd_s16 a, psimd_s16 b) {
- #if defined(__ARM_NEON__) || defined(__ARM_NEON)
- return (psimd_s16) vmaxq_s16((int16x8_t) a, (int16x8_t) b);
- #else
- return psimd_blend_s16(a > b, a, b);
- #endif
- }
- PSIMD_INTRINSIC psimd_u16 psimd_max_u16(psimd_u16 a, psimd_u16 b) {
- #if defined(__ARM_NEON__) || defined(__ARM_NEON)
- return (psimd_u16) vmaxq_u16((uint16x8_t) a, (uint16x8_t) b);
- #else
- return psimd_blend_u16(a > b, a, b);
- #endif
- }
- PSIMD_INTRINSIC psimd_s32 psimd_max_s32(psimd_s32 a, psimd_s32 b) {
- #if defined(__ARM_NEON__) || defined(__ARM_NEON)
- return (psimd_s32) vmaxq_s32((int32x4_t) a, (int32x4_t) b);
- #else
- return psimd_blend_s32(a > b, a, b);
- #endif
- }
- PSIMD_INTRINSIC psimd_u32 psimd_max_u32(psimd_u32 a, psimd_u32 b) {
- #if defined(__ARM_NEON__) || defined(__ARM_NEON)
- return (psimd_u32) vmaxq_u32((uint32x4_t) a, (uint32x4_t) b);
- #else
- return psimd_blend_u32(a > b, a, b);
- #endif
- }
- PSIMD_INTRINSIC psimd_f32 psimd_max_f32(psimd_f32 a, psimd_f32 b) {
- #if defined(__ARM_NEON__) || defined(__ARM_NEON)
- return (psimd_f32) vmaxq_f32((float32x4_t) a, (float32x4_t) b);
- #elif defined(__wasm__) && defined(__wasm_simd128__) && defined(__clang__)
- return __builtin_wasm_max_f32x4(a, b);
- #else
- return psimd_blend_f32(a > b, a, b);
- #endif
- }
- /* Vector minimum */
- PSIMD_INTRINSIC psimd_s8 psimd_min_s8(psimd_s8 a, psimd_s8 b) {
- #if defined(__ARM_NEON__) || defined(__ARM_NEON)
- return (psimd_s8) vminq_s8((int8x16_t) a, (int8x16_t) b);
- #else
- return psimd_blend_s8(a < b, a, b);
- #endif
- }
- PSIMD_INTRINSIC psimd_u8 psimd_min_u8(psimd_u8 a, psimd_u8 b) {
- #if defined(__ARM_NEON__) || defined(__ARM_NEON)
- return (psimd_u8) vminq_u8((uint8x16_t) a, (uint8x16_t) b);
- #else
- return psimd_blend_u8(a < b, a, b);
- #endif
- }
- PSIMD_INTRINSIC psimd_s16 psimd_min_s16(psimd_s16 a, psimd_s16 b) {
- #if defined(__ARM_NEON__) || defined(__ARM_NEON)
- return (psimd_s16) vminq_s16((int16x8_t) a, (int16x8_t) b);
- #else
- return psimd_blend_s16(a < b, a, b);
- #endif
- }
- PSIMD_INTRINSIC psimd_u16 psimd_min_u16(psimd_u16 a, psimd_u16 b) {
- #if defined(__ARM_NEON__) || defined(__ARM_NEON)
- return (psimd_u16) vminq_u16((uint16x8_t) a, (uint16x8_t) b);
- #else
- return psimd_blend_u16(a < b, a, b);
- #endif
- }
- PSIMD_INTRINSIC psimd_s32 psimd_min_s32(psimd_s32 a, psimd_s32 b) {
- #if defined(__ARM_NEON__) || defined(__ARM_NEON)
- return (psimd_s32) vminq_s32((int32x4_t) a, (int32x4_t) b);
- #else
- return psimd_blend_s32(a < b, a, b);
- #endif
- }
- PSIMD_INTRINSIC psimd_u32 psimd_min_u32(psimd_u32 a, psimd_u32 b) {
- #if defined(__ARM_NEON__) || defined(__ARM_NEON)
- return (psimd_u32) vminq_u32((uint32x4_t) a, (uint32x4_t) b);
- #else
- return psimd_blend_u32(a < b, a, b);
- #endif
- }
- PSIMD_INTRINSIC psimd_f32 psimd_min_f32(psimd_f32 a, psimd_f32 b) {
- #if defined(__ARM_NEON__) || defined(__ARM_NEON)
- return (psimd_f32) vminq_f32((float32x4_t) a, (float32x4_t) b);
- #elif defined(__wasm__) && defined(__wasm_simd128__) && defined(__clang__)
- return __builtin_wasm_min_f32x4(a, b);
- #else
- return psimd_blend_f32(a < b, a, b);
- #endif
- }
- PSIMD_INTRINSIC psimd_f32 psimd_cvt_s32_f32(psimd_s32 v) {
- #if defined(__clang__)
- return __builtin_convertvector(v, psimd_f32);
- #elif defined(__ARM_NEON__) || defined(__ARM_NEON)
- return (psimd_f32) vcvtq_f32_s32((int32x4_t) v);
- #elif defined(__SSE2__)
- return (psimd_f32) _mm_cvtepi32_ps((__m128i) v);
- #else
- return (psimd_f32) { (float) v[0], (float) v[1], (float) v[2], (float) v[3] };
- #endif
- }
- /* Broadcast vector element */
- #if defined(__clang__)
- PSIMD_INTRINSIC psimd_f32 psimd_splat0_f32(psimd_f32 v) {
- return __builtin_shufflevector(v, v, 0, 0, 0, 0);
- }
- PSIMD_INTRINSIC psimd_f32 psimd_splat1_f32(psimd_f32 v) {
- return __builtin_shufflevector(v, v, 1, 1, 1, 1);
- }
- PSIMD_INTRINSIC psimd_f32 psimd_splat2_f32(psimd_f32 v) {
- return __builtin_shufflevector(v, v, 2, 2, 2, 2);
- }
- PSIMD_INTRINSIC psimd_f32 psimd_splat3_f32(psimd_f32 v) {
- return __builtin_shufflevector(v, v, 3, 3, 3, 3);
- }
- #else
- PSIMD_INTRINSIC psimd_f32 psimd_splat0_f32(psimd_f32 v) {
- return __builtin_shuffle(v, (psimd_s32) { 0, 0, 0, 0 });
- }
- PSIMD_INTRINSIC psimd_f32 psimd_splat1_f32(psimd_f32 v) {
- return __builtin_shuffle(v, (psimd_s32) { 1, 1, 1, 1 });
- }
- PSIMD_INTRINSIC psimd_f32 psimd_splat2_f32(psimd_f32 v) {
- return __builtin_shuffle(v, (psimd_s32) { 2, 2, 2, 2 });
- }
- PSIMD_INTRINSIC psimd_f32 psimd_splat3_f32(psimd_f32 v) {
- return __builtin_shuffle(v, (psimd_s32) { 3, 3, 3, 3 });
- }
- #endif
- /* Reversal of vector elements */
- #if defined(__clang__)
- PSIMD_INTRINSIC psimd_s8 psimd_reverse_s8(psimd_s8 v) {
- return __builtin_shufflevector(v, v, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0);
- }
- PSIMD_INTRINSIC psimd_u8 psimd_reverse_u8(psimd_u8 v) {
- return __builtin_shufflevector(v, v, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0);
- }
- PSIMD_INTRINSIC psimd_s16 psimd_reverse_s16(psimd_s16 v) {
- return __builtin_shufflevector(v, v, 7, 6, 5, 4, 3, 2, 1, 0);
- }
- PSIMD_INTRINSIC psimd_u16 psimd_reverse_u16(psimd_u16 v) {
- return __builtin_shufflevector(v, v, 7, 6, 5, 4, 3, 2, 1, 0);
- }
- PSIMD_INTRINSIC psimd_s32 psimd_reverse_s32(psimd_s32 v) {
- return __builtin_shufflevector(v, v, 3, 2, 1, 0);
- }
- PSIMD_INTRINSIC psimd_u32 psimd_reverse_u32(psimd_u32 v) {
- return __builtin_shufflevector(v, v, 3, 2, 1, 0);
- }
- PSIMD_INTRINSIC psimd_f32 psimd_reverse_f32(psimd_f32 v) {
- return __builtin_shufflevector(v, v, 3, 2, 1, 0);
- }
- #else
- PSIMD_INTRINSIC psimd_s8 psimd_reverse_s8(psimd_s8 v) {
- return __builtin_shuffle(v, (psimd_s8) { 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 });
- }
- PSIMD_INTRINSIC psimd_u8 psimd_reverse_u8(psimd_u8 v) {
- return __builtin_shuffle(v, (psimd_s8) { 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 });
- }
- PSIMD_INTRINSIC psimd_s16 psimd_reverse_s16(psimd_s16 v) {
- return __builtin_shuffle(v, (psimd_s16) { 7, 6, 5, 4, 3, 2, 1, 0 });
- }
- PSIMD_INTRINSIC psimd_u16 psimd_reverse_u16(psimd_u16 v) {
- return __builtin_shuffle(v, (psimd_s16) { 7, 6, 5, 4, 3, 2, 1, 0 });
- }
- PSIMD_INTRINSIC psimd_s32 psimd_reverse_s32(psimd_s32 v) {
- return __builtin_shuffle(v, (psimd_s32) { 3, 2, 1, 0 });
- }
- PSIMD_INTRINSIC psimd_u32 psimd_reverse_u32(psimd_u32 v) {
- return __builtin_shuffle(v, (psimd_s32) { 3, 2, 1, 0 });
- }
- PSIMD_INTRINSIC psimd_f32 psimd_reverse_f32(psimd_f32 v) {
- return __builtin_shuffle(v, (psimd_s32) { 3, 2, 1, 0 });
- }
- #endif
- /* Interleaving of vector elements */
- #if defined(__clang__)
- PSIMD_INTRINSIC psimd_s16 psimd_interleave_lo_s16(psimd_s16 a, psimd_s16 b) {
- return __builtin_shufflevector(a, b, 0, 8+0, 1, 8+1, 2, 8+2, 3, 8+3);
- }
- PSIMD_INTRINSIC psimd_s16 psimd_interleave_hi_s16(psimd_s16 a, psimd_s16 b) {
- return __builtin_shufflevector(a, b, 4, 8+4, 5, 8+5, 6, 8+6, 7, 8+7);
- }
- PSIMD_INTRINSIC psimd_u16 psimd_interleave_lo_u16(psimd_u16 a, psimd_u16 b) {
- return __builtin_shufflevector(a, b, 0, 8+0, 1, 8+1, 2, 8+2, 3, 8+3);
- }
- PSIMD_INTRINSIC psimd_u16 psimd_interleave_hi_u16(psimd_u16 a, psimd_u16 b) {
- return __builtin_shufflevector(a, b, 4, 8+4, 5, 8+5, 6, 8+6, 7, 8+7);
- }
- PSIMD_INTRINSIC psimd_s32 psimd_interleave_lo_s32(psimd_s32 a, psimd_s32 b) {
- return __builtin_shufflevector(a, b, 0, 4+0, 1, 4+1);
- }
- PSIMD_INTRINSIC psimd_s32 psimd_interleave_hi_s32(psimd_s32 a, psimd_s32 b) {
- return __builtin_shufflevector(a, b, 2, 4+2, 3, 4+3);
- }
- PSIMD_INTRINSIC psimd_u32 psimd_interleave_lo_u32(psimd_u32 a, psimd_u32 b) {
- return __builtin_shufflevector(a, b, 0, 4+0, 1, 4+1);
- }
- PSIMD_INTRINSIC psimd_u32 psimd_interleave_hi_u32(psimd_u32 a, psimd_u32 b) {
- return __builtin_shufflevector(a, b, 2, 4+2, 3, 4+3);
- }
- PSIMD_INTRINSIC psimd_f32 psimd_interleave_lo_f32(psimd_f32 a, psimd_f32 b) {
- return __builtin_shufflevector(a, b, 0, 4+0, 1, 4+1);
- }
- PSIMD_INTRINSIC psimd_f32 psimd_interleave_hi_f32(psimd_f32 a, psimd_f32 b) {
- return __builtin_shufflevector(a, b, 2, 4+2, 3, 4+3);
- }
- #else
- PSIMD_INTRINSIC psimd_s16 psimd_interleave_lo_s16(psimd_s16 a, psimd_s16 b) {
- return __builtin_shuffle(a, b, (psimd_s16) { 0, 8+0, 1, 8+1, 2, 8+2, 3, 8+3 });
- }
- PSIMD_INTRINSIC psimd_s16 psimd_interleave_hi_s16(psimd_s16 a, psimd_s16 b) {
- return __builtin_shuffle(a, b, (psimd_s16) { 4, 8+4, 5, 8+5, 6, 8+6, 7, 8+7 });
- }
- PSIMD_INTRINSIC psimd_u16 psimd_interleave_lo_u16(psimd_u16 a, psimd_u16 b) {
- return __builtin_shuffle(a, b, (psimd_s16) { 0, 8+0, 1, 8+1, 2, 8+2, 3, 8+3 });
- }
- PSIMD_INTRINSIC psimd_u16 psimd_interleave_hi_u16(psimd_u16 a, psimd_u16 b) {
- return __builtin_shuffle(a, b, (psimd_s16) { 4, 8+4, 5, 8+5, 6, 8+6, 7, 8+7 });
- }
- PSIMD_INTRINSIC psimd_s32 psimd_interleave_lo_s32(psimd_s32 a, psimd_s32 b) {
- return __builtin_shuffle(a, b, (psimd_s32) { 0, 4+0, 1, 4+1 });
- }
- PSIMD_INTRINSIC psimd_s32 psimd_interleave_hi_s32(psimd_s32 a, psimd_s32 b) {
- return __builtin_shuffle(a, b, (psimd_s32) { 2, 4+2, 3, 4+3 });
- }
- PSIMD_INTRINSIC psimd_u32 psimd_interleave_lo_u32(psimd_u32 a, psimd_u32 b) {
- return __builtin_shuffle(a, b, (psimd_s32) { 0, 4+0, 1, 4+1 });
- }
- PSIMD_INTRINSIC psimd_u32 psimd_interleave_hi_u32(psimd_u32 a, psimd_u32 b) {
- return __builtin_shuffle(a, b, (psimd_s32) { 2, 4+2, 3, 4+3 });
- }
- PSIMD_INTRINSIC psimd_f32 psimd_interleave_lo_f32(psimd_f32 a, psimd_f32 b) {
- return __builtin_shuffle(a, b, (psimd_s32) { 0, 4+0, 1, 4+1 });
- }
- PSIMD_INTRINSIC psimd_f32 psimd_interleave_hi_f32(psimd_f32 a, psimd_f32 b) {
- return __builtin_shuffle(a, b, (psimd_s32) { 2, 4+2, 3, 4+3 });
- }
- #endif
- /* Concatenation of low/high vector elements */
- #if defined(__clang__)
- PSIMD_INTRINSIC psimd_s16 psimd_concat_lo_s16(psimd_s16 a, psimd_s16 b) {
- return __builtin_shufflevector(a, b, 0, 1, 2, 3, 8+0, 8+1, 8+2, 8+3);
- }
- PSIMD_INTRINSIC psimd_s16 psimd_concat_hi_s16(psimd_s16 a, psimd_s16 b) {
- return __builtin_shufflevector(a, b, 4, 5, 6, 7, 8+4, 8+5, 8+6, 8+7);
- }
- PSIMD_INTRINSIC psimd_u16 psimd_concat_lo_u16(psimd_u16 a, psimd_u16 b) {
- return __builtin_shufflevector(a, b, 0, 1, 2, 3, 8+0, 8+1, 8+2, 8+3);
- }
- PSIMD_INTRINSIC psimd_u16 psimd_concat_hi_u16(psimd_u16 a, psimd_u16 b) {
- return __builtin_shufflevector(a, b, 4, 5, 6, 7, 8+4, 8+5, 8+6, 8+7);
- }
- PSIMD_INTRINSIC psimd_s32 psimd_concat_lo_s32(psimd_s32 a, psimd_s32 b) {
- return __builtin_shufflevector(a, b, 0, 1, 4+0, 4+1);
- }
- PSIMD_INTRINSIC psimd_s32 psimd_concat_hi_s32(psimd_s32 a, psimd_s32 b) {
- return __builtin_shufflevector(a, b, 2, 3, 4+2, 4+3);
- }
- PSIMD_INTRINSIC psimd_u32 psimd_concat_lo_u32(psimd_u32 a, psimd_u32 b) {
- return __builtin_shufflevector(a, b, 0, 1, 4+0, 4+1);
- }
- PSIMD_INTRINSIC psimd_u32 psimd_concat_hi_u32(psimd_u32 a, psimd_u32 b) {
- return __builtin_shufflevector(a, b, 2, 3, 4+2, 4+3);
- }
- PSIMD_INTRINSIC psimd_f32 psimd_concat_lo_f32(psimd_f32 a, psimd_f32 b) {
- return __builtin_shufflevector(a, b, 0, 1, 4+0, 4+1);
- }
- PSIMD_INTRINSIC psimd_f32 psimd_concat_hi_f32(psimd_f32 a, psimd_f32 b) {
- return __builtin_shufflevector(a, b, 2, 3, 4+2, 4+3);
- }
- #else
- PSIMD_INTRINSIC psimd_s16 psimd_concat_lo_s16(psimd_s16 a, psimd_s16 b) {
- return __builtin_shuffle(a, b, (psimd_s16) { 0, 1, 2, 3, 8+0, 8+1, 8+2, 8+3 });
- }
- PSIMD_INTRINSIC psimd_s16 psimd_concat_hi_s16(psimd_s16 a, psimd_s16 b) {
- return __builtin_shuffle(a, b, (psimd_s16) { 4, 5, 6, 7, 8+4, 8+5, 8+6, 8+7 });
- }
- PSIMD_INTRINSIC psimd_u16 psimd_concat_lo_u16(psimd_u16 a, psimd_u16 b) {
- return __builtin_shuffle(a, b, (psimd_s16) { 0, 1, 2, 3, 8+0, 8+1, 8+2, 8+3 });
- }
- PSIMD_INTRINSIC psimd_u16 psimd_concat_hi_u16(psimd_u16 a, psimd_u16 b) {
- return __builtin_shuffle(a, b, (psimd_s16) { 4, 5, 6, 7, 8+4, 8+5, 8+6, 8+7 });
- }
- PSIMD_INTRINSIC psimd_s32 psimd_concat_lo_s32(psimd_s32 a, psimd_s32 b) {
- return __builtin_shuffle(a, b, (psimd_s32) { 0, 1, 4+0, 4+1 });
- }
- PSIMD_INTRINSIC psimd_s32 psimd_concat_hi_s32(psimd_s32 a, psimd_s32 b) {
- return __builtin_shuffle(a, b, (psimd_s32) { 2, 3, 4+2, 4+3 });
- }
- PSIMD_INTRINSIC psimd_u32 psimd_concat_lo_u32(psimd_u32 a, psimd_u32 b) {
- return __builtin_shuffle(a, b, (psimd_s32) { 0, 1, 4+0, 4+1 });
- }
- PSIMD_INTRINSIC psimd_u32 psimd_concat_hi_u32(psimd_u32 a, psimd_u32 b) {
- return __builtin_shuffle(a, b, (psimd_s32) { 2, 3, 4+2, 4+3 });
- }
- PSIMD_INTRINSIC psimd_f32 psimd_concat_lo_f32(psimd_f32 a, psimd_f32 b) {
- return __builtin_shuffle(a, b, (psimd_s32) { 0, 1, 4+0, 4+1 });
- }
- PSIMD_INTRINSIC psimd_f32 psimd_concat_hi_f32(psimd_f32 a, psimd_f32 b) {
- return __builtin_shuffle(a, b, (psimd_s32) { 2, 3, 4+2, 4+3 });
- }
- #endif
- /* Concatenation of even/odd vector elements */
- #if defined(__clang__)
- PSIMD_INTRINSIC psimd_s8 psimd_concat_even_s8(psimd_s8 a, psimd_s8 b) {
- return __builtin_shufflevector(a, b,
- 0, 2, 4, 6, 8, 10, 12, 14, 16+0, 16+2, 16+4, 16+6, 16+8, 16+10, 16+12, 16+14);
- }
- PSIMD_INTRINSIC psimd_s8 psimd_concat_odd_s8(psimd_s8 a, psimd_s8 b) {
- return __builtin_shufflevector(a, b,
- 1, 3, 5, 7, 9, 11, 13, 15, 16+1, 16+3, 16+5, 16+7, 16+9, 16+11, 16+13, 16+15);
- }
- PSIMD_INTRINSIC psimd_u8 psimd_concat_even_u8(psimd_u8 a, psimd_u8 b) {
- return __builtin_shufflevector(a, b,
- 0, 2, 4, 6, 8, 10, 12, 14, 16+0, 16+2, 16+4, 16+6, 16+8, 16+10, 16+12, 16+14);
- }
- PSIMD_INTRINSIC psimd_u8 psimd_concat_odd_u8(psimd_u8 a, psimd_u8 b) {
- return __builtin_shufflevector(a, b,
- 1, 3, 5, 7, 9, 11, 13, 15, 16+1, 16+3, 16+5, 16+7, 16+9, 16+11, 16+13, 16+15);
- }
- PSIMD_INTRINSIC psimd_s16 psimd_concat_even_s16(psimd_s16 a, psimd_s16 b) {
- return __builtin_shufflevector(a, b, 0, 2, 4, 6, 8+0, 8+2, 8+4, 8+6);
- }
- PSIMD_INTRINSIC psimd_s16 psimd_concat_odd_s16(psimd_s16 a, psimd_s16 b) {
- return __builtin_shufflevector(a, b, 1, 3, 5, 7, 8+1, 8+3, 8+5, 8+7);
- }
- PSIMD_INTRINSIC psimd_u16 psimd_concat_even_u16(psimd_u16 a, psimd_u16 b) {
- return __builtin_shufflevector(a, b, 0, 2, 4, 6, 8+0, 8+2, 8+4, 8+6);
- }
- PSIMD_INTRINSIC psimd_u16 psimd_concat_odd_u16(psimd_u16 a, psimd_u16 b) {
- return __builtin_shufflevector(a, b, 1, 3, 5, 7, 8+1, 8+3, 8+5, 8+7);
- }
- PSIMD_INTRINSIC psimd_s32 psimd_concat_even_s32(psimd_s32 a, psimd_s32 b) {
- return __builtin_shufflevector(a, b, 0, 2, 4+0, 4+2);
- }
- PSIMD_INTRINSIC psimd_s32 psimd_concat_odd_s32(psimd_s32 a, psimd_s32 b) {
- return __builtin_shufflevector(a, b, 1, 3, 4+1, 4+3);
- }
- PSIMD_INTRINSIC psimd_u32 psimd_concat_even_u32(psimd_u32 a, psimd_u32 b) {
- return __builtin_shufflevector(a, b, 0, 2, 4+0, 4+2);
- }
- PSIMD_INTRINSIC psimd_u32 psimd_concat_odd_u32(psimd_u32 a, psimd_u32 b) {
- return __builtin_shufflevector(a, b, 1, 3, 4+1, 4+3);
- }
- PSIMD_INTRINSIC psimd_f32 psimd_concat_even_f32(psimd_f32 a, psimd_f32 b) {
- return __builtin_shufflevector(a, b, 0, 2, 4+0, 4+2);
- }
- PSIMD_INTRINSIC psimd_f32 psimd_concat_odd_f32(psimd_f32 a, psimd_f32 b) {
- return __builtin_shufflevector(a, b, 1, 3, 4+1, 4+3);
- }
- #else
- PSIMD_INTRINSIC psimd_s8 psimd_concat_even_s8(psimd_s8 a, psimd_s8 b) {
- return __builtin_shuffle(a, b,
- (psimd_s8) { 0, 2, 4, 6, 8, 10, 12, 14, 16+0, 16+2, 16+4, 16+6, 16+8, 16+10, 16+12, 16+14 });
- }
- PSIMD_INTRINSIC psimd_s8 psimd_concat_odd_s8(psimd_s8 a, psimd_s8 b) {
- return __builtin_shuffle(a, b,
- (psimd_s8) { 1, 3, 5, 7, 9, 11, 13, 15, 16+1, 16+3, 16+5, 16+7, 16+9, 16+11, 16+13, 16+15 });
- }
- PSIMD_INTRINSIC psimd_u8 psimd_concat_even_u8(psimd_u8 a, psimd_u8 b) {
- return __builtin_shuffle(a, b,
- (psimd_s8) { 0, 2, 4, 6, 8, 10, 12, 14, 16+0, 16+2, 16+4, 16+6, 16+8, 16+10, 16+12, 16+14 });
- }
- PSIMD_INTRINSIC psimd_u8 psimd_concat_odd_u8(psimd_u8 a, psimd_u8 b) {
- return __builtin_shuffle(a, b,
- (psimd_s8) { 1, 3, 5, 7, 9, 11, 13, 15, 16+1, 16+3, 16+5, 16+7, 16+9, 16+11, 16+13, 16+15 });
- }
- PSIMD_INTRINSIC psimd_s16 psimd_concat_even_s16(psimd_s16 a, psimd_s16 b) {
- return __builtin_shuffle(a, b, (psimd_s16) { 0, 2, 4, 6, 8+0, 8+2, 8+4, 8+6 });
- }
- PSIMD_INTRINSIC psimd_s16 psimd_concat_odd_s16(psimd_s16 a, psimd_s16 b) {
- return __builtin_shuffle(a, b, (psimd_s16) { 1, 3, 5, 7, 8+1, 8+3, 8+5, 8+7 });
- }
- PSIMD_INTRINSIC psimd_u16 psimd_concat_even_u16(psimd_u16 a, psimd_u16 b) {
- return __builtin_shuffle(a, b, (psimd_s16) { 0, 2, 4, 6, 8+0, 8+2, 8+4, 8+6 });
- }
- PSIMD_INTRINSIC psimd_u16 psimd_concat_odd_u16(psimd_u16 a, psimd_u16 b) {
- return __builtin_shuffle(a, b, (psimd_s16) { 1, 3, 5, 7, 8+1, 8+3, 8+5, 8+7 });
- }
- PSIMD_INTRINSIC psimd_s32 psimd_concat_even_s32(psimd_s32 a, psimd_s32 b) {
- return __builtin_shuffle(a, b, (psimd_s32) { 0, 2, 4+0, 4+2 });
- }
- PSIMD_INTRINSIC psimd_s32 psimd_concat_odd_s32(psimd_s32 a, psimd_s32 b) {
- return __builtin_shuffle(a, b, (psimd_s32) { 1, 3, 4+1, 4+3 });
- }
- PSIMD_INTRINSIC psimd_u32 psimd_concat_even_u32(psimd_u32 a, psimd_u32 b) {
- return __builtin_shuffle(a, b, (psimd_s32) { 0, 2, 4+0, 4+2 });
- }
- PSIMD_INTRINSIC psimd_u32 psimd_concat_odd_u32(psimd_u32 a, psimd_u32 b) {
- return __builtin_shuffle(a, b, (psimd_s32) { 1, 3, 4+1, 4+3 });
- }
- PSIMD_INTRINSIC psimd_f32 psimd_concat_even_f32(psimd_f32 a, psimd_f32 b) {
- return __builtin_shuffle(a, b, (psimd_s32) { 0, 2, 4+0, 4+2 });
- }
- PSIMD_INTRINSIC psimd_f32 psimd_concat_odd_f32(psimd_f32 a, psimd_f32 b) {
- return __builtin_shuffle(a, b, (psimd_s32) { 1, 3, 4+1, 4+3 });
- }
- #endif
- /* Vector reduce */
- #if defined(__clang__)
- PSIMD_INTRINSIC psimd_f32 psimd_allreduce_sum_f32(psimd_f32 v) {
- const psimd_f32 temp = v + __builtin_shufflevector(v, v, 2, 3, 0, 1);
- return temp + __builtin_shufflevector(temp, temp, 1, 0, 3, 2);
- }
- PSIMD_INTRINSIC psimd_f32 psimd_allreduce_max_f32(psimd_f32 v) {
- const psimd_f32 temp = psimd_max_f32(v, __builtin_shufflevector(v, v, 2, 3, 0, 1));
- return psimd_max_f32(temp, __builtin_shufflevector(temp, temp, 1, 0, 3, 2));
- }
- PSIMD_INTRINSIC psimd_f32 psimd_allreduce_min_f32(psimd_f32 v) {
- const psimd_f32 temp = psimd_min_f32(v, __builtin_shufflevector(v, v, 2, 3, 0, 1));
- return psimd_min_f32(temp, __builtin_shufflevector(temp, temp, 1, 0, 3, 2));
- }
- PSIMD_INTRINSIC float psimd_reduce_sum_f32(psimd_f32 v) {
- const psimd_f32 temp = v + __builtin_shufflevector(v, v, 2, 3, -1, -1);
- const psimd_f32 result = temp + __builtin_shufflevector(temp, temp, 1, -1, -1, -1);
- return result[0];
- }
- PSIMD_INTRINSIC float psimd_reduce_max_f32(psimd_f32 v) {
- const psimd_f32 temp = psimd_max_f32(v, __builtin_shufflevector(v, v, 2, 3, -1, -1));
- const psimd_f32 result = psimd_max_f32(temp, __builtin_shufflevector(temp, temp, 1, -1, -1, -1));
- return result[0];
- }
- PSIMD_INTRINSIC float psimd_reduce_min_f32(psimd_f32 v) {
- const psimd_f32 temp = psimd_min_f32(v, __builtin_shufflevector(v, v, 2, 3, -1, -1));
- const psimd_f32 result = psimd_min_f32(temp, __builtin_shufflevector(temp, temp, 1, -1, -1, -1));
- return result[0];
- }
- #else
- PSIMD_INTRINSIC psimd_f32 psimd_allreduce_sum_f32(psimd_f32 v) {
- const psimd_f32 temp = v + __builtin_shuffle(v, (psimd_s32) { 2, 3, 0, 1 });
- return temp + __builtin_shuffle(temp, (psimd_s32) { 1, 0, 3, 2 });
- }
- PSIMD_INTRINSIC psimd_f32 psimd_allreduce_max_f32(psimd_f32 v) {
- const psimd_f32 temp = psimd_max_f32(v, __builtin_shuffle(v, (psimd_s32) { 2, 3, 0, 1 }));
- return psimd_max_f32(temp, __builtin_shuffle(temp, (psimd_s32) { 1, 0, 3, 2 }));
- }
- PSIMD_INTRINSIC psimd_f32 psimd_allreduce_min_f32(psimd_f32 v) {
- const psimd_f32 temp = psimd_min_f32(v, __builtin_shuffle(v, (psimd_s32) { 2, 3, 0, 1 }));
- return psimd_min_f32(temp, __builtin_shuffle(temp, (psimd_s32) { 1, 0, 3, 2 }));
- }
- PSIMD_INTRINSIC float psimd_reduce_sum_f32(psimd_f32 v) {
- const psimd_f32 result = psimd_allreduce_sum_f32(v);
- return result[0];
- }
- PSIMD_INTRINSIC float psimd_reduce_max_f32(psimd_f32 v) {
- const psimd_f32 result = psimd_allreduce_max_f32(v);
- return result[0];
- }
- PSIMD_INTRINSIC float psimd_reduce_min_f32(psimd_f32 v) {
- const psimd_f32 result = psimd_allreduce_min_f32(v);
- return result[0];
- }
- #endif
- #endif
- #endif /* PSIMD_H */
|