1 ! SPDX-License-Identifier: GPL-2.0
2 ! Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
3 ! Imagination Technologies Ltd
5 ! Integer divide routines.
10 .type ___udivsi3,function
14 !! Since core is signed divide case, just set control variable
16 MOV D1Re0,D0Ar2 ! Au already in A1Ar1, Bu -> D1Re0
17 MOV D0Re0,#0 ! Result is 0
18 MOV D0Ar4,#0 ! Return positive result
20 .size ___udivsi3,.-___udivsi3
23 !! 32-bit division signed i/p - passed signed 32-bit numbers
26 .type ___divsi3,function
30 !! A already in D1Ar1, B already in D0Ar2 -> make B abs(B)
32 MOV D1Re0,D0Ar2 ! A already in A1Ar1, B -> D1Re0
33 MOV D0Re0,#0 ! Result is 0
34 XOR D0Ar4,D1Ar1,D1Re0 ! D0Ar4 -ive if result is -ive
35 ABS D1Ar1,D1Ar1 ! abs(A) -> Au
36 ABS D1Re0,D1Re0 ! abs(B) -> Bu
38 CMP D1Ar1,D1Re0 ! Is ( Au > Bu )?
39 LSR D1Ar3,D1Ar1,#2 ! Calculate (Au & (~3)) >> 2
40 CMPHI D1Re0,D1Ar3 ! OR ( (Au & (~3)) <= (Bu << 2) )?
41 LSLSHI D1Ar3,D1Re0,#1 ! Buq = Bu << 1
42 BLS $LIDMCUSetup ! Yes: Do normal divide
44 !! Quick divide setup can assume that CurBit only needs to start at 2
47 CMP D1Ar1,D1Ar3 ! ( A >= Buq )?
48 ADDCC D0Re0,D0Re0,#2 ! If yes result += 2
49 SUBCC D1Ar1,D1Ar1,D1Ar3 ! and A -= Buq
50 CMP D1Ar1,D1Re0 ! ( A >= Bu )?
51 ADDCC D0Re0,D0Re0,#1 ! If yes result += 1
52 SUBCC D1Ar1,D1Ar1,D1Re0 ! and A -= Bu
53 ORS D0Ar4,D0Ar4,D0Ar4 ! Return neg result?
54 NEG D0Ar2,D0Re0 ! Calculate neg result
55 MOVMI D0Re0,D0Ar2 ! Yes: Take neg result
59 !! Setup for general unsigned divide code
61 !! D0Re0 is used to form the result, already set to Zero
62 !! D1Re0 is the input Bu value, this gets trashed
63 !! D0Ar6 is curbit which is set to 1 at the start and shifted up
64 !! D0Ar4 is negative if we should return a negative result
65 !! D1Ar1 is the input Au value, eventually this holds the remainder
68 CMP D1Ar1,D1Re0 ! Is ( Au < Bu )?
69 MOV D0Ar6,#1 ! Set curbit to 1
70 BCS $LIDMCRet ! Yes: Return 0 remainder Au
72 !! Calculate alignment using FFB instruction
74 FFB D1Ar5,D1Ar1 ! Find first bit of Au
75 ANDN D1Ar5,D1Ar5,#31 ! Handle exceptional case.
76 ORN D1Ar5,D1Ar5,#31 ! if N bit set, set to 31
77 FFB D1Ar3,D1Re0 ! Find first bit of Bu
78 ANDN D1Ar3,D1Ar3,#31 ! Handle exceptional case.
79 ORN D1Ar3,D1Ar3,#31 ! if N bit set, set to 31
80 SUBS D1Ar3,D1Ar5,D1Ar3 ! calculate diff, ffbA - ffbB
81 MOV D0Ar2,D1Ar3 ! copy into bank 0
82 LSLGT D1Re0,D1Re0,D1Ar3 ! ( > 0) ? left shift B
83 LSLGT D0Ar6,D0Ar6,D0Ar2 ! ( > 0) ? left shift curbit
85 !! Now we start the divide proper, logic is
87 !! if ( A >= B ) add curbit to result and subtract B from A
88 !! shift curbit and B down by 1 in either case
91 CMP D1Ar1, D1Re0 ! ( A >= B )?
92 ADDCC D0Re0, D0Re0, D0Ar6 ! If yes result += curbit
93 SUBCC D1Ar1, D1Ar1, D1Re0 ! and A -= B
94 LSRS D0Ar6, D0Ar6, #1 ! Shift down curbit, is it zero?
95 LSR D1Re0, D1Re0, #1 ! Shift down B
96 BNZ $LIDMCLoop ! Was single bit in curbit lost?
97 ORS D0Ar4,D0Ar4,D0Ar4 ! Return neg result?
98 NEG D0Ar2,D0Re0 ! Calculate neg result
99 MOVMI D0Re0,D0Ar2 ! Yes: Take neg result
101 .size ___divsi3,.-___divsi3