lib1funcs.S 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. #include <linux/config.h>
  36. #define RET movs
  37. #define RETc(x) mov##x##s
  38. #define RETCOND ^
  39. dividend .req r0
  40. divisor .req r1
  41. result .req r2
  42. overdone .req r2
  43. curbit .req r3
  44. ip .req r12
  45. sp .req r13
  46. lr .req r14
  47. pc .req r15
  48. ENTRY(__udivsi3)
  49. cmp divisor, #0
  50. beq Ldiv0
  51. mov curbit, #1
  52. mov result, #0
  53. cmp dividend, divisor
  54. bcc Lgot_result_udivsi3
  55. 1:
  56. @ Unless the divisor is very big, shift it up in multiples of
  57. @ four bits, since this is the amount of unwinding in the main
  58. @ division loop. Continue shifting until the divisor is
  59. @ larger than the dividend.
  60. cmp divisor, #0x10000000
  61. cmpcc divisor, dividend
  62. movcc divisor, divisor, lsl #4
  63. movcc curbit, curbit, lsl #4
  64. bcc 1b
  65. 2:
  66. @ For very big divisors, we must shift it a bit at a time, or
  67. @ we will be in danger of overflowing.
  68. cmp divisor, #0x80000000
  69. cmpcc divisor, dividend
  70. movcc divisor, divisor, lsl #1
  71. movcc curbit, curbit, lsl #1
  72. bcc 2b
  73. 3:
  74. @ Test for possible subtractions, and note which bits
  75. @ are done in the result. On the final pass, this may subtract
  76. @ too much from the dividend, but the result will be ok, since the
  77. @ "bit" will have been shifted out at the bottom.
  78. cmp dividend, divisor
  79. subcs dividend, dividend, divisor
  80. orrcs result, result, curbit
  81. cmp dividend, divisor, lsr #1
  82. subcs dividend, dividend, divisor, lsr #1
  83. orrcs result, result, curbit, lsr #1
  84. cmp dividend, divisor, lsr #2
  85. subcs dividend, dividend, divisor, lsr #2
  86. orrcs result, result, curbit, lsr #2
  87. cmp dividend, divisor, lsr #3
  88. subcs dividend, dividend, divisor, lsr #3
  89. orrcs result, result, curbit, lsr #3
  90. cmp dividend, #0 @ Early termination?
  91. movnes curbit, curbit, lsr #4 @ No, any more bits to do?
  92. movne divisor, divisor, lsr #4
  93. bne 3b
  94. Lgot_result_udivsi3:
  95. mov r0, result
  96. RET pc, lr
  97. Ldiv0:
  98. str lr, [sp, #-4]!
  99. bl __div0
  100. mov r0, #0 @ about as wrong as it could be
  101. ldmia sp!, {pc}RETCOND
  102. /* __umodsi3 ----------------------- */
  103. ENTRY(__umodsi3)
  104. cmp divisor, #0
  105. beq Ldiv0
  106. mov curbit, #1
  107. cmp dividend, divisor
  108. RETc(cc) pc, lr
  109. 1:
  110. @ Unless the divisor is very big, shift it up in multiples of
  111. @ four bits, since this is the amount of unwinding in the main
  112. @ division loop. Continue shifting until the divisor is
  113. @ larger than the dividend.
  114. cmp divisor, #0x10000000
  115. cmpcc divisor, dividend
  116. movcc divisor, divisor, lsl #4
  117. movcc curbit, curbit, lsl #4
  118. bcc 1b
  119. 2:
  120. @ For very big divisors, we must shift it a bit at a time, or
  121. @ we will be in danger of overflowing.
  122. cmp divisor, #0x80000000
  123. cmpcc divisor, dividend
  124. movcc divisor, divisor, lsl #1
  125. movcc curbit, curbit, lsl #1
  126. bcc 2b
  127. 3:
  128. @ Test for possible subtractions. On the final pass, this may
  129. @ subtract too much from the dividend, so keep track of which
  130. @ subtractions are done, we can fix them up afterwards...
  131. mov overdone, #0
  132. cmp dividend, divisor
  133. subcs dividend, dividend, divisor
  134. cmp dividend, divisor, lsr #1
  135. subcs dividend, dividend, divisor, lsr #1
  136. orrcs overdone, overdone, curbit, ror #1
  137. cmp dividend, divisor, lsr #2
  138. subcs dividend, dividend, divisor, lsr #2
  139. orrcs overdone, overdone, curbit, ror #2
  140. cmp dividend, divisor, lsr #3
  141. subcs dividend, dividend, divisor, lsr #3
  142. orrcs overdone, overdone, curbit, ror #3
  143. mov ip, curbit
  144. cmp dividend, #0 @ Early termination?
  145. movnes curbit, curbit, lsr #4 @ No, any more bits to do?
  146. movne divisor, divisor, lsr #4
  147. bne 3b
  148. @ Any subtractions that we should not have done will be recorded in
  149. @ the top three bits of "overdone". Exactly which were not needed
  150. @ are governed by the position of the bit, stored in ip.
  151. @ If we terminated early, because dividend became zero,
  152. @ then none of the below will match, since the bit in ip will not be
  153. @ in the bottom nibble.
  154. ands overdone, overdone, #0xe0000000
  155. RETc(eq) pc, lr @ No fixups needed
  156. tst overdone, ip, ror #3
  157. addne dividend, dividend, divisor, lsr #3
  158. tst overdone, ip, ror #2
  159. addne dividend, dividend, divisor, lsr #2
  160. tst overdone, ip, ror #1
  161. addne dividend, dividend, divisor, lsr #1
  162. RET pc, lr
  163. ENTRY(__divsi3)
  164. eor ip, dividend, divisor @ Save the sign of the result.
  165. mov curbit, #1
  166. mov result, #0
  167. cmp divisor, #0
  168. rsbmi divisor, divisor, #0 @ Loops below use unsigned.
  169. beq Ldiv0
  170. cmp dividend, #0
  171. rsbmi dividend, dividend, #0
  172. cmp dividend, divisor
  173. bcc Lgot_result_divsi3
  174. 1:
  175. @ Unless the divisor is very big, shift it up in multiples of
  176. @ four bits, since this is the amount of unwinding in the main
  177. @ division loop. Continue shifting until the divisor is
  178. @ larger than the dividend.
  179. cmp divisor, #0x10000000
  180. cmpcc divisor, dividend
  181. movcc divisor, divisor, lsl #4
  182. movcc curbit, curbit, lsl #4
  183. bcc 1b
  184. 2:
  185. @ For very big divisors, we must shift it a bit at a time, or
  186. @ we will be in danger of overflowing.
  187. cmp divisor, #0x80000000
  188. cmpcc divisor, dividend
  189. movcc divisor, divisor, lsl #1
  190. movcc curbit, curbit, lsl #1
  191. bcc 2b
  192. 3:
  193. @ Test for possible subtractions, and note which bits
  194. @ are done in the result. On the final pass, this may subtract
  195. @ too much from the dividend, but the result will be ok, since the
  196. @ "bit" will have been shifted out at the bottom.
  197. cmp dividend, divisor
  198. subcs dividend, dividend, divisor
  199. orrcs result, result, curbit
  200. cmp dividend, divisor, lsr #1
  201. subcs dividend, dividend, divisor, lsr #1
  202. orrcs result, result, curbit, lsr #1
  203. cmp dividend, divisor, lsr #2
  204. subcs dividend, dividend, divisor, lsr #2
  205. orrcs result, result, curbit, lsr #2
  206. cmp dividend, divisor, lsr #3
  207. subcs dividend, dividend, divisor, lsr #3
  208. orrcs result, result, curbit, lsr #3
  209. cmp dividend, #0 @ Early termination?
  210. movnes curbit, curbit, lsr #4 @ No, any more bits to do?
  211. movne divisor, divisor, lsr #4
  212. bne 3b
  213. Lgot_result_divsi3:
  214. mov r0, result
  215. cmp ip, #0
  216. rsbmi r0, r0, #0
  217. RET pc, lr
  218. ENTRY(__modsi3)
  219. mov curbit, #1
  220. cmp divisor, #0
  221. rsbmi divisor, divisor, #0 @ Loops below use unsigned.
  222. beq Ldiv0
  223. @ Need to save the sign of the dividend, unfortunately, we need
  224. @ ip later on; this is faster than pushing lr and using that.
  225. str dividend, [sp, #-4]!
  226. cmp dividend, #0
  227. rsbmi dividend, dividend, #0
  228. cmp dividend, divisor
  229. bcc Lgot_result_modsi3
  230. 1:
  231. @ Unless the divisor is very big, shift it up in multiples of
  232. @ four bits, since this is the amount of unwinding in the main
  233. @ division loop. Continue shifting until the divisor is
  234. @ larger than the dividend.
  235. cmp divisor, #0x10000000
  236. cmpcc divisor, dividend
  237. movcc divisor, divisor, lsl #4
  238. movcc curbit, curbit, lsl #4
  239. bcc 1b
  240. 2:
  241. @ For very big divisors, we must shift it a bit at a time, or
  242. @ we will be in danger of overflowing.
  243. cmp divisor, #0x80000000
  244. cmpcc divisor, dividend
  245. movcc divisor, divisor, lsl #1
  246. movcc curbit, curbit, lsl #1
  247. bcc 2b
  248. 3:
  249. @ Test for possible subtractions. On the final pass, this may
  250. @ subtract too much from the dividend, so keep track of which
  251. @ subtractions are done, we can fix them up afterwards...
  252. mov overdone, #0
  253. cmp dividend, divisor
  254. subcs dividend, dividend, divisor
  255. cmp dividend, divisor, lsr #1
  256. subcs dividend, dividend, divisor, lsr #1
  257. orrcs overdone, overdone, curbit, ror #1
  258. cmp dividend, divisor, lsr #2
  259. subcs dividend, dividend, divisor, lsr #2
  260. orrcs overdone, overdone, curbit, ror #2
  261. cmp dividend, divisor, lsr #3
  262. subcs dividend, dividend, divisor, lsr #3
  263. orrcs overdone, overdone, curbit, ror #3
  264. mov ip, curbit
  265. cmp dividend, #0 @ Early termination?
  266. movnes curbit, curbit, lsr #4 @ No, any more bits to do?
  267. movne divisor, divisor, lsr #4
  268. bne 3b
  269. @ Any subtractions that we should not have done will be recorded in
  270. @ the top three bits of "overdone". Exactly which were not needed
  271. @ are governed by the position of the bit, stored in ip.
  272. @ If we terminated early, because dividend became zero,
  273. @ then none of the below will match, since the bit in ip will not be
  274. @ in the bottom nibble.
  275. ands overdone, overdone, #0xe0000000
  276. beq Lgot_result_modsi3
  277. tst overdone, ip, ror #3
  278. addne dividend, dividend, divisor, lsr #3
  279. tst overdone, ip, ror #2
  280. addne dividend, dividend, divisor, lsr #2
  281. tst overdone, ip, ror #1
  282. addne dividend, dividend, divisor, lsr #1
  283. Lgot_result_modsi3:
  284. ldr ip, [sp], #4
  285. cmp ip, #0
  286. rsbmi dividend, dividend, #0
  287. RET pc, lr