// SPDX-License-Identifier: Apache-2.0 // ---------------------------------------------------------------------------- // Copyright 2019-2023 Arm Limited // // Licensed under the Apache License, Version 2.0 (the "License"); you may not // use this file except in compliance with the License. You may obtain a copy // of the License at: // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the // License for the specific language governing permissions and limitations // under the License. // ---------------------------------------------------------------------------- /** * @brief 4x32-bit vectors, implemented using SSE. * * This module implements 4-wide 32-bit float, int, and mask vectors for x86 * SSE. The implementation requires at least SSE2, but higher levels of SSE can * be selected at compile time to improve performance. * * There is a baseline level of functionality provided by all vector widths and * implementations. This is implemented using identical function signatures, * modulo data type, so we can use them as substitutable implementations in VLA * code. * * The 4-wide vectors are also used as a fixed-width type, and significantly * extend the functionality above that available to VLA code. */ #ifndef ASTC_VECMATHLIB_SSE_4_H_INCLUDED #define ASTC_VECMATHLIB_SSE_4_H_INCLUDED #ifndef ASTCENC_SIMD_INLINE #error "Include astcenc_vecmathlib.h, do not include directly" #endif #include <cstdio> #include <cstring> // ============================================================================ // vfloat4 data type // ============================================================================ /** * @brief Data type for 4-wide floats. */ struct vfloat4 { … }; // ============================================================================ // vint4 data type // ============================================================================ /** * @brief Data type for 4-wide ints. */ struct vint4 { … }; // ============================================================================ // vmask4 data type // ============================================================================ /** * @brief Data type for 4-wide control plane masks. */ struct vmask4 { … }; // ============================================================================ // vmask4 operators and functions // ============================================================================ /** * @brief Overload: mask union (or). */ ASTCENC_SIMD_INLINE vmask4 operator|(vmask4 a, vmask4 b) { … } /** * @brief Overload: mask intersect (and). */ ASTCENC_SIMD_INLINE vmask4 operator&(vmask4 a, vmask4 b) { … } /** * @brief Overload: mask difference (xor). */ ASTCENC_SIMD_INLINE vmask4 operator^(vmask4 a, vmask4 b) { … } /** * @brief Overload: mask invert (not). */ ASTCENC_SIMD_INLINE vmask4 operator~(vmask4 a) { … } /** * @brief Return a 4-bit mask code indicating mask status. * * bit0 = lane 0 */ ASTCENC_SIMD_INLINE unsigned int mask(vmask4 a) { … } // ============================================================================ // vint4 operators and functions // ============================================================================ /** * @brief Overload: vector by vector addition. */ ASTCENC_SIMD_INLINE vint4 operator+(vint4 a, vint4 b) { … } /** * @brief Overload: vector by vector subtraction. */ ASTCENC_SIMD_INLINE vint4 operator-(vint4 a, vint4 b) { … } /** * @brief Overload: vector by vector multiplication. */ ASTCENC_SIMD_INLINE vint4 operator*(vint4 a, vint4 b) { … } /** * @brief Overload: vector bit invert. */ ASTCENC_SIMD_INLINE vint4 operator~(vint4 a) { … } /** * @brief Overload: vector by vector bitwise or. */ ASTCENC_SIMD_INLINE vint4 operator|(vint4 a, vint4 b) { … } /** * @brief Overload: vector by vector bitwise and. */ ASTCENC_SIMD_INLINE vint4 operator&(vint4 a, vint4 b) { … } /** * @brief Overload: vector by vector bitwise xor. */ ASTCENC_SIMD_INLINE vint4 operator^(vint4 a, vint4 b) { … } /** * @brief Overload: vector by vector equality. */ ASTCENC_SIMD_INLINE vmask4 operator==(vint4 a, vint4 b) { … } /** * @brief Overload: vector by vector inequality. */ ASTCENC_SIMD_INLINE vmask4 operator!=(vint4 a, vint4 b) { … } /** * @brief Overload: vector by vector less than. */ ASTCENC_SIMD_INLINE vmask4 operator<(vint4 a, vint4 b) { … } /** * @brief Overload: vector by vector greater than. */ ASTCENC_SIMD_INLINE vmask4 operator>(vint4 a, vint4 b) { … } /** * @brief Logical shift left. */ template <int s> ASTCENC_SIMD_INLINE vint4 lsl(vint4 a) { … } /** * @brief Logical shift right. */ template <int s> ASTCENC_SIMD_INLINE vint4 lsr(vint4 a) { … } /** * @brief Arithmetic shift right. */ template <int s> ASTCENC_SIMD_INLINE vint4 asr(vint4 a) { … } /** * @brief Return the min vector of two vectors. */ ASTCENC_SIMD_INLINE vint4 min(vint4 a, vint4 b) { … } /** * @brief Return the max vector of two vectors. */ ASTCENC_SIMD_INLINE vint4 max(vint4 a, vint4 b) { … } /** * @brief Return the horizontal minimum of a vector. */ ASTCENC_SIMD_INLINE vint4 hmin(vint4 a) { … } /* * @brief Return the horizontal maximum of a vector. */ ASTCENC_SIMD_INLINE vint4 hmax(vint4 a) { … } /** * @brief Return the horizontal sum of a vector as a scalar. */ ASTCENC_SIMD_INLINE int hadd_s(vint4 a) { … } /** * @brief Store a vector to a 16B aligned memory address. */ ASTCENC_SIMD_INLINE void storea(vint4 a, int* p) { … } /** * @brief Store a vector to an unaligned memory address. */ ASTCENC_SIMD_INLINE void store(vint4 a, int* p) { … } /** * @brief Store a vector to an unaligned memory address. */ ASTCENC_SIMD_INLINE void store(vint4 a, uint8_t* p) { … } /** * @brief Store lowest N (vector width) bytes into an unaligned address. */ ASTCENC_SIMD_INLINE void store_nbytes(vint4 a, uint8_t* p) { … } /** * @brief Gather N (vector width) indices from the array. */ ASTCENC_SIMD_INLINE vint4 gatheri(const int* base, vint4 indices) { … } /** * @brief Pack low 8 bits of N (vector width) lanes into bottom of vector. */ ASTCENC_SIMD_INLINE vint4 pack_low_bytes(vint4 a) { … } /** * @brief Return lanes from @c b if @c cond is set, else @c a. */ ASTCENC_SIMD_INLINE vint4 select(vint4 a, vint4 b, vmask4 cond) { … } // ============================================================================ // vfloat4 operators and functions // ============================================================================ /** * @brief Overload: vector by vector addition. */ ASTCENC_SIMD_INLINE vfloat4 operator+(vfloat4 a, vfloat4 b) { … } /** * @brief Overload: vector by vector subtraction. */ ASTCENC_SIMD_INLINE vfloat4 operator-(vfloat4 a, vfloat4 b) { … } /** * @brief Overload: vector by vector multiplication. */ ASTCENC_SIMD_INLINE vfloat4 operator*(vfloat4 a, vfloat4 b) { … } /** * @brief Overload: vector by vector division. */ ASTCENC_SIMD_INLINE vfloat4 operator/(vfloat4 a, vfloat4 b) { … } /** * @brief Overload: vector by vector equality. */ ASTCENC_SIMD_INLINE vmask4 operator==(vfloat4 a, vfloat4 b) { … } /** * @brief Overload: vector by vector inequality. */ ASTCENC_SIMD_INLINE vmask4 operator!=(vfloat4 a, vfloat4 b) { … } /** * @brief Overload: vector by vector less than. */ ASTCENC_SIMD_INLINE vmask4 operator<(vfloat4 a, vfloat4 b) { … } /** * @brief Overload: vector by vector greater than. */ ASTCENC_SIMD_INLINE vmask4 operator>(vfloat4 a, vfloat4 b) { … } /** * @brief Overload: vector by vector less than or equal. */ ASTCENC_SIMD_INLINE vmask4 operator<=(vfloat4 a, vfloat4 b) { … } /** * @brief Overload: vector by vector greater than or equal. */ ASTCENC_SIMD_INLINE vmask4 operator>=(vfloat4 a, vfloat4 b) { … } /** * @brief Return the min vector of two vectors. * * If either lane value is NaN, @c b will be returned for that lane. */ ASTCENC_SIMD_INLINE vfloat4 min(vfloat4 a, vfloat4 b) { … } /** * @brief Return the max vector of two vectors. * * If either lane value is NaN, @c b will be returned for that lane. */ ASTCENC_SIMD_INLINE vfloat4 max(vfloat4 a, vfloat4 b) { … } /** * @brief Return the absolute value of the float vector. */ ASTCENC_SIMD_INLINE vfloat4 abs(vfloat4 a) { … } /** * @brief Return a float rounded to the nearest integer value. */ ASTCENC_SIMD_INLINE vfloat4 round(vfloat4 a) { … } /** * @brief Return the horizontal minimum of a vector. */ ASTCENC_SIMD_INLINE vfloat4 hmin(vfloat4 a) { … } /** * @brief Return the horizontal maximum of a vector. */ ASTCENC_SIMD_INLINE vfloat4 hmax(vfloat4 a) { … } /** * @brief Return the horizontal sum of a vector as a scalar. */ ASTCENC_SIMD_INLINE float hadd_s(vfloat4 a) { … } /** * @brief Return the sqrt of the lanes in the vector. */ ASTCENC_SIMD_INLINE vfloat4 sqrt(vfloat4 a) { … } /** * @brief Return lanes from @c b if @c cond is set, else @c a. */ ASTCENC_SIMD_INLINE vfloat4 select(vfloat4 a, vfloat4 b, vmask4 cond) { … } /** * @brief Return lanes from @c b if MSB of @c cond is set, else @c a. */ ASTCENC_SIMD_INLINE vfloat4 select_msb(vfloat4 a, vfloat4 b, vmask4 cond) { … } /** * @brief Load a vector of gathered results from an array; */ ASTCENC_SIMD_INLINE vfloat4 gatherf(const float* base, vint4 indices) { … } /** * @brief Store a vector to an unaligned memory address. */ ASTCENC_SIMD_INLINE void store(vfloat4 a, float* p) { … } /** * @brief Store a vector to a 16B aligned memory address. */ ASTCENC_SIMD_INLINE void storea(vfloat4 a, float* p) { … } /** * @brief Return a integer value for a float vector, using truncation. */ ASTCENC_SIMD_INLINE vint4 float_to_int(vfloat4 a) { … } /** * @brief Return a integer value for a float vector, using round-to-nearest. */ ASTCENC_SIMD_INLINE vint4 float_to_int_rtn(vfloat4 a) { … } /** * @brief Return a float value for an integer vector. */ ASTCENC_SIMD_INLINE vfloat4 int_to_float(vint4 a) { … } /** * @brief Return a float16 value for a float vector, using round-to-nearest. */ ASTCENC_SIMD_INLINE vint4 float_to_float16(vfloat4 a) { … } /** * @brief Return a float16 value for a float scalar, using round-to-nearest. */ static inline uint16_t float_to_float16(float a) { … } /** * @brief Return a float value for a float16 vector. */ ASTCENC_SIMD_INLINE vfloat4 float16_to_float(vint4 a) { … } /** * @brief Return a float value for a float16 scalar. */ ASTCENC_SIMD_INLINE float float16_to_float(uint16_t a) { … } /** * @brief Return a float value as an integer bit pattern (i.e. no conversion). * * It is a common trick to convert floats into integer bit patterns, perform * some bit hackery based on knowledge they are IEEE 754 layout, and then * convert them back again. This is the first half of that flip. */ ASTCENC_SIMD_INLINE vint4 float_as_int(vfloat4 a) { … } /** * @brief Return a integer value as a float bit pattern (i.e. no conversion). * * It is a common trick to convert floats into integer bit patterns, perform * some bit hackery based on knowledge they are IEEE 754 layout, and then * convert them back again. This is the second half of that flip. */ ASTCENC_SIMD_INLINE vfloat4 int_as_float(vint4 v) { … } /** * @brief Prepare a vtable lookup table for use with the native SIMD size. */ ASTCENC_SIMD_INLINE void vtable_prepare(vint4 t0, vint4& t0p) { … } /** * @brief Prepare a vtable lookup table for use with the native SIMD size. */ ASTCENC_SIMD_INLINE void vtable_prepare(vint4 t0, vint4 t1, vint4& t0p, vint4& t1p) { … } /** * @brief Prepare a vtable lookup table for use with the native SIMD size. */ ASTCENC_SIMD_INLINE void vtable_prepare( vint4 t0, vint4 t1, vint4 t2, vint4 t3, vint4& t0p, vint4& t1p, vint4& t2p, vint4& t3p) { … } /** * @brief Perform an 8-bit 16-entry table lookup, with 32-bit indexes. */ ASTCENC_SIMD_INLINE vint4 vtable_8bt_32bi(vint4 t0, vint4 idx) { … } /** * @brief Perform an 8-bit 32-entry table lookup, with 32-bit indexes. */ ASTCENC_SIMD_INLINE vint4 vtable_8bt_32bi(vint4 t0, vint4 t1, vint4 idx) { … } /** * @brief Perform an 8-bit 64-entry table lookup, with 32-bit indexes. */ ASTCENC_SIMD_INLINE vint4 vtable_8bt_32bi(vint4 t0, vint4 t1, vint4 t2, vint4 t3, vint4 idx) { … } /** * @brief Return a vector of interleaved RGBA data. * * Input vectors have the value stored in the bottom 8 bits of each lane, * with high bits set to zero. * * Output vector stores a single RGBA texel packed in each lane. */ ASTCENC_SIMD_INLINE vint4 interleave_rgba8(vint4 r, vint4 g, vint4 b, vint4 a) { … } /** * @brief Store a single vector lane to an unaligned address. */ ASTCENC_SIMD_INLINE void store_lane(uint8_t* base, int data) { … } /** * @brief Store a vector, skipping masked lanes. * * All masked lanes must be at the end of vector, after all non-masked lanes. */ ASTCENC_SIMD_INLINE void store_lanes_masked(uint8_t* base, vint4 data, vmask4 mask) { … } #if defined(ASTCENC_NO_INVARIANCE) && (ASTCENC_SSE >= 41) #define ASTCENC_USE_NATIVE_DOT_PRODUCT … /** * @brief Return the dot product for the full 4 lanes, returning scalar. */ ASTCENC_SIMD_INLINE float dot_s(vfloat4 a, vfloat4 b) { return _mm_cvtss_f32(_mm_dp_ps(a.m, b.m, 0xFF)); } /** * @brief Return the dot product for the full 4 lanes, returning vector. */ ASTCENC_SIMD_INLINE vfloat4 dot(vfloat4 a, vfloat4 b) { return vfloat4(_mm_dp_ps(a.m, b.m, 0xFF)); } /** * @brief Return the dot product for the bottom 3 lanes, returning scalar. */ ASTCENC_SIMD_INLINE float dot3_s(vfloat4 a, vfloat4 b) { return _mm_cvtss_f32(_mm_dp_ps(a.m, b.m, 0x77)); } /** * @brief Return the dot product for the bottom 3 lanes, returning vector. */ ASTCENC_SIMD_INLINE vfloat4 dot3(vfloat4 a, vfloat4 b) { return vfloat4(_mm_dp_ps(a.m, b.m, 0x77)); } #endif // #if defined(ASTCENC_NO_INVARIANCE) && (ASTCENC_SSE >= 41) #if ASTCENC_POPCNT >= 1 #define ASTCENC_USE_NATIVE_POPCOUNT … /** * @brief Population bit count. * * @param v The value to population count. * * @return The number of 1 bits. */ ASTCENC_SIMD_INLINE int popcount(uint64_t v) { return static_cast<int>(_mm_popcnt_u64(v)); } #endif // ASTCENC_POPCNT >= 1 #endif // #ifndef ASTC_VECMATHLIB_SSE_4_H_INCLUDED