home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 364b.lha / PCQ_v1.1 / Runtime / PCQ / lmath.asm < prev    next >
Encoding:
Assembly Source File  |  1990-04-08  |  3.6 KB  |  223 lines

  1.  
  2.  
  3. ********
  4. lmath.s
  5. ********
  6. * Copyright (c) 1988 by Sozobon, Limited.  Author: Johann Ruegg
  7. *
  8. * Permission is granted to anyone to use this software for any purpose
  9. * on any computer system, and to redistribute it freely, with the
  10. * following restrictions:
  11. * 1) No charge may be made other than reasonable charges for reproduction.
  12. * 2) Modified versions must be clearly marked as such.
  13. * 3) The authors are not responsible for any harmful consequences
  14. *    of using this software, even if they result from defects in it.
  15. *
  16.  
  17. *    For PCQ Pascal:
  18. *         These are the 32-bit math functions from Sozobon-C,
  19. *    as noted above.  I changed the names of the routines to
  20. *    be more similar to the rest of my library, and handle the
  21. *    divide by zero condition differently.  Other than that I
  22. *    haven't changed the code a bit.
  23.  
  24.     XREF    _p%ExitWithAddr
  25.  
  26.     XDEF    _p%ldiv
  27. _p%ldiv:
  28.     move.l    4(a7),d0
  29.     bpl    ld1
  30.     neg.l    d0
  31. ld1:
  32.     move.l    8(a7),d1
  33.     bpl    ld2
  34.     neg.l    d1
  35.     eor.b    #$80,4(a7)
  36. ld2:
  37.     bsr    i_ldiv        /* d0 = d0/d1 */
  38.     tst.b    4(a7)
  39.     bpl    ld3
  40.     neg.l    d0
  41. ld3:
  42.     rts
  43.  
  44.     XDEF    _p%lmul
  45. _p%lmul:
  46.     move.l    4(a7),d0
  47.     bpl    lm1
  48.     neg.l    d0
  49. lm1:
  50.     move.l    8(a7),d1
  51.     bpl    lm2
  52.     neg.l    d1
  53.     eor.b    #$80,4(a7)
  54. lm2:
  55.     bsr    i_lmul        /* d0 = d0*d1 */
  56.     tst.b    4(a7)
  57.     bpl    lm3
  58.     neg.l    d0
  59. lm3:
  60.     rts
  61.  
  62.     XDEF    _p%lrem
  63. _p%lrem:
  64.     move.l    4(a7),d0
  65.     bpl    lr1
  66.     neg.l    d0
  67. lr1:
  68.     move.l    8(a7),d1
  69.     bpl    lr2
  70.     neg.l    d1
  71. lr2:
  72.     bsr    i_ldiv        /* d1 = d0%d1 */
  73.     move.l    d1,d0
  74.     tst.b    4(a7)
  75.     bpl    lr3
  76.     neg.l    d0
  77. lr3:
  78.     rts
  79.  
  80.     XDEF    _p%ldivu
  81. _p%ldivu:
  82.     move.l    4(a7),d0
  83.     move.l    8(a7),d1
  84.     bsr    i_ldiv
  85.     rts
  86.  
  87.     XDEF    _p%lmulu
  88. _p%lmulu:
  89.     move.l    4(a7),d0
  90.     move.l    8(a7),d1
  91.     bsr    i_lmul
  92.     rts
  93.  
  94.     XDEF    _p%lremu
  95. _p%lremu:
  96.     move.l    4(a7),d0
  97.     move.l    8(a7),d1
  98.     bsr    i_ldiv
  99.     move.l    d1,d0
  100.     rts
  101.  
  102. * A in d0, B in d1, return A*B in d0
  103.     XDEF    i_lmul
  104. i_lmul:
  105.     move.l    d3,a2        /* save d3 */
  106.     move.w    d1,d2
  107.     mulu    d0,d2        /* d2 = Al * Bl */
  108.  
  109.     move.l    d1,d3
  110.     swap    d3
  111.     mulu    d0,d3        /* d3 = Al * Bh */
  112.  
  113.     swap    d0
  114.     mulu    d1,d0        /* d0 = Ah * Bl */
  115.  
  116.     add.l    d3,d0        /* d0 = (Ah*Bl + Al*Bh) */
  117.     swap    d0
  118.     clr.w    d0        /* d0 = (Ah*Bl + Al*Bh) << 16 */
  119.  
  120.     add.l    d2,d0        /* d0 = A*B */
  121.     move.l    a2,d3        /* restore d3 */
  122.     rts
  123.  
  124. *A in d0, B in d1, return A/B in d0, A%B in d1
  125.     XDEF    i_ldiv
  126. i_ldiv:
  127.     tst.l    d1
  128.     bne.s    nz1
  129.  
  130. *    divide by zero
  131.     move.l    #55,d0
  132.     jsr    _p%ExitWithAddr    /* cause trap */
  133. nz1:
  134.     move.l    d3,a2        /* save d3 */
  135.     cmp.l    d1,d0
  136.     bhi    norm
  137.     beq    is1
  138. *    A<B, so ret 0, rem A
  139.     move.l    d0,d1
  140.     clr.l    d0
  141.     move.l    a2,d3        /* restore d3 */
  142.     rts
  143. *    A==B, so ret 1, rem 0
  144. is1:
  145.     moveq.l    #1,d0
  146.     clr.l    d1
  147.     move.l    a2,d3        /* restore d3 */
  148.     rts
  149. *    A>B and B is not 0
  150. norm:
  151.     cmp.l    #1,d1
  152.     bne.s    not1
  153. *    B==1, so ret A, rem 0
  154.     clr.l    d1
  155.     move.l    a2,d3        /* restore d3 */
  156.     rts
  157. *  check for A short (implies B short also)
  158. not1:
  159.     cmp.l    #$ffff,d0
  160.     bhi    slow
  161. *  A short and B short -- use 'divu'
  162.     divu    d1,d0        /* d0 = REM:ANS */
  163.     swap    d0        /* d0 = ANS:REM */
  164.     clr.l    d1
  165.     move.w    d0,d1        /* d1 = REM */
  166.     clr.w    d0
  167.     swap    d0
  168.     move.l    a2,d3        /* restore d3 */
  169.     rts
  170. * check for B short
  171. slow:
  172.     cmp.l    #$ffff,d1
  173.     bhi    slower
  174. * A long and B short -- use special stuff from gnu
  175.     move.l    d0,d2
  176.     clr.w    d2
  177.     swap    d2
  178.     divu    d1,d2        /* d2 = REM:ANS of Ahi/B */
  179.     clr.l    d3
  180.     move.w    d2,d3        /* d3 = Ahi/B */
  181.     swap    d3
  182.  
  183.     move.w    d0,d2        /* d2 = REM << 16 + Alo */
  184.     divu    d1,d2        /* d2 = REM:ANS of stuff/B */
  185.  
  186.     move.l    d2,d1
  187.     clr.w    d1
  188.     swap    d1        /* d1 = REM */
  189.  
  190.     clr.l    d0
  191.     move.w    d2,d0
  192.     add.l    d3,d0        /* d0 = ANS */
  193.     move.l    a2,d3        /* restore d3 */
  194.     rts
  195. *    A>B, B > 1
  196. slower:
  197.     move.l    #1,d2
  198.     clr.l    d3
  199. moreadj:
  200.     cmp.l    d0,d1
  201.     bhi.s    adj
  202.     add.l    d2,d2
  203.     add.l    d1,d1
  204.     bpl    moreadj
  205. * we shifted B until its >A or sign bit set
  206. * we shifted #1 (d2) along with it
  207. adj:
  208.     cmp.l    d0,d1
  209.     bhi.s    ltuns
  210.     or.l    d2,d3
  211.     sub.l    d1,d0
  212. ltuns:
  213.     lsr.l    #1,d1
  214.     lsr.l    #1,d2
  215.     bne    adj
  216. * d3=answer, d0=rem
  217.     move.l    d0,d1
  218.     move.l    d3,d0
  219.     move.l    a2,d3        /* restore d3 */
  220.     rts
  221.  
  222.     END
  223.