xref: /original-bsd/lib/libc/sparc/gen/mul.s (revision bdfadfff)
1/*
2 * Copyright (c) 1992 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This software was developed by the Computer Systems Engineering group
6 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7 * contributed to Berkeley.
8 *
9 * %sccs.include.redist.c%
10 *
11 * from: $Header: mul.s,v 1.5 92/06/25 13:24:03 torek Exp $
12 */
13
14#if defined(LIBC_SCCS) && !defined(lint)
15	.asciz "@(#)mul.s	5.1 (Berkeley) 06/25/92"
16#endif /* LIBC_SCCS and not lint */
17
18/*
19 * Signed multiply, from Appendix E of the Sparc Version 8
20 * Architecture Manual.
21 *
22 * Returns %o0 * %o1 in %o1%o0 (i.e., %o1 holds the upper 32 bits of
23 * the 64-bit product).
24 *
25 * This code optimizes short (less than 13-bit) multiplies.
26 */
27
28#include "DEFS.h"
29FUNC(.mul)
30	mov	%o0, %y		! multiplier -> Y
31	andncc	%o0, 0xfff, %g0	! test bits 12..31
32	be	Lmul_shortway	! if zero, can do it the short way
33	andcc	%g0, %g0, %o4	! zero the partial product and clear N and V
34
35	/*
36	 * Long multiply.  32 steps, followed by a final shift step.
37	 */
38	mulscc	%o4, %o1, %o4	! 1
39	mulscc	%o4, %o1, %o4	! 2
40	mulscc	%o4, %o1, %o4	! 3
41	mulscc	%o4, %o1, %o4	! 4
42	mulscc	%o4, %o1, %o4	! 5
43	mulscc	%o4, %o1, %o4	! 6
44	mulscc	%o4, %o1, %o4	! 7
45	mulscc	%o4, %o1, %o4	! 8
46	mulscc	%o4, %o1, %o4	! 9
47	mulscc	%o4, %o1, %o4	! 10
48	mulscc	%o4, %o1, %o4	! 11
49	mulscc	%o4, %o1, %o4	! 12
50	mulscc	%o4, %o1, %o4	! 13
51	mulscc	%o4, %o1, %o4	! 14
52	mulscc	%o4, %o1, %o4	! 15
53	mulscc	%o4, %o1, %o4	! 16
54	mulscc	%o4, %o1, %o4	! 17
55	mulscc	%o4, %o1, %o4	! 18
56	mulscc	%o4, %o1, %o4	! 19
57	mulscc	%o4, %o1, %o4	! 20
58	mulscc	%o4, %o1, %o4	! 21
59	mulscc	%o4, %o1, %o4	! 22
60	mulscc	%o4, %o1, %o4	! 23
61	mulscc	%o4, %o1, %o4	! 24
62	mulscc	%o4, %o1, %o4	! 25
63	mulscc	%o4, %o1, %o4	! 26
64	mulscc	%o4, %o1, %o4	! 27
65	mulscc	%o4, %o1, %o4	! 28
66	mulscc	%o4, %o1, %o4	! 29
67	mulscc	%o4, %o1, %o4	! 30
68	mulscc	%o4, %o1, %o4	! 31
69	mulscc	%o4, %o1, %o4	! 32
70	mulscc	%o4, %g0, %o4	! final shift
71
72	! If %o0 was negative, the result is
73	!	(%o0 * %o1) + (%o1 << 32))
74	! We fix that here.
75
76	tst	%o0
77	bge	1f
78	rd	%y, %o0
79
80	! %o0 was indeed negative; fix upper 32 bits of result by subtracting
81	! %o1 (i.e., return %o4 - %o1 in %o1).
82	retl
83	sub	%o4, %o1, %o1
84
851:
86	retl
87	mov	%o4, %o1
88
89Lmul_shortway:
90	/*
91	 * Short multiply.  12 steps, followed by a final shift step.
92	 * The resulting bits are off by 12 and (32-12) = 20 bit positions,
93	 * but there is no problem with %o0 being negative (unlike above).
94	 */
95	mulscc	%o4, %o1, %o4	! 1
96	mulscc	%o4, %o1, %o4	! 2
97	mulscc	%o4, %o1, %o4	! 3
98	mulscc	%o4, %o1, %o4	! 4
99	mulscc	%o4, %o1, %o4	! 5
100	mulscc	%o4, %o1, %o4	! 6
101	mulscc	%o4, %o1, %o4	! 7
102	mulscc	%o4, %o1, %o4	! 8
103	mulscc	%o4, %o1, %o4	! 9
104	mulscc	%o4, %o1, %o4	! 10
105	mulscc	%o4, %o1, %o4	! 11
106	mulscc	%o4, %o1, %o4	! 12
107	mulscc	%o4, %g0, %o4	! final shift
108
109	/*
110	 *  %o4 has 20 of the bits that should be in the low part of the
111	 * result; %y has the bottom 12 (as %y's top 12).  That is:
112	 *
113	 *	  %o4		    %y
114	 * +----------------+----------------+
115	 * | -12- |   -20-  | -12- |   -20-  |
116	 * +------(---------+------)---------+
117	 *  --hi-- ----low-part----
118	 *
119	 * The upper 12 bits of %o4 should be sign-extended to form the
120	 * high part of the product (i.e., highpart = %o4 >> 20).
121	 */
122
123	rd	%y, %o5
124	sll	%o4, 12, %o0	! shift middle bits left 12
125	srl	%o5, 20, %o5	! shift low bits right 20, zero fill at left
126	or	%o5, %o0, %o0	! construct low part of result
127	retl
128	sra	%o4, 20, %o1	! ... and extract high part of result
129