#ifndef HIGHWAYHASH_VECTOR128_H_
#define HIGHWAYHASH_VECTOR128_H_
#include <stddef.h>
#include <stdint.h>
#include "highwayhash/arch_specific.h"
#include "highwayhash/compiler_specific.h"
#ifndef HH_DISABLE_TARGET_SPECIFIC
#include <smmintrin.h>
namespace highwayhash {
namespace HH_TARGET_NAME {
template <typename T> class V128 {};
#if (defined(_MSC_VER) && defined(_M_IX86) && _MSC_VER < 1900) && \
!defined(_mm_set_epi64x)
#define _mm_set_epi64x(A, B) \
_mm_set_epi32((uint32_t)((uint64_t)(A) >> 32), (uint32_t)(A), \
(uint32_t)((uint64_t)(B) >> 32), (uint32_t)(B))
#endif
#if !(defined(__x86_64) || defined(__x86_64__) || defined(__amd64) || \
defined(_M_X64)) && \
!defined(_mm_cvtsi64_si128)
#define _mm_cvtsi64_si128(V64) _mm_set_epi64x(0, V64)
#endif
#if (HH_GCC_VERSION && HH_GCC_VERSION < 490 && !HH_CLANG_VERSION) || \
(HH_CLANG_VERSION && !__has_builtin(__builtin_ia32_undef128))
extern __inline __m128i __attribute__((__gnu_inline__, __always_inline__))
_mm_undefined_si128(void) {
__m128i __Y = __Y;
return __Y;
}
#endif
template <> class V128<uint8_t> {
public:
using Intrinsic = __m128i;
using T = uint8_t;
static constexpr size_t N = 16;
HH_INLINE V128() : v_(_mm_undefined_si128()) {}
HH_INLINE explicit V128(T i) : v_(_mm_set1_epi8(i)) {}
HH_INLINE explicit V128(const V128 &other) : v_(other.v_) {}
template <typename U>
HH_INLINE explicit V128(const V128<U> &other) : v_(other) {}
HH_INLINE V128 &operator=(const V128 &other) {
v_ = other.v_;
return *this;
}
HH_INLINE V128(const Intrinsic &v) : v_(v) {}
HH_INLINE V128 &operator=(const Intrinsic &v) {
v_ = v;
return *this;
}
HH_INLINE operator Intrinsic() const { return v_; }
HH_INLINE V128 operator==(const V128 &other) const {
return V128(_mm_cmpeq_epi8(v_, other.v_));
}
HH_INLINE V128 &operator+=(const V128 &other) {
v_ = _mm_add_epi8(v_, other.v_);
return *this;
}
HH_INLINE V128 &operator-=(const V128 &other) {
v_ = _mm_sub_epi8(v_, other.v_);
return *this;
}
HH_INLINE V128 &operator&=(const V128 &other) {
v_ = _mm_and_si128(v_, other.v_);
return *this;
}
HH_INLINE V128 &operator|=(const V128 &other) {
v_ = _mm_or_si128(v_, other.v_);
return *this;
}
HH_INLINE V128 &operator^=(const V128 &other) {
v_ = _mm_xor_si128(v_, other.v_);
return *this;
}
private:
Intrinsic v_;
};
template <> class V128<uint16_t> {
public:
using Intrinsic = __m128i;
using T = uint16_t;
static constexpr size_t N = 8;
HH_INLINE V128() {}
HH_INLINE V128(T p_7, T p_6, T p_5, T p_4, T p_3, T p_2, T p_1, T p_0)
: v_(_mm_set_epi16(p_7, p_6, p_5, p_4, p_3, p_2, p_1, p_0)) {}
HH_INLINE explicit V128(T i) : v_(_mm_set1_epi16(i)) {}
HH_INLINE explicit V128(const V128 &other) : v_(other.v_) {}
template <typename U>
HH_INLINE explicit V128(const V128<U> &other) : v_(other) {}
HH_INLINE V128 &operator=(const V128 &other) {
v_ = other.v_;
return *this;
}
HH_INLINE V128(const Intrinsic &v) : v_(v) {}
HH_INLINE V128 &operator=(const Intrinsic &v) {
v_ = v;
return *this;
}
HH_INLINE operator Intrinsic() const { return v_; }
HH_INLINE V128 operator==(const V128 &other) const {
return V128(_mm_cmpeq_epi16(v_, other.v_));
}
HH_INLINE V128 &operator+=(const V128 &other) {
v_ = _mm_add_epi16(v_, other.v_);
return *this;
}
HH_INLINE V128 &operator-=(const V128 &other) {
v_ = _mm_sub_epi16(v_, other.v_);
return *this;
}
HH_INLINE V128 &operator&=(const V128 &other) {
v_ = _mm_and_si128(v_, other.v_);
return *this;
}
HH_INLINE V128 &operator|=(const V128 &other) {
v_ = _mm_or_si128(v_, other.v_);
return *this;
}
HH_INLINE V128 &operator^=(const V128 &other) {
v_ = _mm_xor_si128(v_, other.v_);
return *this;
}
HH_INLINE V128 &operator<<=(const int count) {
v_ = _mm_slli_epi16(v_, count);
return *this;
}
HH_INLINE V128 &operator<<=(const Intrinsic &count) {
v_ = _mm_sll_epi16(v_, count);
return *this;
}
HH_INLINE V128 &operator>>=(const int count) {
v_ = _mm_srli_epi16(v_, count);
return *this;
}
HH_INLINE V128 &operator>>=(const Intrinsic &count) {
v_ = _mm_srl_epi16(v_, count);
return *this;
}
private:
Intrinsic v_;
};
template <> class V128<uint32_t> {
public:
using Intrinsic = __m128i;
using T = uint32_t;
static constexpr size_t N = 4;
HH_INLINE V128() {}
HH_INLINE V128(T p_3, T p_2, T p_1, T p_0)
: v_(_mm_set_epi32(p_3, p_2, p_1, p_0)) {}
HH_INLINE explicit V128(T i) : v_(_mm_set1_epi32(i)) {}
HH_INLINE explicit V128(const V128 &other) : v_(other.v_) {}
template <typename U>
HH_INLINE explicit V128(const V128<U> &other) : v_(other) {}
HH_INLINE V128 &operator=(const V128 &other) {
v_ = other.v_;
return *this;
}
HH_INLINE V128(const Intrinsic &v) : v_(v) {}
HH_INLINE V128 &operator=(const Intrinsic &v) {
v_ = v;
return *this;
}
HH_INLINE operator Intrinsic() const { return v_; }
HH_INLINE V128 operator==(const V128 &other) const {
return V128(_mm_cmpeq_epi32(v_, other.v_));
}
HH_INLINE V128 &operator+=(const V128 &other) {
v_ = _mm_add_epi32(v_, other.v_);
return *this;
}
HH_INLINE V128 &operator-=(const V128 &other) {
v_ = _mm_sub_epi32(v_, other.v_);
return *this;
}
HH_INLINE V128 &operator&=(const V128 &other) {
v_ = _mm_and_si128(v_, other.v_);
return *this;
}
HH_INLINE V128 &operator|=(const V128 &other) {
v_ = _mm_or_si128(v_, other.v_);
return *this;
}
HH_INLINE V128 &operator^=(const V128 &other) {
v_ = _mm_xor_si128(v_, other.v_);
return *this;
}
HH_INLINE V128 &operator<<=(const int count) {
v_ = _mm_slli_epi32(v_, count);
return *this;
}
HH_INLINE V128 &operator<<=(const Intrinsic &count) {
v_ = _mm_sll_epi32(v_, count);
return *this;
}
HH_INLINE V128 &operator>>=(const int count) {
v_ = _mm_srli_epi32(v_, count);
return *this;
}
HH_INLINE V128 &operator>>=(const Intrinsic &count) {
v_ = _mm_srl_epi32(v_, count);
return *this;
}
private:
Intrinsic v_;
};
template <> class V128<uint64_t> {
public:
using Intrinsic = __m128i;
using T = uint64_t;
static constexpr size_t N = 2;
HH_INLINE V128() {}
HH_INLINE V128(T p_1, T p_0) : v_(_mm_set_epi64x(p_1, p_0)) {}
HH_INLINE explicit V128(T i) : v_(_mm_set_epi64x(i, i)) {}
HH_INLINE explicit V128(const V128 &other) : v_(other.v_) {}
template <typename U>
HH_INLINE explicit V128(const V128<U> &other) : v_(other) {}
HH_INLINE V128 &operator=(const V128 &other) {
v_ = other.v_;
return *this;
}
HH_INLINE V128(const Intrinsic &v) : v_(v) {}
HH_INLINE V128 &operator=(const Intrinsic &v) {
v_ = v;
return *this;
}
HH_INLINE operator Intrinsic() const { return v_; }
HH_INLINE V128 operator==(const V128 &other) const {
return V128(_mm_cmpeq_epi64(v_, other.v_));
}
HH_INLINE V128 &operator+=(const V128 &other) {
v_ = _mm_add_epi64(v_, other.v_);
return *this;
}
HH_INLINE V128 &operator-=(const V128 &other) {
v_ = _mm_sub_epi64(v_, other.v_);
return *this;
}
HH_INLINE V128 &operator&=(const V128 &other) {
v_ = _mm_and_si128(v_, other.v_);
return *this;
}
HH_INLINE V128 &operator|=(const V128 &other) {
v_ = _mm_or_si128(v_, other.v_);
return *this;
}
HH_INLINE V128 &operator^=(const V128 &other) {
v_ = _mm_xor_si128(v_, other.v_);
return *this;
}
HH_INLINE V128 &operator<<=(const int count) {
v_ = _mm_slli_epi64(v_, count);
return *this;
}
HH_INLINE V128 &operator<<=(const Intrinsic &count) {
v_ = _mm_sll_epi64(v_, count);
return *this;
}
HH_INLINE V128 &operator>>=(const int count) {
v_ = _mm_srli_epi64(v_, count);
return *this;
}
HH_INLINE V128 &operator>>=(const Intrinsic &count) {
v_ = _mm_srl_epi64(v_, count);
return *this;
}
private:
Intrinsic v_;
};
template <> class V128<float> {
public:
using Intrinsic = __m128;
using T = float;
static constexpr size_t N = 4;
HH_INLINE V128() {}
HH_INLINE V128(T p_3, T p_2, T p_1, T p_0)
: v_(_mm_set_ps(p_3, p_2, p_1, p_0)) {}
HH_INLINE explicit V128(T f) : v_(_mm_set1_ps(f)) {}
HH_INLINE explicit V128(const V128 &other) : v_(other.v_) {}
template <typename U>
HH_INLINE explicit V128(const V128<U> &other) : v_(other) {}
HH_INLINE V128 &operator=(const V128 &other) {
v_ = other.v_;
return *this;
}
HH_INLINE V128(const Intrinsic &v) : v_(v) {}
HH_INLINE V128 &operator=(const Intrinsic &v) {
v_ = v;
return *this;
}
HH_INLINE operator Intrinsic() const { return v_; }
HH_INLINE V128 operator==(const V128 &other) const {
return V128(_mm_cmpeq_ps(v_, other.v_));
}
HH_INLINE V128 operator<(const V128 &other) const {
return V128(_mm_cmplt_ps(v_, other.v_));
}
HH_INLINE V128 operator>(const V128 &other) const {
return V128(_mm_cmplt_ps(other.v_, v_));
}
HH_INLINE V128 &operator*=(const V128 &other) {
v_ = _mm_mul_ps(v_, other.v_);
return *this;
}
HH_INLINE V128 &operator/=(const V128 &other) {
v_ = _mm_div_ps(v_, other.v_);
return *this;
}
HH_INLINE V128 &operator+=(const V128 &other) {
v_ = _mm_add_ps(v_, other.v_);
return *this;
}
HH_INLINE V128 &operator-=(const V128 &other) {
v_ = _mm_sub_ps(v_, other.v_);
return *this;
}
HH_INLINE V128 &operator&=(const V128 &other) {
v_ = _mm_and_ps(v_, other.v_);
return *this;
}
HH_INLINE V128 &operator|=(const V128 &other) {
v_ = _mm_or_ps(v_, other.v_);
return *this;
}
HH_INLINE V128 &operator^=(const V128 &other) {
v_ = _mm_xor_ps(v_, other.v_);
return *this;
}
private:
Intrinsic v_;
};
template <> class V128<double> {
public:
using Intrinsic = __m128d;
using T = double;
static constexpr size_t N = 2;
HH_INLINE V128() {}
HH_INLINE V128(T p_1, T p_0) : v_(_mm_set_pd(p_1, p_0)) {}
HH_INLINE explicit V128(T f) : v_(_mm_set1_pd(f)) {}
HH_INLINE explicit V128(const V128 &other) : v_(other.v_) {}
template <typename U>
HH_INLINE explicit V128(const V128<U> &other) : v_(other) {}
HH_INLINE V128 &operator=(const V128 &other) {
v_ = other.v_;
return *this;
}
HH_INLINE V128(const Intrinsic &v) : v_(v) {}
HH_INLINE V128 &operator=(const Intrinsic &v) {
v_ = v;
return *this;
}
HH_INLINE operator Intrinsic() const { return v_; }
HH_INLINE V128 operator==(const V128 &other) const {
return V128(_mm_cmpeq_pd(v_, other.v_));
}
HH_INLINE V128 operator<(const V128 &other) const {
return V128(_mm_cmplt_pd(v_, other.v_));
}
HH_INLINE V128 operator>(const V128 &other) const {
return V128(_mm_cmplt_pd(other.v_, v_));
}
HH_INLINE V128 &operator*=(const V128 &other) {
v_ = _mm_mul_pd(v_, other.v_);
return *this;
}
HH_INLINE V128 &operator/=(const V128 &other) {
v_ = _mm_div_pd(v_, other.v_);
return *this;
}
HH_INLINE V128 &operator+=(const V128 &other) {
v_ = _mm_add_pd(v_, other.v_);
return *this;
}
HH_INLINE V128 &operator-=(const V128 &other) {
v_ = _mm_sub_pd(v_, other.v_);
return *this;
}
HH_INLINE V128 &operator&=(const V128 &other) {
v_ = _mm_and_pd(v_, other.v_);
return *this;
}
HH_INLINE V128 &operator|=(const V128 &other) {
v_ = _mm_or_pd(v_, other.v_);
return *this;
}
HH_INLINE V128 &operator^=(const V128 &other) {
v_ = _mm_xor_pd(v_, other.v_);
return *this;
}
private:
Intrinsic v_;
};
template <typename T>
HH_INLINE V128<T> operator*(const V128<T> &left, const V128<T> &right) {
V128<T> t(left);
return t *= right;
}
template <typename T>
HH_INLINE V128<T> operator/(const V128<T> &left, const V128<T> &right) {
V128<T> t(left);
return t /= right;
}
template <typename T>
HH_INLINE V128<T> operator+(const V128<T> &left, const V128<T> &right) {
V128<T> t(left);
return t += right;
}
template <typename T>
HH_INLINE V128<T> operator-(const V128<T> &left, const V128<T> &right) {
V128<T> t(left);
return t -= right;
}
template <typename T>
HH_INLINE V128<T> operator&(const V128<T> &left, const V128<T> &right) {
V128<T> t(left);
return t &= right;
}
template <typename T>
HH_INLINE V128<T> operator|(const V128<T> &left, const V128<T> &right) {
V128<T> t(left);
return t |= right;
}
template <typename T>
HH_INLINE V128<T> operator^(const V128<T> &left, const V128<T> &right) {
V128<T> t(left);
return t ^= right;
}
template <typename T>
HH_INLINE V128<T> operator<<(const V128<T> &v, const int count) {
V128<T> t(v);
return t <<= count;
}
template <typename T>
HH_INLINE V128<T> operator>>(const V128<T> &v, const int count) {
V128<T> t(v);
return t >>= count;
}
template <typename T>
HH_INLINE V128<T> operator<<(const V128<T> &v, const __m128i &count) {
V128<T> t(v);
return t <<= count;
}
template <typename T>
HH_INLINE V128<T> operator>>(const V128<T> &v, const __m128i &count) {
V128<T> t(v);
return t >>= count;
}
using V16x8U = V128<uint8_t>;
using V8x16U = V128<uint16_t>;
using V4x32U = V128<uint32_t>;
using V2x64U = V128<uint64_t>;
using V4x32F = V128<float>;
using V2x64F = V128<double>;
template <class V>
HH_INLINE V Load(const typename V::T *const HH_RESTRICT from);
template <class V>
HH_INLINE V LoadUnaligned(const typename V::T *const HH_RESTRICT from);
template <>
HH_INLINE V16x8U Load<V16x8U>(const V16x8U::T *const HH_RESTRICT from) {
const __m128i *const HH_RESTRICT p = reinterpret_cast<const __m128i *>(from);
return V16x8U(_mm_load_si128(p));
}
template <>
HH_INLINE V8x16U Load<V8x16U>(const V8x16U::T *const HH_RESTRICT from) {
const __m128i *const HH_RESTRICT p = reinterpret_cast<const __m128i *>(from);
return V8x16U(_mm_load_si128(p));
}
template <>
HH_INLINE V4x32U Load<V4x32U>(const V4x32U::T *const HH_RESTRICT from) {
const __m128i *const HH_RESTRICT p = reinterpret_cast<const __m128i *>(from);
return V4x32U(_mm_load_si128(p));
}
template <>
HH_INLINE V2x64U Load<V2x64U>(const V2x64U::T *const HH_RESTRICT from) {
const __m128i *const HH_RESTRICT p = reinterpret_cast<const __m128i *>(from);
return V2x64U(_mm_load_si128(p));
}
template <>
HH_INLINE V4x32F Load<V4x32F>(const V4x32F::T *const HH_RESTRICT from) {
return V4x32F(_mm_load_ps(from));
}
template <>
HH_INLINE V2x64F Load<V2x64F>(const V2x64F::T *const HH_RESTRICT from) {
return V2x64F(_mm_load_pd(from));
}
template <>
HH_INLINE V16x8U
LoadUnaligned<V16x8U>(const V16x8U::T *const HH_RESTRICT from) {
const __m128i *const HH_RESTRICT p = reinterpret_cast<const __m128i *>(from);
return V16x8U(_mm_loadu_si128(p));
}
template <>
HH_INLINE V8x16U
LoadUnaligned<V8x16U>(const V8x16U::T *const HH_RESTRICT from) {
const __m128i *const HH_RESTRICT p = reinterpret_cast<const __m128i *>(from);
return V8x16U(_mm_loadu_si128(p));
}
template <>
HH_INLINE V4x32U
LoadUnaligned<V4x32U>(const V4x32U::T *const HH_RESTRICT from) {
const __m128i *const HH_RESTRICT p = reinterpret_cast<const __m128i *>(from);
return V4x32U(_mm_loadu_si128(p));
}
template <>
HH_INLINE V2x64U
LoadUnaligned<V2x64U>(const V2x64U::T *const HH_RESTRICT from) {
const __m128i *const HH_RESTRICT p = reinterpret_cast<const __m128i *>(from);
return V2x64U(_mm_loadu_si128(p));
}
template <>
HH_INLINE V4x32F
LoadUnaligned<V4x32F>(const V4x32F::T *const HH_RESTRICT from) {
return V4x32F(_mm_loadu_ps(from));
}
template <>
HH_INLINE V2x64F
LoadUnaligned<V2x64F>(const V2x64F::T *const HH_RESTRICT from) {
return V2x64F(_mm_loadu_pd(from));
}
template <typename T>
HH_INLINE void Store(const V128<T> &v, T *const HH_RESTRICT to) {
_mm_store_si128(reinterpret_cast<__m128i * HH_RESTRICT>(to), v);
}
HH_INLINE void Store(const V128<float> &v, float *const HH_RESTRICT to) {
_mm_store_ps(to, v);
}
HH_INLINE void Store(const V128<double> &v, double *const HH_RESTRICT to) {
_mm_store_pd(to, v);
}
template <typename T>
HH_INLINE void StoreUnaligned(const V128<T> &v, T *const HH_RESTRICT to) {
_mm_storeu_si128(reinterpret_cast<__m128i * HH_RESTRICT>(to), v);
}
HH_INLINE void StoreUnaligned(const V128<float> &v,
float *const HH_RESTRICT to) {
_mm_storeu_ps(to, v);
}
HH_INLINE void StoreUnaligned(const V128<double> &v,
double *const HH_RESTRICT to) {
_mm_storeu_pd(to, v);
}
template <typename T>
HH_INLINE void Stream(const V128<T> &v, T *const HH_RESTRICT to) {
_mm_stream_si128(reinterpret_cast<__m128i * HH_RESTRICT>(to), v);
}
HH_INLINE void Stream(const V128<float> &v, float *const HH_RESTRICT to) {
_mm_stream_ps(to, v);
}
HH_INLINE void Stream(const V128<double> &v, double *const HH_RESTRICT to) {
_mm_stream_pd(to, v);
}
template <typename T>
HH_INLINE V128<T> RotateLeft(const V128<T> &v, const int count) {
constexpr size_t num_bits = sizeof(T) * 8;
return (v << count) | (v >> (num_bits - count));
}
template <typename T>
HH_INLINE V128<T> AndNot(const V128<T> &neg_mask, const V128<T> &values) {
return V128<T>(_mm_andnot_si128(neg_mask, values));
}
template <>
HH_INLINE V128<float> AndNot(const V128<float> &neg_mask,
const V128<float> &values) {
return V128<float>(_mm_andnot_ps(neg_mask, values));
}
template <>
HH_INLINE V128<double> AndNot(const V128<double> &neg_mask,
const V128<double> &values) {
return V128<double>(_mm_andnot_pd(neg_mask, values));
}
HH_INLINE V4x32F Select(const V4x32F &a, const V4x32F &b, const V4x32F &mask) {
return V4x32F(_mm_blendv_ps(a, b, mask));
}
HH_INLINE V2x64F Select(const V2x64F &a, const V2x64F &b, const V2x64F &mask) {
return V2x64F(_mm_blendv_pd(a, b, mask));
}
HH_INLINE V16x8U Min(const V16x8U &v0, const V16x8U &v1) {
return V16x8U(_mm_min_epu8(v0, v1));
}
HH_INLINE V16x8U Max(const V16x8U &v0, const V16x8U &v1) {
return V16x8U(_mm_max_epu8(v0, v1));
}
HH_INLINE V8x16U Min(const V8x16U &v0, const V8x16U &v1) {
return V8x16U(_mm_min_epu16(v0, v1));
}
HH_INLINE V8x16U Max(const V8x16U &v0, const V8x16U &v1) {
return V8x16U(_mm_max_epu16(v0, v1));
}
HH_INLINE V4x32U Min(const V4x32U &v0, const V4x32U &v1) {
return V4x32U(_mm_min_epu32(v0, v1));
}
HH_INLINE V4x32U Max(const V4x32U &v0, const V4x32U &v1) {
return V4x32U(_mm_max_epu32(v0, v1));
}
HH_INLINE V4x32F Min(const V4x32F &v0, const V4x32F &v1) {
return V4x32F(_mm_min_ps(v0, v1));
}
HH_INLINE V4x32F Max(const V4x32F &v0, const V4x32F &v1) {
return V4x32F(_mm_max_ps(v0, v1));
}
HH_INLINE V2x64F Min(const V2x64F &v0, const V2x64F &v1) {
return V2x64F(_mm_min_pd(v0, v1));
}
HH_INLINE V2x64F Max(const V2x64F &v0, const V2x64F &v1) {
return V2x64F(_mm_max_pd(v0, v1));
}
} }
#endif #endif