do_unsar.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Sources of the "UNSIGNED ARITHMETIC" group instructions
  3. */
  4. /* $Header$ */
  5. #include <em_abs.h>
  6. #include "logging.h"
  7. #include "global.h"
  8. #include "log.h"
  9. #include "mem.h"
  10. #include "trap.h"
  11. #include "warn.h"
  12. #include "text.h"
  13. #include "fra.h"
  14. /************************************************************************
  15. * No checking is performed, except for division by zero. *
  16. * The operands popped from the stack are put in unsigned *
  17. * longs. Now the required operation can be performed *
  18. * immediately. Whether the wordsize is two or four bytes *
  19. * doesn't matter. Alas, arithmetic is performed modulo *
  20. * the highest unsigned number for the given size plus 1. *
  21. ************************************************************************/
  22. #ifdef LOGGING
  23. extern int must_test;
  24. #endif LOGGING
  25. #define adu(w1,w2) (unsigned long)(w1 + w2)
  26. #define sbu(w1,w2) (unsigned long)(w1 - w2)
  27. #define mlu(w1,w2) (unsigned long)(w1 * w2)
  28. PRIVATE unsigned long dvu(), rmu(), slu(), sru();
  29. DoADUl2(arg)
  30. size arg;
  31. {
  32. /* ADU w: Addition */
  33. register size l = (L_arg_2() * arg);
  34. register unsigned long t = upop(arg_wi(l));
  35. LOG(("@U6 DoADUl2(%ld)", l));
  36. spoilFRA();
  37. npush((long) adu(upop(l), t), l);
  38. }
  39. DoADUz()
  40. {
  41. /* ADU w: Addition */
  42. register size l = upop(wsize);
  43. register unsigned long t = upop(arg_wi(l));
  44. LOG(("@U6 DoADUz(%ld)", l));
  45. spoilFRA();
  46. npush((long) adu(upop(l), t), l);
  47. }
  48. DoSBUl2(arg)
  49. size arg;
  50. {
  51. /* SBU w: Subtraction */
  52. register size l = (L_arg_2() * arg);
  53. register unsigned long t = upop(arg_wi(l));
  54. LOG(("@U6 DoSBUl2(%ld)", l));
  55. spoilFRA();
  56. npush((long) sbu(upop(l), t), l);
  57. }
  58. DoSBUz()
  59. {
  60. /* SBU w: Subtraction */
  61. register size l = upop(wsize);
  62. register unsigned long t = upop(arg_wi(l));
  63. LOG(("@U6 DoSBUz(%ld)", l));
  64. spoilFRA();
  65. npush((long) sbu(upop(l), t), l);
  66. }
  67. DoMLUl2(arg)
  68. size arg;
  69. {
  70. /* MLU w: Multiplication */
  71. register size l = (L_arg_2() * arg);
  72. register unsigned long t = upop(arg_wi(l));
  73. LOG(("@U6 DoMLUl2(%ld)", l));
  74. spoilFRA();
  75. npush((long) mlu(upop(l), t), l);
  76. }
  77. DoMLUz()
  78. {
  79. /* MLU w: Multiplication */
  80. register size l = upop(wsize);
  81. register unsigned long t = upop(arg_wi(l));
  82. LOG(("@U6 DoMLUz(%ld)", l));
  83. spoilFRA();
  84. npush((long) mlu(upop(l), t), l);
  85. }
  86. DoDVUl2(arg)
  87. size arg;
  88. {
  89. /* DVU w: Division */
  90. register size l = (L_arg_2() * arg);
  91. register unsigned long t = upop(arg_wi(l));
  92. LOG(("@U6 DoDVUl2(%ld)", l));
  93. spoilFRA();
  94. npush((long) dvu(upop(l), t), l);
  95. }
  96. DoDVUz()
  97. {
  98. /* DVU w: Division */
  99. register size l = upop(wsize);
  100. register unsigned long t = upop(arg_wi(l));
  101. LOG(("@U6 DoDVUz(%ld)", l));
  102. spoilFRA();
  103. npush((long) dvu(upop(l), t), l);
  104. }
  105. DoRMUl2(arg)
  106. size arg;
  107. {
  108. /* RMU w: Remainder */
  109. register size l = (L_arg_2() * arg);
  110. register unsigned long t = upop(arg_wi(l));
  111. LOG(("@U6 DoRMUl2(%ld)", l));
  112. spoilFRA();
  113. npush((long) rmu(upop(l), t), l);
  114. }
  115. DoRMUz()
  116. {
  117. /* RMU w: Remainder */
  118. register size l = upop(wsize);
  119. register unsigned long t = upop(arg_wi(l));
  120. LOG(("@U6 DoRMUz(%ld)", l));
  121. spoilFRA();
  122. npush((long) rmu(upop(l), t), l);
  123. }
  124. DoSLUl2(arg)
  125. size arg;
  126. {
  127. /* SLU w: Shift left */
  128. register size l = (L_arg_2() * arg);
  129. register unsigned long t = upop(wsize);
  130. LOG(("@U6 DoSLUl2(%ld)", l));
  131. spoilFRA();
  132. l = arg_wi(l);
  133. npush((long) slu(upop(l), t, l), l);
  134. }
  135. DoSLUz()
  136. {
  137. /* SLU w: Shift left */
  138. register size l = upop(wsize);
  139. register unsigned long t = upop(wsize);
  140. LOG(("@U6 DoSLUz(%ld)", l));
  141. spoilFRA();
  142. l = arg_wi(l);
  143. npush((long) slu(upop(l), t, l), l);
  144. }
  145. DoSRUl2(arg)
  146. size arg;
  147. {
  148. /* SRU w: Shift right */
  149. register size l = (L_arg_2() * arg);
  150. register unsigned long t = upop(wsize);
  151. LOG(("@U6 DoSRUl2(%ld)", l));
  152. spoilFRA();
  153. l = arg_wi(l);
  154. npush((long) sru(upop(l), t, l), l);
  155. }
  156. DoSRUz()
  157. {
  158. /* SRU w: Shift right */
  159. register size l = upop(wsize);
  160. register unsigned long t = upop(wsize);
  161. LOG(("@U6 DoSRUz(%ld)", l));
  162. spoilFRA();
  163. l = arg_wi(l);
  164. npush((long) sru(upop(l), t, l), l);
  165. }
  166. PRIVATE unsigned long dvu(w1, w2)
  167. unsigned long w1, w2;
  168. {
  169. if (w2 == 0) {
  170. if (!(IgnMask&BIT(EIDIVZ))) {
  171. trap(EIDIVZ);
  172. }
  173. else return (0L);
  174. }
  175. return (w1 / w2);
  176. }
  177. PRIVATE unsigned long rmu(w1, w2)
  178. unsigned long w1, w2;
  179. {
  180. if (w2 == 0) {
  181. if (!(IgnMask&BIT(EIDIVZ))) {
  182. trap(EIDIVZ);
  183. }
  184. else return (0L);
  185. }
  186. return (w1 % w2);
  187. }
  188. /*ARGSUSED*/
  189. PRIVATE unsigned long slu(w1, w2, nbytes) /* w1 << w2 */
  190. unsigned long w1, w2;
  191. size nbytes;
  192. {
  193. #ifdef LOGGING
  194. if (must_test) {
  195. /* check shift distance */
  196. if (w2 >= nbytes*8) {
  197. warning(WSHLARGE);
  198. w2 = nbytes*8 - 1;
  199. }
  200. }
  201. #endif LOGGING
  202. /* calculate result */
  203. return (w1 << w2);
  204. }
  205. /*ARGSUSED*/
  206. PRIVATE unsigned long sru(w1, w2, nbytes) /* w1 >> w2 */
  207. unsigned long w1, w2;
  208. size nbytes;
  209. {
  210. #ifdef LOGGING
  211. if (must_test) {
  212. /* check shift distance */
  213. if (w2 >= nbytes*8) {
  214. warning(WSHLARGE);
  215. w2 = nbytes*8 - 1;
  216. }
  217. }
  218. #endif LOGGING
  219. /* calculate result */
  220. return (w1 >> w2);
  221. }