/* ARM NEON acceleration Copyright 2018 Ahmet Inan */ #pragma once #include template <> union SIMD { static const int SIZE = 4; typedef float value_type; typedef uint32_t uint_type; float32x4_t m; value_type v[SIZE]; uint_type u[SIZE]; }; template <> union SIMD { static const int SIZE = 16; typedef int8_t value_type; typedef uint8_t uint_type; int8x16_t m; value_type v[SIZE]; uint_type u[SIZE]; }; template <> union SIMD { static const int SIZE = 8; typedef int16_t value_type; typedef uint16_t uint_type; int16x8_t m; value_type v[SIZE]; uint_type u[SIZE]; }; template <> union SIMD { static const int SIZE = 4; typedef int32_t value_type; typedef uint32_t uint_type; int32x4_t m; value_type v[SIZE]; uint_type u[SIZE]; }; template <> union SIMD { static const int SIZE = 2; typedef int64_t value_type; typedef uint64_t uint_type; int64x2_t m; value_type v[SIZE]; uint_type u[SIZE]; }; template <> union SIMD { static const int SIZE = 16; typedef uint8_t value_type; typedef uint8_t uint_type; uint8x16_t m; value_type v[SIZE]; uint_type u[SIZE]; }; template <> union SIMD { static const int SIZE = 8; typedef uint16_t value_type; typedef uint16_t uint_type; uint16x8_t m; value_type v[SIZE]; uint_type u[SIZE]; }; template <> union SIMD { static const int SIZE = 4; typedef uint32_t value_type; typedef uint32_t uint_type; uint32x4_t m; value_type v[SIZE]; uint_type u[SIZE]; }; template <> union SIMD { static const int SIZE = 2; typedef uint64_t value_type; typedef uint64_t uint_type; uint64x2_t m; value_type v[SIZE]; uint_type u[SIZE]; }; template <> inline SIMD vreinterpret(SIMD a) { SIMD tmp; tmp.m = (float32x4_t)a.m; return tmp; } template <> inline SIMD vreinterpret(SIMD a) { SIMD tmp; tmp.m = (uint32x4_t)a.m; return tmp; } template <> inline SIMD vreinterpret(SIMD a) { SIMD tmp; tmp.m = (int8x16_t)a.m; return tmp; } template <> inline SIMD vreinterpret(SIMD a) { SIMD tmp; tmp.m = (uint8x16_t)a.m; return tmp; } template <> inline SIMD vreinterpret(SIMD a) { SIMD tmp; tmp.m = (int16x8_t)a.m; return tmp; } template <> inline SIMD vreinterpret(SIMD a) { SIMD tmp; tmp.m = (uint16x8_t)a.m; return tmp; } template <> inline SIMD vreinterpret(SIMD a) { SIMD tmp; tmp.m = (int32x4_t)a.m; return tmp; } template <> inline SIMD vreinterpret(SIMD a) { SIMD tmp; tmp.m = (uint32x4_t)a.m; return tmp; } template <> inline SIMD vdup(float a) { SIMD tmp; tmp.m = vdupq_n_f32(a); return tmp; } template <> inline SIMD vdup(int8_t a) { SIMD tmp; tmp.m = vdupq_n_s8(a); return tmp; } template <> inline SIMD vdup(int16_t a) { SIMD tmp; tmp.m = vdupq_n_s16(a); return tmp; } template <> inline SIMD vdup(int32_t a) { SIMD tmp; tmp.m = vdupq_n_s32(a); return tmp; } template <> inline SIMD vdup(int64_t a) { SIMD tmp; tmp.m = vdupq_n_s64(a); return tmp; } template <> inline SIMD vdup(uint8_t a) { SIMD tmp; tmp.m = vdupq_n_u8(a); return tmp; } template <> inline SIMD vdup(uint16_t a) { SIMD tmp; tmp.m = vdupq_n_u16(a); return tmp; } template <> inline SIMD vdup(uint32_t a) { SIMD tmp; tmp.m = vdupq_n_u32(a); return tmp; } template <> inline SIMD vdup(uint64_t a) { SIMD tmp; tmp.m = vdupq_n_u64(a); return tmp; } template <> inline SIMD vzero() { SIMD tmp; tmp.m = (float32x4_t)veorq_u32((uint32x4_t)tmp.m, (uint32x4_t)tmp.m); return tmp; } template <> inline SIMD vzero() { SIMD tmp; tmp.m = veorq_s8(tmp.m, tmp.m); return tmp; } template <> inline SIMD vzero() { SIMD tmp; tmp.m = veorq_s16(tmp.m, tmp.m); return tmp; } template <> inline SIMD vzero() { SIMD tmp; tmp.m = veorq_s32(tmp.m, tmp.m); return tmp; } template <> inline SIMD vzero() { SIMD tmp; tmp.m = veorq_s64(tmp.m, tmp.m); return tmp; } template <> inline SIMD vzero() { SIMD tmp; tmp.m = veorq_u8(tmp.m, tmp.m); return tmp; } template <> inline SIMD vzero() { SIMD tmp; tmp.m = veorq_u16(tmp.m, tmp.m); return tmp; } template <> inline SIMD vzero() { SIMD tmp; tmp.m = veorq_u32(tmp.m, tmp.m); return tmp; } template <> inline SIMD vzero() { SIMD tmp; tmp.m = veorq_u64(tmp.m, tmp.m); return tmp; } template <> inline SIMD vadd(SIMD a, SIMD b) { SIMD tmp; tmp.m = vaddq_f32(a.m, b.m); return tmp; } template <> inline SIMD vadd(SIMD a, SIMD b) { SIMD tmp; tmp.m = vaddq_s8(a.m, b.m); return tmp; } template <> inline SIMD vadd(SIMD a, SIMD b) { SIMD tmp; tmp.m = vaddq_s16(a.m, b.m); return tmp; } template <> inline SIMD vadd(SIMD a, SIMD b) { SIMD tmp; tmp.m = vaddq_s32(a.m, b.m); return tmp; } template <> inline SIMD vadd(SIMD a, SIMD b) { SIMD tmp; tmp.m = vaddq_s64(a.m, b.m); return tmp; } template <> inline SIMD vqadd(SIMD a, SIMD b) { SIMD tmp; tmp.m = vqaddq_s8(a.m, b.m); return tmp; } template <> inline SIMD vqadd(SIMD a, SIMD b) { SIMD tmp; tmp.m = vqaddq_s16(a.m, b.m); return tmp; } template <> inline SIMD vsub(SIMD a, SIMD b) { SIMD tmp; tmp.m = vsubq_f32(a.m, b.m); return tmp; } template <> inline SIMD vsub(SIMD a, SIMD b) { SIMD tmp; tmp.m = vsubq_s8(a.m, b.m); return tmp; } template <> inline SIMD vsub(SIMD a, SIMD b) { SIMD tmp; tmp.m = vsubq_s16(a.m, b.m); return tmp; } template <> inline SIMD vsub(SIMD a, SIMD b) { SIMD tmp; tmp.m = vsubq_s32(a.m, b.m); return tmp; } template <> inline SIMD vsub(SIMD a, SIMD b) { SIMD tmp; tmp.m = vsubq_s64(a.m, b.m); return tmp; } template <> inline SIMD vqsub(SIMD a, SIMD b) { SIMD tmp; tmp.m = vqsubq_s8(a.m, b.m); return tmp; } template <> inline SIMD vqsub(SIMD a, SIMD b) { SIMD tmp; tmp.m = vqsubq_s16(a.m, b.m); return tmp; } template <> inline SIMD vqsub(SIMD a, SIMD b) { SIMD tmp; tmp.m = vqsubq_u8(a.m, b.m); return tmp; } template <> inline SIMD vqsub(SIMD a, SIMD b) { SIMD tmp; tmp.m = vqsubq_u16(a.m, b.m); return tmp; } template <> inline SIMD vmul(SIMD a, SIMD b) { SIMD tmp; tmp.m = vmulq_f32(a.m, b.m); return tmp; } template <> inline SIMD vmul(SIMD a, SIMD b) { SIMD tmp; tmp.m = vmulq_s8(a.m, b.m); return tmp; } template <> inline SIMD vabs(SIMD a) { SIMD tmp; tmp.m = vabsq_f32(a.m); return tmp; } template <> inline SIMD vqabs(SIMD a) { SIMD tmp; tmp.m = vqabsq_s8(a.m); return tmp; } template <> inline SIMD vqabs(SIMD a) { SIMD tmp; tmp.m = vqabsq_s16(a.m); return tmp; } template <> inline SIMD vsignum(SIMD a) { SIMD tmp; tmp.m = (float32x4_t)vbicq_u32( veorq_u32((uint32x4_t)vdupq_n_f32(1.f), vandq_u32((uint32x4_t)vdupq_n_f32(-0.f), (uint32x4_t)a.m)), vceqq_f32(a.m, vdupq_n_f32(0.f))); return tmp; } template <> inline SIMD vsignum(SIMD a) { SIMD tmp; tmp.m = (int8x16_t)vorrq_u8(vcgtq_s8(vdupq_n_s8(0), a.m), vandq_u8(vcgtq_s8(a.m, vdupq_n_s8(0)), (uint8x16_t)vdupq_n_s8(1))); return tmp; } template <> inline SIMD vsign(SIMD a, SIMD b) { SIMD tmp; tmp.m = (float32x4_t)vbicq_u32( veorq_u32((uint32x4_t)a.m, vandq_u32((uint32x4_t)vdupq_n_f32(-0.f), (uint32x4_t)b.m)), vceqq_f32(b.m, vdupq_n_f32(0.f))); return tmp; } template <> inline SIMD vsign(SIMD a, SIMD b) { SIMD tmp; tmp.m = (int8x16_t)vorrq_u8( vandq_u8(vcgtq_s8(vdupq_n_s8(0), b.m), (uint8x16_t)vnegq_s8(a.m)), vandq_u8(vcgtq_s8(b.m, vdupq_n_s8(0)), (uint8x16_t)a.m)); return tmp; } template <> inline SIMD vcopysign(SIMD a, SIMD b) { SIMD tmp; uint32x4_t negz = (uint32x4_t)vdupq_n_f32(-0.f); tmp.m = (float32x4_t)vorrq_u32( vbicq_u32((uint32x4_t)a.m, negz), vandq_u32((uint32x4_t)b.m, negz)); return tmp; } template <> inline SIMD vorr(SIMD a, SIMD b) { SIMD tmp; tmp.m = vorrq_u8(a.m, b.m); return tmp; } template <> inline SIMD vorr(SIMD a, SIMD b) { SIMD tmp; tmp.m = vorrq_u16(a.m, b.m); return tmp; } template <> inline SIMD vorr(SIMD a, SIMD b) { SIMD tmp; tmp.m = vorrq_u32(a.m, b.m); return tmp; } template <> inline SIMD vorr(SIMD a, SIMD b) { SIMD tmp; tmp.m = vorrq_u64(a.m, b.m); return tmp; } template <> inline SIMD vand(SIMD a, SIMD b) { SIMD tmp; tmp.m = vandq_u8(a.m, b.m); return tmp; } template <> inline SIMD vand(SIMD a, SIMD b) { SIMD tmp; tmp.m = vandq_u16(a.m, b.m); return tmp; } template <> inline SIMD vand(SIMD a, SIMD b) { SIMD tmp; tmp.m = vandq_u32(a.m, b.m); return tmp; } template <> inline SIMD vand(SIMD a, SIMD b) { SIMD tmp; tmp.m = vandq_u64(a.m, b.m); return tmp; } template <> inline SIMD veor(SIMD a, SIMD b) { SIMD tmp; tmp.m = veorq_u8(a.m, b.m); return tmp; } template <> inline SIMD veor(SIMD a, SIMD b) { SIMD tmp; tmp.m = veorq_u16(a.m, b.m); return tmp; } template <> inline SIMD veor(SIMD a, SIMD b) { SIMD tmp; tmp.m = veorq_u32(a.m, b.m); return tmp; } template <> inline SIMD veor(SIMD a, SIMD b) { SIMD tmp; tmp.m = veorq_u64(a.m, b.m); return tmp; } template <> inline SIMD vbic(SIMD a, SIMD b) { SIMD tmp; tmp.m = vbicq_u8(a.m, b.m); return tmp; } template <> inline SIMD vbic(SIMD a, SIMD b) { SIMD tmp; tmp.m = vbicq_u16(a.m, b.m); return tmp; } template <> inline SIMD vbic(SIMD a, SIMD b) { SIMD tmp; tmp.m = vbicq_u32(a.m, b.m); return tmp; } template <> inline SIMD vbic(SIMD a, SIMD b) { SIMD tmp; tmp.m = vbicq_u64(a.m, b.m); return tmp; } template <> inline SIMD vbsl(SIMD a, SIMD b, SIMD c) { SIMD tmp; tmp.m = vbslq_u8(a.m, b.m, c.m); return tmp; } template <> inline SIMD vbsl(SIMD a, SIMD b, SIMD c) { SIMD tmp; tmp.m = vbslq_u16(a.m, b.m, c.m); return tmp; } template <> inline SIMD vbsl(SIMD a, SIMD b, SIMD c) { SIMD tmp; tmp.m = vbslq_u32(a.m, b.m, c.m); return tmp; } template <> inline SIMD vbsl(SIMD a, SIMD b, SIMD c) { SIMD tmp; tmp.m = vbslq_u64(a.m, b.m, c.m); return tmp; } template <> inline SIMD vceqz(SIMD a) { SIMD tmp; tmp.m = vceqq_f32(a.m, vdupq_n_f32(0.f)); return tmp; } template <> inline SIMD vceqz(SIMD a) { SIMD tmp; tmp.m = vceqq_s8(a.m, vdupq_n_s8(0)); return tmp; } template <> inline SIMD vceqz(SIMD a) { SIMD tmp; tmp.m = vceqq_s16(a.m, vdupq_n_s16(0)); return tmp; } template <> inline SIMD vceqz(SIMD a) { SIMD tmp; tmp.m = vceqq_s32(a.m, vdupq_n_s32(0)); return tmp; } template <> inline SIMD vceq(SIMD a, SIMD b) { SIMD tmp; tmp.m = vceqq_f32(a.m, b.m); return tmp; } template <> inline SIMD vceq(SIMD a, SIMD b) { SIMD tmp; tmp.m = vceqq_s8(a.m, b.m); return tmp; } template <> inline SIMD vceq(SIMD a, SIMD b) { SIMD tmp; tmp.m = vceqq_s16(a.m, b.m); return tmp; } template <> inline SIMD vceq(SIMD a, SIMD b) { SIMD tmp; tmp.m = vceqq_s32(a.m, b.m); return tmp; } template <> inline SIMD vcgtz(SIMD a) { SIMD tmp; tmp.m = vcgtq_f32(a.m, vdupq_n_f32(0.f)); return tmp; } template <> inline SIMD vcgtz(SIMD a) { SIMD tmp; tmp.m = vcgtq_s8(a.m, vdupq_n_s8(0)); return tmp; } template <> inline SIMD vcgtz(SIMD a) { SIMD tmp; tmp.m = vcgtq_s16(a.m, vdupq_n_s16(0)); return tmp; } template <> inline SIMD vcgtz(SIMD a) { SIMD tmp; tmp.m = vcgtq_s32(a.m, vdupq_n_s32(0)); return tmp; } template <> inline SIMD vcltz(SIMD a) { SIMD tmp; tmp.m = vcltq_f32(a.m, vdupq_n_f32(0.f)); return tmp; } template <> inline SIMD vcltz(SIMD a) { SIMD tmp; tmp.m = vcltq_s8(a.m, vdupq_n_s8(0)); return tmp; } template <> inline SIMD vcltz(SIMD a) { SIMD tmp; tmp.m = vcltq_s16(a.m, vdupq_n_s16(0)); return tmp; } template <> inline SIMD vcltz(SIMD a) { SIMD tmp; tmp.m = vcltq_s32(a.m, vdupq_n_s32(0)); return tmp; } template <> inline SIMD vclez(SIMD a) { SIMD tmp; tmp.m = vcleq_f32(a.m, vdupq_n_f32(0.f)); return tmp; } template <> inline SIMD vclez(SIMD a) { SIMD tmp; tmp.m = vcleq_s8(a.m, vdupq_n_s8(0)); return tmp; } template <> inline SIMD vclez(SIMD a) { SIMD tmp; tmp.m = vcleq_s16(a.m, vdupq_n_s16(0)); return tmp; } template <> inline SIMD vclez(SIMD a) { SIMD tmp; tmp.m = vcleq_s32(a.m, vdupq_n_s32(0)); return tmp; } template <> inline SIMD vmin(SIMD a, SIMD b) { SIMD tmp; tmp.m = vminq_f32(a.m, b.m); return tmp; } template <> inline SIMD vmin(SIMD a, SIMD b) { SIMD tmp; tmp.m = vminq_s8(a.m, b.m); return tmp; } template <> inline SIMD vmin(SIMD a, SIMD b) { SIMD tmp; tmp.m = vminq_s16(a.m, b.m); return tmp; } template <> inline SIMD vmin(SIMD a, SIMD b) { SIMD tmp; tmp.m = vminq_s32(a.m, b.m); return tmp; } template <> inline SIMD vmax(SIMD a, SIMD b) { SIMD tmp; tmp.m = vmaxq_f32(a.m, b.m); return tmp; } template <> inline SIMD vmax(SIMD a, SIMD b) { SIMD tmp; tmp.m = vmaxq_s8(a.m, b.m); return tmp; } template <> inline SIMD vmax(SIMD a, SIMD b) { SIMD tmp; tmp.m = vmaxq_s16(a.m, b.m); return tmp; } template <> inline SIMD vmax(SIMD a, SIMD b) { SIMD tmp; tmp.m = vmaxq_s32(a.m, b.m); return tmp; } template <> inline SIMD vclamp(SIMD x, float a, float b) { SIMD tmp; tmp.m = vminq_f32(vmaxq_f32(x.m, vdupq_n_f32(a)), vdupq_n_f32(b)); return tmp; } template <> inline SIMD vclamp(SIMD x, int8_t a, int8_t b) { SIMD tmp; tmp.m = vminq_s8(vmaxq_s8(x.m, vdupq_n_s8(a)), vdupq_n_s8(b)); return tmp; } template <> inline SIMD vclamp(SIMD x, int16_t a, int16_t b) { SIMD tmp; tmp.m = vminq_s16(vmaxq_s16(x.m, vdupq_n_s16(a)), vdupq_n_s16(b)); return tmp; } template <> inline SIMD vclamp(SIMD x, int32_t a, int32_t b) { SIMD tmp; tmp.m = vminq_s32(vmaxq_s32(x.m, vdupq_n_s32(a)), vdupq_n_s32(b)); return tmp; } template <> inline SIMD vshuf(SIMD a, SIMD b) { SIMD tmp; #ifdef __aarch64__ tmp.m = vqtbl1q_u8(a.m, b.m); #else uint8x8x2_t c { vget_low_u8(a.m), vget_high_u8(a.m) }; uint8x8_t d = vtbl2_u8(c, vget_low_u8(b.m)); uint8x8_t e = vtbl2_u8(c, vget_high_u8(b.m)); tmp.m = vcombine_u8(d, e); #endif return tmp; } template <> inline SIMD vshuf(SIMD a, SIMD b) { SIMD tmp; #ifdef __aarch64__ tmp.m = vqtbl1q_s8(a.m, b.m); #else int8x8x2_t c { vget_low_s8(a.m), vget_high_s8(a.m) }; int8x8_t d = vtbl2_s8(c, vget_low_s8((int8x16_t)b.m)); int8x8_t e = vtbl2_s8(c, vget_high_s8((int8x16_t)b.m)); tmp.m = vcombine_s8(d, e); #endif return tmp; }