cmpsi2.S 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. ; Copyright (C) 2011-2022 Free Software Foundation, Inc.
  2. ; Contributed by Red Hat.
  3. ;
  4. ; This file is free software; you can redistribute it and/or modify it
  5. ; under the terms of the GNU General Public License as published by the
  6. ; Free Software Foundation; either version 3, or (at your option) any
  7. ; later version.
  8. ;
  9. ; This file is distributed in the hope that it will be useful, but
  10. ; WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ; General Public License for more details.
  13. ;
  14. ; Under Section 7 of GPL version 3, you are granted additional
  15. ; permissions described in the GCC Runtime Library Exception, version
  16. ; 3.1, as published by the Free Software Foundation.
  17. ;
  18. ; You should have received a copy of the GNU General Public License and
  19. ; a copy of the GCC Runtime Library Exception along with this program;
  20. ; see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  21. ; <http://www.gnu.org/licenses/>.
  22. #include "vregs.h"
  23. .text
  24. ;; int __cmpsi2 (signed long A, signed long B)
  25. ;;
  26. ;; Performs a signed comparison of A and B.
  27. ;; If A is less than B it returns 0. If A is greater
  28. ;; than B it returns 2. If they are equal it returns 1.
  29. START_FUNC ___cmpsi2
  30. ;; A is at [sp+4]
  31. ;; B is at [sp+8]
  32. ;; Result put in R8
  33. ;; Initialise default return value.
  34. onew bc
  35. ;; Compare the high words.
  36. movw ax, [sp + 10]
  37. movw de, ax
  38. movw ax, [sp + 6]
  39. cmpw ax, de
  40. skz
  41. br !!.Lconvert_to_signed
  42. .Lcompare_bottom_words:
  43. ;; The top words are equal - compare the bottom words.
  44. ;; Note - code from __ucmpsi2 branches into here.
  45. movw ax, [sp + 8]
  46. movw de, ax
  47. movw ax, [sp + 4]
  48. cmpw ax, de
  49. sknz
  50. br !!.Lless_than_or_greater_than
  51. ;; The words are equal - return 1.
  52. ;; Note - we could branch to the return code at the end of the
  53. ;; function but a branch instruction takes 4 bytes, and the
  54. ;; return sequence itself is only 4 bytes long...
  55. movw ax, bc
  56. movw r8, ax
  57. ret
  58. .Lconvert_to_signed:
  59. ;; The top words are different. Unfortunately the comparison
  60. ;; is always unsigned, so to get a signed result we XOR the CY
  61. ;; flag with the top bits of AX and DE.
  62. xor1 cy, a.7
  63. mov a, d
  64. xor1 cy, a.7
  65. ;; Fall through.
  66. .Lless_than_or_greater_than:
  67. ;; We now have a signed less than/greater than result in CY.
  68. ;; Return 0 for less than, 2 for greater than.
  69. ;; Note - code from __ucmpsi2 branches into here.
  70. incw bc
  71. sknc
  72. clrw bc
  73. ;; Get the result value, currently in BC, into r8
  74. movw ax, bc
  75. movw r8, ax
  76. ret
  77. END_FUNC ___cmpsi2
  78. ;; ------------------------------------------------------
  79. ;; int __ucmpsi2 (unsigned long A, unsigned long B)
  80. ;;
  81. ;; Performs an unsigned comparison of A and B.
  82. ;; If A is less than B it returns 0. If A is greater
  83. ;; than B it returns 2. If they are equal it returns 1.
  84. START_FUNC ___ucmpsi2
  85. ;; A is at [sp+4]
  86. ;; B is at [sp+8]
  87. ;; Result put in R8..R9
  88. ;; Initialise default return value.
  89. onew bc
  90. ;; Compare the high words.
  91. movw ax, [sp + 10]
  92. movw de, ax
  93. movw ax, [sp + 6]
  94. cmpw ax, de
  95. skz
  96. ;; Note: These branches go into the __cmpsi2 code!
  97. br !!.Lless_than_or_greater_than
  98. br !!.Lcompare_bottom_words
  99. END_FUNC ___ucmpsi2
  100. ;; ------------------------------------------------------
  101. ;; signed int __gcc_bcmp (const unsigned char *s1, const unsigned char *s2, size_t size)
  102. ;; Result is negative if S1 is less than S2,
  103. ;; positive if S1 is greater, 0 if S1 and S2 are equal.
  104. START_FUNC __gcc_bcmp
  105. ;; S1 is at [sp+4]
  106. ;; S2 is at [sp+6]
  107. ;; SIZE is at [sp+8]
  108. ;; Result in r8/r9
  109. movw r10, #0
  110. 1:
  111. ;; Compare R10 against the SIZE parameter
  112. movw ax, [sp+8]
  113. subw ax, r10
  114. sknz
  115. br !!1f
  116. ;; Load S2[r10] into R8
  117. movw ax, [sp+6]
  118. addw ax, r10
  119. movw hl, ax
  120. mov a, [hl]
  121. mov r8, a
  122. ;; Load S1[r10] into A
  123. movw ax, [sp+4]
  124. addw ax, r10
  125. movw hl, ax
  126. mov a, [hl]
  127. ;; Increment offset
  128. incw r10
  129. ;; Compare loaded bytes
  130. cmp a, r8
  131. sknz
  132. br !!1b
  133. ;; They differ. Subtract *S2 from *S1 and return as the result.
  134. mov x, a
  135. mov a, #0
  136. mov r9, #0
  137. subw ax, r8
  138. 1:
  139. movw r8, ax
  140. ret
  141. END_FUNC __gcc_bcmp