/*1* Copyright 2004-2009 Analog Devices Inc.2*3* Licensed under the ADI BSD license or the GPL-2 (or later)4*/56#include <linux/linkage.h>78#define CARRY AC0910#ifdef CONFIG_ARITHMETIC_OPS_L111.section .l1.text12#else13.text14#endif151617ENTRY(___udivsi3)1819CC = R0 < R1 (IU); /* If X < Y, always return 0 */20IF CC JUMP .Lreturn_ident;2122R2 = R1 << 16;23CC = R2 <= R0 (IU);24IF CC JUMP .Lidents;2526R2 = R0 >> 31; /* if X is a 31-bit number */27R3 = R1 >> 15; /* and Y is a 15-bit number */28R2 = R2 | R3; /* then it's okay to use the DIVQ builtins (fallthrough to fast)*/29CC = R2;30IF CC JUMP .Ly_16bit;3132/* METHOD 1: FAST DIVQ33We know we have a 31-bit dividend, and 15-bit divisor so we can use the34simple divq approach (first setting AQ to 0 - implying unsigned division,35then 16 DIVQ's).36*/3738AQ = CC; /* Clear AQ (CC==0) */3940/* ISR States: When dividing two integers (32.0/16.0) using divide primitives,41we need to shift the dividend one bit to the left.42We have already checked that we have a 31-bit number so we are safe to do43that.44*/45R0 <<= 1;46DIVQ(R0, R1); // 147DIVQ(R0, R1); // 248DIVQ(R0, R1); // 349DIVQ(R0, R1); // 450DIVQ(R0, R1); // 551DIVQ(R0, R1); // 652DIVQ(R0, R1); // 753DIVQ(R0, R1); // 854DIVQ(R0, R1); // 955DIVQ(R0, R1); // 1056DIVQ(R0, R1); // 1157DIVQ(R0, R1); // 1258DIVQ(R0, R1); // 1359DIVQ(R0, R1); // 1460DIVQ(R0, R1); // 1561DIVQ(R0, R1); // 1662R0 = R0.L (Z);63RTS;6465.Ly_16bit:66/* We know that the upper 17 bits of Y might have bits set,67** or that the sign bit of X might have a bit. If Y is a68** 16-bit number, but not bigger, then we can use the builtins69** with a post-divide correction.70** R3 currently holds Y>>15, which means R3's LSB is the71** bit we're interested in.72*/7374/* According to the ISR, to use the Divide primitives for75** unsigned integer divide, the useable range is 31 bits76*/77CC = ! BITTST(R0, 31);7879/* IF condition is true we can scale our inputs and use the divide primitives,80** with some post-adjustment81*/82R3 += -1; /* if so, Y is 0x00008nnn */83CC &= AZ;8485/* If condition is true we can scale our inputs and use the divide primitives,86** with some post-adjustment87*/88R3 = R1 >> 1; /* Pre-scaled divisor for primitive case */89R2 = R0 >> 16;9091R2 = R3 - R2; /* shifted divisor < upper 16 bits of dividend */92CC &= CARRY;93IF CC JUMP .Lshift_and_correct;9495/* Fall through to the identities */9697/* METHOD 2: identities and manual calculation98We are not able to use the divide primites, but may still catch some special99cases.100*/101.Lidents:102/* Test for common identities. Value to be returned is placed in R2. */103CC = R0 == 0; /* 0/Y => 0 */104IF CC JUMP .Lreturn_r0;105CC = R0 == R1; /* X==Y => 1 */106IF CC JUMP .Lreturn_ident;107CC = R1 == 1; /* X/1 => X */108IF CC JUMP .Lreturn_ident;109110R2.L = ONES R1;111R2 = R2.L (Z);112CC = R2 == 1;113IF CC JUMP .Lpower_of_two;114115[--SP] = (R7:5); /* Push registers R5-R7 */116117/* Idents don't match. Go for the full operation. */118119120R6 = 2; /* assume we'll shift two */121R3 = 1;122123P2 = R1;124/* If either R0 or R1 have sign set, */125/* divide them by two, and note it's */126/* been done. */127CC = R1 < 0;128R2 = R1 >> 1;129IF CC R1 = R2; /* Possibly-shifted R1 */130IF !CC R6 = R3; /* R1 doesn't, so at most 1 shifted */131132P0 = 0;133R3 = -R1;134[--SP] = R3;135R2 = R0 >> 1;136R2 = R0 >> 1;137CC = R0 < 0;138IF CC P0 = R6; /* Number of values divided */139IF !CC R2 = R0; /* Shifted R0 */140141/* P0 is 0, 1 (NR/=2) or 2 (NR/=2, DR/=2) */142143/* r2 holds Copy dividend */144R3 = 0; /* Clear partial remainder */145R7 = 0; /* Initialise quotient bit */146147P1 = 32; /* Set loop counter */148LSETUP(.Lulst, .Lulend) LC0 = P1; /* Set loop counter */149.Lulst: R6 = R2 >> 31; /* R6 = sign bit of R2, for carry */150R2 = R2 << 1; /* Shift 64 bit dividend up by 1 bit */151R3 = R3 << 1 || R5 = [SP];152R3 = R3 | R6; /* Include any carry */153CC = R7 < 0; /* Check quotient(AQ) */154/* If AQ==0, we'll sub divisor */155IF CC R5 = R1; /* and if AQ==1, we'll add it. */156R3 = R3 + R5; /* Add/sub divsor to partial remainder */157R7 = R3 ^ R1; /* Generate next quotient bit */158159R5 = R7 >> 31; /* Get AQ */160BITTGL(R5, 0); /* Invert it, to get what we'll shift */161.Lulend: R2 = R2 + R5; /* and "shift" it in. */162163CC = P0 == 0; /* Check how many inputs we shifted */164IF CC JUMP .Lno_mult; /* if none... */165R6 = R2 << 1;166CC = P0 == 1;167IF CC R2 = R6; /* if 1, Q = Q*2 */168IF !CC R1 = P2; /* if 2, restore stored divisor */169170R3 = R2; /* Copy of R2 */171R3 *= R1; /* Q * divisor */172R5 = R0 - R3; /* Z = (dividend - Q * divisor) */173CC = R1 <= R5 (IU); /* Check if divisor <= Z? */174R6 = CC; /* if yes, R6 = 1 */175R2 = R2 + R6; /* if yes, add one to quotient(Q) */176.Lno_mult:177SP += 4;178(R7:5) = [SP++]; /* Pop registers R5-R7 */179R0 = R2; /* Store quotient */180RTS;181182.Lreturn_ident:183CC = R0 < R1 (IU); /* If X < Y, always return 0 */184R2 = 0;185IF CC JUMP .Ltrue_return_ident;186R2 = -1 (X); /* X/0 => 0xFFFFFFFF */187CC = R1 == 0;188IF CC JUMP .Ltrue_return_ident;189R2 = -R2; /* R2 now 1 */190CC = R0 == R1; /* X==Y => 1 */191IF CC JUMP .Ltrue_return_ident;192R2 = R0; /* X/1 => X */193/*FALLTHRU*/194195.Ltrue_return_ident:196R0 = R2;197.Lreturn_r0:198RTS;199200.Lpower_of_two:201/* Y has a single bit set, which means it's a power of two.202** That means we can perform the division just by shifting203** X to the right the appropriate number of bits204*/205206/* signbits returns the number of sign bits, minus one.207** 1=>30, 2=>29, ..., 0x40000000=>0. Which means we need208** to shift right n-signbits spaces. It also means 0x80000000209** is a special case, because that *also* gives a signbits of 0210*/211212R2 = R0 >> 31;213CC = R1 < 0;214IF CC JUMP .Ltrue_return_ident;215216R1.l = SIGNBITS R1;217R1 = R1.L (Z);218R1 += -30;219R0 = LSHIFT R0 by R1.L;220RTS;221222/* METHOD 3: PRESCALE AND USE THE DIVIDE PRIMITIVES WITH SOME POST-CORRECTION223Two scaling operations are required to use the divide primitives with a224divisor > 0x7FFFF.225Firstly (as in method 1) we need to shift the dividend 1 to the left for226integer division.227Secondly we need to shift both the divisor and dividend 1 to the right so228both are in range for the primitives.229The left/right shift of the dividend does nothing so we can skip it.230*/231.Lshift_and_correct:232R2 = R0;233// R3 is already R1 >> 1234CC=!CC;235AQ = CC; /* Clear AQ, got here with CC = 0 */236DIVQ(R2, R3); // 1237DIVQ(R2, R3); // 2238DIVQ(R2, R3); // 3239DIVQ(R2, R3); // 4240DIVQ(R2, R3); // 5241DIVQ(R2, R3); // 6242DIVQ(R2, R3); // 7243DIVQ(R2, R3); // 8244DIVQ(R2, R3); // 9245DIVQ(R2, R3); // 10246DIVQ(R2, R3); // 11247DIVQ(R2, R3); // 12248DIVQ(R2, R3); // 13249DIVQ(R2, R3); // 14250DIVQ(R2, R3); // 15251DIVQ(R2, R3); // 16252253/* According to the Instruction Set Reference:254To divide by a divisor > 0x7FFF,2551. prescale and perform divide to obtain quotient (Q) (done above),2562. multiply quotient by unscaled divisor (result M)2573. subtract the product from the divident to get an error (E = X - M)2584. if E < divisor (Y) subtract 1, if E > divisor (Y) add 1, else return quotient (Q)259*/260R3 = R2.L (Z); /* Q = X' / Y' */261R2 = R3; /* Preserve Q */262R2 *= R1; /* M = Q * Y */263R2 = R0 - R2; /* E = X - M */264R0 = R3; /* Copy Q into result reg */265266/* Correction: If result of the multiply is negative, we overflowed267and need to correct the result by subtracting 1 from the result.*/268R3 = 0xFFFF (Z);269R2 = R2 >> 16; /* E >> 16 */270CC = R2 == R3;271R3 = 1 ;272R1 = R0 - R3;273IF CC R0 = R1;274RTS;275276ENDPROC(___udivsi3)277278279