divsi3.S 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include <linux/linkage.h>
  2. /*
  3. * Divide operation for 32 bit integers.
  4. * Input : Dividend in Reg r5
  5. * Divisor in Reg r6
  6. * Output: Result in Reg r3
  7. */
  8. .text
  9. .globl __divsi3
  10. .type __divsi3, @function
  11. .ent __divsi3
  12. __divsi3:
  13. .frame r1, 0, r15
  14. addik r1, r1, -16
  15. swi r28, r1, 0
  16. swi r29, r1, 4
  17. swi r30, r1, 8
  18. swi r31, r1, 12
  19. beqi r6, div_by_zero /* div_by_zero - division error */
  20. beqi r5, result_is_zero /* result is zero */
  21. bgeid r5, r5_pos
  22. xor r28, r5, r6 /* get the sign of the result */
  23. rsubi r5, r5, 0 /* make r5 positive */
  24. r5_pos:
  25. bgei r6, r6_pos
  26. rsubi r6, r6, 0 /* make r6 positive */
  27. r6_pos:
  28. addik r30, r0, 0 /* clear mod */
  29. addik r3, r0, 0 /* clear div */
  30. addik r29, r0, 32 /* initialize the loop count */
  31. /* first part try to find the first '1' in the r5 */
  32. div0:
  33. blti r5, div2 /* this traps r5 == 0x80000000 */
  34. div1:
  35. add r5, r5, r5 /* left shift logical r5 */
  36. bgtid r5, div1
  37. addik r29, r29, -1
  38. div2:
  39. /* left shift logical r5 get the '1' into the carry */
  40. add r5, r5, r5
  41. addc r30, r30, r30 /* move that bit into the mod register */
  42. rsub r31, r6, r30 /* try to subtract (r30 a r6) */
  43. blti r31, mod_too_small
  44. /* move the r31 to mod since the result was positive */
  45. or r30, r0, r31
  46. addik r3, r3, 1
  47. mod_too_small:
  48. addik r29, r29, -1
  49. beqi r29, loop_end
  50. add r3, r3, r3 /* shift in the '1' into div */
  51. bri div2 /* div2 */
  52. loop_end:
  53. bgei r28, return_here
  54. brid return_here
  55. rsubi r3, r3, 0 /* negate the result */
  56. div_by_zero:
  57. result_is_zero:
  58. or r3, r0, r0 /* set result to 0 */
  59. return_here:
  60. /* restore values of csrs and that of r3 and the divisor and the dividend */
  61. lwi r28, r1, 0
  62. lwi r29, r1, 4
  63. lwi r30, r1, 8
  64. lwi r31, r1, 12
  65. rtsd r15, 8
  66. addik r1, r1, 16
  67. .size __divsi3, . - __divsi3
  68. .end __divsi3