Path: blob/master/thirdparty/astcenc/astcenc_vecmathlib_neon_4.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 4x32-bit vectors, implemented using Armv8-A NEON.19*20* This module implements 4-wide 32-bit float, int, and mask vectors for21* Armv8-A NEON.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*28* The 4-wide vectors are also used as a fixed-width type, and significantly29* extend the functionality above that available to VLA code.30*/3132#ifndef ASTC_VECMATHLIB_NEON_4_H_INCLUDED33#define ASTC_VECMATHLIB_NEON_4_H_INCLUDED3435#ifndef ASTCENC_SIMD_INLINE36#error "Include astcenc_vecmathlib.h, do not include directly"37#endif3839#include <cstdio>40#include <cstring>4142// ============================================================================43// vfloat4 data type44// ============================================================================4546/**47* @brief Data type for 4-wide floats.48*/49struct vfloat450{51/**52* @brief Construct from zero-initialized value.53*/54ASTCENC_SIMD_INLINE vfloat4() = default;5556/**57* @brief Construct from 4 values loaded from an unaligned address.58*59* Consider using loada() which is better with vectors if data is aligned60* to vector length.61*/62ASTCENC_SIMD_INLINE explicit vfloat4(const float *p)63{64m = vld1q_f32(p);65}6667/**68* @brief Construct from 1 scalar value replicated across all lanes.69*70* Consider using zero() for constexpr zeros.71*/72ASTCENC_SIMD_INLINE explicit vfloat4(float a)73{74m = vdupq_n_f32(a);75}7677/**78* @brief Construct from 4 scalar values.79*80* The value of @c a is stored to lane 0 (LSB) in the SIMD register.81*/82ASTCENC_SIMD_INLINE explicit vfloat4(float a, float b, float c, float d)83{84float v[4] { a, b, c, d };85m = vld1q_f32(v);86}8788/**89* @brief Construct from an existing SIMD register.90*/91ASTCENC_SIMD_INLINE explicit vfloat4(float32x4_t a)92{93m = a;94}9596/**97* @brief Get the scalar value of a single lane.98*/99template <int l> ASTCENC_SIMD_INLINE float lane() const100{101return vgetq_lane_f32(m, l);102}103104/**105* @brief Set the scalar value of a single lane.106*/107template <int l> ASTCENC_SIMD_INLINE void set_lane(float a)108{109m = vsetq_lane_f32(a, m, l);110}111112/**113* @brief Factory that returns a vector of zeros.114*/115static ASTCENC_SIMD_INLINE vfloat4 zero()116{117return vfloat4(0.0f);118}119120/**121* @brief Factory that returns a replicated scalar loaded from memory.122*/123static ASTCENC_SIMD_INLINE vfloat4 load1(const float* p)124{125return vfloat4(vld1q_dup_f32(p));126}127128/**129* @brief Factory that returns a vector loaded from 16B aligned memory.130*/131static ASTCENC_SIMD_INLINE vfloat4 loada(const float* p)132{133return vfloat4(vld1q_f32(p));134}135136/**137* @brief Return a swizzled float 2.138*/139template <int l0, int l1> ASTCENC_SIMD_INLINE vfloat4 swz() const140{141return vfloat4(lane<l0>(), lane<l1>(), 0.0f, 0.0f);142}143144/**145* @brief Return a swizzled float 3.146*/147template <int l0, int l1, int l2> ASTCENC_SIMD_INLINE vfloat4 swz() const148{149return vfloat4(lane<l0>(), lane<l1>(), lane<l2>(), 0.0f);150}151152/**153* @brief Return a swizzled float 4.154*/155template <int l0, int l1, int l2, int l3> ASTCENC_SIMD_INLINE vfloat4 swz() const156{157return vfloat4(lane<l0>(), lane<l1>(), lane<l2>(), lane<l3>());158}159160/**161* @brief The vector ...162*/163float32x4_t m;164};165166// ============================================================================167// vint4 data type168// ============================================================================169170/**171* @brief Data type for 4-wide ints.172*/173struct vint4174{175/**176* @brief Construct from zero-initialized value.177*/178ASTCENC_SIMD_INLINE vint4() = default;179180/**181* @brief Construct from 4 values loaded from an unaligned address.182*183* Consider using loada() which is better with vectors if data is aligned184* to vector length.185*/186ASTCENC_SIMD_INLINE explicit vint4(const int *p)187{188m = vld1q_s32(p);189}190191/**192* @brief Construct from 4 uint8_t loaded from an unaligned address.193*/194ASTCENC_SIMD_INLINE explicit vint4(const uint8_t *p)195{196#if ASTCENC_SVE == 0197// Cast is safe - NEON loads are allowed to be unaligned198uint32x2_t t8 = vld1_dup_u32(reinterpret_cast<const uint32_t*>(p));199uint16x4_t t16 = vget_low_u16(vmovl_u8(vreinterpret_u8_u32(t8)));200m = vreinterpretq_s32_u32(vmovl_u16(t16));201#else202svint32_t data = svld1ub_s32(svptrue_pat_b32(SV_VL4), p);203m = svget_neonq(data);204#endif205}206207/**208* @brief Construct from 1 scalar value replicated across all lanes.209*210* Consider using zero() for constexpr zeros.211*/212ASTCENC_SIMD_INLINE explicit vint4(int a)213{214m = vdupq_n_s32(a);215}216217/**218* @brief Construct from 4 scalar values.219*220* The value of @c a is stored to lane 0 (LSB) in the SIMD register.221*/222ASTCENC_SIMD_INLINE explicit vint4(int a, int b, int c, int d)223{224int v[4] { a, b, c, d };225m = vld1q_s32(v);226}227228/**229* @brief Construct from an existing SIMD register.230*/231ASTCENC_SIMD_INLINE explicit vint4(int32x4_t a)232{233m = a;234}235236/**237* @brief Get the scalar from a single lane.238*/239template <int l> ASTCENC_SIMD_INLINE int lane() const240{241return vgetq_lane_s32(m, l);242}243244/**245* @brief Set the scalar value of a single lane.246*/247template <int l> ASTCENC_SIMD_INLINE void set_lane(int a)248{249m = vsetq_lane_s32(a, m, l);250}251252/**253* @brief Factory that returns a vector of zeros.254*/255static ASTCENC_SIMD_INLINE vint4 zero()256{257return vint4(0);258}259260/**261* @brief Factory that returns a replicated scalar loaded from memory.262*/263static ASTCENC_SIMD_INLINE vint4 load1(const int* p)264{265return vint4(*p);266}267268/**269* @brief Factory that returns a vector loaded from unaligned memory.270*/271static ASTCENC_SIMD_INLINE vint4 load(const uint8_t* p)272{273vint4 data;274std::memcpy(&data.m, p, 4 * sizeof(int));275return data;276}277278/**279* @brief Factory that returns a vector loaded from 16B aligned memory.280*/281static ASTCENC_SIMD_INLINE vint4 loada(const int* p)282{283return vint4(p);284}285286/**287* @brief Factory that returns a vector containing the lane IDs.288*/289static ASTCENC_SIMD_INLINE vint4 lane_id()290{291alignas(16) static const int data[4] { 0, 1, 2, 3 };292return vint4(vld1q_s32(data));293}294295/**296* @brief The vector ...297*/298int32x4_t m;299};300301// ============================================================================302// vmask4 data type303// ============================================================================304305/**306* @brief Data type for 4-wide control plane masks.307*/308struct vmask4309{310/**311* @brief Construct from an existing SIMD register.312*/313ASTCENC_SIMD_INLINE explicit vmask4(uint32x4_t a)314{315m = a;316}317318#if !defined(_MSC_VER)319/**320* @brief Construct from an existing SIMD register.321*/322ASTCENC_SIMD_INLINE explicit vmask4(int32x4_t a)323{324m = vreinterpretq_u32_s32(a);325}326#endif327328/**329* @brief Construct from 1 scalar value.330*/331ASTCENC_SIMD_INLINE explicit vmask4(bool a)332{333m = vreinterpretq_u32_s32(vdupq_n_s32(a == true ? -1 : 0));334}335336/**337* @brief Construct from 4 scalar values.338*339* The value of @c a is stored to lane 0 (LSB) in the SIMD register.340*/341ASTCENC_SIMD_INLINE explicit vmask4(bool a, bool b, bool c, bool d)342{343int v[4] {344a == true ? -1 : 0,345b == true ? -1 : 0,346c == true ? -1 : 0,347d == true ? -1 : 0348};349350int32x4_t ms = vld1q_s32(v);351m = vreinterpretq_u32_s32(ms);352}353354/**355* @brief Get the scalar from a single lane.356*/357template <int32_t l> ASTCENC_SIMD_INLINE bool lane() const358{359return vgetq_lane_u32(m, l) != 0;360}361362/**363* @brief The vector ...364*/365uint32x4_t m;366};367368// ============================================================================369// vmask4 operators and functions370// ============================================================================371372/**373* @brief Overload: mask union (or).374*/375ASTCENC_SIMD_INLINE vmask4 operator|(vmask4 a, vmask4 b)376{377return vmask4(vorrq_u32(a.m, b.m));378}379380/**381* @brief Overload: mask intersect (and).382*/383ASTCENC_SIMD_INLINE vmask4 operator&(vmask4 a, vmask4 b)384{385return vmask4(vandq_u32(a.m, b.m));386}387388/**389* @brief Overload: mask difference (xor).390*/391ASTCENC_SIMD_INLINE vmask4 operator^(vmask4 a, vmask4 b)392{393return vmask4(veorq_u32(a.m, b.m));394}395396/**397* @brief Overload: mask invert (not).398*/399ASTCENC_SIMD_INLINE vmask4 operator~(vmask4 a)400{401return vmask4(vmvnq_u32(a.m));402}403404/**405* @brief Return a 4-bit mask code indicating mask status.406*407* bit0 = lane 0408*/409ASTCENC_SIMD_INLINE unsigned int mask(vmask4 a)410{411static const int shifta[4] { 0, 1, 2, 3 };412static const int32x4_t shift = vld1q_s32(shifta);413414uint32x4_t tmp = vshrq_n_u32(a.m, 31);415return vaddvq_u32(vshlq_u32(tmp, shift));416}417418/**419* @brief True if any lanes are enabled, false otherwise.420*/421ASTCENC_SIMD_INLINE bool any(vmask4 a)422{423return vmaxvq_u32(a.m) != 0;424}425426/**427* @brief True if all lanes are enabled, false otherwise.428*/429ASTCENC_SIMD_INLINE bool all(vmask4 a)430{431return vminvq_u32(a.m) != 0;432}433434// ============================================================================435// vint4 operators and functions436// ============================================================================437438/**439* @brief Overload: vector by vector addition.440*/441ASTCENC_SIMD_INLINE vint4 operator+(vint4 a, vint4 b)442{443return vint4(vaddq_s32(a.m, b.m));444}445446/**447* @brief Overload: vector by vector subtraction.448*/449ASTCENC_SIMD_INLINE vint4 operator-(vint4 a, vint4 b)450{451return vint4(vsubq_s32(a.m, b.m));452}453454/**455* @brief Overload: vector by vector multiplication.456*/457ASTCENC_SIMD_INLINE vint4 operator*(vint4 a, vint4 b)458{459return vint4(vmulq_s32(a.m, b.m));460}461462/**463* @brief Overload: vector bit invert.464*/465ASTCENC_SIMD_INLINE vint4 operator~(vint4 a)466{467return vint4(vmvnq_s32(a.m));468}469470/**471* @brief Overload: vector by vector bitwise or.472*/473ASTCENC_SIMD_INLINE vint4 operator|(vint4 a, vint4 b)474{475return vint4(vorrq_s32(a.m, b.m));476}477478/**479* @brief Overload: vector by vector bitwise and.480*/481ASTCENC_SIMD_INLINE vint4 operator&(vint4 a, vint4 b)482{483return vint4(vandq_s32(a.m, b.m));484}485486/**487* @brief Overload: vector by vector bitwise xor.488*/489ASTCENC_SIMD_INLINE vint4 operator^(vint4 a, vint4 b)490{491return vint4(veorq_s32(a.m, b.m));492}493494/**495* @brief Overload: vector by vector equality.496*/497ASTCENC_SIMD_INLINE vmask4 operator==(vint4 a, vint4 b)498{499return vmask4(vceqq_s32(a.m, b.m));500}501502/**503* @brief Overload: vector by vector inequality.504*/505ASTCENC_SIMD_INLINE vmask4 operator!=(vint4 a, vint4 b)506{507return ~vmask4(vceqq_s32(a.m, b.m));508}509510/**511* @brief Overload: vector by vector less than.512*/513ASTCENC_SIMD_INLINE vmask4 operator<(vint4 a, vint4 b)514{515return vmask4(vcltq_s32(a.m, b.m));516}517518/**519* @brief Overload: vector by vector greater than.520*/521ASTCENC_SIMD_INLINE vmask4 operator>(vint4 a, vint4 b)522{523return vmask4(vcgtq_s32(a.m, b.m));524}525526/**527* @brief Logical shift left.528*/529template <int s> ASTCENC_SIMD_INLINE vint4 lsl(vint4 a)530{531return vint4(vshlq_s32(a.m, vdupq_n_s32(s)));532}533534/**535* @brief Logical shift right.536*/537template <int s> ASTCENC_SIMD_INLINE vint4 lsr(vint4 a)538{539uint32x4_t ua = vreinterpretq_u32_s32(a.m);540ua = vshlq_u32(ua, vdupq_n_s32(-s));541return vint4(vreinterpretq_s32_u32(ua));542}543544/**545* @brief Arithmetic shift right.546*/547template <int s> ASTCENC_SIMD_INLINE vint4 asr(vint4 a)548{549return vint4(vshlq_s32(a.m, vdupq_n_s32(-s)));550}551552/**553* @brief Return the min vector of two vectors.554*/555ASTCENC_SIMD_INLINE vint4 min(vint4 a, vint4 b)556{557return vint4(vminq_s32(a.m, b.m));558}559560/**561* @brief Return the max vector of two vectors.562*/563ASTCENC_SIMD_INLINE vint4 max(vint4 a, vint4 b)564{565return vint4(vmaxq_s32(a.m, b.m));566}567568/**569* @brief Return the horizontal minimum of a vector.570*/571ASTCENC_SIMD_INLINE vint4 hmin(vint4 a)572{573return vint4(vminvq_s32(a.m));574}575576/**577* @brief Return the horizontal maximum of a vector.578*/579ASTCENC_SIMD_INLINE vint4 hmax(vint4 a)580{581return vint4(vmaxvq_s32(a.m));582}583584/**585* @brief Store a vector to a 16B aligned memory address.586*/587ASTCENC_SIMD_INLINE void storea(vint4 a, int* p)588{589vst1q_s32(p, a.m);590}591592/**593* @brief Store a vector to an unaligned memory address.594*/595ASTCENC_SIMD_INLINE void store(vint4 a, int* p)596{597vst1q_s32(p, a.m);598}599600/**601* @brief Store a vector to an unaligned memory address.602*/603ASTCENC_SIMD_INLINE void store(vint4 a, uint8_t* p)604{605std::memcpy(p, &a.m, sizeof(int) * 4);606}607608/**609* @brief Store lowest N (vector width) bytes into an unaligned address.610*/611ASTCENC_SIMD_INLINE void store_nbytes(vint4 a, uint8_t* p)612{613vst1q_lane_s32(reinterpret_cast<int32_t*>(p), a.m, 0);614}615616/**617* @brief Pack and store low 8 bits of each vector lane.618*/619ASTCENC_SIMD_INLINE void pack_and_store_low_bytes(vint4 a, uint8_t* data)620{621alignas(16) uint8_t shuf[16] {6220, 4, 8, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0623};624uint8x16_t idx = vld1q_u8(shuf);625int8x16_t av = vreinterpretq_s8_s32(a.m);626a = vint4(vreinterpretq_s32_s8(vqtbl1q_s8(av, idx)));627store_nbytes(a, data);628}629630/**631* @brief Return lanes from @c b if @c cond is set, else @c a.632*/633ASTCENC_SIMD_INLINE vint4 select(vint4 a, vint4 b, vmask4 cond)634{635return vint4(vbslq_s32(cond.m, b.m, a.m));636}637638// ============================================================================639// vfloat4 operators and functions640// ============================================================================641642/**643* @brief Overload: vector by vector addition.644*/645ASTCENC_SIMD_INLINE vfloat4 operator+(vfloat4 a, vfloat4 b)646{647return vfloat4(vaddq_f32(a.m, b.m));648}649650/**651* @brief Overload: vector by vector subtraction.652*/653ASTCENC_SIMD_INLINE vfloat4 operator-(vfloat4 a, vfloat4 b)654{655return vfloat4(vsubq_f32(a.m, b.m));656}657658/**659* @brief Overload: vector by vector multiplication.660*/661ASTCENC_SIMD_INLINE vfloat4 operator*(vfloat4 a, vfloat4 b)662{663return vfloat4(vmulq_f32(a.m, b.m));664}665666/**667* @brief Overload: vector by vector division.668*/669ASTCENC_SIMD_INLINE vfloat4 operator/(vfloat4 a, vfloat4 b)670{671return vfloat4(vdivq_f32(a.m, b.m));672}673674/**675* @brief Overload: vector by vector equality.676*/677ASTCENC_SIMD_INLINE vmask4 operator==(vfloat4 a, vfloat4 b)678{679return vmask4(vceqq_f32(a.m, b.m));680}681682/**683* @brief Overload: vector by vector inequality.684*/685ASTCENC_SIMD_INLINE vmask4 operator!=(vfloat4 a, vfloat4 b)686{687return vmask4(vmvnq_u32(vceqq_f32(a.m, b.m)));688}689690/**691* @brief Overload: vector by vector less than.692*/693ASTCENC_SIMD_INLINE vmask4 operator<(vfloat4 a, vfloat4 b)694{695return vmask4(vcltq_f32(a.m, b.m));696}697698/**699* @brief Overload: vector by vector greater than.700*/701ASTCENC_SIMD_INLINE vmask4 operator>(vfloat4 a, vfloat4 b)702{703return vmask4(vcgtq_f32(a.m, b.m));704}705706/**707* @brief Overload: vector by vector less than or equal.708*/709ASTCENC_SIMD_INLINE vmask4 operator<=(vfloat4 a, vfloat4 b)710{711return vmask4(vcleq_f32(a.m, b.m));712}713714/**715* @brief Overload: vector by vector greater than or equal.716*/717ASTCENC_SIMD_INLINE vmask4 operator>=(vfloat4 a, vfloat4 b)718{719return vmask4(vcgeq_f32(a.m, b.m));720}721722/**723* @brief Return the min vector of two vectors.724*725* If either lane value is NaN, @c b will be returned for that lane.726*/727ASTCENC_SIMD_INLINE vfloat4 min(vfloat4 a, vfloat4 b)728{729// Do not reorder - second operand will return if either is NaN730return vfloat4(vminnmq_f32(a.m, b.m));731}732733/**734* @brief Return the max vector of two vectors.735*736* If either lane value is NaN, @c b will be returned for that lane.737*/738ASTCENC_SIMD_INLINE vfloat4 max(vfloat4 a, vfloat4 b)739{740// Do not reorder - second operand will return if either is NaN741return vfloat4(vmaxnmq_f32(a.m, b.m));742}743744/**745* @brief Return the absolute value of the float vector.746*/747ASTCENC_SIMD_INLINE vfloat4 abs(vfloat4 a)748{749float32x4_t zero = vdupq_n_f32(0.0f);750float32x4_t inv = vsubq_f32(zero, a.m);751return vfloat4(vmaxq_f32(a.m, inv));752}753754/**755* @brief Return a float rounded to the nearest integer value.756*/757ASTCENC_SIMD_INLINE vfloat4 round(vfloat4 a)758{759return vfloat4(vrndnq_f32(a.m));760}761762/**763* @brief Return the horizontal minimum of a vector.764*/765ASTCENC_SIMD_INLINE vfloat4 hmin(vfloat4 a)766{767return vfloat4(vminvq_f32(a.m));768}769770/**771* @brief Return the horizontal maximum of a vector.772*/773ASTCENC_SIMD_INLINE vfloat4 hmax(vfloat4 a)774{775return vfloat4(vmaxvq_f32(a.m));776}777778/**779* @brief Return the horizontal sum of a vector.780*/781ASTCENC_SIMD_INLINE float hadd_s(vfloat4 a)782{783// Perform halving add to ensure invariance; we cannot use vaddqv as this784// does (0 + 1 + 2 + 3) which is not invariant with x86 (0 + 2) + (1 + 3).785float32x2_t t = vadd_f32(vget_high_f32(a.m), vget_low_f32(a.m));786return vget_lane_f32(vpadd_f32(t, t), 0);787}788789/**790* @brief Return the sqrt of the lanes in the vector.791*/792ASTCENC_SIMD_INLINE vfloat4 sqrt(vfloat4 a)793{794return vfloat4(vsqrtq_f32(a.m));795}796797/**798* @brief Return lanes from @c b if @c cond is set, else @c a.799*/800ASTCENC_SIMD_INLINE vfloat4 select(vfloat4 a, vfloat4 b, vmask4 cond)801{802return vfloat4(vbslq_f32(cond.m, b.m, a.m));803}804805/**806* @brief Load a vector of gathered results from an array;807*/808ASTCENC_SIMD_INLINE vfloat4 gatherf(const float* base, vint4 indices)809{810#if ASTCENC_SVE == 0811alignas(16) int idx[4];812storea(indices, idx);813alignas(16) float vals[4];814vals[0] = base[idx[0]];815vals[1] = base[idx[1]];816vals[2] = base[idx[2]];817vals[3] = base[idx[3]];818return vfloat4(vals);819#else820svint32_t offsets = svset_neonq_s32(svundef_s32(), indices.m);821svfloat32_t data = svld1_gather_s32index_f32(svptrue_pat_b32(SV_VL4), base, offsets);822return vfloat4(svget_neonq_f32(data));823#endif824}825826/**827* @brief Load a vector of gathered results from an array using byte indices from memory828*/829template<>830ASTCENC_SIMD_INLINE vfloat4 gatherf_byte_inds<vfloat4>(const float* base, const uint8_t* indices)831{832#if ASTCENC_SVE == 0833alignas(16) float vals[4];834vals[0] = base[indices[0]];835vals[1] = base[indices[1]];836vals[2] = base[indices[2]];837vals[3] = base[indices[3]];838return vfloat4(vals);839#else840svint32_t offsets = svld1ub_s32(svptrue_pat_b32(SV_VL4), indices);841svfloat32_t data = svld1_gather_s32index_f32(svptrue_pat_b32(SV_VL4), base, offsets);842return vfloat4(svget_neonq_f32(data));843#endif844}845/**846* @brief Store a vector to an unaligned memory address.847*/848ASTCENC_SIMD_INLINE void store(vfloat4 a, float* p)849{850vst1q_f32(p, a.m);851}852853/**854* @brief Store a vector to a 16B aligned memory address.855*/856ASTCENC_SIMD_INLINE void storea(vfloat4 a, float* p)857{858vst1q_f32(p, a.m);859}860861/**862* @brief Return a integer value for a float vector, using truncation.863*/864ASTCENC_SIMD_INLINE vint4 float_to_int(vfloat4 a)865{866return vint4(vcvtq_s32_f32(a.m));867}868869/**870* @brief Return a integer value for a float vector, using round-to-nearest.871*/872ASTCENC_SIMD_INLINE vint4 float_to_int_rtn(vfloat4 a)873{874a = a + vfloat4(0.5f);875return vint4(vcvtq_s32_f32(a.m));876}877878/**879* @brief Return a float value for an integer vector.880*/881ASTCENC_SIMD_INLINE vfloat4 int_to_float(vint4 a)882{883return vfloat4(vcvtq_f32_s32(a.m));884}885886/**887* @brief Return a float16 value for a float vector, using round-to-nearest.888*/889ASTCENC_SIMD_INLINE vint4 float_to_float16(vfloat4 a)890{891// Generate float16 value892float16x4_t f16 = vcvt_f16_f32(a.m);893894// Convert each 16-bit float pattern to a 32-bit pattern895uint16x4_t u16 = vreinterpret_u16_f16(f16);896uint32x4_t u32 = vmovl_u16(u16);897return vint4(vreinterpretq_s32_u32(u32));898}899900/**901* @brief Return a float16 value for a float scalar, using round-to-nearest.902*/903static inline uint16_t float_to_float16(float a)904{905vfloat4 av(a);906return static_cast<uint16_t>(float_to_float16(av).lane<0>());907}908909/**910* @brief Return a float value for a float16 vector.911*/912ASTCENC_SIMD_INLINE vfloat4 float16_to_float(vint4 a)913{914// Convert each 32-bit float pattern to a 16-bit pattern915uint32x4_t u32 = vreinterpretq_u32_s32(a.m);916uint16x4_t u16 = vmovn_u32(u32);917float16x4_t f16 = vreinterpret_f16_u16(u16);918919// Generate float16 value920return vfloat4(vcvt_f32_f16(f16));921}922923/**924* @brief Return a float value for a float16 scalar.925*/926ASTCENC_SIMD_INLINE float float16_to_float(uint16_t a)927{928vint4 av(a);929return float16_to_float(av).lane<0>();930}931932/**933* @brief Return a float value as an integer bit pattern (i.e. no conversion).934*935* It is a common trick to convert floats into integer bit patterns, perform936* some bit hackery based on knowledge they are IEEE 754 layout, and then937* convert them back again. This is the first half of that flip.938*/939ASTCENC_SIMD_INLINE vint4 float_as_int(vfloat4 a)940{941return vint4(vreinterpretq_s32_f32(a.m));942}943944/**945* @brief Return a integer value as a float bit pattern (i.e. no conversion).946*947* It is a common trick to convert floats into integer bit patterns, perform948* some bit hackery based on knowledge they are IEEE 754 layout, and then949* convert them back again. This is the second half of that flip.950*/951ASTCENC_SIMD_INLINE vfloat4 int_as_float(vint4 v)952{953return vfloat4(vreinterpretq_f32_s32(v.m));954}955956/*957* Table structure for a 16x 8-bit entry table.958*/959struct vtable4_16x8 {960uint8x16_t t0;961};962963/*964* Table structure for a 32x 8-bit entry table.965*/966struct vtable4_32x8 {967uint8x16x2_t t01;968};969970/*971* Table structure for a 64x 8-bit entry table.972*/973struct vtable4_64x8 {974uint8x16x4_t t0123;975};976977/**978* @brief Prepare a vtable lookup table for 16x 8-bit entry table.979*/980ASTCENC_SIMD_INLINE void vtable_prepare(981vtable4_16x8& table,982const uint8_t* data983) {984table.t0 = vld1q_u8(data);985}986987/**988* @brief Prepare a vtable lookup table for 32x 8-bit entry table.989*/990ASTCENC_SIMD_INLINE void vtable_prepare(991vtable4_32x8& table,992const uint8_t* data993) {994table.t01 = uint8x16x2_t {995vld1q_u8(data),996vld1q_u8(data + 16)997};998}9991000/**1001* @brief Prepare a vtable lookup table 64x 8-bit entry table.1002*/1003ASTCENC_SIMD_INLINE void vtable_prepare(1004vtable4_64x8& table,1005const uint8_t* data1006) {1007table.t0123 = uint8x16x4_t {1008vld1q_u8(data),1009vld1q_u8(data + 16),1010vld1q_u8(data + 32),1011vld1q_u8(data + 48)1012};1013}10141015/**1016* @brief Perform a vtable lookup in a 16x 8-bit table with 32-bit indices.1017*/1018ASTCENC_SIMD_INLINE vint4 vtable_lookup_32bit(1019const vtable4_16x8& tbl,1020vint4 idx1021) {1022// Set index byte above max index for unused bytes so table lookup returns zero1023int32x4_t idx_masked = vorrq_s32(idx.m, vdupq_n_s32(0xFFFFFF00));1024uint8x16_t idx_bytes = vreinterpretq_u8_s32(idx_masked);10251026return vint4(vreinterpretq_s32_u8(vqtbl1q_u8(tbl.t0, idx_bytes)));1027}10281029/**1030* @brief Perform a vtable lookup in a 32x 8-bit table with 32-bit indices.1031*/1032ASTCENC_SIMD_INLINE vint4 vtable_lookup_32bit(1033const vtable4_32x8& tbl,1034vint4 idx1035) {1036// Set index byte above max index for unused bytes so table lookup returns zero1037int32x4_t idx_masked = vorrq_s32(idx.m, vdupq_n_s32(0xFFFFFF00));1038uint8x16_t idx_bytes = vreinterpretq_u8_s32(idx_masked);10391040return vint4(vreinterpretq_s32_u8(vqtbl2q_u8(tbl.t01, idx_bytes)));1041}10421043/**1044* @brief Perform a vtable lookup in a 64x 8-bit table with 32-bit indices.1045*/1046ASTCENC_SIMD_INLINE vint4 vtable_lookup_32bit(1047const vtable4_64x8& tbl,1048vint4 idx1049) {1050// Set index byte above max index for unused bytes so table lookup returns zero1051int32x4_t idx_masked = vorrq_s32(idx.m, vdupq_n_s32(0xFFFFFF00));1052uint8x16_t idx_bytes = vreinterpretq_u8_s32(idx_masked);10531054return vint4(vreinterpretq_s32_u8(vqtbl4q_u8(tbl.t0123, idx_bytes)));1055}10561057/**1058* @brief Return a vector of interleaved RGBA data.1059*1060* Input vectors have the value stored in the bottom 8 bits of each lane,1061* with high bits set to zero.1062*1063* Output vector stores a single RGBA texel packed in each lane.1064*/1065ASTCENC_SIMD_INLINE vint4 interleave_rgba8(vint4 r, vint4 g, vint4 b, vint4 a)1066{1067return r + lsl<8>(g) + lsl<16>(b) + lsl<24>(a);1068}10691070/**1071* @brief Store a single vector lane to an unaligned address.1072*/1073ASTCENC_SIMD_INLINE void store_lane(uint8_t* base, int data)1074{1075std::memcpy(base, &data, sizeof(int));1076}10771078/**1079* @brief Store a vector, skipping masked lanes.1080*1081* All masked lanes must be at the end of vector, after all non-masked lanes.1082*/1083ASTCENC_SIMD_INLINE void store_lanes_masked(uint8_t* base, vint4 data, vmask4 mask)1084{1085if (mask.lane<3>())1086{1087store(data, base);1088}1089else if (mask.lane<2>() != 0.0f)1090{1091store_lane(base + 0, data.lane<0>());1092store_lane(base + 4, data.lane<1>());1093store_lane(base + 8, data.lane<2>());1094}1095else if (mask.lane<1>() != 0.0f)1096{1097store_lane(base + 0, data.lane<0>());1098store_lane(base + 4, data.lane<1>());1099}1100else if (mask.lane<0>() != 0.0f)1101{1102store_lane(base + 0, data.lane<0>());1103}1104}11051106#define ASTCENC_USE_NATIVE_POPCOUNT 111071108/**1109* @brief Population bit count.1110*1111* @param v The value to population count.1112*1113* @return The number of 1 bits.1114*/1115ASTCENC_SIMD_INLINE int popcount(uint64_t v)1116{1117return static_cast<int>(vaddlv_u8(vcnt_u8(vcreate_u8(v))));1118}11191120#endif // #ifndef ASTC_VECMATHLIB_NEON_4_H_INCLUDED112111221123