Path: blob/master/thirdparty/astcenc/astcenc_vecmathlib_avx2_8.h
9898 views
// SPDX-License-Identifier: Apache-2.01// ----------------------------------------------------------------------------2// Copyright 2019-2025 Arm Limited3//4// Licensed under the Apache License, Version 2.0 (the "License"); you may not5// use this file except in compliance with the License. You may obtain a copy6// of the License at:7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing, software11// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT12// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the13// License for the specific language governing permissions and limitations14// under the License.15// ----------------------------------------------------------------------------1617/**18* @brief 8x32-bit vectors, implemented using AVX2.19*20* This module implements 8-wide 32-bit float, int, and mask vectors for x8621* AVX2.22*23* There is a baseline level of functionality provided by all vector widths and24* implementations. This is implemented using identical function signatures,25* modulo data type, so we can use them as substitutable implementations in VLA26* code.27*/2829#ifndef ASTC_VECMATHLIB_AVX2_8_H_INCLUDED30#define ASTC_VECMATHLIB_AVX2_8_H_INCLUDED3132#ifndef ASTCENC_SIMD_INLINE33#error "Include astcenc_vecmathlib.h, do not include directly"34#endif3536#include <cstdio>3738// Define convenience intrinsics that are missing on older compilers39#define astcenc_mm256_set_m128i(m, n) _mm256_insertf128_si256(_mm256_castsi128_si256((n)), (m), 1)4041// ============================================================================42// vfloat8 data type43// ============================================================================4445/**46* @brief Data type for 8-wide floats.47*/48struct vfloat849{50/**51* @brief Construct from zero-initialized value.52*/53ASTCENC_SIMD_INLINE vfloat8() = default;5455/**56* @brief Construct from 8 values loaded from an unaligned address.57*58* Consider using loada() which is better with vectors if data is aligned59* to vector length.60*/61ASTCENC_SIMD_INLINE explicit vfloat8(const float *p)62{63m = _mm256_loadu_ps(p);64}6566/**67* @brief Construct from 1 scalar value replicated across all lanes.68*69* Consider using zero() for constexpr zeros.70*/71ASTCENC_SIMD_INLINE explicit vfloat8(float a)72{73m = _mm256_set1_ps(a);74}7576/**77* @brief Construct from an existing SIMD register.78*/79ASTCENC_SIMD_INLINE explicit vfloat8(__m256 a)80{81m = a;82}8384/**85* @brief Factory that returns a vector of zeros.86*/87static ASTCENC_SIMD_INLINE vfloat8 zero()88{89return vfloat8(_mm256_setzero_ps());90}9192/**93* @brief Factory that returns a replicated scalar loaded from memory.94*/95static ASTCENC_SIMD_INLINE vfloat8 load1(const float* p)96{97return vfloat8(_mm256_broadcast_ss(p));98}99100/**101* @brief Factory that returns a vector loaded from 32B aligned memory.102*/103static ASTCENC_SIMD_INLINE vfloat8 loada(const float* p)104{105return vfloat8(_mm256_load_ps(p));106}107108/**109* @brief The vector ...110*/111__m256 m;112};113114// ============================================================================115// vint8 data type116// ============================================================================117118/**119* @brief Data type for 8-wide ints.120*/121struct vint8122{123/**124* @brief Construct from zero-initialized value.125*/126ASTCENC_SIMD_INLINE vint8() = default;127128/**129* @brief Construct from 8 values loaded from an unaligned address.130*131* Consider using loada() which is better with vectors if data is aligned132* to vector length.133*/134ASTCENC_SIMD_INLINE explicit vint8(const int *p)135{136m = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(p));137}138139/**140* @brief Construct from 8 uint8_t loaded from an unaligned address.141*/142ASTCENC_SIMD_INLINE explicit vint8(const uint8_t *p)143{144// _mm_loadu_si64 would be nicer syntax, but missing on older GCC145m = _mm256_cvtepu8_epi32(_mm_cvtsi64_si128(*reinterpret_cast<const long long*>(p)));146}147148/**149* @brief Construct from 1 scalar value replicated across all lanes.150*151* Consider using zero() for constexpr zeros.152*/153ASTCENC_SIMD_INLINE explicit vint8(int a)154{155m = _mm256_set1_epi32(a);156}157158/**159* @brief Construct from an existing SIMD register.160*/161ASTCENC_SIMD_INLINE explicit vint8(__m256i a)162{163m = a;164}165166/**167* @brief Factory that returns a vector of zeros.168*/169static ASTCENC_SIMD_INLINE vint8 zero()170{171return vint8(_mm256_setzero_si256());172}173174/**175* @brief Factory that returns a replicated scalar loaded from memory.176*/177static ASTCENC_SIMD_INLINE vint8 load1(const int* p)178{179__m128i a = _mm_set1_epi32(*p);180return vint8(_mm256_broadcastd_epi32(a));181}182183/**184* @brief Factory that returns a vector loaded from unaligned memory.185*/186static ASTCENC_SIMD_INLINE vint8 load(const uint8_t* p)187{188return vint8(_mm256_lddqu_si256(reinterpret_cast<const __m256i*>(p)));189}190191/**192* @brief Factory that returns a vector loaded from 32B aligned memory.193*/194static ASTCENC_SIMD_INLINE vint8 loada(const int* p)195{196return vint8(_mm256_load_si256(reinterpret_cast<const __m256i*>(p)));197}198199/**200* @brief Factory that returns a vector containing the lane IDs.201*/202static ASTCENC_SIMD_INLINE vint8 lane_id()203{204return vint8(_mm256_set_epi32(7, 6, 5, 4, 3, 2, 1, 0));205}206207/**208* @brief The vector ...209*/210__m256i m;211};212213// ============================================================================214// vmask8 data type215// ============================================================================216217/**218* @brief Data type for 8-wide control plane masks.219*/220struct vmask8221{222/**223* @brief Construct from an existing SIMD register.224*/225ASTCENC_SIMD_INLINE explicit vmask8(__m256 a)226{227m = a;228}229230/**231* @brief Construct from an existing SIMD register.232*/233ASTCENC_SIMD_INLINE explicit vmask8(__m256i a)234{235m = _mm256_castsi256_ps(a);236}237238/**239* @brief Construct from 1 scalar value.240*/241ASTCENC_SIMD_INLINE explicit vmask8(bool a)242{243vint8 mask(a == false ? 0 : -1);244m = _mm256_castsi256_ps(mask.m);245}246247/**248* @brief The vector ...249*/250__m256 m;251};252253// ============================================================================254// vmask8 operators and functions255// ============================================================================256257/**258* @brief Overload: mask union (or).259*/260ASTCENC_SIMD_INLINE vmask8 operator|(vmask8 a, vmask8 b)261{262return vmask8(_mm256_or_ps(a.m, b.m));263}264265/**266* @brief Overload: mask intersect (and).267*/268ASTCENC_SIMD_INLINE vmask8 operator&(vmask8 a, vmask8 b)269{270return vmask8(_mm256_and_ps(a.m, b.m));271}272273/**274* @brief Overload: mask difference (xor).275*/276ASTCENC_SIMD_INLINE vmask8 operator^(vmask8 a, vmask8 b)277{278return vmask8(_mm256_xor_ps(a.m, b.m));279}280281/**282* @brief Overload: mask invert (not).283*/284ASTCENC_SIMD_INLINE vmask8 operator~(vmask8 a)285{286return vmask8(_mm256_xor_si256(_mm256_castps_si256(a.m), _mm256_set1_epi32(-1)));287}288289/**290* @brief Return a 8-bit mask code indicating mask status.291*292* bit0 = lane 0293*/294ASTCENC_SIMD_INLINE unsigned int mask(vmask8 a)295{296return static_cast<unsigned int>(_mm256_movemask_ps(a.m));297}298299/**300* @brief True if any lanes are enabled, false otherwise.301*/302ASTCENC_SIMD_INLINE bool any(vmask8 a)303{304return mask(a) != 0;305}306307/**308* @brief True if all lanes are enabled, false otherwise.309*/310ASTCENC_SIMD_INLINE bool all(vmask8 a)311{312return mask(a) == 0xFF;313}314315// ============================================================================316// vint8 operators and functions317// ============================================================================318/**319* @brief Overload: vector by vector addition.320*/321ASTCENC_SIMD_INLINE vint8 operator+(vint8 a, vint8 b)322{323return vint8(_mm256_add_epi32(a.m, b.m));324}325326/**327* @brief Overload: vector by vector incremental addition.328*/329ASTCENC_SIMD_INLINE vint8& operator+=(vint8& a, const vint8& b)330{331a = a + b;332return a;333}334335/**336* @brief Overload: vector by vector subtraction.337*/338ASTCENC_SIMD_INLINE vint8 operator-(vint8 a, vint8 b)339{340return vint8(_mm256_sub_epi32(a.m, b.m));341}342343/**344* @brief Overload: vector by vector multiplication.345*/346ASTCENC_SIMD_INLINE vint8 operator*(vint8 a, vint8 b)347{348return vint8(_mm256_mullo_epi32(a.m, b.m));349}350351/**352* @brief Overload: vector bit invert.353*/354ASTCENC_SIMD_INLINE vint8 operator~(vint8 a)355{356return vint8(_mm256_xor_si256(a.m, _mm256_set1_epi32(-1)));357}358359/**360* @brief Overload: vector by vector bitwise or.361*/362ASTCENC_SIMD_INLINE vint8 operator|(vint8 a, vint8 b)363{364return vint8(_mm256_or_si256(a.m, b.m));365}366367/**368* @brief Overload: vector by vector bitwise and.369*/370ASTCENC_SIMD_INLINE vint8 operator&(vint8 a, vint8 b)371{372return vint8(_mm256_and_si256(a.m, b.m));373}374375/**376* @brief Overload: vector by vector bitwise xor.377*/378ASTCENC_SIMD_INLINE vint8 operator^(vint8 a, vint8 b)379{380return vint8(_mm256_xor_si256(a.m, b.m));381}382383/**384* @brief Overload: vector by vector equality.385*/386ASTCENC_SIMD_INLINE vmask8 operator==(vint8 a, vint8 b)387{388return vmask8(_mm256_cmpeq_epi32(a.m, b.m));389}390391/**392* @brief Overload: vector by vector inequality.393*/394ASTCENC_SIMD_INLINE vmask8 operator!=(vint8 a, vint8 b)395{396return ~vmask8(_mm256_cmpeq_epi32(a.m, b.m));397}398399/**400* @brief Overload: vector by vector less than.401*/402ASTCENC_SIMD_INLINE vmask8 operator<(vint8 a, vint8 b)403{404return vmask8(_mm256_cmpgt_epi32(b.m, a.m));405}406407/**408* @brief Overload: vector by vector greater than.409*/410ASTCENC_SIMD_INLINE vmask8 operator>(vint8 a, vint8 b)411{412return vmask8(_mm256_cmpgt_epi32(a.m, b.m));413}414415/**416* @brief Logical shift left.417*/418template <int s> ASTCENC_SIMD_INLINE vint8 lsl(vint8 a)419{420return vint8(_mm256_slli_epi32(a.m, s));421}422423/**424* @brief Arithmetic shift right.425*/426template <int s> ASTCENC_SIMD_INLINE vint8 asr(vint8 a)427{428return vint8(_mm256_srai_epi32(a.m, s));429}430431/**432* @brief Logical shift right.433*/434template <int s> ASTCENC_SIMD_INLINE vint8 lsr(vint8 a)435{436return vint8(_mm256_srli_epi32(a.m, s));437}438439/**440* @brief Return the min vector of two vectors.441*/442ASTCENC_SIMD_INLINE vint8 min(vint8 a, vint8 b)443{444return vint8(_mm256_min_epi32(a.m, b.m));445}446447/**448* @brief Return the max vector of two vectors.449*/450ASTCENC_SIMD_INLINE vint8 max(vint8 a, vint8 b)451{452return vint8(_mm256_max_epi32(a.m, b.m));453}454455/**456* @brief Return the horizontal minimum of a vector.457*/458ASTCENC_SIMD_INLINE vint8 hmin(vint8 a)459{460// Build min within groups of 2, then 4, then 8461__m256i m = _mm256_min_epi32(a.m, _mm256_shuffle_epi32(a.m, _MM_SHUFFLE(2, 3, 0, 1)));462m = _mm256_min_epi32(m, _mm256_shuffle_epi32(m, _MM_SHUFFLE(1, 0, 3, 2)));463m = _mm256_min_epi32(m, _mm256_permute2x128_si256(m, m, 0x01));464465vint8 vmin(m);466return vmin;467}468469/**470* @brief Return the horizontal minimum of a vector.471*/472ASTCENC_SIMD_INLINE int hmin_s(vint8 a)473{474return _mm256_cvtsi256_si32(hmin(a).m);475}476477/**478* @brief Return the horizontal maximum of a vector.479*/480ASTCENC_SIMD_INLINE vint8 hmax(vint8 a)481{482// Build max within groups of 2, then 4, then 8483__m256i m = _mm256_max_epi32(a.m, _mm256_shuffle_epi32(a.m, _MM_SHUFFLE(2, 3, 0, 1)));484m = _mm256_max_epi32(m, _mm256_shuffle_epi32(m, _MM_SHUFFLE(1, 0, 3, 2)));485m = _mm256_max_epi32(m, _mm256_permute2x128_si256(m, m, 0x01));486487vint8 vmax(m);488return vmax;489}490491/**492* @brief Return the horizontal maximum of a vector.493*/494ASTCENC_SIMD_INLINE int hmax_s(vint8 a)495{496return _mm256_cvtsi256_si32(hmax(a).m);497}498499/**500* @brief Generate a vint8 from a size_t.501*/502ASTCENC_SIMD_INLINE vint8 vint8_from_size(size_t a)503{504assert(a <= std::numeric_limits<int>::max());505return vint8(static_cast<int>(a));506}507508/**509* @brief Store a vector to a 16B aligned memory address.510*/511ASTCENC_SIMD_INLINE void storea(vint8 a, int* p)512{513_mm256_store_si256(reinterpret_cast<__m256i*>(p), a.m);514}515516/**517* @brief Store a vector to an unaligned memory address.518*/519ASTCENC_SIMD_INLINE void store(vint8 a, int* p)520{521_mm256_storeu_si256(reinterpret_cast<__m256i*>(p), a.m);522}523524/**525* @brief Store lowest N (vector width) bytes into an unaligned address.526*/527ASTCENC_SIMD_INLINE void store_nbytes(vint8 a, uint8_t* p)528{529// This is the most logical implementation, but the convenience intrinsic530// is missing on older compilers (supported in g++ 9 and clang++ 9).531// _mm_storeu_si64(ptr, _mm256_extracti128_si256(v.m, 0))532_mm_storel_epi64(reinterpret_cast<__m128i*>(p), _mm256_extracti128_si256(a.m, 0));533}534535/**536* @brief Pack low 8 bits of N (vector width) lanes into bottom of vector.537*/538ASTCENC_SIMD_INLINE void pack_and_store_low_bytes(vint8 v, uint8_t* p)539{540__m256i shuf = _mm256_set_epi8(0, 0, 0, 0, 0, 0, 0, 0,5410, 0, 0, 0, 28, 24, 20, 16,5420, 0, 0, 0, 0, 0, 0, 0,5430, 0, 0, 0, 12, 8, 4, 0);544__m256i a = _mm256_shuffle_epi8(v.m, shuf);545__m128i a0 = _mm256_extracti128_si256(a, 0);546__m128i a1 = _mm256_extracti128_si256(a, 1);547__m128i b = _mm_unpacklo_epi32(a0, a1);548549__m256i r = astcenc_mm256_set_m128i(b, b);550551store_nbytes(vint8(r), p);552}553554/**555* @brief Return lanes from @c b if @c cond is set, else @c a.556*/557ASTCENC_SIMD_INLINE vint8 select(vint8 a, vint8 b, vmask8 cond)558{559__m256i condi = _mm256_castps_si256(cond.m);560return vint8(_mm256_blendv_epi8(a.m, b.m, condi));561}562563// ============================================================================564// vfloat8 operators and functions565// ============================================================================566567/**568* @brief Overload: vector by vector addition.569*/570ASTCENC_SIMD_INLINE vfloat8 operator+(vfloat8 a, vfloat8 b)571{572return vfloat8(_mm256_add_ps(a.m, b.m));573}574575/**576* @brief Overload: vector by vector incremental addition.577*/578ASTCENC_SIMD_INLINE vfloat8& operator+=(vfloat8& a, const vfloat8& b)579{580a = a + b;581return a;582}583584/**585* @brief Overload: vector by vector subtraction.586*/587ASTCENC_SIMD_INLINE vfloat8 operator-(vfloat8 a, vfloat8 b)588{589return vfloat8(_mm256_sub_ps(a.m, b.m));590}591592/**593* @brief Overload: vector by vector multiplication.594*/595ASTCENC_SIMD_INLINE vfloat8 operator*(vfloat8 a, vfloat8 b)596{597return vfloat8(_mm256_mul_ps(a.m, b.m));598}599600/**601* @brief Overload: vector by scalar multiplication.602*/603ASTCENC_SIMD_INLINE vfloat8 operator*(vfloat8 a, float b)604{605return vfloat8(_mm256_mul_ps(a.m, _mm256_set1_ps(b)));606}607608/**609* @brief Overload: scalar by vector multiplication.610*/611ASTCENC_SIMD_INLINE vfloat8 operator*(float a, vfloat8 b)612{613return vfloat8(_mm256_mul_ps(_mm256_set1_ps(a), b.m));614}615616/**617* @brief Overload: vector by vector division.618*/619ASTCENC_SIMD_INLINE vfloat8 operator/(vfloat8 a, vfloat8 b)620{621return vfloat8(_mm256_div_ps(a.m, b.m));622}623624/**625* @brief Overload: vector by scalar division.626*/627ASTCENC_SIMD_INLINE vfloat8 operator/(vfloat8 a, float b)628{629return vfloat8(_mm256_div_ps(a.m, _mm256_set1_ps(b)));630}631632/**633* @brief Overload: scalar by vector division.634*/635ASTCENC_SIMD_INLINE vfloat8 operator/(float a, vfloat8 b)636{637return vfloat8(_mm256_div_ps(_mm256_set1_ps(a), b.m));638}639640/**641* @brief Overload: vector by vector equality.642*/643ASTCENC_SIMD_INLINE vmask8 operator==(vfloat8 a, vfloat8 b)644{645return vmask8(_mm256_cmp_ps(a.m, b.m, _CMP_EQ_OQ));646}647648/**649* @brief Overload: vector by vector inequality.650*/651ASTCENC_SIMD_INLINE vmask8 operator!=(vfloat8 a, vfloat8 b)652{653return vmask8(_mm256_cmp_ps(a.m, b.m, _CMP_NEQ_OQ));654}655656/**657* @brief Overload: vector by vector less than.658*/659ASTCENC_SIMD_INLINE vmask8 operator<(vfloat8 a, vfloat8 b)660{661return vmask8(_mm256_cmp_ps(a.m, b.m, _CMP_LT_OQ));662}663664/**665* @brief Overload: vector by vector greater than.666*/667ASTCENC_SIMD_INLINE vmask8 operator>(vfloat8 a, vfloat8 b)668{669return vmask8(_mm256_cmp_ps(a.m, b.m, _CMP_GT_OQ));670}671672/**673* @brief Overload: vector by vector less than or equal.674*/675ASTCENC_SIMD_INLINE vmask8 operator<=(vfloat8 a, vfloat8 b)676{677return vmask8(_mm256_cmp_ps(a.m, b.m, _CMP_LE_OQ));678}679680/**681* @brief Overload: vector by vector greater than or equal.682*/683ASTCENC_SIMD_INLINE vmask8 operator>=(vfloat8 a, vfloat8 b)684{685return vmask8(_mm256_cmp_ps(a.m, b.m, _CMP_GE_OQ));686}687688/**689* @brief Return the min vector of two vectors.690*691* If either lane value is NaN, @c b will be returned for that lane.692*/693ASTCENC_SIMD_INLINE vfloat8 min(vfloat8 a, vfloat8 b)694{695return vfloat8(_mm256_min_ps(a.m, b.m));696}697698/**699* @brief Return the min vector of a vector and a scalar.700*701* If either lane value is NaN, @c b will be returned for that lane.702*/703ASTCENC_SIMD_INLINE vfloat8 min(vfloat8 a, float b)704{705return min(a, vfloat8(b));706}707708/**709* @brief Return the max vector of two vectors.710*711* If either lane value is NaN, @c b will be returned for that lane.712*/713ASTCENC_SIMD_INLINE vfloat8 max(vfloat8 a, vfloat8 b)714{715return vfloat8(_mm256_max_ps(a.m, b.m));716}717718/**719* @brief Return the max vector of a vector and a scalar.720*721* If either lane value is NaN, @c b will be returned for that lane.722*/723ASTCENC_SIMD_INLINE vfloat8 max(vfloat8 a, float b)724{725return max(a, vfloat8(b));726}727728/**729* @brief Return the clamped value between min and max.730*731* It is assumed that neither @c min nor @c max are NaN values. If @c a is NaN732* then @c min will be returned for that lane.733*/734ASTCENC_SIMD_INLINE vfloat8 clamp(float min, float max, vfloat8 a)735{736// Do not reorder - second operand will return if either is NaN737a.m = _mm256_max_ps(a.m, _mm256_set1_ps(min));738a.m = _mm256_min_ps(a.m, _mm256_set1_ps(max));739return a;740}741742/**743* @brief Return a clamped value between 0.0f and 1.0f.744*745* If @c a is NaN then zero will be returned for that lane.746*/747ASTCENC_SIMD_INLINE vfloat8 clampzo(vfloat8 a)748{749a.m = _mm256_max_ps(a.m, _mm256_setzero_ps());750a.m = _mm256_min_ps(a.m, _mm256_set1_ps(1.0f));751return a;752}753754/**755* @brief Return the absolute value of the float vector.756*/757ASTCENC_SIMD_INLINE vfloat8 abs(vfloat8 a)758{759__m256 msk = _mm256_castsi256_ps(_mm256_set1_epi32(0x7fffffff));760return vfloat8(_mm256_and_ps(a.m, msk));761}762763/**764* @brief Return a float rounded to the nearest integer value.765*/766ASTCENC_SIMD_INLINE vfloat8 round(vfloat8 a)767{768constexpr int flags = _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC;769return vfloat8(_mm256_round_ps(a.m, flags));770}771772/**773* @brief Return the horizontal minimum of a vector.774*/775ASTCENC_SIMD_INLINE vfloat8 hmin(vfloat8 a)776{777__m128 vlow = _mm256_castps256_ps128(a.m);778__m128 vhigh = _mm256_extractf128_ps(a.m, 1);779vlow = _mm_min_ps(vlow, vhigh);780781// First do an horizontal reduction.782__m128 shuf = _mm_shuffle_ps(vlow, vlow, _MM_SHUFFLE(2, 3, 0, 1));783__m128 mins = _mm_min_ps(vlow, shuf);784shuf = _mm_movehl_ps(shuf, mins);785mins = _mm_min_ss(mins, shuf);786787// This is the most logical implementation, but the convenience intrinsic788// is missing on older compilers (supported in g++ 9 and clang++ 9).789//__m256i r = _mm256_set_m128(m, m)790__m256 r = _mm256_insertf128_ps(_mm256_castps128_ps256(mins), mins, 1);791792return vfloat8(_mm256_permute_ps(r, 0));793}794795/**796* @brief Return the horizontal minimum of a vector.797*/798ASTCENC_SIMD_INLINE float hmin_s(vfloat8 a)799{800return _mm256_cvtss_f32(hmin(a).m);801}802803/**804* @brief Return the horizontal maximum of a vector.805*/806ASTCENC_SIMD_INLINE vfloat8 hmax(vfloat8 a)807{808__m128 vlow = _mm256_castps256_ps128(a.m);809__m128 vhigh = _mm256_extractf128_ps(a.m, 1);810vhigh = _mm_max_ps(vlow, vhigh);811812// First do an horizontal reduction.813__m128 shuf = _mm_shuffle_ps(vhigh, vhigh, _MM_SHUFFLE(2, 3, 0, 1));814__m128 maxs = _mm_max_ps(vhigh, shuf);815shuf = _mm_movehl_ps(shuf,maxs);816maxs = _mm_max_ss(maxs, shuf);817818// This is the most logical implementation, but the convenience intrinsic819// is missing on older compilers (supported in g++ 9 and clang++ 9).820//__m256i r = _mm256_set_m128(m, m)821__m256 r = _mm256_insertf128_ps(_mm256_castps128_ps256(maxs), maxs, 1);822return vfloat8(_mm256_permute_ps(r, 0));823}824825/**826* @brief Return the horizontal maximum of a vector.827*/828ASTCENC_SIMD_INLINE float hmax_s(vfloat8 a)829{830return _mm256_cvtss_f32(hmax(a).m);831}832833/**834* @brief Return the horizontal sum of a vector.835*/836ASTCENC_SIMD_INLINE float hadd_s(vfloat8 a)837{838// Two sequential 4-wide adds gives invariance with 4-wide code839vfloat4 lo(_mm256_extractf128_ps(a.m, 0));840vfloat4 hi(_mm256_extractf128_ps(a.m, 1));841return hadd_s(lo) + hadd_s(hi);842}843844/**845* @brief Return lanes from @c b if @c cond is set, else @c a.846*/847ASTCENC_SIMD_INLINE vfloat8 select(vfloat8 a, vfloat8 b, vmask8 cond)848{849return vfloat8(_mm256_blendv_ps(a.m, b.m, cond.m));850}851852/**853* @brief Accumulate lane-wise sums for a vector, folded 4-wide.854*855* This is invariant with 4-wide implementations.856*/857ASTCENC_SIMD_INLINE void haccumulate(vfloat4& accum, vfloat8 a)858{859vfloat4 lo(_mm256_extractf128_ps(a.m, 0));860haccumulate(accum, lo);861862vfloat4 hi(_mm256_extractf128_ps(a.m, 1));863haccumulate(accum, hi);864}865866/**867* @brief Accumulate lane-wise sums for a vector.868*869* This is NOT invariant with 4-wide implementations.870*/871ASTCENC_SIMD_INLINE void haccumulate(vfloat8& accum, vfloat8 a)872{873accum += a;874}875876/**877* @brief Accumulate masked lane-wise sums for a vector, folded 4-wide.878*879* This is invariant with 4-wide implementations.880*/881ASTCENC_SIMD_INLINE void haccumulate(vfloat4& accum, vfloat8 a, vmask8 m)882{883a = select(vfloat8::zero(), a, m);884haccumulate(accum, a);885}886887/**888* @brief Accumulate masked lane-wise sums for a vector.889*890* This is NOT invariant with 4-wide implementations.891*/892ASTCENC_SIMD_INLINE void haccumulate(vfloat8& accum, vfloat8 a, vmask8 m)893{894a = select(vfloat8::zero(), a, m);895haccumulate(accum, a);896}897898/**899* @brief Return the sqrt of the lanes in the vector.900*/901ASTCENC_SIMD_INLINE vfloat8 sqrt(vfloat8 a)902{903return vfloat8(_mm256_sqrt_ps(a.m));904}905906/**907* @brief Load a vector of gathered results from an array;908*/909ASTCENC_SIMD_INLINE vfloat8 gatherf(const float* base, vint8 indices)910{911return vfloat8(_mm256_i32gather_ps(base, indices.m, 4));912}913914/**915* @brief Load a vector of gathered results from an array using byte indices from memory916*/917template<>918ASTCENC_SIMD_INLINE vfloat8 gatherf_byte_inds<vfloat8>(const float* base, const uint8_t* indices)919{920#if ASTCENC_X86_GATHERS == 0921// Perform manual gather using scalar loads in two separate dependency chains,922// then merge late. MSVC translates this 1:1, which is OK. Clang turns it923// into a bunch of memory-operand inserts on 128-bit halves then merges late,924// which performs significantly worse in tests.925__m256 m0 = _mm256_broadcast_ss(base + indices[0]);926__m256 m1 = _mm256_broadcast_ss(base + indices[1]);927m0 = _mm256_blend_ps(m0, _mm256_broadcast_ss(base + indices[2]), 1 << 2);928m1 = _mm256_blend_ps(m1, _mm256_broadcast_ss(base + indices[3]), 1 << 3);929m0 = _mm256_blend_ps(m0, _mm256_broadcast_ss(base + indices[4]), 1 << 4);930m1 = _mm256_blend_ps(m1, _mm256_broadcast_ss(base + indices[5]), 1 << 5);931m0 = _mm256_blend_ps(m0, _mm256_broadcast_ss(base + indices[6]), 1 << 6);932m1 = _mm256_blend_ps(m1, _mm256_broadcast_ss(base + indices[7]), 1 << 7);933934return vfloat8(_mm256_blend_ps(m0, m1, 0xaa));935#else936vint8 inds(indices);937return gatherf(base, inds);938#endif939}940941/**942* @brief Store a vector to an unaligned memory address.943*/944ASTCENC_SIMD_INLINE void store(vfloat8 a, float* p)945{946_mm256_storeu_ps(p, a.m);947}948949/**950* @brief Store a vector to a 32B aligned memory address.951*/952ASTCENC_SIMD_INLINE void storea(vfloat8 a, float* p)953{954_mm256_store_ps(p, a.m);955}956957/**958* @brief Return a integer value for a float vector, using truncation.959*/960ASTCENC_SIMD_INLINE vint8 float_to_int(vfloat8 a)961{962return vint8(_mm256_cvttps_epi32(a.m));963}964965/**966* @brief Return a integer value for a float vector, using round-to-nearest.967*/968ASTCENC_SIMD_INLINE vint8 float_to_int_rtn(vfloat8 a)969{970a = a + vfloat8(0.5f);971return vint8(_mm256_cvttps_epi32(a.m));972}973974975/**976* @brief Return a float value for an integer vector.977*/978ASTCENC_SIMD_INLINE vfloat8 int_to_float(vint8 a)979{980return vfloat8(_mm256_cvtepi32_ps(a.m));981}982983/**984* @brief Return a float value as an integer bit pattern (i.e. no conversion).985*986* It is a common trick to convert floats into integer bit patterns, perform987* some bit hackery based on knowledge they are IEEE 754 layout, and then988* convert them back again. This is the first half of that flip.989*/990ASTCENC_SIMD_INLINE vint8 float_as_int(vfloat8 a)991{992return vint8(_mm256_castps_si256(a.m));993}994995/**996* @brief Return a integer value as a float bit pattern (i.e. no conversion).997*998* It is a common trick to convert floats into integer bit patterns, perform999* some bit hackery based on knowledge they are IEEE 754 layout, and then1000* convert them back again. This is the second half of that flip.1001*/1002ASTCENC_SIMD_INLINE vfloat8 int_as_float(vint8 a)1003{1004return vfloat8(_mm256_castsi256_ps(a.m));1005}10061007/*1008* Table structure for a 16x 8-bit entry table.1009*/1010struct vtable8_16x8 {1011vint8 t0;1012};10131014/*1015* Table structure for a 32x 8-bit entry table.1016*/1017struct vtable8_32x8 {1018vint8 t0;1019vint8 t1;1020};10211022/*1023* Table structure for a 64x 8-bit entry table.1024*/1025struct vtable8_64x8 {1026vint8 t0;1027vint8 t1;1028vint8 t2;1029vint8 t3;1030};10311032/**1033* @brief Prepare a vtable lookup table for 16x 8-bit entry table.1034*/1035ASTCENC_SIMD_INLINE void vtable_prepare(1036vtable8_16x8& table,1037const uint8_t* data1038) {1039// AVX2 tables duplicate table entries in each 128-bit half-register1040vint4 d0 = vint4::load(data);10411042table.t0 = vint8(astcenc_mm256_set_m128i(d0.m, d0.m));1043}10441045/**1046* @brief Prepare a vtable lookup table for 32x 8-bit entry table.1047*/1048ASTCENC_SIMD_INLINE void vtable_prepare(1049vtable8_32x8& table,1050const uint8_t* data1051) {1052// AVX2 tables duplicate table entries in each 128-bit half-register1053vint4 d0 = vint4::load(data);1054vint4 d1 = vint4::load(data + 16);10551056table.t0 = vint8(astcenc_mm256_set_m128i(d0.m, d0.m));1057table.t1 = vint8(astcenc_mm256_set_m128i(d1.m, d1.m));10581059// XOR chain the high rows to allow table emulation1060table.t1 = table.t1 ^ table.t0;1061}10621063/**1064* @brief Prepare a vtable lookup table 64x 8-bit entry table.1065*/1066ASTCENC_SIMD_INLINE void vtable_prepare(1067vtable8_64x8& table,1068const uint8_t* data1069) {1070// AVX2 tables duplicate table entries in each 128-bit half-register1071vint4 d0 = vint4::load(data);1072vint4 d1 = vint4::load(data + 16);1073vint4 d2 = vint4::load(data + 32);1074vint4 d3 = vint4::load(data + 48);10751076table.t0 = vint8(astcenc_mm256_set_m128i(d0.m, d0.m));1077table.t1 = vint8(astcenc_mm256_set_m128i(d1.m, d1.m));1078table.t2 = vint8(astcenc_mm256_set_m128i(d2.m, d2.m));1079table.t3 = vint8(astcenc_mm256_set_m128i(d3.m, d3.m));10801081// XOR chain the high rows to allow table emulation1082table.t3 = table.t3 ^ table.t2;1083table.t2 = table.t2 ^ table.t1;1084table.t1 = table.t1 ^ table.t0;1085}10861087/**1088* @brief Perform a vtable lookup in a 16x 8-bit table with 32-bit indices.1089*/1090ASTCENC_SIMD_INLINE vint8 vtable_lookup_32bit(1091const vtable8_16x8& tbl,1092vint8 idx1093) {1094// Set index byte MSB to 1 for unused bytes so shuffle returns zero1095__m256i idxx = _mm256_or_si256(idx.m, _mm256_set1_epi32(static_cast<int>(0xFFFFFF00)));10961097__m256i result = _mm256_shuffle_epi8(tbl.t0.m, idxx);1098return vint8(result);1099}11001101/**1102* @brief Perform a vtable lookup in a 32x 8-bit table with 32-bit indices.1103*/1104ASTCENC_SIMD_INLINE vint8 vtable_lookup_32bit(1105const vtable8_32x8& tbl,1106vint8 idx1107) {1108// Set index byte MSB to 1 for unused bytes so shuffle returns zero1109__m256i idxx = _mm256_or_si256(idx.m, _mm256_set1_epi32(static_cast<int>(0xFFFFFF00)));11101111__m256i result = _mm256_shuffle_epi8(tbl.t0.m, idxx);1112idxx = _mm256_sub_epi8(idxx, _mm256_set1_epi8(16));11131114__m256i result2 = _mm256_shuffle_epi8(tbl.t1.m, idxx);1115result = _mm256_xor_si256(result, result2);1116return vint8(result);1117}11181119/**1120* @brief Perform a vtable lookup in a 64x 8-bit table with 32-bit indices.1121*/1122ASTCENC_SIMD_INLINE vint8 vtable_lookup_32bit(1123const vtable8_64x8& tbl,1124vint8 idx1125) {1126// Set index byte MSB to 1 for unused bytes so shuffle returns zero1127__m256i idxx = _mm256_or_si256(idx.m, _mm256_set1_epi32(static_cast<int>(0xFFFFFF00)));11281129__m256i result = _mm256_shuffle_epi8(tbl.t0.m, idxx);1130idxx = _mm256_sub_epi8(idxx, _mm256_set1_epi8(16));11311132__m256i result2 = _mm256_shuffle_epi8(tbl.t1.m, idxx);1133result = _mm256_xor_si256(result, result2);1134idxx = _mm256_sub_epi8(idxx, _mm256_set1_epi8(16));11351136result2 = _mm256_shuffle_epi8(tbl.t2.m, idxx);1137result = _mm256_xor_si256(result, result2);1138idxx = _mm256_sub_epi8(idxx, _mm256_set1_epi8(16));11391140result2 = _mm256_shuffle_epi8(tbl.t3.m, idxx);1141result = _mm256_xor_si256(result, result2);11421143return vint8(result);1144}11451146/**1147* @brief Return a vector of interleaved RGBA data.1148*1149* Input vectors have the value stored in the bottom 8 bits of each lane,1150* with high bits set to zero.1151*1152* Output vector stores a single RGBA texel packed in each lane.1153*/1154ASTCENC_SIMD_INLINE vint8 interleave_rgba8(vint8 r, vint8 g, vint8 b, vint8 a)1155{1156return r + lsl<8>(g) + lsl<16>(b) + lsl<24>(a);1157}11581159/**1160* @brief Store a vector, skipping masked lanes.1161*1162* All masked lanes must be at the end of vector, after all non-masked lanes.1163*/1164ASTCENC_SIMD_INLINE void store_lanes_masked(uint8_t* base, vint8 data, vmask8 mask)1165{1166_mm256_maskstore_epi32(reinterpret_cast<int*>(base), _mm256_castps_si256(mask.m), data.m);1167}11681169/**1170* @brief Debug function to print a vector of ints.1171*/1172ASTCENC_SIMD_INLINE void print(vint8 a)1173{1174alignas(32) int v[8];1175storea(a, v);1176printf("v8_i32:\n %8d %8d %8d %8d %8d %8d %8d %8d\n",1177v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]);1178}11791180/**1181* @brief Debug function to print a vector of ints.1182*/1183ASTCENC_SIMD_INLINE void printx(vint8 a)1184{1185alignas(32) int v[8];1186storea(a, v);11871188unsigned int uv[8];1189std::memcpy(uv, v, sizeof(int) * 8);11901191printf("v8_i32:\n %08x %08x %08x %08x %08x %08x %08x %08x\n",1192uv[0], uv[1], uv[2], uv[3], uv[4], uv[5], uv[6], uv[7]);1193}11941195/**1196* @brief Debug function to print a vector of floats.1197*/1198ASTCENC_SIMD_INLINE void print(vfloat8 a)1199{1200alignas(32) float v[8];1201storea(a, v);1202printf("v8_f32:\n %0.4f %0.4f %0.4f %0.4f %0.4f %0.4f %0.4f %0.4f\n",1203static_cast<double>(v[0]), static_cast<double>(v[1]),1204static_cast<double>(v[2]), static_cast<double>(v[3]),1205static_cast<double>(v[4]), static_cast<double>(v[5]),1206static_cast<double>(v[6]), static_cast<double>(v[7]));1207}12081209/**1210* @brief Debug function to print a vector of masks.1211*/1212ASTCENC_SIMD_INLINE void print(vmask8 a)1213{1214print(select(vint8(0), vint8(1), a));1215}12161217#endif // #ifndef ASTC_VECMATHLIB_AVX2_8_H_INCLUDED121812191220