udivsi3.S 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* libgcc1 routines for 68000 w/o floating-point hardware.
  2. Copyright (C) 1994, 1996, 1997, 1998 Free Software Foundation, Inc.
  3. This file is part of GNU CC.
  4. GNU CC 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 2, or (at your option) any
  7. later version.
  8. In addition to the permissions in the GNU General Public License, the
  9. Free Software Foundation gives you unlimited permission to link the
  10. compiled version of this file with other programs, and to distribute
  11. those programs without any restriction coming from the use of this
  12. file. (The General Public License restrictions do apply in other
  13. respects; for example, they cover modification of the file, and
  14. distribution when not linked into another program.)
  15. This file is distributed in the hope that it will be useful, but
  16. WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. General Public License for more details. */
  19. /* As a special exception, if you link this library with files
  20. compiled with GCC to produce an executable, this does not cause
  21. the resulting executable to be covered by the GNU General Public License.
  22. This exception does not however invalidate any other reasons why
  23. the executable file might be covered by the GNU General Public License. */
  24. /* Use this one for any 680x0; assumes no floating point hardware.
  25. The trailing " '" appearing on some lines is for ANSI preprocessors. Yuk.
  26. Some of this code comes from MINIX, via the folks at ericsson.
  27. D. V. Henkel-Wallace (gumby@cygnus.com) Fete Bastille, 1992
  28. */
  29. /* These are predefined by new versions of GNU cpp. */
  30. #ifndef __USER_LABEL_PREFIX__
  31. #define __USER_LABEL_PREFIX__ _
  32. #endif
  33. #ifndef __REGISTER_PREFIX__
  34. #define __REGISTER_PREFIX__
  35. #endif
  36. #ifndef __IMMEDIATE_PREFIX__
  37. #define __IMMEDIATE_PREFIX__ #
  38. #endif
  39. /* ANSI concatenation macros. */
  40. #define CONCAT1(a, b) CONCAT2(a, b)
  41. #define CONCAT2(a, b) a ## b
  42. /* Use the right prefix for global labels. */
  43. #define SYM(x) CONCAT1 (__USER_LABEL_PREFIX__, x)
  44. /* Use the right prefix for registers. */
  45. #define REG(x) CONCAT1 (__REGISTER_PREFIX__, x)
  46. /* Use the right prefix for immediate values. */
  47. #define IMM(x) CONCAT1 (__IMMEDIATE_PREFIX__, x)
  48. #define d0 REG (d0)
  49. #define d1 REG (d1)
  50. #define d2 REG (d2)
  51. #define d3 REG (d3)
  52. #define d4 REG (d4)
  53. #define d5 REG (d5)
  54. #define d6 REG (d6)
  55. #define d7 REG (d7)
  56. #define a0 REG (a0)
  57. #define a1 REG (a1)
  58. #define a2 REG (a2)
  59. #define a3 REG (a3)
  60. #define a4 REG (a4)
  61. #define a5 REG (a5)
  62. #define a6 REG (a6)
  63. #define fp REG (fp)
  64. #define sp REG (sp)
  65. .text
  66. .proc
  67. .globl SYM (__udivsi3)
  68. SYM (__udivsi3):
  69. #if !(defined(__mcf5200__) || defined(__mcoldfire__))
  70. movel d2, sp@-
  71. movel sp@(12), d1 /* d1 = divisor */
  72. movel sp@(8), d0 /* d0 = dividend */
  73. cmpl IMM (0x10000), d1 /* divisor >= 2 ^ 16 ? */
  74. jcc L3 /* then try next algorithm */
  75. movel d0, d2
  76. clrw d2
  77. swap d2
  78. divu d1, d2 /* high quotient in lower word */
  79. movew d2, d0 /* save high quotient */
  80. swap d0
  81. movew sp@(10), d2 /* get low dividend + high rest */
  82. divu d1, d2 /* low quotient */
  83. movew d2, d0
  84. jra L6
  85. L3: movel d1, d2 /* use d2 as divisor backup */
  86. L4: lsrl IMM (1), d1 /* shift divisor */
  87. lsrl IMM (1), d0 /* shift dividend */
  88. cmpl IMM (0x10000), d1 /* still divisor >= 2 ^ 16 ? */
  89. jcc L4
  90. divu d1, d0 /* now we have 16 bit divisor */
  91. andl IMM (0xffff), d0 /* mask out divisor, ignore remainder */
  92. /* Multiply the 16 bit tentative quotient with the 32 bit divisor. Because of
  93. the operand ranges, this might give a 33 bit product. If this product is
  94. greater than the dividend, the tentative quotient was too large. */
  95. movel d2, d1
  96. mulu d0, d1 /* low part, 32 bits */
  97. swap d2
  98. mulu d0, d2 /* high part, at most 17 bits */
  99. swap d2 /* align high part with low part */
  100. tstw d2 /* high part 17 bits? */
  101. jne L5 /* if 17 bits, quotient was too large */
  102. addl d2, d1 /* add parts */
  103. jcs L5 /* if sum is 33 bits, quotient was too large */
  104. cmpl sp@(8), d1 /* compare the sum with the dividend */
  105. jls L6 /* if sum > dividend, quotient was too large */
  106. L5: subql IMM (1), d0 /* adjust quotient */
  107. L6: movel sp@+, d2
  108. rts
  109. #else /* __mcf5200__ || __mcoldfire__ */
  110. /* Coldfire implementation of non-restoring division algorithm from
  111. Hennessy & Patterson, Appendix A. */
  112. link a6,IMM (-12)
  113. moveml d2-d4,sp@
  114. movel a6@(8),d0
  115. movel a6@(12),d1
  116. clrl d2 | clear p
  117. moveq IMM (31),d4
  118. L1: addl d0,d0 | shift reg pair (p,a) one bit left
  119. addxl d2,d2
  120. movl d2,d3 | subtract b from p, store in tmp.
  121. subl d1,d3
  122. jcs L2 | if no carry,
  123. bset IMM (0),d0 | set the low order bit of a to 1,
  124. movl d3,d2 | and store tmp in p.
  125. L2: subql IMM (1),d4
  126. jcc L1
  127. moveml sp@,d2-d4 | restore data registers
  128. unlk a6 | and return
  129. rts
  130. #endif /* __mcf5200__ || __mcoldfire__ */