Path: blob/master/thirdparty/astcenc/astcenc_vecmathlib_sve_8.h
9898 views
// SPDX-License-Identifier: Apache-2.01// ----------------------------------------------------------------------------2// Copyright 2019-2024 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 SVE.19*20* This module implements 8-wide 32-bit float, int, and mask vectors for Arm21* SVE.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_SVE_8_H_INCLUDED30#define ASTC_VECMATHLIB_SVE_8_H_INCLUDED3132#ifndef ASTCENC_SIMD_INLINE33#error "Include astcenc_vecmathlib.h, do not include directly"34#endif3536#include <cstdio>3738typedef svbool_t svbool_8_t __attribute__((arm_sve_vector_bits(256)));39typedef svuint8_t svuint8_8_t __attribute__((arm_sve_vector_bits(256)));40typedef svuint16_t svuint16_8_t __attribute__((arm_sve_vector_bits(256)));41typedef svuint32_t svuint32_8_t __attribute__((arm_sve_vector_bits(256)));42typedef svint32_t svint32_8_t __attribute__((arm_sve_vector_bits(256)));43typedef svfloat32_t svfloat32_8_t __attribute__((arm_sve_vector_bits(256)));4445// ============================================================================46// vfloat8 data type47// ============================================================================4849/**50* @brief Data type for 8-wide floats.51*/52struct vfloat853{54/**55* @brief Construct from zero-initialized value.56*/57ASTCENC_SIMD_INLINE vfloat8() = default;5859/**60* @brief Construct from 8 values loaded from an unaligned address.61*62* Consider using loada() which is better with vectors if data is aligned63* to vector length.64*/65ASTCENC_SIMD_INLINE explicit vfloat8(const float *p)66{67m = svld1_f32(svptrue_b32(), p);68}6970/**71* @brief Construct from 1 scalar value replicated across all lanes.72*73* Consider using zero() for constexpr zeros.74*/75ASTCENC_SIMD_INLINE explicit vfloat8(float a)76{77m = svdup_f32(a);78}7980/**81* @brief Construct from an existing SIMD register.82*/83ASTCENC_SIMD_INLINE explicit vfloat8(svfloat32_8_t a)84{85m = a;86}8788/**89* @brief Factory that returns a vector of zeros.90*/91static ASTCENC_SIMD_INLINE vfloat8 zero()92{93return vfloat8(0.0f);94}9596/**97* @brief Factory that returns a replicated scalar loaded from memory.98*/99static ASTCENC_SIMD_INLINE vfloat8 load1(const float* p)100{101return vfloat8(*p);102}103104/**105* @brief Factory that returns a vector loaded from 32B aligned memory.106*/107static ASTCENC_SIMD_INLINE vfloat8 loada(const float* p)108{109return vfloat8(p);110}111112/**113* @brief The vector ...114*/115svfloat32_8_t m;116};117118// ============================================================================119// vint8 data type120// ============================================================================121122/**123* @brief Data type for 8-wide ints.124*/125struct vint8126{127/**128* @brief Construct from zero-initialized value.129*/130ASTCENC_SIMD_INLINE vint8() = default;131132/**133* @brief Construct from 8 values loaded from an unaligned address.134*135* Consider using loada() which is better with vectors if data is aligned136* to vector length.137*/138ASTCENC_SIMD_INLINE explicit vint8(const int *p)139{140m = svld1_s32(svptrue_b32(), p);141}142143/**144* @brief Construct from 8 uint8_t loaded from an unaligned address.145*/146ASTCENC_SIMD_INLINE explicit vint8(const uint8_t *p)147{148// Load 8-bit values and expand to 32-bits149m = svld1ub_s32(svptrue_b32(), p);150}151152/**153* @brief Construct from 1 scalar value replicated across all lanes.154*155* Consider using zero() for constexpr zeros.156*/157ASTCENC_SIMD_INLINE explicit vint8(int a)158{159m = svdup_s32(a);160}161162/**163* @brief Construct from an existing SIMD register.164*/165ASTCENC_SIMD_INLINE explicit vint8(svint32_8_t a)166{167m = a;168}169170/**171* @brief Factory that returns a vector of zeros.172*/173static ASTCENC_SIMD_INLINE vint8 zero()174{175return vint8(0.0f);176}177178/**179* @brief Factory that returns a replicated scalar loaded from memory.180*/181static ASTCENC_SIMD_INLINE vint8 load1(const int* p)182{183return vint8(*p);184}185186/**187* @brief Factory that returns a vector loaded from unaligned memory.188*/189static ASTCENC_SIMD_INLINE vint8 load(const uint8_t* p)190{191svuint8_8_t data = svld1_u8(svptrue_b8(), p);192return vint8(svreinterpret_s32_u8(data));193}194195/**196* @brief Factory that returns a vector loaded from 32B aligned memory.197*/198static ASTCENC_SIMD_INLINE vint8 loada(const int* p)199{200return vint8(p);201}202203/**204* @brief Factory that returns a vector containing the lane IDs.205*/206static ASTCENC_SIMD_INLINE vint8 lane_id()207{208return vint8(svindex_s32(0, 1));209}210211/**212* @brief The vector ...213*/214svint32_8_t m;215};216217// ============================================================================218// vmask8 data type219// ============================================================================220221/**222* @brief Data type for 8-wide control plane masks.223*/224struct vmask8225{226/**227* @brief Construct from an existing SIMD register.228*/229ASTCENC_SIMD_INLINE explicit vmask8(svbool_8_t a)230{231m = a;232}233234/**235* @brief Construct from 1 scalar value.236*/237ASTCENC_SIMD_INLINE explicit vmask8(bool a)238{239m = svdup_b32(a);240}241242/**243* @brief The vector ...244*/245svbool_8_t m;246};247248// ============================================================================249// vmask8 operators and functions250// ============================================================================251252/**253* @brief Overload: mask union (or).254*/255ASTCENC_SIMD_INLINE vmask8 operator|(vmask8 a, vmask8 b)256{257return vmask8(svorr_z(svptrue_b32(), a.m, b.m));258}259260/**261* @brief Overload: mask intersect (and).262*/263ASTCENC_SIMD_INLINE vmask8 operator&(vmask8 a, vmask8 b)264{265return vmask8(svand_z(svptrue_b32(), a.m, b.m));266}267268/**269* @brief Overload: mask difference (xor).270*/271ASTCENC_SIMD_INLINE vmask8 operator^(vmask8 a, vmask8 b)272{273return vmask8(sveor_z(svptrue_b32(), a.m, b.m));274}275276/**277* @brief Overload: mask invert (not).278*/279ASTCENC_SIMD_INLINE vmask8 operator~(vmask8 a)280{281return vmask8(svnot_z(svptrue_b32(), a.m));282}283284/**285* @brief Return a 8-bit mask code indicating mask status.286*287* bit0 = lane 0288*/289ASTCENC_SIMD_INLINE unsigned int mask(vmask8 a)290{291alignas(32) const int shifta[8] { 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80 };292svint32_8_t template_vals = svld1_s32(svptrue_b32(), shifta);293svint32_8_t active_vals = svsel_s32(a.m, template_vals, svdup_s32(0));294return static_cast<unsigned int>(svaddv_s32(svptrue_b32(), active_vals));295}296297/**298* @brief True if any lanes are enabled, false otherwise.299*/300ASTCENC_SIMD_INLINE bool any(vmask8 a)301{302return svptest_any(svptrue_b32(), a.m);303}304305/**306* @brief True if all lanes are enabled, false otherwise.307*/308ASTCENC_SIMD_INLINE bool all(vmask8 a)309{310return !svptest_any(svptrue_b32(), (~a).m);311}312313// ============================================================================314// vint8 operators and functions315// ============================================================================316/**317* @brief Overload: vector by vector addition.318*/319ASTCENC_SIMD_INLINE vint8 operator+(vint8 a, vint8 b)320{321return vint8(svadd_s32_x(svptrue_b32(), a.m, b.m));322}323324/**325* @brief Overload: vector by vector incremental addition.326*/327ASTCENC_SIMD_INLINE vint8& operator+=(vint8& a, const vint8& b)328{329a = a + b;330return a;331}332333/**334* @brief Overload: vector by vector subtraction.335*/336ASTCENC_SIMD_INLINE vint8 operator-(vint8 a, vint8 b)337{338return vint8(svsub_s32_x(svptrue_b32(), a.m, b.m));339}340341/**342* @brief Overload: vector by vector multiplication.343*/344ASTCENC_SIMD_INLINE vint8 operator*(vint8 a, vint8 b)345{346return vint8(svmul_s32_x(svptrue_b32(), a.m, b.m));347}348349/**350* @brief Overload: vector bit invert.351*/352ASTCENC_SIMD_INLINE vint8 operator~(vint8 a)353{354return vint8(svnot_s32_x(svptrue_b32(), a.m));355}356357/**358* @brief Overload: vector by vector bitwise or.359*/360ASTCENC_SIMD_INLINE vint8 operator|(vint8 a, vint8 b)361{362return vint8(svorr_s32_x(svptrue_b32(), a.m, b.m));363}364365/**366* @brief Overload: vector by vector bitwise and.367*/368ASTCENC_SIMD_INLINE vint8 operator&(vint8 a, vint8 b)369{370return vint8(svand_s32_x(svptrue_b32(), a.m, b.m));371}372373/**374* @brief Overload: vector by vector bitwise xor.375*/376ASTCENC_SIMD_INLINE vint8 operator^(vint8 a, vint8 b)377{378return vint8(sveor_s32_x(svptrue_b32(), a.m, b.m));379}380381/**382* @brief Overload: vector by vector equality.383*/384ASTCENC_SIMD_INLINE vmask8 operator==(vint8 a, vint8 b)385{386return vmask8(svcmpeq_s32(svptrue_b32(), a.m, b.m));387}388389/**390* @brief Overload: vector by vector inequality.391*/392ASTCENC_SIMD_INLINE vmask8 operator!=(vint8 a, vint8 b)393{394return vmask8(svcmpne_s32(svptrue_b32(), a.m, b.m));395}396397/**398* @brief Overload: vector by vector less than.399*/400ASTCENC_SIMD_INLINE vmask8 operator<(vint8 a, vint8 b)401{402return vmask8(svcmplt_s32(svptrue_b32(), a.m, b.m));403}404405/**406* @brief Overload: vector by vector greater than.407*/408ASTCENC_SIMD_INLINE vmask8 operator>(vint8 a, vint8 b)409{410return vmask8(svcmpgt_s32(svptrue_b32(), a.m, b.m));411}412413/**414* @brief Logical shift left.415*/416template <int s> ASTCENC_SIMD_INLINE vint8 lsl(vint8 a)417{418return vint8(svlsl_n_s32_x(svptrue_b32(), a.m, s));419}420421/**422* @brief Arithmetic shift right.423*/424template <int s> ASTCENC_SIMD_INLINE vint8 asr(vint8 a)425{426return vint8(svasr_n_s32_x(svptrue_b32(), a.m, s));427}428429/**430* @brief Logical shift right.431*/432template <int s> ASTCENC_SIMD_INLINE vint8 lsr(vint8 a)433{434svuint32_8_t r = svreinterpret_u32_s32(a.m);435r = svlsr_n_u32_x(svptrue_b32(), r, s);436return vint8(svreinterpret_s32_u32(r));437}438439/**440* @brief Return the min vector of two vectors.441*/442ASTCENC_SIMD_INLINE vint8 min(vint8 a, vint8 b)443{444return vint8(svmin_s32_x(svptrue_b32(), 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(svmax_s32_x(svptrue_b32(), a.m, b.m));453}454455/**456* @brief Return the horizontal minimum of a vector.457*/458ASTCENC_SIMD_INLINE vint8 hmin(vint8 a)459{460return vint8(svminv_s32(svptrue_b32(), a.m));461}462463/**464* @brief Return the horizontal minimum of a vector.465*/466ASTCENC_SIMD_INLINE int hmin_s(vint8 a)467{468return svminv_s32(svptrue_b32(), a.m);469}470471/**472* @brief Return the horizontal maximum of a vector.473*/474ASTCENC_SIMD_INLINE vint8 hmax(vint8 a)475{476return vint8(svmaxv_s32(svptrue_b32(), a.m));477}478479/**480* @brief Return the horizontal maximum of a vector.481*/482ASTCENC_SIMD_INLINE int hmax_s(vint8 a)483{484return svmaxv_s32(svptrue_b32(), a.m);485}486487/**488* @brief Generate a vint8 from a size_t.489*/490ASTCENC_SIMD_INLINE vint8 vint8_from_size(size_t a)491{492assert(a <= std::numeric_limits<int>::max());493return vint8(static_cast<int>(a));494}495496/**497* @brief Store a vector to a 16B aligned memory address.498*/499ASTCENC_SIMD_INLINE void storea(vint8 a, int* p)500{501svst1_s32(svptrue_b32(), p, a.m);502}503504/**505* @brief Store a vector to an unaligned memory address.506*/507ASTCENC_SIMD_INLINE void store(vint8 a, int* p)508{509svst1_s32(svptrue_b32(), p, a.m);510}511512/**513* @brief Store lowest N (vector width) bytes into an unaligned address.514*/515ASTCENC_SIMD_INLINE void store_nbytes(vint8 a, uint8_t* p)516{517svuint8_8_t r = svreinterpret_u8_s32(a.m);518svst1_u8(svptrue_pat_b8(SV_VL8), p, r);519}520521/**522* @brief Pack low 8 bits of N (vector width) lanes into bottom of vector.523*/524ASTCENC_SIMD_INLINE void pack_and_store_low_bytes(vint8 v, uint8_t* p)525{526svuint32_8_t data = svreinterpret_u32_s32(v.m);527svst1b_u32(svptrue_b32(), p, data);528}529530/**531* @brief Return lanes from @c b if @c cond is set, else @c a.532*/533ASTCENC_SIMD_INLINE vint8 select(vint8 a, vint8 b, vmask8 cond)534{535return vint8(svsel_s32(cond.m, b.m, a.m));536}537538// ============================================================================539// vfloat8 operators and functions540// ============================================================================541542/**543* @brief Overload: vector by vector addition.544*/545ASTCENC_SIMD_INLINE vfloat8 operator+(vfloat8 a, vfloat8 b)546{547return vfloat8(svadd_f32_x(svptrue_b32(), a.m, b.m));548}549550/**551* @brief Overload: vector by vector incremental addition.552*/553ASTCENC_SIMD_INLINE vfloat8& operator+=(vfloat8& a, const vfloat8& b)554{555a = a + b;556return a;557}558559/**560* @brief Overload: vector by vector subtraction.561*/562ASTCENC_SIMD_INLINE vfloat8 operator-(vfloat8 a, vfloat8 b)563{564return vfloat8(svsub_f32_x(svptrue_b32(), a.m, b.m));565}566567/**568* @brief Overload: vector by vector multiplication.569*/570ASTCENC_SIMD_INLINE vfloat8 operator*(vfloat8 a, vfloat8 b)571{572return vfloat8(svmul_f32_x(svptrue_b32(), a.m, b.m));573}574575/**576* @brief Overload: vector by scalar multiplication.577*/578ASTCENC_SIMD_INLINE vfloat8 operator*(vfloat8 a, float b)579{580return vfloat8(svmul_f32_x(svptrue_b32(), a.m, svdup_f32(b)));581}582583/**584* @brief Overload: scalar by vector multiplication.585*/586ASTCENC_SIMD_INLINE vfloat8 operator*(float a, vfloat8 b)587{588return vfloat8(svmul_f32_x(svptrue_b32(), svdup_f32(a), b.m));589}590591/**592* @brief Overload: vector by vector division.593*/594ASTCENC_SIMD_INLINE vfloat8 operator/(vfloat8 a, vfloat8 b)595{596return vfloat8(svdiv_f32_x(svptrue_b32(), a.m, b.m));597}598599/**600* @brief Overload: vector by scalar division.601*/602ASTCENC_SIMD_INLINE vfloat8 operator/(vfloat8 a, float b)603{604return vfloat8(svdiv_f32_x(svptrue_b32(), a.m, svdup_f32(b)));605}606607/**608* @brief Overload: scalar by vector division.609*/610ASTCENC_SIMD_INLINE vfloat8 operator/(float a, vfloat8 b)611{612return vfloat8(svdiv_f32_x(svptrue_b32(), svdup_f32(a), b.m));613}614615/**616* @brief Overload: vector by vector equality.617*/618ASTCENC_SIMD_INLINE vmask8 operator==(vfloat8 a, vfloat8 b)619{620return vmask8(svcmpeq_f32(svptrue_b32(), a.m, b.m));621}622623/**624* @brief Overload: vector by vector inequality.625*/626ASTCENC_SIMD_INLINE vmask8 operator!=(vfloat8 a, vfloat8 b)627{628return vmask8(svcmpne_f32(svptrue_b32(), a.m, b.m));629}630631/**632* @brief Overload: vector by vector less than.633*/634ASTCENC_SIMD_INLINE vmask8 operator<(vfloat8 a, vfloat8 b)635{636return vmask8(svcmplt_f32(svptrue_b32(), a.m, b.m));;637}638639/**640* @brief Overload: vector by vector greater than.641*/642ASTCENC_SIMD_INLINE vmask8 operator>(vfloat8 a, vfloat8 b)643{644return vmask8(svcmpgt_f32(svptrue_b32(), a.m, b.m));645}646647/**648* @brief Overload: vector by vector less than or equal.649*/650ASTCENC_SIMD_INLINE vmask8 operator<=(vfloat8 a, vfloat8 b)651{652return vmask8(svcmple_f32(svptrue_b32(), a.m, b.m));653}654655/**656* @brief Overload: vector by vector greater than or equal.657*/658ASTCENC_SIMD_INLINE vmask8 operator>=(vfloat8 a, vfloat8 b)659{660return vmask8(svcmpge_f32(svptrue_b32(), a.m, b.m));661}662663/**664* @brief Return the min vector of two vectors.665*666* If either lane value is NaN, the other lane will be returned.667*/668ASTCENC_SIMD_INLINE vfloat8 min(vfloat8 a, vfloat8 b)669{670return vfloat8(svminnm_f32_x(svptrue_b32(), a.m, b.m));671}672673/**674* @brief Return the min vector of a vector and a scalar.675*676* If either lane value is NaN, the other lane will be returned.677*/678ASTCENC_SIMD_INLINE vfloat8 min(vfloat8 a, float b)679{680return min(a, vfloat8(b));681}682683/**684* @brief Return the max vector of two vectors.685*686* If either lane value is NaN, the other lane will be returned.687*/688ASTCENC_SIMD_INLINE vfloat8 max(vfloat8 a, vfloat8 b)689{690return vfloat8(svmaxnm_f32_x(svptrue_b32(), a.m, b.m));691}692693/**694* @brief Return the max vector of a vector and a scalar.695*696* If either lane value is NaN, the other lane will be returned.697*/698ASTCENC_SIMD_INLINE vfloat8 max(vfloat8 a, float b)699{700return max(a, vfloat8(b));701}702703/**704* @brief Return the clamped value between min and max.705*706* It is assumed that neither @c min nor @c max are NaN values. If @c a is NaN707* then @c min will be returned for that lane.708*/709ASTCENC_SIMD_INLINE vfloat8 clamp(float minv, float maxv, vfloat8 a)710{711return min(max(a, minv), maxv);712}713714/**715* @brief Return a clamped value between 0.0f and 1.0f.716*717* If @c a is NaN then zero will be returned for that lane.718*/719ASTCENC_SIMD_INLINE vfloat8 clampzo(vfloat8 a)720{721return clamp(0.0f, 1.0f, a);722}723724/**725* @brief Return the absolute value of the float vector.726*/727ASTCENC_SIMD_INLINE vfloat8 abs(vfloat8 a)728{729return vfloat8(svabs_f32_x(svptrue_b32(), a.m));730}731732/**733* @brief Return a float rounded to the nearest integer value.734*/735ASTCENC_SIMD_INLINE vfloat8 round(vfloat8 a)736{737return vfloat8(svrintn_f32_x(svptrue_b32(), a.m));738}739740/**741* @brief Return the horizontal minimum of a vector.742*/743ASTCENC_SIMD_INLINE vfloat8 hmin(vfloat8 a)744{745return vfloat8(svminnmv_f32(svptrue_b32(), a.m));746}747748/**749* @brief Return the horizontal minimum of a vector.750*/751ASTCENC_SIMD_INLINE float hmin_s(vfloat8 a)752{753return svminnmv_f32(svptrue_b32(), a.m);754}755756/**757* @brief Return the horizontal maximum of a vector.758*/759ASTCENC_SIMD_INLINE vfloat8 hmax(vfloat8 a)760{761return vfloat8(svmaxnmv_f32(svptrue_b32(), a.m));762}763764/**765* @brief Return the horizontal maximum of a vector.766*/767ASTCENC_SIMD_INLINE float hmax_s(vfloat8 a)768{769return svmaxnmv_f32(svptrue_b32(), a.m);770}771772/**773* @brief Return the horizontal sum of a vector.774*/775ASTCENC_SIMD_INLINE float hadd_s(vfloat8 a)776{777// Can't use svaddv - it's not invariant778vfloat4 lo(svget_neonq_f32(a.m));779vfloat4 hi(svget_neonq_f32(svext_f32(a.m, a.m, 4)));780return hadd_s(lo) + hadd_s(hi);781}782783/**784* @brief Return lanes from @c b if @c cond is set, else @c a.785*/786ASTCENC_SIMD_INLINE vfloat8 select(vfloat8 a, vfloat8 b, vmask8 cond)787{788return vfloat8(svsel_f32(cond.m, b.m, a.m));789}790791/**792* @brief Accumulate lane-wise sums for a vector, folded 4-wide.793*794* This is invariant with 4-wide implementations.795*/796ASTCENC_SIMD_INLINE void haccumulate(vfloat4& accum, vfloat8 a)797{798vfloat4 lo(svget_neonq_f32(a.m));799haccumulate(accum, lo);800801vfloat4 hi(svget_neonq_f32(svext_f32(a.m, a.m, 4)));802haccumulate(accum, hi);803}804805/**806* @brief Accumulate lane-wise sums for a vector.807*808* This is NOT invariant with 4-wide implementations.809*/810ASTCENC_SIMD_INLINE void haccumulate(vfloat8& accum, vfloat8 a)811{812accum += a;813}814815/**816* @brief Accumulate masked lane-wise sums for a vector, folded 4-wide.817*818* This is invariant with 4-wide implementations.819*/820ASTCENC_SIMD_INLINE void haccumulate(vfloat4& accum, vfloat8 a, vmask8 m)821{822a = select(vfloat8::zero(), a, m);823haccumulate(accum, a);824}825826/**827* @brief Accumulate masked lane-wise sums for a vector.828*829* This is NOT invariant with 4-wide implementations.830*/831ASTCENC_SIMD_INLINE void haccumulate(vfloat8& accum, vfloat8 a, vmask8 m)832{833accum.m = svadd_f32_m(m.m, accum.m, a.m);834}835836/**837* @brief Return the sqrt of the lanes in the vector.838*/839ASTCENC_SIMD_INLINE vfloat8 sqrt(vfloat8 a)840{841return vfloat8(svsqrt_f32_x(svptrue_b32(), a.m));842}843844/**845* @brief Load a vector of gathered results from an array;846*/847ASTCENC_SIMD_INLINE vfloat8 gatherf(const float* base, vint8 indices)848{849return vfloat8(svld1_gather_s32index_f32(svptrue_b32(), base, indices.m));850}851852/**853* @brief Load a vector of gathered results from an array using byte indices from memory854*/855template<>856ASTCENC_SIMD_INLINE vfloat8 gatherf_byte_inds<vfloat8>(const float* base, const uint8_t* indices)857{858svint32_t offsets = svld1ub_s32(svptrue_b32(), indices);859return vfloat8(svld1_gather_s32index_f32(svptrue_b32(), base, offsets));860}861862/**863* @brief Store a vector to an unaligned memory address.864*/865ASTCENC_SIMD_INLINE void store(vfloat8 a, float* p)866{867svst1_f32(svptrue_b32(), p, a.m);868}869870/**871* @brief Store a vector to a 32B aligned memory address.872*/873ASTCENC_SIMD_INLINE void storea(vfloat8 a, float* p)874{875svst1_f32(svptrue_b32(), p, a.m);876}877878/**879* @brief Return a integer value for a float vector, using truncation.880*/881ASTCENC_SIMD_INLINE vint8 float_to_int(vfloat8 a)882{883return vint8(svcvt_s32_f32_x(svptrue_b32(), a.m));884}885886/**887* @brief Return a integer value for a float vector, using round-to-nearest.888*/889ASTCENC_SIMD_INLINE vint8 float_to_int_rtn(vfloat8 a)890{891a = a + vfloat8(0.5f);892return vint8(svcvt_s32_f32_x(svptrue_b32(), a.m));893}894895/**896* @brief Return a float value for an integer vector.897*/898ASTCENC_SIMD_INLINE vfloat8 int_to_float(vint8 a)899{900return vfloat8(svcvt_f32_s32_x(svptrue_b32(), a.m));901}902903/**904* @brief Return a float value as an integer bit pattern (i.e. no conversion).905*906* It is a common trick to convert floats into integer bit patterns, perform907* some bit hackery based on knowledge they are IEEE 754 layout, and then908* convert them back again. This is the first half of that flip.909*/910ASTCENC_SIMD_INLINE vint8 float_as_int(vfloat8 a)911{912return vint8(svreinterpret_s32_f32(a.m));913}914915/**916* @brief Return a integer value as a float bit pattern (i.e. no conversion).917*918* It is a common trick to convert floats into integer bit patterns, perform919* some bit hackery based on knowledge they are IEEE 754 layout, and then920* convert them back again. This is the second half of that flip.921*/922ASTCENC_SIMD_INLINE vfloat8 int_as_float(vint8 a)923{924return vfloat8(svreinterpret_f32_s32(a.m));925}926927/*928* Table structure for a 16x 8-bit entry table.929*/930struct vtable8_16x8 {931svuint8_8_t t0;932};933934/*935* Table structure for a 32x 8-bit entry table.936*/937struct vtable8_32x8 {938svuint8_8_t t0;939};940941/*942* Table structure for a 64x 8-bit entry table.943*/944struct vtable8_64x8 {945svuint8_8_t t0;946svuint8_8_t t1;947};948949/**950* @brief Prepare a vtable lookup table for 16x 8-bit entry table.951*/952ASTCENC_SIMD_INLINE void vtable_prepare(953vtable8_16x8& table,954const uint8_t* data955) {956// Top half of register will be zeros957table.t0 = svld1_u8(svptrue_pat_b8(SV_VL16), data);958}959960/**961* @brief Prepare a vtable lookup table for 32x 8-bit entry table.962*/963ASTCENC_SIMD_INLINE void vtable_prepare(964vtable8_32x8& table,965const uint8_t* data966) {967table.t0 = svld1_u8(svptrue_b8(), data);968}969970/**971* @brief Prepare a vtable lookup table 64x 8-bit entry table.972*/973ASTCENC_SIMD_INLINE void vtable_prepare(974vtable8_64x8& table,975const uint8_t* data976) {977table.t0 = svld1_u8(svptrue_b8(), data);978table.t1 = svld1_u8(svptrue_b8(), data + 32);979}980981/**982* @brief Perform a vtable lookup in a 16x 8-bit table with 32-bit indices.983*/984ASTCENC_SIMD_INLINE vint8 vtable_lookup_32bit(985const vtable8_16x8& tbl,986vint8 idx987) {988// Set index byte above max index for unused bytes so table lookup returns zero989svint32_8_t idx_masked = svorr_s32_x(svptrue_b32(), idx.m, svdup_s32(0xFFFFFF00));990svuint8_8_t idx_bytes = svreinterpret_u8_s32(idx_masked);991992svuint8_8_t result = svtbl_u8(tbl.t0, idx_bytes);993return vint8(svreinterpret_s32_u8(result));994}995996/**997* @brief Perform a vtable lookup in a 32x 8-bit table with 32-bit indices.998*/999ASTCENC_SIMD_INLINE vint8 vtable_lookup_32bit(1000const vtable8_32x8& tbl,1001vint8 idx1002) {1003// Set index byte above max index for unused bytes so table lookup returns zero1004svint32_8_t idx_masked = svorr_s32_x(svptrue_b32(), idx.m, svdup_s32(0xFFFFFF00));1005svuint8_8_t idx_bytes = svreinterpret_u8_s32(idx_masked);10061007svuint8_8_t result = svtbl_u8(tbl.t0, idx_bytes);1008return vint8(svreinterpret_s32_u8(result));1009}10101011/**1012* @brief Perform a vtable lookup in a 64x 8-bit table with 32-bit indices.1013*1014* Future: SVE2 can directly do svtbl2_u8() for a two register table.1015*/1016ASTCENC_SIMD_INLINE vint8 vtable_lookup_32bit(1017const vtable8_64x8& tbl,1018vint8 idx1019) {1020// Set index byte above max index for unused bytes so table lookup returns zero1021svint32_8_t idxm = svorr_s32_x(svptrue_b32(), idx.m, svdup_s32(0xFFFFFF00));10221023svuint8_8_t idxm8 = svreinterpret_u8_s32(idxm);1024svuint8_8_t t0_lookup = svtbl_u8(tbl.t0, idxm8);10251026idxm8 = svsub_u8_x(svptrue_b8(), idxm8, svdup_u8(32));1027svuint8_8_t t1_lookup = svtbl_u8(tbl.t1, idxm8);10281029svuint8_8_t result = svorr_u8_x(svptrue_b32(), t0_lookup, t1_lookup);1030return vint8(svreinterpret_s32_u8(result));1031}10321033/**1034* @brief Return a vector of interleaved RGBA data.1035*1036* Input vectors have the value stored in the bottom 8 bits of each lane,1037* with high bits set to zero.1038*1039* Output vector stores a single RGBA texel packed in each lane.1040*/1041ASTCENC_SIMD_INLINE vint8 interleave_rgba8(vint8 r, vint8 g, vint8 b, vint8 a)1042{1043return r + lsl<8>(g) + lsl<16>(b) + lsl<24>(a);1044}10451046/**1047* @brief Store a vector, skipping masked lanes.1048*1049* All masked lanes must be at the end of vector, after all non-masked lanes.1050*/1051ASTCENC_SIMD_INLINE void store_lanes_masked(uint8_t* base, vint8 data, vmask8 mask)1052{1053svst1_s32(mask.m, reinterpret_cast<int32_t*>(base), data.m);1054}10551056/**1057* @brief Debug function to print a vector of ints.1058*/1059ASTCENC_SIMD_INLINE void print(vint8 a)1060{1061alignas(32) int v[8];1062storea(a, v);1063printf("v8_i32:\n %8d %8d %8d %8d %8d %8d %8d %8d\n",1064v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]);1065}10661067/**1068* @brief Debug function to print a vector of ints.1069*/1070ASTCENC_SIMD_INLINE void printx(vint8 a)1071{1072alignas(32) int v[8];1073storea(a, v);1074printf("v8_i32:\n %08x %08x %08x %08x %08x %08x %08x %08x\n",1075v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]);1076}10771078/**1079* @brief Debug function to print a vector of floats.1080*/1081ASTCENC_SIMD_INLINE void print(vfloat8 a)1082{1083alignas(32) float v[8];1084storea(a, v);1085printf("v8_f32:\n %0.4f %0.4f %0.4f %0.4f %0.4f %0.4f %0.4f %0.4f\n",1086static_cast<double>(v[0]), static_cast<double>(v[1]),1087static_cast<double>(v[2]), static_cast<double>(v[3]),1088static_cast<double>(v[4]), static_cast<double>(v[5]),1089static_cast<double>(v[6]), static_cast<double>(v[7]));1090}10911092/**1093* @brief Debug function to print a vector of masks.1094*/1095ASTCENC_SIMD_INLINE void print(vmask8 a)1096{1097print(select(vint8(0), vint8(1), a));1098}10991100#endif // #ifndef ASTC_VECMATHLIB_SVE_8_H_INCLUDED110111021103