lib1funcs.S 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. @ libgcc1 routines for ARM cpu.
  2. @ Division routines, written by Richard Earnshaw, (rearnsha@armltd.co.uk)
  3. /* Copyright (C) 1995, 1996, 1998 Free Software Foundation, Inc.
  4. This file is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any
  7. later version.
  8. In addition to the permissions in the GNU General Public License, the
  9. Free Software Foundation gives you unlimited permission to link the
  10. compiled version of this file with other programs, and to distribute
  11. those programs without any restriction coming from the use of this
  12. file. (The General Public License restrictions do apply in other
  13. respects; for example, they cover modification of the file, and
  14. distribution when not linked into another program.)
  15. This file is distributed in the hope that it will be useful, but
  16. WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. General Public License for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with this program; see the file COPYING. If not, write to
  21. the Free Software Foundation, 59 Temple Place - Suite 330,
  22. Boston, MA 02111-1307, USA. */
  23. /* As a special exception, if you link this library with other files,
  24. some of which are compiled with GCC, to produce an executable,
  25. this library does not by itself cause the resulting executable
  26. to be covered by the GNU General Public License.
  27. This exception does not however invalidate any other reasons why
  28. the executable file might be covered by the GNU General Public License.
  29. */
  30. /* This code is derived from gcc 2.95.3 */
  31. /* I Molton 29/07/01 */
  32. #include <linux/linkage.h>
  33. #include <asm/assembler.h>
  34. #include <asm/hardware.h>
  35. #define RET movs
  36. #define RETc(x) mov##x##s
  37. #define RETCOND ^
  38. dividend .req r0
  39. divisor .req r1
  40. result .req r2
  41. overdone .req r2
  42. curbit .req r3
  43. ip .req r12
  44. sp .req r13
  45. lr .req r14
  46. pc .req r15
  47. ENTRY(__udivsi3)
  48. cmp divisor, #0
  49. beq Ldiv0
  50. mov curbit, #1
  51. mov result, #0
  52. cmp dividend, divisor
  53. bcc Lgot_result_udivsi3
  54. 1:
  55. @ Unless the divisor is very big, shift it up in multiples of
  56. @ four bits, since this is the amount of unwinding in the main
  57. @ division loop. Continue shifting until the divisor is
  58. @ larger than the dividend.
  59. cmp divisor, #0x10000000
  60. cmpcc divisor, dividend
  61. movcc divisor, divisor, lsl #4
  62. movcc curbit, curbit, lsl #4
  63. bcc 1b
  64. 2:
  65. @ For very big divisors, we must shift it a bit at a time, or
  66. @ we will be in danger of overflowing.
  67. cmp divisor, #0x80000000
  68. cmpcc divisor, dividend
  69. movcc divisor, divisor, lsl #1
  70. movcc curbit, curbit, lsl #1
  71. bcc 2b
  72. 3:
  73. @ Test for possible subtractions, and note which bits
  74. @ are done in the result. On the final pass, this may subtract
  75. @ too much from the dividend, but the result will be ok, since the
  76. @ "bit" will have been shifted out at the bottom.
  77. cmp dividend, divisor
  78. subcs dividend, dividend, divisor
  79. orrcs result, result, curbit
  80. cmp dividend, divisor, lsr #1
  81. subcs dividend, dividend, divisor, lsr #1
  82. orrcs result, result, curbit, lsr #1
  83. cmp dividend, divisor, lsr #2
  84. subcs dividend, dividend, divisor, lsr #2
  85. orrcs result, result, curbit, lsr #2
  86. cmp dividend, divisor, lsr #3
  87. subcs dividend, dividend, divisor, lsr #3
  88. orrcs result, result, curbit, lsr #3
  89. cmp dividend, #0 @ Early termination?
  90. movnes curbit, curbit, lsr #4 @ No, any more bits to do?
  91. movne divisor, divisor, lsr #4
  92. bne 3b
  93. Lgot_result_udivsi3:
  94. mov r0, result
  95. RET pc, lr
  96. Ldiv0:
  97. str lr, [sp, #-4]!
  98. bl __div0
  99. mov r0, #0 @ about as wrong as it could be
  100. ldmia sp!, {pc}RETCOND
  101. /* __umodsi3 ----------------------- */
  102. ENTRY(__umodsi3)
  103. cmp divisor, #0
  104. beq Ldiv0
  105. mov curbit, #1
  106. cmp dividend, divisor
  107. RETc(cc) pc, lr
  108. 1:
  109. @ Unless the divisor is very big, shift it up in multiples of
  110. @ four bits, since this is the amount of unwinding in the main
  111. @ division loop. Continue shifting until the divisor is
  112. @ larger than the dividend.
  113. cmp divisor, #0x10000000
  114. cmpcc divisor, dividend
  115. movcc divisor, divisor, lsl #4
  116. movcc curbit, curbit, lsl #4
  117. bcc 1b
  118. 2:
  119. @ For very big divisors, we must shift it a bit at a time, or
  120. @ we will be in danger of overflowing.
  121. cmp divisor, #0x80000000
  122. cmpcc divisor, dividend
  123. movcc divisor, divisor, lsl #1
  124. movcc curbit, curbit, lsl #1
  125. bcc 2b
  126. 3:
  127. @ Test for possible subtractions. On the final pass, this may
  128. @ subtract too much from the dividend, so keep track of which
  129. @ subtractions are done, we can fix them up afterwards...
  130. mov overdone, #0
  131. cmp dividend, divisor
  132. subcs dividend, dividend, divisor
  133. cmp dividend, divisor, lsr #1
  134. subcs dividend, dividend, divisor, lsr #1
  135. orrcs overdone, overdone, curbit, ror #1
  136. cmp dividend, divisor, lsr #2
  137. subcs dividend, dividend, divisor, lsr #2
  138. orrcs overdone, overdone, curbit, ror #2
  139. cmp dividend, divisor, lsr #3
  140. subcs dividend, dividend, divisor, lsr #3
  141. orrcs overdone, overdone, curbit, ror #3
  142. mov ip, curbit
  143. cmp dividend, #0 @ Early termination?
  144. movnes curbit, curbit, lsr #4 @ No, any more bits to do?
  145. movne divisor, divisor, lsr #4
  146. bne 3b
  147. @ Any subtractions that we should not have done will be recorded in
  148. @ the top three bits of "overdone". Exactly which were not needed
  149. @ are governed by the position of the bit, stored in ip.
  150. @ If we terminated early, because dividend became zero,
  151. @ then none of the below will match, since the bit in ip will not be
  152. @ in the bottom nibble.
  153. ands overdone, overdone, #0xe0000000
  154. RETc(eq) pc, lr @ No fixups needed
  155. tst overdone, ip, ror #3
  156. addne dividend, dividend, divisor, lsr #3
  157. tst overdone, ip, ror #2
  158. addne dividend, dividend, divisor, lsr #2
  159. tst overdone, ip, ror #1
  160. addne dividend, dividend, divisor, lsr #1
  161. RET pc, lr
  162. ENTRY(__divsi3)
  163. eor ip, dividend, divisor @ Save the sign of the result.
  164. mov curbit, #1
  165. mov result, #0
  166. cmp divisor, #0
  167. rsbmi divisor, divisor, #0 @ Loops below use unsigned.
  168. beq Ldiv0
  169. cmp dividend, #0
  170. rsbmi dividend, dividend, #0
  171. cmp dividend, divisor
  172. bcc Lgot_result_divsi3
  173. 1:
  174. @ Unless the divisor is very big, shift it up in multiples of
  175. @ four bits, since this is the amount of unwinding in the main
  176. @ division loop. Continue shifting until the divisor is
  177. @ larger than the dividend.
  178. cmp divisor, #0x10000000
  179. cmpcc divisor, dividend
  180. movcc divisor, divisor, lsl #4
  181. movcc curbit, curbit, lsl #4
  182. bcc 1b
  183. 2:
  184. @ For very big divisors, we must shift it a bit at a time, or
  185. @ we will be in danger of overflowing.
  186. cmp divisor, #0x80000000
  187. cmpcc divisor, dividend
  188. movcc divisor, divisor, lsl #1
  189. movcc curbit, curbit, lsl #1
  190. bcc 2b
  191. 3:
  192. @ Test for possible subtractions, and note which bits
  193. @ are done in the result. On the final pass, this may subtract
  194. @ too much from the dividend, but the result will be ok, since the
  195. @ "bit" will have been shifted out at the bottom.
  196. cmp dividend, divisor
  197. subcs dividend, dividend, divisor
  198. orrcs result, result, curbit
  199. cmp dividend, divisor, lsr #1
  200. subcs dividend, dividend, divisor, lsr #1
  201. orrcs result, result, curbit, lsr #1
  202. cmp dividend, divisor, lsr #2
  203. subcs dividend, dividend, divisor, lsr #2
  204. orrcs result, result, curbit, lsr #2
  205. cmp dividend, divisor, lsr #3
  206. subcs dividend, dividend, divisor, lsr #3
  207. orrcs result, result, curbit, lsr #3
  208. cmp dividend, #0 @ Early termination?
  209. movnes curbit, curbit, lsr #4 @ No, any more bits to do?
  210. movne divisor, divisor, lsr #4
  211. bne 3b
  212. Lgot_result_divsi3:
  213. mov r0, result
  214. cmp ip, #0
  215. rsbmi r0, r0, #0
  216. RET pc, lr
  217. ENTRY(__modsi3)
  218. mov curbit, #1
  219. cmp divisor, #0
  220. rsbmi divisor, divisor, #0 @ Loops below use unsigned.
  221. beq Ldiv0
  222. @ Need to save the sign of the dividend, unfortunately, we need
  223. @ ip later on; this is faster than pushing lr and using that.
  224. str dividend, [sp, #-4]!
  225. cmp dividend, #0
  226. rsbmi dividend, dividend, #0
  227. cmp dividend, divisor
  228. bcc Lgot_result_modsi3
  229. 1:
  230. @ Unless the divisor is very big, shift it up in multiples of
  231. @ four bits, since this is the amount of unwinding in the main
  232. @ division loop. Continue shifting until the divisor is
  233. @ larger than the dividend.
  234. cmp divisor, #0x10000000
  235. cmpcc divisor, dividend
  236. movcc divisor, divisor, lsl #4
  237. movcc curbit, curbit, lsl #4
  238. bcc 1b
  239. 2:
  240. @ For very big divisors, we must shift it a bit at a time, or
  241. @ we will be in danger of overflowing.
  242. cmp divisor, #0x80000000
  243. cmpcc divisor, dividend
  244. movcc divisor, divisor, lsl #1
  245. movcc curbit, curbit, lsl #1
  246. bcc 2b
  247. 3:
  248. @ Test for possible subtractions. On the final pass, this may
  249. @ subtract too much from the dividend, so keep track of which
  250. @ subtractions are done, we can fix them up afterwards...
  251. mov overdone, #0
  252. cmp dividend, divisor
  253. subcs dividend, dividend, divisor
  254. cmp dividend, divisor, lsr #1
  255. subcs dividend, dividend, divisor, lsr #1
  256. orrcs overdone, overdone, curbit, ror #1
  257. cmp dividend, divisor, lsr #2
  258. subcs dividend, dividend, divisor, lsr #2
  259. orrcs overdone, overdone, curbit, ror #2
  260. cmp dividend, divisor, lsr #3
  261. subcs dividend, dividend, divisor, lsr #3
  262. orrcs overdone, overdone, curbit, ror #3
  263. mov ip, curbit
  264. cmp dividend, #0 @ Early termination?
  265. movnes curbit, curbit, lsr #4 @ No, any more bits to do?
  266. movne divisor, divisor, lsr #4
  267. bne 3b
  268. @ Any subtractions that we should not have done will be recorded in
  269. @ the top three bits of "overdone". Exactly which were not needed
  270. @ are governed by the position of the bit, stored in ip.
  271. @ If we terminated early, because dividend became zero,
  272. @ then none of the below will match, since the bit in ip will not be
  273. @ in the bottom nibble.
  274. ands overdone, overdone, #0xe0000000
  275. beq Lgot_result_modsi3
  276. tst overdone, ip, ror #3
  277. addne dividend, dividend, divisor, lsr #3
  278. tst overdone, ip, ror #2
  279. addne dividend, dividend, divisor, lsr #2
  280. tst overdone, ip, ror #1
  281. addne dividend, dividend, divisor, lsr #1
  282. Lgot_result_modsi3:
  283. ldr ip, [sp], #4
  284. cmp ip, #0
  285. rsbmi dividend, dividend, #0
  286. RET pc, lr