1 /* This is an assembly language implementation of libgcc1.c for the sparc
4 These routines are derived from the Sparc Architecture Manual, version 8,
5 slightly edited to match the desired calling convention, and also to
6 optimize them for our purposes. */
14 or %o0, %o1, %o4 ! logical or of multiplier and multiplicand
15 mov %o0, %y ! multiplier to Y register
16 andncc %o4, 0xfff, %o5 ! mask out lower 12 bits
17 be mul_shortway ! can do it the short way
18 andcc %g0, %g0, %o4 ! zero the partial product and clear NV cc
22 mulscc %o4, %o1, %o4 ! first iteration of 33
53 mulscc %o4, %o1, %o4 ! 32nd iteration
54 mulscc %o4, %g0, %o4 ! last iteration only shifts
55 ! the upper 32 bits of product are wrong, but we do not care
62 mulscc %o4, %o1, %o4 ! first iteration of 13
73 mulscc %o4, %o1, %o4 ! 12th iteration
74 mulscc %o4, %g0, %o4 ! last iteration only shifts
76 sll %o4, 12, %o4 ! left shift partial product by 12 bits
77 srl %o5, 20, %o5 ! right shift partial product by 20 bits
79 or %o5, %o4, %o0 ! merge for true product
90 mov 0, %l2 ! result always positive
95 orcc %i1, %i0, %g0 ! is either operand negative
96 bge divide ! if not, skip this junk
97 xor %i1, %i0, %l2 ! record sign of result in sign of %l2
108 ! Compute size of quotient, scale comparand.
109 orcc %i1, %g0, %l1 ! movcc %i1, %l1
113 sethi %hi(1<<(32-4-1)), %l3
118 ! Here, the %i0 is >= 2^(31-3) or so. We must be careful here,
119 ! as our usual 3-at-a-shot divide step will cause overflow and havoc.
120 ! The total number of bits in the result here is 3*%l0+%l4, where
122 ! Compute %l0 in an unorthodox manner: know we need to Shift %l1 into
123 ! the top decade: so do not even bother to compare to %i3.
133 2: addcc %l1, %l1, %l1
137 ! We are here if the %i1 overflowed when Shifting.
138 ! This means that %i3 has the high-order bit set.
139 ! Restore %l1 and subtract from %i3.
151 ! %l1 > %i3: went too far: back up 1 step
154 ! do single-bit divide steps
156 ! We have to be careful here. We know that %i3 >= %l1, so we can do the
157 ! first divide step without thinking. BUT, the others are conditional,
158 ! and are only done if %i3 >= 0. Because both %i3 and %l1 may have the
159 ! high-order bit set in the first step, just falling into the regular
160 ! division loop will mess up the first time around.
161 ! So we unroll slightly...
164 bl end_regular_divide
195 ! Do the main division iteration
197 ! Fall through into divide loop
200 ! depth 1, accumulated bits 0
203 ! remainder is positive
205 ! depth 2, accumulated bits 1
208 ! remainder is positive
210 ! depth 3, accumulated bits 3
213 ! remainder is positive
216 add %i2, (3*2+1), %i2
217 L.3.11: ! remainder is negative
220 add %i2, (3*2-1), %i2
221 L.2.9: ! remainder is negative
223 ! depth 3, accumulated bits 1
226 ! remainder is positive
229 add %i2, (1*2+1), %i2
230 L.3.9: ! remainder is negative
233 add %i2, (1*2-1), %i2
234 L.1.8: ! remainder is negative
236 ! depth 2, accumulated bits -1
239 ! remainder is positive
241 ! depth 3, accumulated bits -1
244 ! remainder is positive
247 add %i2, (-1*2+1), %i2
248 L.3.7: ! remainder is negative
251 add %i2, (-1*2-1), %i2
252 L.2.7: ! remainder is negative
254 ! depth 3, accumulated bits -3
257 ! remainder is positive
260 add %i2, (-3*2+1), %i2
261 L.3.5: ! remainder is negative
264 add %i2, (-3*2-1), %i2
271 ! non-restoring fixup here
278 retl ! leaf-routine return
279 neg %o2, %o0 ! quotient <- -%i2
280 1: retl ! leaf-routine return
281 mov %o2, %o0 ! quotient <- %i2
292 mov 0, %l2 ! result always positive
297 orcc %i1, %i0, %g0 ! is either operand negative
298 bge divide ! if not, skip this junk
299 mov %i0, %l2 ! record sign of result in sign of %i2
310 ! Compute size of quotient, scale comparand.
311 orcc %i1, %g0, %l1 ! movcc %i1, %l1
315 sethi %hi(1<<(32-4-1)), %l3
320 ! Here, the %i0 is >= 2^(31-3) or so. We must be careful here,
321 ! as our usual 3-at-a-shot divide step will cause overflow and havoc.
322 ! The total number of bits in the result here is 3*%l0+%l4, where
324 ! Compute %l0 in an unorthodox manner: know we need to Shift %l1 into
325 ! the top decade: so do not even bother to compare to %i3.
335 2: addcc %l1, %l1, %l1
339 ! We are here if the %i1 overflowed when Shifting.
340 ! This means that %i3 has the high-order bit set.
341 ! Restore %l1 and subtract from %i3.
353 ! %l1 > %i3: went too far: back up 1 step
356 ! do single-bit divide steps
358 ! We have to be careful here. We know that %i3 >= %l1, so we can do the
359 ! first divide step without thinking. BUT, the others are conditional,
360 ! and are only done if %i3 >= 0. Because both %i3 and %l1 may have the
361 ! high-order bit set in the first step, just falling into the regular
362 ! division loop will mess up the first time around.
363 ! So we unroll slightly...
366 bl end_regular_divide
397 ! Do the main division iteration
399 ! Fall through into divide loop
402 ! depth 1, accumulated bits 0
405 ! remainder is positive
407 ! depth 2, accumulated bits 1
410 ! remainder is positive
412 ! depth 3, accumulated bits 3
415 ! remainder is positive
418 add %i2, (3*2+1), %i2
419 L.3.11: ! remainder is negative
422 add %i2, (3*2-1), %i2
423 L.2.9: ! remainder is negative
425 ! depth 3, accumulated bits 1
428 ! remainder is positive
431 add %i2, (1*2+1), %i2
432 L.3.9: ! remainder is negative
435 add %i2, (1*2-1), %i2
436 L.1.8: ! remainder is negative
438 ! depth 2, accumulated bits -1
441 ! remainder is positive
443 ! depth 3, accumulated bits -1
446 ! remainder is positive
449 add %i2, (-1*2+1), %i2
450 L.3.7: ! remainder is negative
453 add %i2, (-1*2-1), %i2
454 L.2.7: ! remainder is negative
456 ! depth 3, accumulated bits -3
459 ! remainder is positive
462 add %i2, (-3*2+1), %i2
463 L.3.5: ! remainder is negative
466 add %i2, (-3*2-1), %i2
473 ! non-restoring fixup here
480 retl ! leaf-routine return
481 neg %o3, %o0 ! remainder <- -%i3
482 1: retl ! leaf-routine return
483 mov %o3, %o0 ! remainder <- %i3