xref: /titanic_50/usr/src/lib/libc/i386/gen/ldivide.s (revision 79d2f1e1d69dd8621f962e48584f1e2434b3c837)
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 (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22/*
23 * Copyright 2008 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/ Double long divide routine.
32
33#include "SYS.h"
34
35	.set	lop,16
36	.set	rop,24
37	.set	ans,0
38
39	ENTRY(ldivide)
40	popl	%eax
41	xchgl	%eax,0(%esp)
42	pushl	%eax
43
44	pushl	%esi
45	pushl	%edi
46
47	movl	lop(%esp),%eax
48	movl	lop+4(%esp),%edx
49
50/ the following code is only for compatibility with original ldivide code
51	orl	%edx,%edx	/ force numerator positive
52	jns	.ldiv1
53	notl	%edx
54	negl	%eax
55	sbbl	$0xffffffff,%edx
56.ldiv1:
57	testl	$0x80000000,rop+4(%esp)
58	jz	.ldiv2
59	notl	rop+4(%esp)	/ force denominator positive
60	negl	rop(%esp)
61	sbbl	$0xffffffff,rop+4(%esp)
62.ldiv2:
63/ end of compatibility code
64
65	xorl	%esi,%esi	/ initialize remainder to 0
66	movl	%esi,%edi
67	movl	$64,%ecx	/ initialize counter for 64-bits
68.div_mod_loop:
69	shll	$1,%edi
70	rcll	$1,%esi		/ remainder * 2
71	shll	$1,%eax
72	rcll	$1,%edx		/ numerator * 2 (also quotient)
73	adcl	$0,%edi		/ add in any carry from the shift
74	subl	rop(%esp),%edi	/ subtract denominator from remainder
75	sbbl	rop+4(%esp),%esi
76	incl	%eax		/ turn on quotient bit for now
77	jnc	.inc_remainder	/ inc didn't affect carry flag
78/ can't subtract the denominator from the remainder, add it back
79	addl	rop(%esp),%edi
80	adcl	rop+4(%esp),%esi
81	decl	%eax		/ turn quotient bit off
82.inc_remainder:
83	loop	.div_mod_loop
84
85/ at this point, %edx:%eax has the quotient and %edi:%esi has the remainder
86	popl	%edi
87	popl	%esi
88	movl	%eax,%ecx
89	popl	%eax
90	movl	%ecx,ans(%eax)
91	movl	%edx,ans+4(%eax)
92	ret
93	SET_SIZE(ldivide)
94