home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / sys / mac / programm / 18485 < prev    next >
Encoding:
Internet Message Format  |  1992-11-17  |  5.3 KB

  1. Path: sparky!uunet!ferkel.ucsb.edu!taco!rock!stanford.edu!agate!doc.ic.ac.uk!uknet!mcsun!news.funet.fi!funic!nntp.hut.fi!vipunen.hut.fi!jmunkki
  2. From: jmunkki@vipunen.hut.fi (Juri Munkki)
  3. Newsgroups: comp.sys.mac.programmer
  4. Subject: Re: Help! making an assembly routine faster
  5. Message-ID: <1992Nov16.190947.9920@nntp.hut.fi>
  6. Date: 16 Nov 92 19:09:47 GMT
  7. References: <1992Nov14.091905.29520@cs.uoregon.edu> <1992Nov14.200831.20477@nntp.hut.fi> <1992Nov16.014850.28678@cs.uoregon.edu>
  8. Sender: usenet@nntp.hut.fi (Usenet pseudouser id)
  9. Reply-To: jmunkki@vipunen.hut.fi (Juri Munkki)
  10. Organization: Helsinki University of Technology
  11. Lines: 123
  12. Nntp-Posting-Host: vipunen.hut.fi
  13.  
  14. In article <1992Nov16.014850.28678@cs.uoregon.edu> mkelly@mystix.cs.uoregon.edu (Michael A. Kelly) writes:
  15. >So, here are the resulting routines.  The first uses the jump table approach,
  16. >the second uses the wide mask approach.  Can they be made even faster??
  17.  
  18. Yes.
  19.  
  20. >    @2:                               ; copy the next eight bytes in the row
  21. >    
  22. >        MOVE.B    (A2), D2            ; copy the next mask byte
  23. >        
  24. >        TST.B     D2
  25.  
  26. A move instruction always does an implied tst, so you can just throw away
  27. the test instruction.
  28.  
  29. >        BEQ       @nocopy             ; if zero, don't copy anything
  30. >        
  31. >        CMPI.B    #0xFF, D2
  32. >        BNE       @hardway            ; don't copy everything
  33.  
  34. An addq.w #1, and then a beq might prove to be faster than the cmp with
  35. an immediate value. You have to adjust the mask back to its old value,
  36. if the test fails, but this can be done either with the jump tables
  37. (not with the ones you are using now, but the longer ones I will suggest
  38. later in this article) or by a subq.w #1
  39.  
  40. >        
  41. >        MOVE.L    (A0)+, (A1)+        ; copy all bytes
  42. >        MOVE.L    (A0)+, (A1)+
  43. >        ADDQ.L    #1, A2
  44.  
  45. Do a move.b (A2)+ instead of this instruction. I can't see any reason why
  46. you can't do the increment there.
  47.  
  48. >        JMP       @endloop
  49.  
  50. Copy the end of the loop here. So that you have the DBF instruction here
  51. instead of a JMP. Put the jump after the DBF. There's absolutely no reason
  52. to jump around when you can just use another DBF.
  53.  
  54. >    @nocopy:                          ; copy no bytes
  55. >        ADDQ.L    #8, A0
  56. >        ADDQ.L    #8, A1
  57. >        ADDQ.L    #1, A2
  58. >        JMP       @endloop
  59.  
  60. Same here as above.
  61.  
  62. >    @hardway:
  63. >        ANDI.L    #0xF0, D2           ; mask off the low four bits
  64. >        LSR.W     #4, D2              ; shift bits 4-7 into bits 0-3
  65.  
  66. The AND is totally wasted. The LSR will do the masking for you. This
  67. is assuming that you can keep the high bytes of D2 cleared. I think
  68. you should be able to do it. (I think it's already that way.)
  69.  
  70. You can also eliminate the and and lsr, if you use two 256-entry jump
  71. tables that simply ignore the high or low 4 bits. The tables will take
  72. some memory (2 x 4 x 256 bytes), but they are easy to construct with
  73. copy and paste.
  74.  
  75. >        ADD.W     D2, D2              ; double the index
  76. >        ADD.W     @table(D2.W), D2    ; calculate the address
  77. >        JSR       @table(D2.W)        ; plot four pixels
  78.  
  79. The 68020 has addressing modes that do the multiplication of the index.
  80. I haven't needed them myself, but I'm fairly certain that you can improve
  81. this part with the right addressing mode.
  82.  
  83. Replace the jsr with a LEA An to the return address and a JMP to the
  84. subroutine. Then jump back with a JMP (An). This is quite a bit faster
  85. than a JSR/RTS combination, although it's not "good style".
  86.  
  87. >        CLR.L     D2                  ; clear the mask register
  88. >        MOVE.B    (A2)+, D2           ; copy the next mask byte
  89. >        ANDI.B    #0xF, D2            ; mask off the high four bits
  90.  
  91. Use BFEXTU, if you must read the mask again. Remember that you can use
  92. -1(A2), if you already incremented A2 or you might be able to account
  93. for this with the bitfield offset. You can also use constant bitfield
  94. offsets, if I remember correctly. I think you have some registers that
  95. you could use, so you could store fairly constant bitfield indices
  96. there.
  97.  
  98. >    @sub6:                            ; mask = 0110
  99. >        ADDQ.L    #1, A0
  100. >        ADDQ.L    #1, A1
  101. >        MOVE.B    (A0)+, (A1)+
  102.  
  103. This should be a move.w
  104.  
  105. >        ADDQ.L    #1, A0
  106. >        ADDQ.L    #1, A1
  107. >        RTS
  108. >    
  109. >    @sub8:                            ; mask = 1000
  110. >        MOVE.B    (A0)+, (A1)+
  111. >        ADDQ.L    #3, A0
  112. >        ADDQ.L    #3, A1
  113. >        RTS
  114.  
  115. A move.b (a0),(a1) along with addq #4 is faster on a 68000, but I
  116. don't think it matters on new processors. I may be wrong, but you'll
  117. probably never see the difference.
  118.  
  119. In the deep mask version, you could unroll the loop. It's kind of
  120. surprising the the 1 bit mask is actually faster, but it's mostly
  121. because of the superior algorithm that allows you to directly copy
  122. 8 bytes at a time in the most common case.
  123.  
  124. I think you did really well with the assembly. My changes will probably
  125. not make a big difference. I think 5% is the best you can hope for, but
  126. it might be as much as 10%. The only way to go beyond this is to make
  127. the move.l commands aligned on long word destinations, as I mentioned
  128. in my previous article.
  129.  
  130. I hope my articles offer proof for the other half of my .signature... :-)
  131. Can anyone do significantly better? I really love optimizing graphics
  132. routines.
  133.  
  134. -- 
  135.   Juri Munkki                           Windsurf: fast sailing
  136.  jmunkki@hut.fi                          Macintosh: fast software
  137.