2 * udivdi3.S - unsigned long long division
4 * Copyright 2003-2007 Analog Devices Inc.
5 * Enter bugs at http://blackfin.uclinux.org/
7 * Licensed under the GPLv2 or later.
10 #include <linux/linkage.h>
14 #ifdef CONFIG_ARITHMETIC_OPS_L1
23 [--SP] = (R7:4, P5:3);
25 /* Attempt to use divide primitive first; these will handle
26 ** most cases, and they're quick - avoids stalls incurred by
27 ** testing for identities.
32 IF CC JUMP .LDIV_BY_ZERO;
35 R4 >>>= 16; // R4 now 0xFFFF8000
36 R5 = R0 | R2; // If either dividend or
37 R4 = R5 & R4; // divisor have bits in
38 CC = R4; // top half or low half's sign
39 IF CC JUMP .LIDENTS; // bit, skip builtins.
40 R4 = R1 | R3; // Also check top halves
44 /* Can use the builtins. */
46 AQ = CC; // Clear AQ (CC==0)
66 (R7:4, P5:3) = [SP++];
70 /* Test for common identities. Value to be returned is
73 // Check for 0/y, return 0
76 IF CC JUMP .LRETURN_R0;
78 // Check for x/x, return 1
79 R6 = R0 - R2; // If x == y, then both R6 and R7 will be zero
81 R4 = R6 | R7; // making R4 zero.
82 R6 += 1; // which would now make R6:R7==1.
84 IF CC JUMP .LRETURN_IDENT;
86 // Check for x/1, return x
90 IF !CC JUMP .Lnexttest;
92 IF CC JUMP .LRETURN_IDENT;
95 R4.L = ONES R2; // check for div by power of two which
96 R5.L = ONES R3; // can be done using a shift
97 R6 = PACK (R5.L, R4.L);
99 IF CC JUMP .Lpower_of_two_upper_zero;
100 R6 = PACK (R4.L, R5.L);
102 IF CC JUMP .Lpower_of_two_lower_zero;
104 // Check for x < y, return 0
108 IF CC JUMP .LRETURN_IDENT;
110 IF !CC JUMP .Lno_idents;
112 IF CC JUMP .LRETURN_IDENT;
114 .Lno_idents: // Idents don't match. Go for the full operation
117 // If X, or X and Y have high bit set, it'll affect the
118 // results, so shift right one to stop this. Note: we've already
119 // checked that X >= Y, so Y's msb won't be set unless X's
124 IF !CC JUMP .Lx_msb_clear;
126 R1 = ROT R1 BY -1; // Shift X >> 1
127 R0 = ROT R0 BY -1; // lsb -> CC
128 BITSET(R4,31); // to record only x msb was set
130 IF !CC JUMP .Ly_msb_clear;
132 R3 = ROT R3 BY -1; // Shift Y >> 1
134 BITCLR(R4,31); // clear bit to record only x msb was set
138 // Bit 31 in R4 indicates X msb set, but Y msb wasn't, and no bits
139 // were lost, so we should shift result left by one.
141 [--SP] = R4; // save for later
143 // In the loop that follows, each iteration we add
144 // either Y' or -Y' to the Remainder. We compute the
145 // negated Y', and store, for convenience. Y' goes
146 // into P0:P1, while -Y' goes into P2:P3.
157 R6 = 0; // remainder = 0
160 [--SP] = R2; P2 = SP;
161 [--SP] = R3; P3 = SP;
162 [--SP] = R6; P5 = SP; // AQ = 0
165 /* In the loop that follows, we use the following
166 ** register assignments:
167 ** R0,R1 X, workspace
168 ** R2,R3 Y, workspace
170 ** R6,R7 partial remainder
172 ** The remainder and div form a 128-bit number, with
173 ** the remainder in the high 64-bits.
179 P4 = 64; // Iterate once per bit
180 LSETUP(.LULST,.LULEND) LC0 = P4;
182 /* Shift Div and remainder up by one. The bit shifted
183 ** out of the top of the quotient is shifted into the bottom
188 R5 = ROT R5 BY 1 || // low q to high q
189 R2 = [P5]; // load saved AQ
190 R6 = ROT R6 BY 1 || // high q to low r
191 R0 = [P2]; // load -Y'
192 R7 = ROT R7 BY 1 || // low r to high r
196 CC = R2 < 0; // But if AQ is set...
197 IF CC R0 = P0; // then add Y' instead
200 R6 = R6 + R0; // Rem += (Y' or -Y')
206 // Set the next AQ bit
207 R1 = R7 ^ R1; // from Remainder and Y'
208 R1 = R1 >> 31 || // Negate AQ's value, and
209 [P5] = R1; // save next AQ
210 BITTGL(R1, 0); // add neg AQ to the Div
211 .LULEND: R4 = R4 + R1;
217 CC = BITTST(R6,30); // Just set CC=0
218 R4 = ROT R0 BY 1; // but if we had to shift X,
219 R5 = ROT R1 BY 1; // and didn't shift any bits out,
220 CC = BITTST(R6,31); // then the result will be half as
221 IF CC R0 = R4; // much as required, so shift left
222 IF CC R1 = R5; // one space.
225 (R7:4, P5:3) = [SP++];
229 /* Y has a single bit set, which means it's a power of two.
230 ** That means we can perform the division just by shifting
231 ** X to the right the appropriate number of bits
234 /* signbits returns the number of sign bits, minus one.
235 ** 1=>30, 2=>29, ..., 0x40000000=>0. Which means we need
236 ** to shift right n-signbits spaces. It also means 0x80000000
237 ** is a special case, because that *also* gives a signbits of 0
239 .Lpower_of_two_lower_zero:
243 IF CC JUMP .LRETURN_IDENT;
248 (R7:4, P5:3) = [SP++];
251 .Lpower_of_two_upper_zero:
253 IF CC JUMP .Lmaxint_shift;
258 (R7:4, P5:3) = [SP++];
263 (R7:4, P5:3) = [SP++];
270 (R7:4, P5:3) = [SP++];
275 (R7:4, P5:3) = [SP++];
283 IF CC JUMP .Lfinished; // nothing to do
288 IF !CC JUMP .Lretzero;
290 // We're shifting left, and it's less than 64 bits, so
291 // a valid result will be returned.
293 R3 >>= 1; // R3 now 32
296 IF !CC JUMP .Lzerohalf;
298 // We're shifting left, between 1 and 31 bits, which means
299 // some of the low half will be shifted into the high half.
300 // Work out how much.
304 // Save that much data from the bottom half.
310 // Adjust both parts of the parameter.
315 // And include the bits moved across.
322 // We're shifting left, between 32 and 63 bits, so the
323 // bottom half will become zero, and the top half will
324 // lose some bits. How many?
326 R2 = R2 - R3; // N - 32
327 R1 = LSHIFT R0 BY R2.L;
338 // We're shifting right, but by how much?
342 IF !CC JUMP .Lretzero;
344 // Shifting right less than 64 bits, so some result bits will
347 R3 >>= 1; // R3 now 32
349 IF !CC JUMP .Lsignhalf;
351 // Shifting right between 1 and 31 bits, so need to copy
352 // data across words.
365 // Shifting right between 32 and 63 bits, so the top half
366 // will become all zero-bits, and the bottom half is some
367 // of the top half. But how much?