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 1987 Sun Micro"
29 #ident "%Z%%M% %I% %E% SMI"
31 ! Copyright
(c
) 1987 by Sun Microsystems
, Inc.
34 #include <sys/asm_linkage.h>
37 * procedure to perform a 32 by 32 unsigned integer multiply.
38 * pass the multiplier into %o0, and the multiplicand into %o1
39 * the least significant 32 bits of the result will be returned in %o0,
40 * and the most significant in %o1
42 * Most unsigned integer multiplies involve small numbers, so it is
43 * worthwhile to optimize for short multiplies at the expense of long
44 * multiplies. This code checks the size of the multiplier, and has
45 * special cases for the following:
47 * 4 or fewer bit multipliers: 19 or 21 instruction cycles
48 * 8 or fewer bit multipliers: 26 or 28 instruction cycles
49 * 12 or fewer bit multipliers: 34 or 36 instruction cycles
50 * 16 or fewer bit multipliers: 42 or 44 instruction cycles
52 * Long multipliers require 58 or 60 instruction cycles:
54 * This code indicates that overflow has occured, by leaving the Z condition
55 * code clear. The following call sequence would be used if you wish to
59 * nop ( or set up last parameter here )
60 * bnz overflow_code (or tnz to overflow handler)
66 wr
%o0
, %y
! multiplier to Y register
68 andncc
%o0
, 0xf, %o4
! mask out lower
4 bits; if branch
69 ! taken
, %o4
, N
and V have been cleared
71 be umul_4bit
! 4-bit multiplier
72 sethi
%hi
(0xffff0000), %o5
! mask for
16-bit case; have to
73 ! wait
3 instructions after wd
74 ! before
%y has stabilized anyway
77 be,a umul_8bit
! 8-bit multiplier
78 mulscc
%o4
, %o1
, %o4
! first iteration of
9
80 andncc
%o0
, 0xfff, %o4
81 be,a umul_12bit
! 12-bit multiplier
82 mulscc
%o4
, %o1
, %o4
! first iteration of
13
85 be,a umul_16bit
! 16-bit multiplier
86 mulscc
%o4
, %o1
, %o4
! first iteration of
17
88 andcc
%g0
, %g0
, %o4
! zero the partial product
89 ! and clear N
and V conditions
93 mulscc
%o4
, %o1
, %o4
! first iteration of
33
124 mulscc
%o4
, %o1
, %o4
! 32nd iteration
125 mulscc
%o4
, %g0
, %o4
! last iteration only shifts
127 ! For unsigned multiplies
, a pure shifty-
add approach yields the
128 ! correct result. Signed multiplies introduce complications.
130 ! With
32-bit twos-complement numbers
, -x can
be represented as
132 ! ((2 - (x
/(2**32)) mod
2) * 2**32.
134 ! To simplify the equations
, the radix point can
be moved to just
135 ! to the left of the sign bit. So
:
138 ! -x
* y
= (2 - x
) mod
2 * y
= (2y
- xy
) mod
2
139 ! x
* -y
= x
* (2 - y
) mod
2 = (2x
- xy
) mod
2
140 ! -x
* -y
= (2 - x
) * (2 - y
) = (4 - 2x
- 2y
+ xy
) mod
2
142 ! Because of the way the shift into the partial product is calculated
143 ! (N
xor V
), the extra term is automagically removed for negative
144 ! multiplicands
, so no adjustment is necessary.
146 ! But for unsigned multiplies
, the high-order bit of the multiplicand
147 ! is incorrectly treated as
a sign bit. For unsigned multiplies where
148 ! the high-order bit of the multiplicand is one
, the result is
158 add %o4
, %o0
, %o4
! add (2**32) * %o0; bits
63-32
159 ! of the product are in
%o4
161 ! The multiply hasn
't overflowed if the high-order bits are 0
163 ! if you are not interested in detecting overflow,
164 ! replace the following code with:
173 retl ! leaf routine return
174 addcc %o4, %g0, %o1 ! return high-order bits and set Z if
175 ! high order bits are 0
180 mulscc %o4, %o1, %o4 ! first iteration of 5
183 mulscc %o4, %o1, %o4 ! 4th iteration
184 mulscc %o4, %g0, %o4 ! last iteration only shifts
188 ! The folowing code adds (2**32) * %o0 to the product if the
189 ! multiplicand had it's high bit set
(see
32-bit case for explanation
)
193 sra %o4
, 28, %o1
! right shift high bits by
28 bits
197 ! The multiply hasn
't overflowed if high-order bits are 0
199 ! if you are not interested in detecting overflow,
200 ! replace the following code with:
209 sll %o4, 4, %o0 ! left shift middle bits by 4 bits
210 srl %o5, 28, %o5 ! right shift low bits by 28 bits
211 or %o5, %o0, %o0 ! merge for true product
212 retl ! leaf routine return
213 tst %o1 ! set Z if high order bits are 0
218 mulscc %o4, %o1, %o4 ! second iteration of 9
224 mulscc %o4, %o1, %o4 ! 8th iteration
225 mulscc %o4, %g0, %o4 ! last iteration only shifts
229 ! The folowing code adds (2**32) * %o0 to the product if the
230 ! multiplicand had it's high bit set
(see
32-bit case for explanation
)
234 sra %o4
, 24, %o1
! right shift high bits by
24 bits
238 ! The multiply hasn
't overflowed if high-order bits are 0
240 ! if you are not interested in detecting overflow,
241 ! replace the following code with:
250 sll %o4, 8, %o0 ! left shift middle bits by 8 bits
251 srl %o5, 24, %o5 ! right shift low bits by 24 bits
252 or %o5, %o0, %o0 ! merge for true product
253 retl ! leaf routine return
254 tst %o1 ! set Z if high order bits are 0
259 mulscc %o4, %o1, %o4 ! second iteration of 13
269 mulscc %o4, %o1, %o4 ! 12th iteration
270 mulscc %o4, %g0, %o4 ! last iteration only shifts
274 ! The folowing code adds (2**32) * %o0 to the product if the
275 ! multiplicand had it's high bit set
(see
32-bit case for explanation
)
279 sra %o4
, 20, %o1
! right shift high bits by
20 bits
283 ! The multiply hasn
't overflowed if high-order bits are 0
285 ! if you are not interested in detecting overflow,
286 ! replace the following code with:
295 sll %o4, 12, %o0 ! left shift middle bits by 12 bits
296 srl %o5, 20, %o5 ! right shift low bits by 20 bits
297 or %o5, %o0, %o0 ! merge for true product
298 retl ! leaf routine return
299 tst %o1 ! set Z if high order bits are 0
304 mulscc %o4, %o1, %o4 ! second iteration of 17
318 mulscc %o4, %o1, %o4 ! 16th iteration
319 mulscc %o4, %g0, %o4 ! last iteration only shifts
323 ! The folowing code adds (2**32) * %o0 to the product if the
324 ! multiplicand had it's high bit set
(see
32-bit case for explanation
)
328 sra %o4
, 16, %o1
! right shift high bits by
16 bits
332 ! The multiply hasn
't overflowed if high-order bits are 0
334 ! if you are not interested in detecting overflow,
335 ! replace the following code with:
344 sll %o4, 16, %o0 ! left shift middle bits by 16 bits
345 srl %o5, 16, %o5 ! right shift low bits by 16 bits
346 or %o5, %o0, %o0 ! merge for true product
347 retl ! leaf routine return
348 tst %o1 ! set Z if high order bits are 0