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