Path: blob/21.2-virgl/src/gallium/drivers/nouveau/codegen/lib/gk110.asm
4574 views
.section #gk110_builtin_code1// DIV U322//3// UNR recurrence (q = a / b):4// look for z such that 2^32 - b <= b * z < 2^325// then q - 1 <= (a * z) / 2^32 <= q6//7// INPUT: $r0: dividend, $r1: divisor8// OUTPUT: $r0: result, $r1: modulus9// CLOBBER: $r2 - $r3, $p0 - $p110// SIZE: 22 / 14 * 8 bytes11//12gk110_div_u32:13sched 0x28 0x04 0x28 0x04 0x28 0x28 0x2814bfind u32 $r2 $r115xor b32 $r2 $r2 0x1f16mov b32 $r3 0x117shl b32 $r2 $r3 clamp $r218cvt u32 $r1 neg u32 $r119mul $r3 u32 $r1 u32 $r220add $r2 (mul high u32 $r2 u32 $r3) $r221sched 0x28 0x28 0x28 0x28 0x28 0x28 0x2822mul $r3 u32 $r1 u32 $r223add $r2 (mul high u32 $r2 u32 $r3) $r224mul $r3 u32 $r1 u32 $r225add $r2 (mul high u32 $r2 u32 $r3) $r226mul $r3 u32 $r1 u32 $r227add $r2 (mul high u32 $r2 u32 $r3) $r228mul $r3 u32 $r1 u32 $r229sched 0x04 0x28 0x04 0x28 0x28 0x2c 0x0430add $r2 (mul high u32 $r2 u32 $r3) $r231mov b32 $r3 $r032mul high $r0 u32 $r0 u32 $r233cvt u32 $r2 neg u32 $r134add $r1 (mul u32 $r1 u32 $r0) $r335set $p0 0x1 ge u32 $r1 $r236$p0 sub b32 $r1 $r1 $r237sched 0x28 0x2c 0x04 0x20 0x2e 0x28 0x2038$p0 add b32 $r0 $r0 0x139$p0 set $p0 0x1 ge u32 $r1 $r240$p0 sub b32 $r1 $r1 $r241$p0 add b32 $r0 $r0 0x142ret4344// DIV S32, like DIV U32 after taking ABS(inputs)45//46// INPUT: $r0: dividend, $r1: divisor47// OUTPUT: $r0: result, $r1: modulus48// CLOBBER: $r2 - $r3, $p0 - $p349//50gk110_div_s32:51set $p2 0x1 lt s32 $r0 0x052set $p3 0x1 lt s32 $r1 0x0 xor $p253sched 0x20 0x28 0x28 0x04 0x28 0x04 0x2854cvt s32 $r0 abs s32 $r055cvt s32 $r1 abs s32 $r156bfind u32 $r2 $r157xor b32 $r2 $r2 0x1f58mov b32 $r3 0x159shl b32 $r2 $r3 clamp $r260cvt u32 $r1 neg u32 $r161sched 0x28 0x28 0x28 0x28 0x28 0x28 0x2862mul $r3 u32 $r1 u32 $r263add $r2 (mul high u32 $r2 u32 $r3) $r264mul $r3 u32 $r1 u32 $r265add $r2 (mul high u32 $r2 u32 $r3) $r266mul $r3 u32 $r1 u32 $r267add $r2 (mul high u32 $r2 u32 $r3) $r268mul $r3 u32 $r1 u32 $r269sched 0x28 0x28 0x04 0x28 0x04 0x28 0x2870add $r2 (mul high u32 $r2 u32 $r3) $r271mul $r3 u32 $r1 u32 $r272add $r2 (mul high u32 $r2 u32 $r3) $r273mov b32 $r3 $r074mul high $r0 u32 $r0 u32 $r275cvt u32 $r2 neg u32 $r176add $r1 (mul u32 $r1 u32 $r0) $r377sched 0x2c 0x04 0x28 0x2c 0x04 0x28 0x2078set $p0 0x1 ge u32 $r1 $r279$p0 sub b32 $r1 $r1 $r280$p0 add b32 $r0 $r0 0x181$p0 set $p0 0x1 ge u32 $r1 $r282$p0 sub b32 $r1 $r1 $r283$p0 add b32 $r0 $r0 0x184$p3 cvt s32 $r0 neg s32 $r085sched 0x04 0x2e 0x28 0x04 0x28 0x28 0x2886$p2 cvt s32 $r1 neg s32 $r187ret8889// RCP F6490//91// INPUT: $r0d92// OUTPUT: $r0d93// CLOBBER: $r2 - $r9, $p094//95// The core of RCP and RSQ implementation is Newton-Raphson step, which is96// used to find successively better approximation from an imprecise initial97// value (single precision rcp in RCP and rsqrt64h in RSQ).98//99gk110_rcp_f64:100// Step 1: classify input according to exponent and value, and calculate101// result for 0/inf/nan. $r2 holds the exponent value, which starts at102// bit 52 (bit 20 of the upper half) and is 11 bits in length103ext u32 $r2 $r1 0xb14104add b32 $r3 $r2 0xffffffff105joinat #rcp_rejoin106// We want to check whether the exponent is 0 or 0x7ff (i.e. NaN, inf,107// denorm, or 0). Do this by subtracting 1 from the exponent, which will108// mean that it's > 0x7fd in those cases when doing unsigned comparison109set b32 $p0 0x1 gt u32 $r3 0x7fd110// $r3: 0 for norms, 0x36 for denorms, -1 for others111mov b32 $r3 0x0112sched 0x2f 0x04 0x2d 0x2b 0x2f 0x28 0x28113join (not $p0) nop114// Process all special values: NaN, inf, denorm, 0115mov b32 $r3 0xffffffff116// A number is NaN if its abs value is greater than or unordered with inf117set $p0 0x1 gtu f64 abs $r0d 0x7ff0000000000000118(not $p0) bra #rcp_inf_or_denorm_or_zero119// NaN -> NaN, the next line sets the "quiet" bit of the result. This120// behavior is both seen on the CPU and the blob121join or b32 $r1 $r1 0x80000122rcp_inf_or_denorm_or_zero:123and b32 $r4 $r1 0x7ff00000124// Other values with nonzero in exponent field should be inf125set b32 $p0 0x1 eq s32 $r4 0x0126sched 0x2b 0x04 0x2f 0x2d 0x2b 0x2f 0x20127$p0 bra #rcp_denorm_or_zero128// +/-Inf -> +/-0129xor b32 $r1 $r1 0x7ff00000130join mov b32 $r0 0x0131rcp_denorm_or_zero:132set $p0 0x1 gtu f64 abs $r0d 0x0133$p0 bra #rcp_denorm134// +/-0 -> +/-Inf135join or b32 $r1 $r1 0x7ff00000136rcp_denorm:137// non-0 denorms: multiply with 2^54 (the 0x36 in $r3), join with norms138mul rn f64 $r0d $r0d 0x4350000000000000139sched 0x2f 0x28 0x2b 0x28 0x28 0x04 0x28140join mov b32 $r3 0x36141rcp_rejoin:142// All numbers with -1 in $r3 have their result ready in $r0d, return them143// others need further calculation144set b32 $p0 0x1 lt s32 $r3 0x0145$p0 bra #rcp_end146// Step 2: Before the real calculation goes on, renormalize the values to147// range [1, 2) by setting exponent field to 0x3ff (the exponent of 1)148// result in $r6d. The exponent will be recovered later.149ext u32 $r2 $r1 0xb14150and b32 $r7 $r1 0x800fffff151add b32 $r7 $r7 0x3ff00000152mov b32 $r6 $r0153sched 0x2b 0x04 0x28 0x28 0x2a 0x2b 0x2e154// Step 3: Convert new value to float (no overflow will occur due to step155// 2), calculate rcp and do newton-raphson step once156cvt rz f32 $r5 f64 $r6d157rcp f32 $r4 $r5158mov b32 $r0 0xbf800000159fma rn f32 $r5 $r4 $r5 $r0160fma rn f32 $r0 neg $r4 $r5 $r4161// Step 4: convert result $r0 back to double, do newton-raphson steps162cvt f64 $r0d f32 $r0163cvt f64 $r6d f64 neg $r6d164sched 0x2e 0x29 0x29 0x29 0x29 0x29 0x29165cvt f64 $r8d f32 0x3f800000166// 4 Newton-Raphson Steps, tmp in $r4d, result in $r0d167// The formula used here (and above) is:168// RCP_{n + 1} = 2 * RCP_{n} - x * RCP_{n} * RCP_{n}169// The following code uses 2 FMAs for each step, and it will basically170// looks like:171// tmp = -src * RCP_{n} + 1172// RCP_{n + 1} = RCP_{n} * tmp + RCP_{n}173fma rn f64 $r4d $r6d $r0d $r8d174fma rn f64 $r0d $r0d $r4d $r0d175fma rn f64 $r4d $r6d $r0d $r8d176fma rn f64 $r0d $r0d $r4d $r0d177fma rn f64 $r4d $r6d $r0d $r8d178fma rn f64 $r0d $r0d $r4d $r0d179sched 0x29 0x20 0x28 0x28 0x28 0x28 0x28180fma rn f64 $r4d $r6d $r0d $r8d181fma rn f64 $r0d $r0d $r4d $r0d182// Step 5: Exponent recovery and final processing183// The exponent is recovered by adding what we added to the exponent.184// Suppose we want to calculate rcp(x), but we have rcp(cx), then185// rcp(x) = c * rcp(cx)186// The delta in exponent comes from two sources:187// 1) The renormalization in step 2. The delta is:188// 0x3ff - $r2189// 2) (For the denorm input) The 2^54 we multiplied at rcp_denorm, stored190// in $r3191// These 2 sources are calculated in the first two lines below, and then192// added to the exponent extracted from the result above.193// Note that after processing, the new exponent may >= 0x7ff (inf)194// or <= 0 (denorm). Those cases will be handled respectively below195subr b32 $r2 $r2 0x3ff196add b32 $r4 $r2 $r3197ext u32 $r3 $r1 0xb14198// New exponent in $r3199add b32 $r3 $r3 $r4200add b32 $r2 $r3 0xffffffff201sched 0x28 0x2b 0x28 0x2b 0x28 0x28 0x2b202// (exponent-1) < 0x7fe (unsigned) means the result is in norm range203// (same logic as in step 1)204set b32 $p0 0x1 lt u32 $r2 0x7fe205(not $p0) bra #rcp_result_inf_or_denorm206// Norms: convert exponents back and return207shl b32 $r4 $r4 clamp 0x14208add b32 $r1 $r4 $r1209bra #rcp_end210rcp_result_inf_or_denorm:211// New exponent >= 0x7ff means that result is inf212set b32 $p0 0x1 ge s32 $r3 0x7ff213(not $p0) bra #rcp_result_denorm214sched 0x20 0x25 0x28 0x2b 0x23 0x25 0x2f215// Infinity216and b32 $r1 $r1 0x80000000217mov b32 $r0 0x0218add b32 $r1 $r1 0x7ff00000219bra #rcp_end220rcp_result_denorm:221// Denorm result comes from huge input. The greatest possible fp64, i.e.222// 0x7fefffffffffffff's rcp is 0x0004000000000000, 1/4 of the smallest223// normal value. Other rcp result should be greater than that. If we224// set the exponent field to 1, we can recover the result by multiplying225// it with 1/2 or 1/4. 1/2 is used if the "exponent" $r3 is 0, otherwise226// 1/4 ($r3 should be -1 then). This is quite tricky but greatly simplifies227// the logic here.228set b32 $p0 0x1 ne u32 $r3 0x0229and b32 $r1 $r1 0x800fffff230// 0x3e800000: 1/4231$p0 cvt f64 $r6d f32 0x3e800000232sched 0x2f 0x28 0x2c 0x2e 0x2a 0x20 0x27233// 0x3f000000: 1/2234(not $p0) cvt f64 $r6d f32 0x3f000000235add b32 $r1 $r1 0x00100000236mul rn f64 $r0d $r0d $r6d237rcp_end:238ret239240// RSQ F64241//242// INPUT: $r0d243// OUTPUT: $r0d244// CLOBBER: $r2 - $r9, $p0 - $p1245//246gk110_rsq_f64:247// Before getting initial result rsqrt64h, two special cases should be248// handled first.249// 1. NaN: set the highest bit in mantissa so it'll be surely recognized250// as NaN in rsqrt64h251set $p0 0x1 gtu f64 abs $r0d 0x7ff0000000000000252$p0 or b32 $r1 $r1 0x00080000253and b32 $r2 $r1 0x7fffffff254sched 0x27 0x20 0x28 0x2c 0x25 0x28 0x28255// 2. denorms and small normal values: using their original value will256// lose precision either at rsqrt64h or the first step in newton-raphson257// steps below. Take 2 as a threshold in exponent field, and multiply258// with 2^54 if the exponent is smaller or equal. (will multiply 2^27259// to recover in the end)260ext u32 $r3 $r1 0xb14261set b32 $p1 0x1 le u32 $r3 0x2262or b32 $r2 $r0 $r2263$p1 mul rn f64 $r0d $r0d 0x4350000000000000264rsqrt64h f32 $r5 $r1265// rsqrt64h will give correct result for 0/inf/nan, the following logic266// checks whether the input is one of those (exponent is 0x7ff or all 0267// except for the sign bit)268set b32 $r6 ne u32 $r3 0x7ff269and b32 $r2 $r2 $r6270sched 0x28 0x2b 0x20 0x27 0x28 0x2e 0x28271set b32 $p0 0x1 ne u32 $r2 0x0272$p0 bra #rsq_norm273// For 0/inf/nan, make sure the sign bit agrees with input and return274and b32 $r1 $r1 0x80000000275mov b32 $r0 0x0276or b32 $r1 $r1 $r5277ret278rsq_norm:279// For others, do 4 Newton-Raphson steps with the formula:280// RSQ_{n + 1} = RSQ_{n} * (1.5 - 0.5 * x * RSQ_{n} * RSQ_{n})281// In the code below, each step is written as:282// tmp1 = 0.5 * x * RSQ_{n}283// tmp2 = -RSQ_{n} * tmp1 + 0.5284// RSQ_{n + 1} = RSQ_{n} * tmp2 + RSQ_{n}285mov b32 $r4 0x0286sched 0x2f 0x29 0x29 0x29 0x29 0x29 0x29287// 0x3f000000: 1/2288cvt f64 $r8d f32 0x3f000000289mul rn f64 $r2d $r0d $r8d290mul rn f64 $r0d $r2d $r4d291fma rn f64 $r6d neg $r4d $r0d $r8d292fma rn f64 $r4d $r4d $r6d $r4d293mul rn f64 $r0d $r2d $r4d294fma rn f64 $r6d neg $r4d $r0d $r8d295sched 0x29 0x29 0x29 0x29 0x29 0x29 0x29296fma rn f64 $r4d $r4d $r6d $r4d297mul rn f64 $r0d $r2d $r4d298fma rn f64 $r6d neg $r4d $r0d $r8d299fma rn f64 $r4d $r4d $r6d $r4d300mul rn f64 $r0d $r2d $r4d301fma rn f64 $r6d neg $r4d $r0d $r8d302fma rn f64 $r4d $r4d $r6d $r4d303sched 0x29 0x20 0x28 0x2e 0x00 0x00 0x00304// Multiply 2^27 to result for small inputs to recover305$p1 mul rn f64 $r4d $r4d 0x41a0000000000000306mov b32 $r1 $r5307mov b32 $r0 $r4308ret309310.section #gk110_builtin_offsets311.b64 #gk110_div_u32312.b64 #gk110_div_s32313.b64 #gk110_rcp_f64314.b64 #gk110_rsq_f64315316317