udivsi3.S 4.9 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. #include <asm/export.h>
  30. /* These are predefined by new versions of GNU cpp. */
  31. #ifndef __USER_LABEL_PREFIX__
  32. #define __USER_LABEL_PREFIX__ _
  33. #endif
  34. #ifndef __REGISTER_PREFIX__
  35. #define __REGISTER_PREFIX__
  36. #endif
  37. #ifndef __IMMEDIATE_PREFIX__
  38. #define __IMMEDIATE_PREFIX__ #
  39. #endif
  40. /* ANSI concatenation macros. */
  41. #define CONCAT1(a, b) CONCAT2(a, b)
  42. #define CONCAT2(a, b) a ## b
  43. /* Use the right prefix for global labels. */
  44. #define SYM(x) CONCAT1 (__USER_LABEL_PREFIX__, x)
  45. /* Use the right prefix for registers. */
  46. #define REG(x) CONCAT1 (__REGISTER_PREFIX__, x)
  47. /* Use the right prefix for immediate values. */
  48. #define IMM(x) CONCAT1 (__IMMEDIATE_PREFIX__, x)
  49. #define d0 REG (d0)
  50. #define d1 REG (d1)
  51. #define d2 REG (d2)
  52. #define d3 REG (d3)
  53. #define d4 REG (d4)
  54. #define d5 REG (d5)
  55. #define d6 REG (d6)
  56. #define d7 REG (d7)
  57. #define a0 REG (a0)
  58. #define a1 REG (a1)
  59. #define a2 REG (a2)
  60. #define a3 REG (a3)
  61. #define a4 REG (a4)
  62. #define a5 REG (a5)
  63. #define a6 REG (a6)
  64. #define fp REG (fp)
  65. #define sp REG (sp)
  66. .text
  67. .proc
  68. .globl SYM (__udivsi3)
  69. SYM (__udivsi3):
  70. #if !(defined(__mcf5200__) || defined(__mcoldfire__))
  71. movel d2, sp@-
  72. movel sp@(12), d1 /* d1 = divisor */
  73. movel sp@(8), d0 /* d0 = dividend */
  74. cmpl IMM (0x10000), d1 /* divisor >= 2 ^ 16 ? */
  75. jcc L3 /* then try next algorithm */
  76. movel d0, d2
  77. clrw d2
  78. swap d2
  79. divu d1, d2 /* high quotient in lower word */
  80. movew d2, d0 /* save high quotient */
  81. swap d0
  82. movew sp@(10), d2 /* get low dividend + high rest */
  83. divu d1, d2 /* low quotient */
  84. movew d2, d0
  85. jra L6
  86. L3: movel d1, d2 /* use d2 as divisor backup */
  87. L4: lsrl IMM (1), d1 /* shift divisor */
  88. lsrl IMM (1), d0 /* shift dividend */
  89. cmpl IMM (0x10000), d1 /* still divisor >= 2 ^ 16 ? */
  90. jcc L4
  91. divu d1, d0 /* now we have 16 bit divisor */
  92. andl IMM (0xffff), d0 /* mask out divisor, ignore remainder */
  93. /* Multiply the 16 bit tentative quotient with the 32 bit divisor. Because of
  94. the operand ranges, this might give a 33 bit product. If this product is
  95. greater than the dividend, the tentative quotient was too large. */
  96. movel d2, d1
  97. mulu d0, d1 /* low part, 32 bits */
  98. swap d2
  99. mulu d0, d2 /* high part, at most 17 bits */
  100. swap d2 /* align high part with low part */
  101. tstw d2 /* high part 17 bits? */
  102. jne L5 /* if 17 bits, quotient was too large */
  103. addl d2, d1 /* add parts */
  104. jcs L5 /* if sum is 33 bits, quotient was too large */
  105. cmpl sp@(8), d1 /* compare the sum with the dividend */
  106. jls L6 /* if sum > dividend, quotient was too large */
  107. L5: subql IMM (1), d0 /* adjust quotient */
  108. L6: movel sp@+, d2
  109. rts
  110. #else /* __mcf5200__ || __mcoldfire__ */
  111. /* Coldfire implementation of non-restoring division algorithm from
  112. Hennessy & Patterson, Appendix A. */
  113. link a6,IMM (-12)
  114. moveml d2-d4,sp@
  115. movel a6@(8),d0
  116. movel a6@(12),d1
  117. clrl d2 | clear p
  118. moveq IMM (31),d4
  119. L1: addl d0,d0 | shift reg pair (p,a) one bit left
  120. addxl d2,d2
  121. movl d2,d3 | subtract b from p, store in tmp.
  122. subl d1,d3
  123. jcs L2 | if no carry,
  124. bset IMM (0),d0 | set the low order bit of a to 1,
  125. movl d3,d2 | and store tmp in p.
  126. L2: subql IMM (1),d4
  127. jcc L1
  128. moveml sp@,d2-d4 | restore data registers
  129. unlk a6 | and return
  130. rts
  131. #endif /* __mcf5200__ || __mcoldfire__ */
  132. EXPORT_SYMBOL(__udivsi3)