xref: /titanic_41/usr/src/lib/libc/sparc/crt/hwmuldiv.s (revision 6185db853e024a486ff8837e6784dd290d866112)
1/*
2 * CDDL HEADER START
3 *
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
7 * with the License.
8 *
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.
13 *
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]
19 *
20 * CDDL HEADER END
21 */
22/*
23 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26
27#pragma ident	"%Z%%M%	%I%	%E% SMI"
28
29	.file	"%M%"
30
31#include <sys/asm_linkage.h>
32
33/*
34 * Versions of .mul .umul .div .udiv .rem .urem written using
35 * appropriate SPARC V8 instructions.
36 */
37
38	ENTRY(.mul)
39	smul	%o0, %o1, %o0
40	rd	%y, %o1
41	sra	%o0, 31, %o2
42	retl
43	cmp	%o1, %o2	! return with Z set if %y == (%o0 >> 31)
44	SET_SIZE(.mul)
45
46	ENTRY(.umul)
47	umul	%o0, %o1, %o0
48	rd	%y, %o1
49	retl
50	tst	%o1		! return with Z set if high order bits are zero
51	SET_SIZE(.umul)
52
53	ENTRY(.div)
54	sra	%o0, 31, %o2
55	wr	%g0, %o2, %y
56	nop
57	nop
58	nop
59	sdivcc	%o0, %o1, %o0
60	bvs,a	1f
61	xnor	%o0, %g0, %o0	! Corbett Correction Factor
621:	retl
63	nop
64	SET_SIZE(.div)
65
66	ENTRY(.udiv)
67	wr	%g0, %g0, %y
68	nop
69	nop
70	retl
71	udiv	%o0, %o1, %o0
72	SET_SIZE(.udiv)
73
74	ENTRY(.rem)
75	sra	%o0, 31, %o4
76	wr	%o4, %g0, %y
77	nop
78	nop
79	nop
80	sdivcc	%o0, %o1, %o2
81	bvs,a	1f
82	xnor	%o2, %g0, %o2	! Corbett Correction Factor
831:	smul	%o2, %o1, %o2
84	retl
85	sub	%o0, %o2, %o0
86	SET_SIZE(.rem)
87
88	ENTRY(.urem)
89	wr	%g0, %g0, %y
90	nop
91	nop
92	nop
93	udiv	%o0, %o1, %o2
94	umul	%o2, %o1, %o2
95	retl
96	sub	%o0, %o2, %o0
97	SET_SIZE(.urem)
98
99/*
100 * v8plus versions of __{u,}{mul,div,rem}64 compiler support routines
101 */
102
103/*
104 * Convert 32-bit arg pairs in %o0:o1 and %o2:%o3 to 64-bit args in %o1 and %o2
105 */
106#define	ARGS_TO_64				\
107	sllx	%o0, 32, %o0;			\
108	srl	%o1, 0, %o1;			\
109	sllx	%o2, 32, %o2;			\
110	srl	%o3, 0, %o3;			\
111	or	%o0, %o1, %o1;			\
112	or	%o2, %o3, %o2
113
114!
115! division, signed
116!
117	ENTRY(__div64)
118	ARGS_TO_64
119	sdivx	%o1, %o2, %o1
120	retl
121	srax	%o1, 32, %o0
122	SET_SIZE(__div64)
123
124!
125! division, unsigned
126!
127	ENTRY(__udiv64)
128	ARGS_TO_64
129	udivx	%o1, %o2, %o1
130	retl
131	srax	%o1, 32, %o0
132	SET_SIZE(__udiv64)
133
134!
135! multiplication, signed and unsigned
136!
137	ENTRY(__mul64)
138	ALTENTRY(__umul64)
139	ARGS_TO_64
140	sub	%o1, %o2, %o0	! %o0 = a - b
141	movrlz	%o0, %g0, %o0	! %o0 = (a < b) ? 0 : a - b
142	sub	%o1, %o0, %o1	! %o1 = (a < b) ? a : b = min(a, b)
143	add	%o2, %o0, %o2	! %o2 = (a < b) ? b : a = max(a, b)
144	mulx	%o1, %o2, %o1	! min(a, b) in "rs1" for early exit
145	retl
146	srax	%o1, 32, %o0
147	SET_SIZE(__mul64)
148	SET_SIZE(__umul64)
149
150!
151! unsigned remainder
152!
153	ENTRY(__urem64)
154	ARGS_TO_64
155	udivx	%o1, %o2, %o3
156	mulx	%o3, %o2, %o3
157	sub	%o1, %o3, %o1
158	retl
159	srax	%o1, 32, %o0
160	SET_SIZE(__urem64)
161
162!
163! signed remainder
164!
165	ENTRY(__rem64)
166	ARGS_TO_64
167	sdivx	%o1, %o2, %o3
168	mulx	%o2, %o3, %o3
169	sub	%o1, %o3, %o1
170	retl
171	srax	%o1, 32, %o0
172	SET_SIZE(__rem64)
173