mulhi3.S 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. //===------------ mulhi3.S - int16 multiplication -------------------------===//
  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. // The corresponding C code is something like:
  10. //
  11. // int __mulhi3(int A, int B) {
  12. // int S = 0;
  13. // while (A != 0) {
  14. // if (A & 1)
  15. // S += B;
  16. // A = ((unsigned int) A) >> 1;
  17. // B <<= 1;
  18. // }
  19. // return S;
  20. // }
  21. //
  22. // __mulhi3 has special ABI, as the implementation of libgcc, R25:R24 is used
  23. // to return result, while Rtmp/R21/R22/R23 are clobbered.
  24. //
  25. //===----------------------------------------------------------------------===//
  26. .text
  27. .align 2
  28. #ifdef __AVR_TINY__
  29. .set __tmp_reg__, 16
  30. .set __zero_reg__, 17
  31. #else
  32. .set __tmp_reg__, 0
  33. .set __zero_reg__, 1
  34. #endif
  35. .globl __mulhi3
  36. .type __mulhi3, @function
  37. __mulhi3:
  38. ; Use Rzero:Rtmp to store the result.
  39. clr __tmp_reg__
  40. clr __zero_reg__ ; S = 0;
  41. __mulhi3_loop:
  42. clr r21
  43. cp r24, r21
  44. cpc r25, r21
  45. breq __mulhi3_end ; while (A != 0) {
  46. mov r21, r24
  47. andi r21, 1
  48. breq __mulhi3_loop_a ; if (A & 1)
  49. add __tmp_reg__, r22
  50. adc __zero_reg__, r23 ; S += B;
  51. __mulhi3_loop_a:
  52. lsr r25
  53. ror r24 ; A = ((unsigned int) A) >> 1;
  54. lsl r22
  55. rol r23 ; B <<= 1;
  56. rjmp __mulhi3_loop ; }
  57. __mulhi3_end:
  58. ; Return the result via R25:R24.
  59. mov r24, __tmp_reg__
  60. mov r25, __zero_reg__
  61. ; Restore __zero_reg__ to 0.
  62. clr __zero_reg__
  63. ret ; return S;