__aeabi_ldivmod.S revision 1.1.2.1 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.2.1 2013/06/23 06:26:13 tls Exp $")
33
34 ENTRY(__aeabi_ldivmod)
35 #if !defined(_KERNEL) && !defined(_STANDALONE)
36 orrs ip, r2, r3
37 beq .Ldivbyzero
38 #endif
39
40 push {r4-r5, sl, lr}
41 #define NEG r5
42 mov NEG, #0
43
44 #ifdef __ARMEB__
45 #define ALO r1 /* incoming numerator, outgoing quotient */
46 #define AHI r0 /* incoming numerator, outgoing quotient */
47 #define BLO r3 /* incoming denominator, outgoing remainder */
48 #define BHI r2 /* incoming denominator, outgoing remainder */
49 #else
50 #define ALO r0 /* incoming numerator, outgoing quotient */
51 #define AHI r1 /* incoming numerator, outgoing quotient */
52 #define BLO r2 /* incoming denominator, outgoing remainder */
53 #define BHI r3 /* incoming denominator, outgoing remainder */
54 #endif
55
56 cmp BHI, #0
57 bge 2f
58 eor NEG, NEG, #1 /* flip quotient sign */
59 bl .Lnegate_b
60 bcs .Lmaxdenom
61
62 2:
63 cmp AHI, #0
64 /* bge 3f */
65 eorlt NEG, NEG, #3 /* flip quotient sign, flip remainder sign */
66 bllt .Lnegate_a
67 3:
68 /*
69 * Arguments are setup, allocate some stack for the remainder
70 * and call __qdivrem for the heavy lifting.
71 */
72 sub sp, sp, #16
73 add ip, sp, #8
74 str ip, [sp]
75 bl PLT_SYM(__qdivrem)
76 add sp, sp, #8
77
78 teq NEG, #0 /* any signs to flip? */
79 /*
80 * The quotient is already in the right place and neither value
81 * needs its sign flipped.
82 */
83 popeq {r2-r5, sl, lr}
84 RETc(eq)
85
86 pop {r2, r3}
87 tst NEG, #2 /* does remainder need to be negative? */
88 blne .Lnegate_b
89 tst NEG, #1 /* does quotient need to be negative? */
90 blne .Lnegate_a
91 pop {r4-r5, sl, lr}
92 RET
93
94 .Lnegate_a:
95 rsbs ALO, ALO, #0
96 rsc AHI, AHI, #0
97 RET
98
99 .Lnegate_b:
100 rsbs BLO, BLO, #0
101 rsc BHI, BHI, #0
102 RET
103
104 .Lmaxdenom:
105 /*
106 * We had a carry so the denominator must have INT64_MIN
107 * Also BLO and BHI never changed values so we can use
108 * them to see if the numerator has the same value. We
109 * don't have to worry about sign.
110 */
111 teq BHI, AHI
112 teqeq BLO, ALO
113 bne 1f
114
115 /*
116 * They were equal, so we return a quotient of 1 and remainder of 0.
117 */
118 mov ALO, #1
119 mov AHI, #0
120 mov BLO, #0
121 mov BHI, #0
122 pop {r4-r5, sl, lr}
123 RET
124
125 /*
126 * Our remainder must be the numerator and our quotient is 0.
127 */
128 1: mov BLO, ALO
129 mov BHI, AHI
130 mov ALO, #0
131 mov AHI, #0
132 pop {r4-r5, sl, lr}
133 RET
134
135 #if !defined(_KERNEL) && !defined(_STANDALONE)
136 .Ldivbyzero:
137 push {r0-r1, ip, lr}
138 cmp AHI, #0
139 mvnge ALO, #0
140 movge AHI, ALO, lsr #1
141 movlt ALO, #0
142 andlt AHI, AHI, #0x800000000
143 bl PLT_SYM(__aeabi_ldiv0)
144 pop {r2-r3, ip, lr}
145 RET
146 #endif
147 END(__aeabi_ldivmod)
148