divremu.S 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. ;; SPDX-License-Identifier: GPL-2.0-or-later
  2. ;; Copyright 2011 Free Software Foundation, Inc.
  3. ;; Contributed by Bernd Schmidt <bernds@codesourcery.com>.
  4. ;;
  5. #include <linux/linkage.h>
  6. .text
  7. ENTRY(__c6xabi_divremu)
  8. ;; We use a series of up to 31 subc instructions. First, we find
  9. ;; out how many leading zero bits there are in the divisor. This
  10. ;; gives us both a shift count for aligning (shifting) the divisor
  11. ;; to the, and the number of times we have to execute subc.
  12. ;; At the end, we have both the remainder and most of the quotient
  13. ;; in A4. The top bit of the quotient is computed first and is
  14. ;; placed in A2.
  15. ;; Return immediately if the dividend is zero. Setting B4 to 1
  16. ;; is a trick to allow us to leave the following insns in the jump
  17. ;; delay slot without affecting the result.
  18. mv .s2x A4, B1
  19. [b1] lmbd .l2 1, B4, B1
  20. ||[!b1] b .s2 B3 ; RETURN A
  21. ||[!b1] mvk .d2 1, B4
  22. ||[!b1] zero .s1 A5
  23. mv .l1x B1, A6
  24. || shl .s2 B4, B1, B4
  25. ;; The loop performs a maximum of 28 steps, so we do the
  26. ;; first 3 here.
  27. cmpltu .l1x A4, B4, A2
  28. [!A2] sub .l1x A4, B4, A4
  29. || shru .s2 B4, 1, B4
  30. || xor .s1 1, A2, A2
  31. shl .s1 A2, 31, A2
  32. || [b1] subc .l1x A4,B4,A4
  33. || [b1] add .s2 -1, B1, B1
  34. [b1] subc .l1x A4,B4,A4
  35. || [b1] add .s2 -1, B1, B1
  36. ;; RETURN A may happen here (note: must happen before the next branch)
  37. __divremu0:
  38. cmpgt .l2 B1, 7, B0
  39. || [b1] subc .l1x A4,B4,A4
  40. || [b1] add .s2 -1, B1, B1
  41. [b1] subc .l1x A4,B4,A4
  42. || [b1] add .s2 -1, B1, B1
  43. || [b0] b .s1 __divremu0
  44. [b1] subc .l1x A4,B4,A4
  45. || [b1] add .s2 -1, B1, B1
  46. [b1] subc .l1x A4,B4,A4
  47. || [b1] add .s2 -1, B1, B1
  48. [b1] subc .l1x A4,B4,A4
  49. || [b1] add .s2 -1, B1, B1
  50. [b1] subc .l1x A4,B4,A4
  51. || [b1] add .s2 -1, B1, B1
  52. [b1] subc .l1x A4,B4,A4
  53. || [b1] add .s2 -1, B1, B1
  54. ;; loop backwards branch happens here
  55. ret .s2 B3
  56. || mvk .s1 32, A1
  57. sub .l1 A1, A6, A6
  58. || extu .s1 A4, A6, A5
  59. shl .s1 A4, A6, A4
  60. shru .s1 A4, 1, A4
  61. || sub .l1 A6, 1, A6
  62. or .l1 A2, A4, A4
  63. shru .s1 A4, A6, A4
  64. nop
  65. ENDPROC(__c6xabi_divremu)