xref: /netbsd-src/external/lgpl3/gmp/dist/mpn/arm/udiv.asm (revision d909946ca08dceb44d7d0f22ec9488679695d976)
1dnl  ARM mpn_udiv_qrnnd -- divide a two limb dividend and a one limb divisor.
2dnl  Return quotient and store remainder through a supplied pointer.
3
4dnl  Copyright 2001, 2012 Free Software Foundation, Inc.
5
6dnl  This file is part of the GNU MP Library.
7
8dnl  The GNU MP Library is free software; you can redistribute it and/or modify
9dnl  it under the terms of the GNU Lesser General Public License as published
10dnl  by the Free Software Foundation; either version 3 of the License, or (at
11dnl  your option) any later version.
12
13dnl  The GNU MP Library is distributed in the hope that it will be useful, but
14dnl  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15dnl  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16dnl  License for more details.
17
18dnl  You should have received a copy of the GNU Lesser General Public License
19dnl  along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.
20
21include(`../config.m4')
22
23C INPUT PARAMETERS
24define(`rem_ptr',`r0')
25define(`n1',`r1')
26define(`n0',`r2')
27define(`d',`r3')
28
29C divstep -- develop one quotient bit.  Dividend in $1$2, divisor in $3.
30C Quotient bit is shifted into $2.
31define(`divstep',
32       `adcs	$2, $2, $2
33	adc	$1, $1, $1
34	cmp	$1, $3
35	subcs	$1, $1, $3')
36
37ASM_START()
38PROLOGUE(mpn_udiv_qrnnd)
39	mov	r12, #8			C loop counter for both loops below
40	cmp	d, #0x80000000		C check divisor msb and clear carry
41	bcs	L(_large_divisor)
42
43L(oop):	divstep(n1,n0,d)
44	divstep(n1,n0,d)
45	divstep(n1,n0,d)
46	divstep(n1,n0,d)
47	sub	r12, r12, #1
48	teq	r12, #0
49	bne	L(oop)
50
51	str	n1, [rem_ptr]		C store remainder
52	adc	r0, n0, n0		C quotient: add last carry from divstep
53ifdef(`ARM_THUMB_MODE',
54`	bx	lr
55',`	mov	pc, lr
56')
57
58L(_large_divisor):
59	stmfd	sp!, { r8, lr }
60
61	and	r8, n0, #1		C save lsb of dividend
62	mov	lr, n1, lsl #31
63	orrs	n0, lr, n0, lsr #1	C n0 = lo(n1n0 >> 1)
64	mov	n1, n1, lsr #1		C n1 = hi(n1n0 >> 1)
65
66	and	lr, d, #1		C save lsb of divisor
67	movs	d, d, lsr #1		C d = floor(orig_d / 2)
68	adc	d, d, #0		C d = ceil(orig_d / 2)
69
70L(oop2):
71	divstep(n1,n0,d)
72	divstep(n1,n0,d)
73	divstep(n1,n0,d)
74	divstep(n1,n0,d)
75	sub	r12, r12, #1
76	teq	r12, #0
77	bne	L(oop2)
78
79	adc	n0, n0, n0		C shift and add last carry from divstep
80	add	n1, r8, n1, lsl #1	C shift in omitted dividend lsb
81	tst	lr, lr			C test saved divisor lsb
82	beq	L(_even_divisor)
83
84	rsb	d, lr, d, lsl #1	C restore orig d value
85	adds	n1, n1, n0		C fix remainder for omitted divisor lsb
86	addcs	n0, n0, #1		C adjust quotient if rem. fix carried
87	subcs	n1, n1, d		C adjust remainder accordingly
88	cmp	n1, d			C remainder >= divisor?
89	subcs	n1, n1, d		C adjust remainder
90	addcs	n0, n0, #1		C adjust quotient
91
92L(_even_divisor):
93	str	n1, [rem_ptr]		C store remainder
94	mov	r0, n0			C quotient
95	ldmfd	sp!, { r8, pc }
96EPILOGUE(mpn_udiv_qrnnd)
97