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