home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / sys / mac / programm / 18775 < prev    next >
Encoding:
Text File  |  1992-11-22  |  2.9 KB  |  129 lines

  1. Newsgroups: comp.sys.mac.programmer
  2. Path: sparky!uunet!mcsun!fuug!funic!nntp.hut.fi!vipunen.hut.fi!jmunkki
  3. From: jmunkki@vipunen.hut.fi (Juri Munkki)
  4. Subject: Re: 64-bit multiply and divide on 68000
  5. Message-ID: <1992Nov22.105122.4558@nntp.hut.fi>
  6. Keywords: 68000 Assembler Math discrete
  7. Sender: usenet@nntp.hut.fi (Usenet pseudouser id)
  8. Nntp-Posting-Host: vipunen.hut.fi
  9. Reply-To: jmunkki@vipunen.hut.fi (Juri Munkki)
  10. Organization: Helsinki University of Technology
  11. References: <By3BM7.InD@world.std.com>
  12. Date: Sun, 22 Nov 1992 10:51:22 GMT
  13. Lines: 114
  14.  
  15. In article <By3BM7.InD@world.std.com> squeegee@world.std.com (Stephen C. Gilardi) writes:
  16. >I need to calculate a linear interpolation rapidly.  The equation is
  17. >
  18. >  y = x * numer / denom,
  19. >
  20. >where y, numer, and denom are 32-bit unsigned longs and x is a 16-bit
  21. >unsigned short.
  22. >
  23. >On the 68020, I can use the built-in "long" multiply and divide instructions.
  24. >However on the 68000 it's more involved.
  25. >
  26. >I found 68000 code in a book to do the 64-bit unsigned multiply.  I'd
  27. >like to have the divide in assembly language as well.
  28. >
  29. >Does anyone have such a routine, or a reference to where I can find one?
  30.  
  31. I'm also interested in a good division routine for the 68K. Here's a routine
  32. that does essentially what you wanted to do and a bit more. It uses 32 bit
  33. fixed point numbers. I would very much like to see an improved division
  34. routine. I wasn't able to figure out anything better on my own and since
  35. my main target processor is the 68030, this version is not used all that
  36. often.
  37.  
  38. Since your x is a short, you will save quite a bit of time by using that
  39. knowledge to optimize the code.
  40.  
  41. Fixed    FMulDiv68000(a,b,c)
  42. Fixed    a,b,c;
  43. {
  44. asm    {
  45.         movem.l    D3-D5,-(sp)
  46.         move.l    a,D0
  47.         bpl.s    @positive_a
  48.         neg.l    c
  49.         neg.l    D0
  50. @positive_a
  51.         move.l    b,D1
  52.         bpl.s    @positive_b
  53.         neg.l    D1
  54.         neg.l    c
  55. @positive_b
  56.         move.w    D1,D2
  57.         mulu.w    D0,D2        ;    D2 = Lo * Lo
  58.  
  59.         move.w    D1,D3
  60.         swap    D0
  61.         mulu.w    D0,D3        ;    D3 = Hi * Lo
  62.         
  63.         swap    D1
  64.         move.w    D1,D4
  65.         mulu.w    D0,D4        ;    D4 = Hi * Hi
  66.         
  67.         swap    D0
  68.         mulu.w    D0,D1        ;    D1 = Lo * Hi
  69.         
  70.         clr.l    D5
  71.         move.w    D3,D5        
  72.         swap    D5
  73.         clr.w    D3
  74.         swap    D3
  75.         add.l    D5,D2        ;    64 bit addition Hi*Hi:Lo*Lo += Hi*Lo
  76.         addx.l    D3,D4
  77.         
  78.         clr.l    D5
  79.         move.w    D1,D5
  80.         swap    D5
  81.         clr.w    D1
  82.         swap    D1
  83.         add.l    D5,D2        ;    64 bit addition Hi*Hi:Lo*Lo+Hi*Lo += Lo*Hi
  84.         addx.l    D1,D4        ;    Result is now in D4:D2
  85.         
  86.         add.l    D2,D2
  87.         addx.l    D4,D4
  88.     
  89.         move.l    c,D1
  90.         bpl.s    @positive_c
  91.         neg.l    D1
  92. @positive_c
  93.         moveq.l    #1,D0
  94.         bra.s    @divloop
  95. @divok    
  96.         lsl.l    #1,D0
  97.         bcs.s    @divdone
  98.         add.l    D2,D2
  99.         addx.l    D4,D4
  100. @divloop
  101.         sub.l    D1,D4
  102.         bcc.s    @divok
  103.  
  104.         addx.l    D0,D0
  105.         bcs.s    @divdone
  106.  
  107.         add.l    D1,D4
  108.         add.l    D2,D2
  109.         addx.l    D4,D4
  110.         bra.s    @divloop
  111.  
  112. @divdone
  113.         move.l    c,D1
  114.         bpl.s    @positive_result
  115.         
  116.         addq.l    #1,D0
  117.         bra.s    @done
  118. @positive_result
  119.         eor.l    #-1,D0
  120. @done
  121.         movem.l    (sp)+,D3-D5
  122.     }
  123. }
  124.  
  125.  
  126. -- 
  127.   Juri Munkki                           Windsurf: fast sailing
  128.  jmunkki@hut.fi                          Macintosh: fast software
  129.