4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
24 ! .asciz "Copyr 1986 Sun Micro"
27 #ident "%Z%%M% %I% %E% SMI"
30 * Copyright 1986 Sun Microsystems, Inc. All rights reserved.
31 * Use is subject to license terms.
38 * dividend -- the thing being divided
39 * divisor -- how many ways to divide
40 * Important parameters:
41 * N -- how many bits per iteration we try to get
42 * as our current guess:
43 * WORDSIZE -- how many bits altogether we're talking about:
46 * TOPBITS -- how many bits are in the top "decade" of a number:
48 * Important variables are:
49 * Q -- the partial quotient under development -- initally 0
50 * R -- the remainder so far -- initially == the dividend
51 * ITER -- number of iterations of the main division loop will
52 * be required. Equal to CEIL( lg2(quotient)/4 )
53 * Note that this is log_base_(2^4) of the quotient.
54 * V -- the current comparand -- initially divisor*2^(ITER*4-1)
56 * current estimate for non-large dividend is
57 * CEIL( lg2(quotient) / 4 ) x ( 10 + 74/2 ) + C
58 * a large dividend is one greater than 2^(31-4 ) and takes a
59 * different path, as the upper bits of the quotient must be developed
64 #include <sys/asm_linkage.h>
77 * this is the recursive definition of how we develop quotient digits.
78 * it takes three important parameters:
79 * $1 -- the current depth, 1<=$1<=4
80 * $2 -- the current accumulation of quotient bits
82 * We add a new bit to $2 and either recurse or
83 * insert the bits in the quotient.
85 * %o3 -- current remainder
86 * %o2 -- current quotient
87 * %o5 -- current comparand
88 * cc -- set on current value of %o3
90 * %o3', %o2', %o5', cc'
97 ! RTENTRY
(.udiv) ! unsigned divide
101 mov
0,%g1
! result always positive
103 ! RTENTRY
(.div) ! SIGNED DIVIDE
106 orcc
%o1
,%o0
,%g0
! are either
%o0
or %o1 negative
107 bge divide
! if
not, skip this junk
108 xor %o1
,%o0
,%g1
! record sign of result in sign of
%g1
122 ! compute size of quotient
, scale comparand
123 orcc
%o1
,%g0
,%o5
! movcc
%o1
,%o5
130 blu got_result
! if
%o3
<%o5 already
, there
's no point in continuing
132 sethi %hi(1<<(32-4 -1)),%g2
137 ! here, the %o0 is >= 2^(31-4) or so. We must be careful here, as
138 ! our usual 4-at-a-shot divide step will cause overflow and havoc. The
139 ! total number of bits in the result here is 4*%o4+%g3, where %g3 <= 4.
140 ! compute %o4, in an unorthodox manner: know we need to Shift %o5 into
141 ! the top decade: so don't even bother to compare to
%o3.
151 bcc not_too_big
! bcc not_too_big
154 ! here if the
%o1 overflowed when Shifting
155 ! this means that
%o3 has the high-order bit set
156 ! restore
%o5
and subtract from
%o3
157 sll
%g2
,4 ,%g2
! high order bit
158 srl
%o5
,1,%o5
! rest of
%o5
168 ! %o5
> %o3
: went too far
: back up
1 step
171 ! do single-bit divide steps
173 ! we have to
be careful here. We know that
%o3
>= %o5
, so we can do the
174 ! first divide step without thinking. BUT
, the others are conditional
,
175 ! and are only done if
%o3
>= 0. Because both %o3 and %o5 may have the high-
176 ! order bit set in the first step
, just falling into the regular
177 ! division loop will mess up the first time around.
178 ! So we unroll slightly.
..
181 bl end_regular_divide
185 b,a end_single_divloop
202 b,a end_regular_divide
214 ! do the main division iteration
216 ! fall through into divide loop
219 !depth
1, accumulated bits
0
222 ! remainder is positive
224 !depth
2, accumulated bits
1
227 ! remainder is positive
229 !depth
3, accumulated bits
3
232 ! remainder is positive
234 !depth
4, accumulated bits
7
237 ! remainder is positive
240 add %o2
, (7*2+1), %o2
242 L.4
.23: ! remainder is negative
245 add %o2
, (7*2-1), %o2
250 L.3
.19: ! remainder is negative
252 !depth
4, accumulated bits
5
255 ! remainder is positive
258 add %o2
, (5*2+1), %o2
260 L.4
.21: ! remainder is negative
263 add %o2
, (5*2-1), %o2
271 L.2
.17: ! remainder is negative
273 !depth
3, accumulated bits
1
276 ! remainder is positive
278 !depth
4, accumulated bits
3
281 ! remainder is positive
284 add %o2
, (3*2+1), %o2
286 L.4
.19: ! remainder is negative
289 add %o2
, (3*2-1), %o2
294 L.3
.17: ! remainder is negative
296 !depth
4, accumulated bits
1
299 ! remainder is positive
302 add %o2
, (1*2+1), %o2
304 L.4
.17: ! remainder is negative
307 add %o2
, (1*2-1), %o2
318 L.1
.16: ! remainder is negative
320 !depth
2, accumulated bits
-1
323 ! remainder is positive
325 !depth
3, accumulated bits
-1
328 ! remainder is positive
330 !depth
4, accumulated bits
-1
333 ! remainder is positive
336 add %o2
, (-1*2+1), %o2
338 L.4
.15: ! remainder is negative
341 add %o2
, (-1*2-1), %o2
346 L.3
.15: ! remainder is negative
348 !depth
4, accumulated bits
-3
351 ! remainder is positive
354 add %o2
, (-3*2+1), %o2
356 L.4
.13: ! remainder is negative
359 add %o2
, (-3*2-1), %o2
367 L.2
.15: ! remainder is negative
369 !depth
3, accumulated bits
-3
372 ! remainder is positive
374 !depth
4, accumulated bits
-5
377 ! remainder is positive
380 add %o2
, (-5*2+1), %o2
382 L.4
.11: ! remainder is negative
385 add %o2
, (-5*2-1), %o2
390 L.3
.13: ! remainder is negative
392 !depth
4, accumulated bits
-7
395 ! remainder is positive
398 add %o2
, (-7*2+1), %o2
400 L.4
.9: ! remainder is negative
403 add %o2
, (-7*2-1), %o2
427 neg %o2
! quotient
<- -%o2
431 mov
%o2
,%o0
! quotient
<- %o2
435 ta ST_DIV0
! divide by zero
trap
436 retl
! if handled
, ignored
, return