home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 303.lha / AssemTools / Include / mathlib.i < prev    next >
Text File  |  1980-12-03  |  9KB  |  507 lines

  1.  
  2. * Include File for METACC AMIGA MACRO ASSEMBLER
  3. * for handling long integer values arithmetically
  4. * 011188 v1.0 by TM + JM
  5.  
  6.  
  7. ;  sortentry.comments added -> v1.1  04.03.89
  8. ;  pull d2 -> pull d2/d2 for a68k -> v1.2 11.03.89
  9. ;  "badd, bsub, bneg, bclr, btst, bchk, bcmp, bmove",
  10. ;  "order" -> v1.3 20.06.89
  11. ;  "sqr, sqrt, hyp" -> v1.4 21.06.89
  12. ;  "random" -> v1.41 17.07.89
  13.  
  14.  
  15. *T
  16. *T    MATHLIB.I * Metacc Include File
  17. *T        Version 1.41
  18. *T           Date 17.07.89
  19. *T
  20. *B
  21.  
  22. ;  mulu        (multiply / 32*32->64 bits, unsigned /)
  23. ;  in:        d0, d1: integer;
  24. ;  call:    mathlib    mulu;
  25. ;  out:        d1 (63..32), d0 (31..0): double longint;
  26.  
  27. ;  muls        (multiply / 32*32->64 bits, signed /)
  28. ;  in:        d0, d1: integer;
  29. ;  call:    mathlib    muls;
  30. ;  out:        d1 (63..32), d0 (31..0): double longint;
  31.  
  32. ;  divu        (divide / 32/32->32 bits, unsigned /)
  33. ;  in:        d0, d1: integer;  { d0/d1 }
  34. ;  call:    mathlib divu;
  35. ;  out:        d0: d0 div d1;
  36. ;          d1: d0 mod d1;
  37.  
  38. ;  divs        (divide / 32/32->32 bits, signed /)
  39. ;  in:        d0, d1: integer;  { d0/d1 }
  40. ;  call:    mathlib divs;
  41. ;  out:        d0: d0 div d1;
  42. ;          d1: d0 mod d1;
  43.  
  44. ;  abs        (abs(x))
  45. ;  in:        d0: integer;
  46. ;  call:    mathlib abs;
  47. ;  out:        d0: positive integer;
  48.  
  49. ;  sgn        (sgn(x))
  50. ;  in:        d0: integer;
  51. ;  call:    mathlib sgn;
  52. ;  out:        d0: integer in (-1,0,1);
  53.  
  54. ;  sqr        (x²)
  55. ;  in:        d0: integer;
  56. ;  call:    mathlib    sqr;
  57. ;  out:        d0: integer; /-1 if error/
  58.  
  59. ;  sqrt        (sqrt(x))
  60. ;  in:        d0: integer;
  61. ;  call:    mathlib    sqrt;
  62. ;  out:        d0: integer; /-1 if error/
  63. ;  notes:    Rounded to lower integer, if decimals in root
  64.  
  65. ;  hyp        (sqrt(x²+y²))
  66. ;  in:        d0, d1: integer;
  67. ;  call:    mathlib    hyp;
  68. ;  out:        d0: integer;
  69. ;  notes:    See "sqrt"
  70.  
  71. ;  extbl    (sign extend byte to long)
  72. ;  in:        d0: byte;
  73. ;  call:    mathlib extbl;
  74. ;  out:        d0: long;
  75.  
  76. ;  order    (return higher and lower of two)
  77. ;  in:        d0, d1: integer;
  78. ;  call:    mathlib    order;
  79. ;  out:        d0: lower, d1: higher;
  80.  
  81. ;  random    (generate 'random' numbers)
  82. ;  in:        d0: uword range, d1: word seed;
  83. ;  call:    mathlib    random;
  84. ;  out:        d0: uword number, d1: word new_seed;
  85. ;  notes:    The number returned is in the range from,
  86. ;        and including, 0, up to, but excluding, the
  87. ;        value given in d0, or, [0,d0[.
  88. ;        The new_seed can be given back to the routine
  89. ;        on the next calling.
  90.  
  91. ;  bmove    (move and extend bcd)
  92. ;  in:        a0, d0: bcd; a1, d1: bcd;  {a1=a0}
  93. ;  call:    mathlib    bmove;
  94. ;  notes:    See "badd";
  95. ;        If the bcd a1 is longer than a0, the
  96. ;        value is sign-extended to full length.
  97.  
  98. ;  badd        (add bcd)
  99. ;  in:        a0, d0: bcd; a1, d1: bcd;  {a0=a0+a1}
  100. ;  call:    mathlib    badd;
  101. ;  notes:    d0 is UWORD length of bcd a0 (in bytes),
  102. ;          d1 is the same for a1. Action taken when
  103. ;          either of the lengths is zero is undefined.
  104. ;          The pointers a0, a1 point to the first (most
  105. ;          significant) byte of the signed bcd.
  106.  
  107. ;  bsub        (subtract bcd)
  108. ;  in:        a0, d0: bcd; a1, d1: bcd;  {a0=a0-a1}
  109. ;  call:    mathlib    bsub;
  110. ;  notes:    See "badd"
  111.  
  112. ;  bcmp        (compare bcd)
  113. ;  in:        a0, d0: bcd1; a1, d1: bcd2;  {cmp a1,a0}
  114. ;  call:    mathlib    cbmp;
  115. ;  out:        d0: sign_of_result = in {-1, 0, 1};
  116. ;        p.flags = set_according_to_comparison;
  117. ;  notes:    See "badd"
  118.  
  119. ;  bneg        (negate bcd)
  120. ;  in:        a0, d0: bcd;  {a0=-a0}
  121. ;  call:    mathlib    bneg;
  122. ;  notes:    See "badd"
  123.  
  124. ;  bclr        (clear bcd)
  125. ;  in:        a0, d0: bcd;  {a0=0}
  126. ;  call:    mathlib    bclr;
  127. ;  notes:    See "badd"
  128.  
  129. ;  btst        (test bcd)
  130. ;  in:        a0, d0: bcd;  {a0?}
  131. ;  call:    mathlib    btst;
  132. ;  out:        d0: integer in {-1, 0, 1};
  133. ;  notes:    See "badd"
  134.  
  135. ;  bchk        (check bcd)
  136. ;  in:        a0, d0: bcd;  {check if valid bcd value}
  137. ;  call:    mathlib    bchk;
  138. ;  out:        d0: result; {if 0, value is OK}
  139. ;  notes:    See "badd"
  140.  
  141. *E
  142.  
  143. ;;;
  144.  
  145.  
  146. mathlib    macro
  147.       ifnc      '\1',''
  148. _MATHF\1    set        1
  149.         bsr        _MATH\1
  150.         mexit
  151.       endc
  152.  
  153.         ifd      _MATHFrandom
  154.         xref    _custom
  155. _MATHrandom    push    d2/a0
  156.         lea.l    _custom,a0
  157.         move.w    6(a0),d2
  158.         lsl.w    #7,d1
  159.         eor.w    d1,d2
  160.         eor.w    #$f62a,d2
  161.         move.w    d2,d1
  162.         mulu.w    d0,d2
  163.         swap    d2
  164.         moveq.l    #0,d0
  165.         move.w    d2,d0
  166.         pull    d2/a0
  167.         rts
  168.         endc
  169.  
  170.         ifd      _MATHFhyp
  171. _MATHhyp    push    d1
  172.         mathlib    sqr
  173.         exg.l    d0,d1
  174.         mathlib    sqr
  175.         add.l    d1,d0
  176.         mathlib    sqrt
  177.         pull    d1/d1
  178.         rts
  179.         endc
  180.  
  181.         ifd      _MATHFsqr
  182. _MATHsqr    tst.l    d0
  183.         bpl.s    _MATHsqr1
  184.         neg.l    d0
  185. _MATHsqr1    cmp.l    #65535,d0
  186.         bhi.s    _MATHsqr0
  187.         mulu.w    d0,d0
  188.         rts
  189. _MATHsqr0    moveq    #-1,d0
  190.         rts
  191.         endc
  192.  
  193.         ifd      _MATHFsqrt
  194. _MATHsqrt    push    d1-d4
  195.         tst.l    d0
  196.         bmi.s    _MATHsqrt4
  197.         moveq.l    #0,d2
  198.         move.l    #46340,d3
  199. _MATHsqrt1    move.l    d2,d1
  200.         add.l    d3,d1
  201.         asr.l    #1,d1
  202.         cmp.l    d2,d3
  203.         beq.s    _MATHsqrt0
  204.         move.l    d1,d4
  205.         mulu.w    d4,d4
  206.         cmp.l    d0,d4
  207.         blt.s    _MATHsqrt2
  208.         bgt.s    _MATHsqrt3
  209. _MATHsqrt0    move.l    d1,d0
  210.         pull    d1-d4
  211.         rts
  212. _MATHsqrt2    add.l    d1,d4
  213.         add.l    d1,d4
  214.         cmp.l    d0,d4
  215.         bge.s    _MATHsqrt0
  216.         move.l    d1,d2
  217.         bra.s    _MATHsqrt1
  218. _MATHsqrt3    move.l    d1,d3
  219.         bra.s    _MATHsqrt1
  220. _MATHsqrt4    moveq    #-1,d1
  221.         bra.s    _MATHsqrt0
  222.         endc
  223.  
  224.         ifd      _MATHForder
  225. _MATHorder    cmp.l    d0,d1
  226.         bge.s    _MATHorder1
  227.         exg.l    d0,d1
  228. _MATHorder1    rts
  229.         endc
  230.  
  231.         ifd      _MATHFabs
  232. _MATHabs    tst.l    d0
  233.         bpl.s    _MATHabs1
  234.         neg.l    d0
  235. _MATHabs1    rts
  236.         endc
  237.  
  238.         ifd      _MATHFsgn
  239. _MATHsgn    tst.l    d0
  240.         beq.s    _MATHsgn1
  241.         bpl.s    _MATHsgn2
  242.         moveq.l    #-1,d0
  243. _MATHsgn1    rts
  244. _MATHsgn2    moveq.l    #1,d0
  245.         rts
  246.         endc
  247.  
  248.         ifd      _MATHFextbl
  249. _MATHextbl    ext.w    d0
  250.         ext.l    d0
  251.         rts
  252.         endc
  253.  
  254.         ifd      _MATHFbmove
  255. _MATHbmove    push    a0-a3/d0-d3
  256.         clr.b    d2
  257.         cmp.b    #$49,(a0)
  258.         bls.s    _MATHbmove1
  259.         move.b    #$99,d2
  260. _MATHbmove1    add.w    d0,a0
  261.         add.w    d1,a1
  262. _MATHbmove2    tst.w    d1
  263.         beq.s    _MATHbmove0
  264.         subq.w    #1,d1
  265.         tst.w    d0
  266.         bne.s    _MATHbmove3
  267.         move.b    d2,-(a1)
  268.         bra.s    _MATHbmove2
  269. _MATHbmove3    subq.w    #1,d0
  270.         move.b    -(a0),-(a1)
  271.         bra.s    _MATHbmove2
  272. _MATHbmove0    pull    a0-a3/d0-d3
  273.         rts
  274.         endc
  275.  
  276.         ifd      _MATHFbclr
  277. _MATHbclr    push    a0/d0
  278.         add.w    d0,a0
  279. _MATHbclr1    tst.w    d0
  280.         beq.s    _MATHbclr0
  281.         subq.w    #1,d0
  282.         clr.b    -(a0)
  283.         bra.s    _MATHbclr1
  284. _MATHbclr0    pull    a0/d0
  285.         rts
  286.         endc
  287.  
  288.         ifd      _MATHFbtst
  289. _MATHbtst    push    a0-a3/d1-d3
  290.         moveq    #-1,d1
  291.         cmp.b    #$49,(a0)
  292.         bhi.s    _MATHbtst0
  293.         moveq    #1,d1
  294. _MATHbtst1    tst.b    (a0)+
  295.         bne.s    _MATHbtst0
  296.         subq.w    #1,d0
  297.         bne.s    _MATHbtst1
  298.         moveq    #0,d1
  299. _MATHbtst0    move.l    d1,d0
  300.         pull    a0-a3/d1-d3
  301.         rts
  302.         endc
  303.  
  304.         ifd      _MATHFbneg
  305. _MATHbneg    push    a0-a3/d0-d3
  306.         moveq    #0,d3
  307.         add.w    d0,a0
  308. _MATHbneg1    tst.w    d0
  309.         beq.s    _MATHbneg0
  310.         subq.w    #1,d0
  311.         roxr.w    #1,d3
  312.         nbcd.b    -(a0)
  313.         roxl.w    #1,d3
  314.         bra.s    _MATHbneg1
  315. _MATHbneg0    pull    a0-a3/d0-d3
  316.         rts
  317.         endc
  318.  
  319.         ifd      _MATHFbadd
  320. _MATHbadd    push    a0-a3/d0-d3
  321.         clr.w    -(sp)
  322.         cmp.b    #$49,(a1)
  323.         bls.s    _MATHbadd0b
  324.         move.w    #$9999,(sp)
  325. _MATHbadd0b    add.w    d0,a0
  326.         add.w    d1,a1
  327.         moveq    #0,d3
  328. _MATHbadd1    tst.w    d0
  329.         beq.s    _MATHbadd0
  330.         subq.w    #1,d0
  331.         lea.l    2(sp),a2
  332.         tst.w    d1
  333.         beq.s    _MATHbadd2
  334.         subq.w    #1,d1
  335.         move.l    a1,a2
  336. _MATHbadd2    roxr.w    #1,d3
  337.         abcd.b    -(a2),-(a0)
  338.         roxl.w    #1,d3
  339.         move.l    a2,a1
  340.         bra.s    _MATHbadd1
  341. _MATHbadd0    pull    a0-a3/d0-d3
  342.         rts
  343.         endc
  344.  
  345.         ifd      _MATHFbsub
  346. _MATHbsub    push    a0-a3/d0-d3
  347.         clr.w    -(sp)
  348.         cmp.b    #$49,(a1)
  349.         bls.s    _MATHbsub0b
  350.         move.w    #$9999,(sp)
  351. _MATHbsub0b    add.w    d0,a0
  352.         add.w    d1,a1
  353.         moveq    #0,d3
  354. _MATHbsub1    tst.w    d0
  355.         beq.s    _MATHbsub0
  356.         subq.w    #1,d0
  357.         lea.l    2(sp),a2
  358.         tst.w    d1
  359.         beq.s    _MATHbsub2
  360.         subq.w    #1,d1
  361.         move.l    a1,a2
  362. _MATHbsub2    roxr.w    #1,d3
  363.         sbcd.b    -(a2),-(a0)
  364.         roxl.w    #1,d3
  365.         move.l    a2,a1
  366.         bra.s    _MATHbsub1
  367. _MATHbsub0    pull    a0-a3/d0-d3
  368.         rts
  369.         endc
  370.  
  371.         ifd      _MATHFbcmp
  372. _MATHbcmp    push    a0-a3/d1-d7
  373.         clr.b    d6    ;a0 pad
  374.         cmp.b    #$49,(a0)
  375.         bls.s    1$
  376.         move.b    #$99,d6
  377. 1$        clr.b    d7    ;a1 pad
  378.         cmp.b    #$49,(a1)
  379.         bls.s    2$
  380.         move.b    #$99,d7
  381. 2$        move.b    d6,d2
  382.         cmp.w    d1,d0
  383.         blo.s    3$
  384.         move.b    (a0)+,d2
  385.         subq.w    #1,d0
  386.         move.b    d7,d3
  387.         cmp.w    d0,d1
  388.         blo.s    4$
  389. 3$        move.b    (a1)+,d3
  390.         subq.w    #1,d1
  391. 4$        clrx
  392.         setz
  393.         sbcd.b    d3,d2
  394.         cmp.b    #$49,d2
  395.         bhi.s    _MATHbcmp.lt
  396.         tst.b    d2
  397.         bne.s    _MATHbcmp.gt
  398.         tst.w    d0
  399.         bne.s    2$
  400.         tst.w    d1
  401.         bne.s    2$
  402.         moveq    #0,d0
  403.         bra.s    _MATHbcmp0
  404. _MATHbcmp.lt    moveq    #-1,d0
  405.         bra.s    _MATHbcmp0
  406. _MATHbcmp.gt    moveq    #1,d0
  407. _MATHbcmp0    pull    d1-d7/a0-a3
  408.         cmp.w    #0,d0
  409.         rts
  410.         endc
  411.  
  412.         ifd      _MATHFbchk
  413. _MATHbchk    push    a0
  414.         add.w    d0,a0
  415. _MATHbchk1    tst.w    d0
  416.         beq.s    _MATHbchk0
  417.         subq.w    #1,d0
  418.         cmp.b    #$99,-(a0)
  419.         bls.s    _MATHbchk1
  420.         moveq    #-1,d0
  421. _MATHbchk0    pull    a0
  422.         ext.l    d0
  423.         rts
  424.         endc
  425.  
  426.         ifd      _MATHFmuls
  427. _MATHmuls    push    d2
  428.         move.l    d0,d2
  429.         eor.l    d1,d2
  430.         tst.l    d0
  431.         bpl.s    _muls1
  432.         neg.l    d0
  433. _muls1        tst.l    d1
  434.         bpl.s    _muls2
  435.         neg.l    d1
  436. _muls2        mathlib    mulu
  437.         tst.l    d2
  438.         bpl.s    _muls3
  439.         neg.l    d0
  440.         negx.l    d1
  441. _muls3        pull    d2/d2
  442.         rts
  443.         endc
  444.  
  445.         ifd      _MATHFmulu
  446. _MATHmulu    push    d2-d4
  447.         moveq    #0,d2
  448.         moveq    #0,d3
  449.         moveq    #0,d4
  450. _mulu1        lsr.l    #1,d0
  451.         bcc.s    _mulu2
  452.         add.l    d1,d2
  453.         addx.l    d4,d3
  454. _mulu2        asl.l    #1,d1
  455.         roxl.l    #1,d4
  456.         tst.l    d0
  457.         bne.s    _mulu1
  458.         move.l    d2,d0
  459.         move.l    d3,d1
  460.         pull    d2-d4
  461.         rts
  462.         endc
  463.  
  464.         ifd      _MATHFdivs
  465. _MATHdivs    push    d2-d3
  466.         move.l    d0,d2
  467.         move.l    d0,d3
  468.         eor.l    d1,d2
  469.         tst.l    d0
  470.         bpl.s    _divs1
  471.         neg.l    d0
  472. _divs1        tst.l    d1
  473.         bpl.s    _divs2
  474.         neg.l    d1
  475. _divs2        mathlib    divu
  476.         tst.l    d2
  477.         bpl.s    _divs3
  478.         neg.l    d0
  479. _divs3        tst.l    d3
  480.         bpl.s    _divs4
  481.         neg.l    d1
  482. _divs4        pull    d2-d3
  483.         rts
  484.         endc
  485.  
  486.         ifd      _MATHFdivu
  487. _MATHdivu    push    d2-d4
  488.         moveq.l    #0,d2
  489.         moveq.l    #31,d4
  490. _divu1        roxl.l    #1,d0    ; divident
  491.         roxl.l    #1,d2    ; work accum
  492.         cmp.l    d1,d2    ; cmp with divisor
  493.         blo.s    _divu2
  494.         sub.l    d1,d2
  495.         setx
  496. _divu2        roxl.l    #1,d3    ; result
  497.         dbf    d4,_divu1
  498.         move.l    d3,d0
  499.         move.l    d2,d1
  500.         pull    d2-d4
  501.         rts
  502.         endc
  503.  
  504.     endm
  505.  
  506.  
  507.