udivmodhi4.S 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. //===------------ udivmodhi4.S - uint16 div & mod -------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // As described at
  10. // https://gcc.gnu.org/wiki/avr-gcc#Exceptions_to_the_Calling_Convention, the
  11. // prototype is `struct {uint16, uint16} __udivmodhi4(uint16, uint16)`.
  12. // The uint16 quotient is returned via R23:R22, and the uint16 remainder is
  13. // returned via R25:R24, while R21/R26/R27 are clobbered.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. .text
  17. .align 2
  18. .globl __udivmodhi4
  19. .type __udivmodhi4, @function
  20. __udivmodhi4:
  21. sub r26, r26
  22. sub r27, r27 ; Initialize the remainder to zero.
  23. ldi r21, 17 ; Only loop 16 rounds for uint16.
  24. __udivmodhi4_loop:
  25. adc r24, r24
  26. adc r25, r25
  27. dec r21
  28. breq __udivmodhi4_end
  29. adc r26, r26
  30. adc r27, r27
  31. cp r26, r22
  32. cpc r27, r23 ; Compare with the divisor.
  33. brcs __udivmodhi4_loop
  34. sub r26, r22
  35. sbc r27, r23 ; Subtract the divisor.
  36. rjmp __udivmodhi4_loop
  37. __udivmodhi4_end:
  38. com r24
  39. com r25
  40. mov r22, r24
  41. mov r23, r25 ; The quotient is returned in R23:R22.
  42. mov r24, r26
  43. mov r25, r27 ; The remainder is returned in in R25:R24.
  44. ret