__aeabi_ldivmod.S revision 1.1.4.2 1 /*-
2 * Copyright (c) 2012 The NetBSD Foundation, Inc.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Matt Thomas of 3am Software Foundry.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include <machine/asm.h>
31
32 RCSID("$NetBSD: __aeabi_ldivmod.S,v 1.1.4.2 2012/10/30 18:46:13 yamt Exp $")
33
34 ENTRY(__aeabi_ldivmod)
35 push {r4-r5, sl, lr}
36 #define NEG r5
37 mov NEG, #0
38
39 #ifdef __ARMEB__
40 #define ALO r1 /* incoming numerator, outgoing quotient */
41 #define AHI r0 /* incoming numerator, outgoing quotient */
42 #define BLO r3 /* incoming denominator, outgoing remainder */
43 #define BHI r2 /* incoming denominator, outgoing remainder */
44 #else
45 #define ALO r0 /* incoming numerator, outgoing quotient */
46 #define AHI r1 /* incoming numerator, outgoing quotient */
47 #define BLO r2 /* incoming denominator, outgoing remainder */
48 #define BHI r3 /* incoming denominator, outgoing remainder */
49 #endif
50
51 cmp BHI, #0
52 bge 2f
53 eor NEG, NEG, #1 /* flip quotient sign */
54 bl .Lnegate_b
55 bcs .Lmaxdenom
56
57 2:
58 cmp AHI, #0
59 /* bge 3f */
60 eorlt NEG, NEG, #3 /* flip quotient sign, flip remainder sign */
61 bllt .Lnegate_a
62 3:
63 /*
64 * Arguments are setup, allocate some stack for the remainder
65 * and call __qdivrem for the heavy lifting.
66 */
67 sub sp, sp, #8
68 mov r4, sp /* pointer to remainder */
69 bl PLT_SYM(__qdivrem)
70
71 teq NEG, #0 /* any signs to flip? */
72 /*
73 * The quotient is already in the right place and neither value
74 * needs its sign flipped.
75 */
76 popeq {r2-r5, sl, lr}
77 RETc(eq)
78
79 pop {r2, r3}
80 tst NEG, #2 /* does remainder need to be negative? */
81 bleq .Lnegate_b
82 tst NEG, #1 /* does quotient need to be negative? */
83 bleq .Lnegate_a
84 pop {r4-r5, sl, lr}
85 RET
86
87 .Lnegate_a:
88 rsbs ALO, ALO, #0
89 rsc AHI, AHI, #0
90 RET
91
92 .Lnegate_b:
93 rsbs BLO, BLO, #0
94 rsc BHI, BHI, #0
95 RET
96
97 .Lmaxdenom:
98 /*
99 * We had a carry so the denominator must have INT64_MIN
100 * Also BLO and BHI never changed values so we can use
101 * them to see if the numerator has the same value. We
102 * don't have to worry about sign.
103 */
104 teq BHI, AHI
105 teqeq BLO, ALO
106 bne 1f
107
108 /*
109 * They were equal, so we return a quotient of 1 and remainder of 0.
110 */
111 mov ALO, #1
112 mov AHI, #0
113 mov BLO, #0
114 mov BHI, #0
115 pop {r4-r5, sl, lr}
116 RET
117
118 /*
119 * Our remainder must be the numerator and our quotient is 0.
120 */
121 1: mov BLO, ALO
122 mov BHI, AHI
123 mov ALO, #0
124 mov AHI, #0
125 pop {r4-r5, sl, lr}
126 RET
127
128 END(__aeabi_ldivmod)
129