divide.inc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. {
  2. Divide/modulo for Acorn RISC Machine
  3. ... taken from a GP2X Ogg Tremor port by Dzz and converted to
  4. Pascal.
  5. Copyright (c) 2007, Daniel Mantione
  6. Copyright (c) 2006, Dzz
  7. Copyright (c) 2002, Xiph.org Foundation
  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. - Redistributions of source code must retain the above copyright
  12. notice, this list of conditions and the following disclaimer.
  13. - 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. - Neither the name of the Xiph.org Foundation nor the names of its
  17. contributors may be used to endorse or promote products derived from
  18. this software without specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION
  23. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. }
  31. {$ifndef FPC_SYSTEM_HAS_DIV_DWORD}
  32. {$define FPC_SYSTEM_HAS_DIV_DWORD}
  33. function fpc_div_dword(n,z:dword):dword;[public,alias: 'FPC_DIV_DWORD'];assembler;nostackframe;
  34. asm
  35. {$if defined(CPUCORTEXM3) or defined(CPUARMV7M)}
  36. {$ifdef CPUCORTEXM3}
  37. udiv r0, r1, r0
  38. {$else}
  39. mov r3, #0
  40. rsbs r2, r0, r1, LSR#3
  41. bcc .Ldiv_3bits
  42. rsbs r2, r0, r1, LSR#8
  43. bcc .Ldiv_8bits
  44. mov r0, r0, LSL#8
  45. orr r3, r3, #0xFF000000
  46. rsbs r2, r0, r1, LSR#4
  47. bcc .Ldiv_4bits
  48. rsbs r2, r0, r1, LSR#8
  49. bcc .Ldiv_8bits
  50. mov r0, r0, LSL#8
  51. orr r3, r3, #0x00FF0000
  52. rsbs r2, r0, r1, LSR#8
  53. itt cs
  54. movcs r0, r0, LSL#8
  55. orrcs r3, r3, #0x0000FF00
  56. rsbs r2, r0, r1, LSR#4
  57. bcc .Ldiv_4bits
  58. rsbs r2, r0, #0
  59. bcs .Ldiv_by_0
  60. .Ldiv_loop:
  61. it cs
  62. movcs r0, r0, LSR#8
  63. .Ldiv_8bits:
  64. rsbs r2, r0, r1, LSR#7
  65. it cs
  66. subcs r1, r1, r0, LSL#7
  67. adc r3, r3, r3
  68. rsbs r2, r0, r1, LSR#6
  69. it cs
  70. subcs r1, r1, r0, LSL#6
  71. adc r3, r3, r3
  72. rsbs r2, r0, r1, LSR#5
  73. it cs
  74. subcs r1, r1, r0, LSL#5
  75. adc r3, r3, r3
  76. rsbs r2, r0, r1, LSR#4
  77. it cs
  78. subcs r1, r1, r0, LSL#4
  79. adc r3, r3, r3
  80. .Ldiv_4bits:
  81. rsbs r2, r0, r1, LSR#3
  82. it cs
  83. subcs r1, r1, r0, LSL#3
  84. adc r3, r3, r3
  85. .Ldiv_3bits:
  86. rsbs r2, r0, r1, LSR#2
  87. it cs
  88. subcs r1, r1, r0, LSL#2
  89. adc r3, r3, r3
  90. rsbs r2, r0, r1, LSR#1
  91. it cs
  92. subcs r1, r1, r0, LSL#1
  93. adc r3, r3, r3
  94. rsbs r2, r0, r1
  95. it cs
  96. subcs r1, r1, r0
  97. adcs r3, r3, r3
  98. .Ldiv_next:
  99. bcs .Ldiv_loop
  100. mov r0, r3
  101. mov pc, lr
  102. .Ldiv_by_0:
  103. mov r0, #200
  104. mov r1, r11
  105. bl handleerrorframe
  106. mov pc, lr
  107. {$endif}
  108. {$else}
  109. mov r3, #0
  110. rsbs r2, r0, r1, LSR#3
  111. bcc .Ldiv_3bits
  112. rsbs r2, r0, r1, LSR#8
  113. bcc .Ldiv_8bits
  114. mov r0, r0, LSL#8
  115. orr r3, r3, #0xFF000000
  116. rsbs r2, r0, r1, LSR#4
  117. bcc .Ldiv_4bits
  118. rsbs r2, r0, r1, LSR#8
  119. bcc .Ldiv_8bits
  120. mov r0, r0, LSL#8
  121. orr r3, r3, #0x00FF0000
  122. rsbs r2, r0, r1, LSR#8
  123. movcs r0, r0, LSL#8
  124. orrcs r3, r3, #0x0000FF00
  125. rsbs r2, r0, r1, LSR#4
  126. bcc .Ldiv_4bits
  127. rsbs r2, r0, #0
  128. bcs .Ldiv_by_0
  129. .Ldiv_loop:
  130. movcs r0, r0, LSR#8
  131. .Ldiv_8bits:
  132. rsbs r2, r0, r1, LSR#7
  133. subcs r1, r1, r0, LSL#7
  134. adc r3, r3, r3
  135. rsbs r2, r0, r1, LSR#6
  136. subcs r1, r1, r0, LSL#6
  137. adc r3, r3, r3
  138. rsbs r2, r0, r1, LSR#5
  139. subcs r1, r1, r0, LSL#5
  140. adc r3, r3, r3
  141. rsbs r2, r0, r1, LSR#4
  142. subcs r1, r1, r0, LSL#4
  143. adc r3, r3, r3
  144. .Ldiv_4bits:
  145. rsbs r2, r0, r1, LSR#3
  146. subcs r1, r1, r0, LSL#3
  147. adc r3, r3, r3
  148. .Ldiv_3bits:
  149. rsbs r2, r0, r1, LSR#2
  150. subcs r1, r1, r0, LSL#2
  151. adc r3, r3, r3
  152. rsbs r2, r0, r1, LSR#1
  153. subcs r1, r1, r0, LSL#1
  154. adc r3, r3, r3
  155. rsbs r2, r0, r1
  156. subcs r1, r1, r0
  157. adcs r3, r3, r3
  158. .Ldiv_next:
  159. bcs .Ldiv_loop
  160. mov r0, r3
  161. mov pc, lr
  162. .Ldiv_by_0:
  163. mov r0, #200
  164. mov r1, r11
  165. bl handleerrorframe
  166. mov pc, lr
  167. {$endif}
  168. end;
  169. {It is a compilerproc (systemh.inc), make an alias for internal use.}
  170. function fpc_div_dword(n,z:dword):dword;external name 'FPC_DIV_DWORD';
  171. {$endif}
  172. {$ifndef FPC_SYSTEM_HAS_DIV_LONGINT}
  173. {$define FPC_SYSTEM_HAS_DIV_LONGINT}
  174. function fpc_div_longint(n,z:longint):longint;[public,alias: 'FPC_DIV_LONGINT'];assembler;nostackframe;
  175. asm
  176. {$if defined(CPUCORTEXM3) or defined(CPUARMV7M)}
  177. {$ifdef CPUCORTEXM3}
  178. sdiv r0, r1, r0
  179. {$else}
  180. stmfd sp!, {lr}
  181. ands r12, r0, #1<<31 (* r12:=r0 and $80000000 *)
  182. it mi
  183. rsbmi r0, r0, #0 (* if signed(r0) then r0:=0-r0 *)
  184. eors r12, r12, r1, ASR#32 (* r12:=r12 xor (r1 asr 32) *)
  185. it cs
  186. rsbcs r1, r1, #0 (* if signed(r12) then r1:=0-r1 *)
  187. bl fpc_div_dword
  188. movs r12, r12, LSL#1 (* carry:=sign(r12) *)
  189. it cs
  190. rsbcs r0, r0, #0
  191. it mi
  192. rsbmi r1, r1, #0
  193. ldmfd sp!, {pc}
  194. {$endif}
  195. {$else}
  196. stmfd sp!, {lr}
  197. ands r12, r0, #1<<31 (* r12:=r0 and $80000000 *)
  198. rsbmi r0, r0, #0 (* if signed(r0) then r0:=0-r0 *)
  199. eors r12, r12, r1, ASR#32 (* r12:=r12 xor (r1 asr 32) *)
  200. rsbcs r1, r1, #0 (* if signed(r12) then r1:=0-r1 *)
  201. bl fpc_div_dword
  202. movs r12, r12, LSL#1 (* carry:=sign(r12) *)
  203. rsbcs r0, r0, #0
  204. rsbmi r1, r1, #0
  205. ldmfd sp!, {pc}
  206. {$endif}
  207. end;
  208. {It is a compilerproc (systemh.inc), make an alias for internal use.}
  209. function fpc_div_longint(n,z:longint):longint;external name 'FPC_DIV_LONGINT';
  210. {$endif}
  211. {$ifndef FPC_SYSTEM_HAS_MOD_DWORD}
  212. {$define FPC_SYSTEM_HAS_MOD_DWORD}
  213. function fpc_mod_dword(n,z:dword):dword;[public,alias: 'FPC_MOD_DWORD'];assembler;nostackframe;
  214. asm
  215. stmfd sp!, {lr}
  216. bl fpc_div_dword
  217. mov r0, r1
  218. ldmfd sp!, {pc}
  219. end;
  220. {It is a compilerproc (systemh.inc), make an alias for internal use.}
  221. function fpc_mod_dword(n,z:dword):dword;external name 'FPC_MOD_DWORD';
  222. {$endif}
  223. {$ifndef FPC_SYSTEM_HAS_MOD_LONGINT}
  224. {$define FPC_SYSTEM_HAS_MOD_LONGINT}
  225. function fpc_mod_longint(n,z:longint):longint;[public,alias: 'FPC_MOD_LONGINT'];assembler;nostackframe;
  226. asm
  227. stmfd sp!, {lr}
  228. bl fpc_div_longint
  229. mov r0, r1
  230. ldmfd sp!, {pc}
  231. end;
  232. {It is a compilerproc (systemh.inc), make an alias for internal use.}
  233. function fpc_mod_longint(n,z:longint):longint;external name 'FPC_MOD_LONGINT';
  234. {$endif}