Path: blob/master/thirdparty/basis_universal/encoder/cppspmd_math.h
9902 views
// Do not include this header directly.1//2// Copyright 2020-2024 Binomial LLC3//4// Licensed under the Apache License, Version 2.0 (the "License");5// you may not use this file except in compliance with the License.6// You may obtain a copy of the License at7//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,12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13// See the License for the specific language governing permissions and14// limitations under the License.1516// The general goal of these vectorized estimated math functions is scalability/performance.17// There are explictly no checks NaN's/Inf's on the input arguments. There are no assertions either.18// These are fast estimate functions - if you need more than that, use stdlib. Please do a proper19// engineering analysis before relying on them.20// I have chosen functions written by others, ported them to CppSPMD, then measured their abs/rel errors.21// I compared each to the ones in DirectXMath and stdlib's for accuracy/performance.2223CPPSPMD_FORCE_INLINE vfloat fmod_inv(const vfloat& a, const vfloat& b, const vfloat& b_inv)24{25vfloat c = frac(abs(a * b_inv)) * abs(b);26return spmd_ternaryf(a < 0, -c, c);27}2829CPPSPMD_FORCE_INLINE vfloat fmod_inv_p(const vfloat& a, const vfloat& b, const vfloat& b_inv)30{31return frac(a * b_inv) * b;32}3334// Avoids dividing by zero or very small values.35CPPSPMD_FORCE_INLINE vfloat safe_div(vfloat a, vfloat b, float fDivThresh = 1e-7f)36{37return a / spmd_ternaryf( abs(b) > fDivThresh, b, spmd_ternaryf(b < 0.0f, -fDivThresh, fDivThresh) );38}3940/*41clang 9.0.0 for win /fp:precise release42f range: 0.0000000000001250 10000000000.0000000000000000, vals: 10737418244344log2_est():45max abs err: 0.000002307680873146max rel err: 0.000000075667888147avg abs err: 0.000000753545272448avg rel err: 0.00000002351178434950XMVectorLog2():51max abs err: 0.000002332970993352max rel err: 0.000000082696104653avg abs err: 0.000000756488968454avg rel err: 0.00000002360518995556std::log2f():57max abs err: 0.000002026597940158max rel err: 0.000000062664765459avg abs err: 0.000000749444522760avg rel err: 0.000000023380098561*/6263// See https://tech.ebayinc.com/engineering/fast-approximate-logarithms-part-iii-the-formulas/64inline vfloat spmd_kernel::log2_est(vfloat v)65{66vfloat signif, fexp;6768// Just clamp to a very small value, instead of checking for invalid inputs.69vfloat x = max(v, 2.2e-38f);7071/*72* Assume IEEE representation, which is sgn(1):exp(8):frac(23)73* representing (1+frac)*2^(exp-127). Call 1+frac the significand74*/7576// get exponent77vint ux1_i = cast_vfloat_to_vint(x);7879vint exp = VUINT_SHIFT_RIGHT(ux1_i & 0x7F800000, 23);8081// actual exponent is exp-127, will subtract 127 later8283vint ux2_i;84vfloat ux2_f;8586vint greater = ux1_i & 0x00400000; // true if signif > 1.587SPMD_SIF(greater != 0)88{89// signif >= 1.5 so need to divide by 2. Accomplish this by stuffing exp = 126 which corresponds to an exponent of -190store_all(ux2_i, (ux1_i & 0x007FFFFF) | 0x3f000000);9192store_all(ux2_f, cast_vint_to_vfloat(ux2_i));9394// 126 instead of 127 compensates for division by 295store_all(fexp, vfloat(exp - 126));96}97SPMD_SELSE(greater != 0)98{99// get signif by stuffing exp = 127 which corresponds to an exponent of 0100store(ux2_i, (ux1_i & 0x007FFFFF) | 0x3f800000);101102store(ux2_f, cast_vint_to_vfloat(ux2_i));103104store(fexp, vfloat(exp - 127));105}106SPMD_SENDIF107108store_all(signif, ux2_f);109store_all(signif, signif - 1.0f);110111const float a = 0.1501692f, b = 3.4226132f, c = 5.0225057f, d = 4.1130283f, e = 3.4813372f;112113vfloat xm1 = signif;114vfloat xm1sqr = xm1 * xm1;115116return fexp + ((a * (xm1sqr * xm1) + b * xm1sqr + c * xm1) / (xm1sqr + d * xm1 + e));117118// fma lowers accuracy for SSE4.1 - no idea why (compiler reordering?)119//return fexp + ((vfma(a, (xm1sqr * xm1), vfma(b, xm1sqr, c * xm1))) / (xm1sqr + vfma(d, xm1, e)));120}121122// Uses log2_est(), so this function must be <= the precision of that.123inline vfloat spmd_kernel::log_est(vfloat v)124{125return log2_est(v) * 0.693147181f;126}127128CPPSPMD_FORCE_INLINE void spmd_kernel::reduce_expb(vfloat& arg, vfloat& two_int_a, vint& adjustment)129{130// Assume we're using equation (2)131store_all(adjustment, 0);132133// integer part of the input argument134vint int_arg = (vint)arg;135136// if frac(arg) is in [0.5, 1.0]...137SPMD_SIF((arg - int_arg) > 0.5f)138{139store(adjustment, 1);140141// then change it to [0.0, 0.5]142store(arg, arg - 0.5f);143}144SPMD_SENDIF145146// arg == just the fractional part147store_all(arg, arg - (vfloat)int_arg);148149// Now compute 2** (int) arg.150store_all(int_arg, min(int_arg + 127, 254));151152store_all(two_int_a, cast_vint_to_vfloat(VINT_SHIFT_LEFT(int_arg, 23)));153}154155/*156clang 9.0.0 for win /fp:precise release157f range : -50.0000000000000000 49.9999940395355225, vals : 16777216158159exp2_est():160Total passed near - zero check : 16777216161Total sign diffs : 0162max abs err: 1668910609.7500000000000000163max rel err: 0.0000015642030031164avg abs err: 10793794.4007573910057545165avg rel err: 0.0000003890893282166167XMVectorExp2():168Total passed near-zero check: 16777216169Total sign diffs: 0170max abs err: 1665552836.8750000000000000171max rel err: 0.0000114674862370172avg abs err: 10771868.2627860084176064173avg rel err: 0.0000011218880770174175std::exp2f():176Total passed near-zero check: 16777216177Total sign diffs: 0178max abs err: 1591636585.6250000000000000179max rel err: 0.0000014849731018180avg abs err: 10775800.3204844966530800181avg rel err: 0.0000003851496422182*/183184// http://www.ganssle.com/item/approximations-c-code-exponentiation-log.htm185inline vfloat spmd_kernel::exp2_est(vfloat arg)186{187SPMD_BEGIN_CALL188189const vfloat P00 = +7.2152891521493f;190const vfloat P01 = +0.0576900723731f;191const vfloat Q00 = +20.8189237930062f;192const vfloat Q01 = +1.0f;193const vfloat sqrt2 = 1.4142135623730950488f; // sqrt(2) for scaling194195vfloat result = 0.0f;196197// Return 0 if arg is too large.198// We're not introducing inf/nan's into calculations, or risk doing so by returning huge default values.199SPMD_IF(abs(arg) > 126.0f)200{201spmd_return();202}203SPMD_END_IF204205// 2**(int(a))206vfloat two_int_a;207208// set to 1 by reduce_expb209vint adjustment;210211// 0 if arg is +; 1 if negative212vint negative = 0;213214// If the input is negative, invert it. At the end we'll take the reciprocal, since n**(-1) = 1/(n**x).215SPMD_SIF(arg < 0.0f)216{217store(arg, -arg);218store(negative, 1);219}220SPMD_SENDIF221222store_all(arg, min(arg, 126.0f));223224// reduce to [0.0, 0.5]225reduce_expb(arg, two_int_a, adjustment);226227// The format of the polynomial is:228// answer=(Q(x**2) + x*P(x**2))/(Q(x**2) - x*P(x**2))229//230// The following computes the polynomial in several steps:231232// Q(x**2)233vfloat Q = vfma(Q01, (arg * arg), Q00);234235// x*P(x**2)236vfloat x_P = arg * (vfma(P01, arg * arg, P00));237238vfloat answer = (Q + x_P) / (Q - x_P);239240// Now correct for the scaling factor of 2**(int(a))241store_all(answer, answer * two_int_a);242243// If the result had a fractional part > 0.5, correct for that244store_all(answer, spmd_ternaryf(adjustment != 0, answer * sqrt2, answer));245246// Correct for a negative input247SPMD_SIF(negative != 0)248{249store(answer, 1.0f / answer);250}251SPMD_SENDIF252253store(result, answer);254255return result;256}257258inline vfloat spmd_kernel::exp_est(vfloat arg)259{260// e^x = exp2(x / log_base_e(2))261// constant is 1.0/(log(2)/log(e)) or 1/log(2)262return exp2_est(arg * 1.44269504f);263}264265inline vfloat spmd_kernel::pow_est(vfloat arg1, vfloat arg2)266{267return exp_est(log_est(arg1) * arg2);268}269270/*271clang 9.0.0 for win /fp:precise release272Total near-zero: 144, output above near-zero tresh: 30273Total near-zero avg: 0.0000067941016621 max: 0.0000134706497192274Total near-zero sign diffs: 5275Total passed near-zero check: 16777072276Total sign diffs: 5277max abs err: 0.0000031375306036278max rel err: 0.1140846017075028279avg abs err: 0.0000003026226621280avg rel err: 0.0000033564977623281*/282283// Math from this web page: http://developer.download.nvidia.com/cg/sin.html284// This is ~2x slower than sin_est() or cos_est(), and less accurate, but I'm keeping it here for comparison purposes to help validate/sanity check sin_est() and cos_est().285inline vfloat spmd_kernel::sincos_est_a(vfloat a, bool sin_flag)286{287const float c0_x = 0.0f, c0_y = 0.5f, c0_z = 1.0f;288const float c1_x = 0.25f, c1_y = -9.0f, c1_z = 0.75f, c1_w = 0.159154943091f;289const float c2_x = 24.9808039603f, c2_y = -24.9808039603f, c2_z = -60.1458091736f, c2_w = 60.1458091736f;290const float c3_x = 85.4537887573f, c3_y = -85.4537887573f, c3_z = -64.9393539429f, c3_w = 64.9393539429f;291const float c4_x = 19.7392082214f, c4_y = -19.7392082214f, c4_z = -1.0f, c4_w = 1.0f;292293vfloat r0_x, r0_y, r0_z, r1_x, r1_y, r1_z, r2_x, r2_y, r2_z;294295store_all(r1_x, sin_flag ? vfms(c1_w, a, c1_x) : c1_w * a);296297store_all(r1_y, frac(r1_x));298299store_all(r2_x, (vfloat)(r1_y < c1_x));300301store_all(r2_y, (vfloat)(r1_y >= c1_y));302store_all(r2_z, (vfloat)(r1_y >= c1_z));303304store_all(r2_y, vfma(r2_x, c4_z, vfma(r2_y, c4_w, r2_z * c4_z)));305306store_all(r0_x, c0_x - r1_y);307store_all(r0_y, c0_y - r1_y);308store_all(r0_z, c0_z - r1_y);309310store_all(r0_x, r0_x * r0_x);311store_all(r0_y, r0_y * r0_y);312store_all(r0_z, r0_z * r0_z);313314store_all(r1_x, vfma(c2_x, r0_x, c2_z));315store_all(r1_y, vfma(c2_y, r0_y, c2_w));316store_all(r1_z, vfma(c2_x, r0_z, c2_z));317318store_all(r1_x, vfma(r1_x, r0_x, c3_x));319store_all(r1_y, vfma(r1_y, r0_y, c3_y));320store_all(r1_z, vfma(r1_z, r0_z, c3_x));321322store_all(r1_x, vfma(r1_x, r0_x, c3_z));323store_all(r1_y, vfma(r1_y, r0_y, c3_w));324store_all(r1_z, vfma(r1_z, r0_z, c3_z));325326store_all(r1_x, vfma(r1_x, r0_x, c4_x));327store_all(r1_y, vfma(r1_y, r0_y, c4_y));328store_all(r1_z, vfma(r1_z, r0_z, c4_x));329330store_all(r1_x, vfma(r1_x, r0_x, c4_z));331store_all(r1_y, vfma(r1_y, r0_y, c4_w));332store_all(r1_z, vfma(r1_z, r0_z, c4_z));333334store_all(r0_x, vfnma(r1_x, r2_x, vfnma(r1_y, r2_y, r1_z * -r2_z)));335336return r0_x;337}338339// positive values only340CPPSPMD_FORCE_INLINE vfloat spmd_kernel::recip_est1(const vfloat& q)341{342//const int mag = 0x7EF312AC; // 2 NR iters, 3 is 0x7EEEEBB3343const int mag = 0x7EF311C3;344const float fMinThresh = .0000125f;345346vfloat l = spmd_ternaryf(q >= fMinThresh, q, cast_vint_to_vfloat(vint(mag)));347348vint x_l = vint(mag) - cast_vfloat_to_vint(l);349350vfloat rcp_l = cast_vint_to_vfloat(x_l);351352return rcp_l * vfnma(rcp_l, q, 2.0f);353}354355CPPSPMD_FORCE_INLINE vfloat spmd_kernel::recip_est1_pn(const vfloat& t)356{357//const int mag = 0x7EF312AC; // 2 NR iters, 3 is 0x7EEEEBB3358const int mag = 0x7EF311C3;359const float fMinThresh = .0000125f;360361vfloat s = sign(t);362vfloat q = abs(t);363364vfloat l = spmd_ternaryf(q >= fMinThresh, q, cast_vint_to_vfloat(vint(mag)));365366vint x_l = vint(mag) - cast_vfloat_to_vint(l);367368vfloat rcp_l = cast_vint_to_vfloat(x_l);369370return rcp_l * vfnma(rcp_l, q, 2.0f) * s;371}372373// https://basesandframes.files.wordpress.com/2020/04/even_faster_math_functions_green_2020.pdf374// https://github.com/hcs0/Hackers-Delight/blob/master/rsqrt.c.txt375CPPSPMD_FORCE_INLINE vfloat spmd_kernel::rsqrt_est1(vfloat x0)376{377vfloat xhalf = 0.5f * x0;378vfloat x = cast_vint_to_vfloat(vint(0x5F375A82) - (VINT_SHIFT_RIGHT(cast_vfloat_to_vint(x0), 1)));379return x * vfnma(xhalf * x, x, 1.5008909f);380}381382CPPSPMD_FORCE_INLINE vfloat spmd_kernel::rsqrt_est2(vfloat x0)383{384vfloat xhalf = 0.5f * x0;385vfloat x = cast_vint_to_vfloat(vint(0x5F37599E) - (VINT_SHIFT_RIGHT(cast_vfloat_to_vint(x0), 1)));386vfloat x1 = x * vfnma(xhalf * x, x, 1.5);387vfloat x2 = x1 * vfnma(xhalf * x1, x1, 1.5);388return x2;389}390391// Math from: http://developer.download.nvidia.com/cg/atan2.html392// TODO: Needs more validation, parameter checking.393CPPSPMD_FORCE_INLINE vfloat spmd_kernel::atan2_est(vfloat y, vfloat x)394{395vfloat t1 = abs(y);396vfloat t3 = abs(x);397398vfloat t0 = max(t3, t1);399store_all(t1, min(t3, t1));400401store_all(t3, t1 / t0);402403vfloat t4 = t3 * t3;404store_all(t0, vfma(-0.013480470f, t4, 0.057477314f));405store_all(t0, vfms(t0, t4, 0.121239071f));406store_all(t0, vfma(t0, t4, 0.195635925f));407store_all(t0, vfms(t0, t4, 0.332994597f));408store_all(t0, vfma(t0, t4, 0.999995630f));409store_all(t3, t0 * t3);410411store_all(t3, spmd_ternaryf(abs(y) > abs(x), vfloat(1.570796327f) - t3, t3));412413store_all(t3, spmd_ternaryf(x < 0.0f, vfloat(3.141592654f) - t3, t3));414store_all(t3, spmd_ternaryf(y < 0.0f, -t3, t3));415416return t3;417}418419/*420clang 9.0.0 for win /fp:precise release421Tested range: -25.1327412287183449 25.1327382326621169, vals : 16777216422Skipped angles near 90/270 within +- .001 radians.423Near-zero threshold: .0000125f424Near-zero output above check threshold: 1e-6f425426Total near-zero: 144, output above near-zero tresh: 20427Total near-zero avg: 0.0000067510751968 max: 0.0000133514404297428Total near-zero sign diffs: 5429Total passed near-zero check: 16766400430Total sign diffs: 5431max abs err: 1.4982600811139264432max rel err: 0.1459155900188041433avg rel err: 0.0000054659502568434435XMVectorTan() precise:436Total near-zero: 144, output above near-zero tresh: 18437Total near-zero avg: 0.0000067641216186 max: 0.0000133524126795438Total near-zero sign diffs: 0439Total passed near-zero check: 16766400440Total sign diffs: 0441max abs err: 1.9883573246424930442max rel err: 0.1459724171926864443avg rel err: 0.0000054965766843444445std::tanf():446Total near-zero: 144, output above near-zero tresh: 0447Total near-zero avg: 0.0000067116930779 max: 0.0000127713074107448Total near-zero sign diffs: 11449Total passed near-zero check: 16766400450Total sign diffs: 11451max abs err: 0.8989131818294709452max rel err: 0.0573181403173166453avg rel err: 0.0000030791301203454455Originally from:456http://www.ganssle.com/approx.htm457*/458459CPPSPMD_FORCE_INLINE vfloat spmd_kernel::tan82(vfloat x)460{461// Original double version was 8.2 digits462//double c1 = 211.849369664121f, c2 = -12.5288887278448f, c3 = 269.7350131214121f, c4 = -71.4145309347748f;463// Tuned float constants for lower avg rel error (without using FMA3):464const float c1 = 211.849350f, c2 = -12.5288887f, c3 = 269.734985f, c4 = -71.4145203f;465vfloat x2 = x * x;466return (x * (vfma(c2, x2, c1)) / (vfma(x2, (c4 + x2), c3)));467}468469// Don't call this for angles close to 90/270!.470inline vfloat spmd_kernel::tan_est(vfloat x)471{472const float fPi = 3.141592653589793f, fOneOverPi = 0.3183098861837907f;473CPPSPMD_DECL(const uint8_t, s_table0[16]) = { 128 + 0, 128 + 2, 128 + -2, 128 + 4, 128 + 0, 128 + 2, 128 + -2, 128 + 4, 128 + 0, 128 + 2, 128 + -2, 128 + 4, 128 + 0, 128 + 2, 128 + -2, 128 + 4 };474475vint table = init_lookup4(s_table0); // a load476vint sgn = cast_vfloat_to_vint(x) & 0x80000000;477478store_all(x, abs(x));479vfloat orig_x = x;480481vfloat q = x * fOneOverPi;482store_all(x, q - floor(q));483484vfloat x4 = x * 4.0f;485vint octant = (vint)(x4);486487vfloat x0 = spmd_ternaryf((octant & 1) != 0, -x4, x4);488489vint k = table_lookup4_8(octant, table) & 0xFF; // a shuffle490491vfloat bias = (vfloat)k + -128.0f;492vfloat y = x0 + bias;493494vfloat z = tan82(y);495496vfloat r;497498vbool octant_one_or_two = (octant == 1) || (octant == 2);499500// SPMD optimization - skip costly divide if we can501if (spmd_any(octant_one_or_two))502{503const float fDivThresh = .4371e-7f;504vfloat one_over_z = 1.0f / spmd_ternaryf(abs(z) > fDivThresh, z, spmd_ternaryf(z < 0.0f, -fDivThresh, fDivThresh));505506vfloat b = spmd_ternaryf(octant_one_or_two, one_over_z, z);507store_all(r, spmd_ternaryf((octant & 2) != 0, -b, b));508}509else510{511store_all(r, spmd_ternaryf(octant == 0, z, -z));512}513514// Small angle approximation, to decrease the max rel error near Pi.515SPMD_SIF(x >= (1.0f - .0003125f*4.0f))516{517store(r, vfnma(floor(q) + 1.0f, fPi, orig_x));518}519SPMD_SENDIF520521return cast_vint_to_vfloat(cast_vfloat_to_vint(r) ^ sgn);522}523524inline void spmd_kernel::seed_rand(rand_context& x, vint seed)525{526store(x.a, 0xf1ea5eed);527store(x.b, seed ^ 0xd8487b1f);528store(x.c, seed ^ 0xdbadef9a);529store(x.d, seed);530for (int i = 0; i < 20; ++i)531(void)get_randu(x);532}533534// https://burtleburtle.net/bob/rand/smallprng.html535// Returns 32-bit unsigned random numbers.536inline vint spmd_kernel::get_randu(rand_context& x)537{538vint e = x.a - VINT_ROT(x.b, 27);539store(x.a, x.b ^ VINT_ROT(x.c, 17));540store(x.b, x.c + x.d);541store(x.c, x.d + e);542store(x.d, e + x.a);543return x.d;544}545546// Returns random numbers between [low, high), or low if low >= high547inline vint spmd_kernel::get_randi(rand_context& x, vint low, vint high)548{549vint rnd = get_randu(x);550551vint range = high - low;552553vint rnd_range = mulhiu(rnd, range);554555return spmd_ternaryi(low < high, low + rnd_range, low);556}557558// Returns random numbers between [low, high), or low if low >= high559inline vfloat spmd_kernel::get_randf(rand_context& x, vfloat low, vfloat high)560{561vint rndi = get_randu(x) & 0x7fffff;562563vfloat rnd = (vfloat)(rndi) * (1.0f / 8388608.0f);564565return spmd_ternaryf(low < high, vfma(high - low, rnd, low), low);566}567568CPPSPMD_FORCE_INLINE void spmd_kernel::init_reverse_bits(vint& tab1, vint& tab2)569{570const uint8_t tab1_bytes[16] = { 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15 };571const uint8_t tab2_bytes[16] = { 0, 8 << 4, 4 << 4, 12 << 4, 2 << 4, 10 << 4, 6 << 4, 14 << 4, 1 << 4, 9 << 4, 5 << 4, 13 << 4, 3 << 4, 11 << 4, 7 << 4, 15 << 4 };572store_all(tab1, init_lookup4(tab1_bytes));573store_all(tab2, init_lookup4(tab2_bytes));574}575576CPPSPMD_FORCE_INLINE vint spmd_kernel::reverse_bits(vint k, vint tab1, vint tab2)577{578vint r0 = table_lookup4_8(k & 0x7F7F7F7F, tab2);579vint r1 = table_lookup4_8(VUINT_SHIFT_RIGHT(k, 4) & 0x7F7F7F7F, tab1);580vint r3 = r0 | r1;581return byteswap(r3);582}583584CPPSPMD_FORCE_INLINE vint spmd_kernel::count_leading_zeros(vint x)585{586CPPSPMD_DECL(const uint8_t, s_tab[16]) = { 0, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 };587588vint tab = init_lookup4(s_tab);589590//x <= 0x0000ffff591vbool c0 = (x & 0xFFFF0000) == 0;592vint n0 = spmd_ternaryi(c0, 16, 0);593vint x0 = spmd_ternaryi(c0, VINT_SHIFT_LEFT(x, 16), x);594595//x <= 0x00ffffff596vbool c1 = (x0 & 0xFF000000) == 0;597vint n1 = spmd_ternaryi(c1, n0 + 8, n0);598vint x1 = spmd_ternaryi(c1, VINT_SHIFT_LEFT(x0, 8), x0);599600//x <= 0x0fffffff601vbool c2 = (x1 & 0xF0000000) == 0;602vint n2 = spmd_ternaryi(c2, n1 + 4, n1);603vint x2 = spmd_ternaryi(c2, VINT_SHIFT_LEFT(x1, 4), x1);604605return table_lookup4_8(VUINT_SHIFT_RIGHT(x2, 28), tab) + n2;606}607608CPPSPMD_FORCE_INLINE vint spmd_kernel::count_leading_zeros_alt(vint x)609{610//x <= 0x0000ffff611vbool c0 = (x & 0xFFFF0000) == 0;612vint n0 = spmd_ternaryi(c0, 16, 0);613vint x0 = spmd_ternaryi(c0, VINT_SHIFT_LEFT(x, 16), x);614615//x <= 0x00ffffff616vbool c1 = (x0 & 0xFF000000) == 0;617vint n1 = spmd_ternaryi(c1, n0 + 8, n0);618vint x1 = spmd_ternaryi(c1, VINT_SHIFT_LEFT(x0, 8), x0);619620//x <= 0x0fffffff621vbool c2 = (x1 & 0xF0000000) == 0;622vint n2 = spmd_ternaryi(c2, n1 + 4, n1);623vint x2 = spmd_ternaryi(c2, VINT_SHIFT_LEFT(x1, 4), x1);624625// x <= 0x3fffffff626vbool c3 = (x2 & 0xC0000000) == 0;627vint n3 = spmd_ternaryi(c3, n2 + 2, n2);628vint x3 = spmd_ternaryi(c3, VINT_SHIFT_LEFT(x2, 2), x2);629630// x <= 0x7fffffff631vbool c4 = (x3 & 0x80000000) == 0;632return spmd_ternaryi(c4, n3 + 1, n3);633}634635CPPSPMD_FORCE_INLINE vint spmd_kernel::count_trailing_zeros(vint x)636{637// cast the least significant bit in v to a float638vfloat f = (vfloat)(x & -x);639640// extract exponent and adjust641return VUINT_SHIFT_RIGHT(cast_vfloat_to_vint(f), 23) - 0x7F;642}643644CPPSPMD_FORCE_INLINE vint spmd_kernel::count_set_bits(vint x)645{646vint v = x - (VUINT_SHIFT_RIGHT(x, 1) & 0x55555555);647vint v1 = (v & 0x33333333) + (VUINT_SHIFT_RIGHT(v, 2) & 0x33333333);648return VUINT_SHIFT_RIGHT(((v1 + (VUINT_SHIFT_RIGHT(v1, 4) & 0xF0F0F0F)) * 0x1010101), 24);649}650651CPPSPMD_FORCE_INLINE vint cmple_epu16(const vint &a, const vint &b)652{653return cmpeq_epi16(subs_epu16(a, b), vint(0));654}655656CPPSPMD_FORCE_INLINE vint cmpge_epu16(const vint &a, const vint &b)657{658return cmple_epu16(b, a);659}660661CPPSPMD_FORCE_INLINE vint cmpgt_epu16(const vint &a, const vint &b)662{663return andnot(cmpeq_epi16(a, b), cmple_epu16(b, a));664}665666CPPSPMD_FORCE_INLINE vint cmplt_epu16(const vint &a, const vint &b)667{668return cmpgt_epu16(b, a);669}670671CPPSPMD_FORCE_INLINE vint cmpge_epi16(const vint &a, const vint &b)672{673return cmpeq_epi16(a, b) | cmpgt_epi16(a, b);674}675676CPPSPMD_FORCE_INLINE vint cmple_epi16(const vint &a, const vint &b)677{678return cmpge_epi16(b, a);679}680681void spmd_kernel::print_vint(vint v)682{683for (uint32_t i = 0; i < PROGRAM_COUNT; i++)684printf("%i ", extract(v, i));685printf("\n");686}687688void spmd_kernel::print_vbool(vbool v)689{690for (uint32_t i = 0; i < PROGRAM_COUNT; i++)691printf("%i ", extract(v, i) ? 1 : 0);692printf("\n");693}694695void spmd_kernel::print_vint_hex(vint v)696{697for (uint32_t i = 0; i < PROGRAM_COUNT; i++)698printf("0x%X ", extract(v, i));699printf("\n");700}701702void spmd_kernel::print_active_lanes(const char *pPrefix)703{704CPPSPMD_DECL(int, flags[PROGRAM_COUNT]);705memset(flags, 0, sizeof(flags));706storeu_linear(flags, vint(1));707708if (pPrefix)709printf("%s", pPrefix);710711for (uint32_t i = 0; i < PROGRAM_COUNT; i++)712{713if (flags[i])714printf("%u ", i);715}716printf("\n");717}718719void spmd_kernel::print_vfloat(vfloat v)720{721for (uint32_t i = 0; i < PROGRAM_COUNT; i++)722printf("%f ", extract(v, i));723printf("\n");724}725726727