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