home *** CD-ROM | disk | FTP | other *** search
/ Kosovo Orphans' Appeal Charity CD / KosovoOrphansAppeal.iso / archimedesworld_cd2 / acornanswers / _scape2 / s / macros1 < prev    next >
Text File  |  1995-12-01  |  9KB  |  290 lines

  1. ; rbbc
  2. ; Reverse Bit Binary Counter.
  3. ; A macro to increment a register according to a reverse bit binary count.
  4. ; It takes four parameters, each of which should be a register name.
  5. ;
  6. ; $r:   holds value to be rbbc incremented. Will be modified
  7. ; $k:   k is st k-1=bit # (1 to 31) of msb in register (lsb in rbbc) (thence old w=3*(32-k)).
  8. ; $e:   scratch register used to hold an address in the code
  9. ;
  10.         MACRO
  11. $label  rbbc    $r, $k, $e
  12.  
  13.         ASSERT  $r <> $k
  14.         ASSERT  $r <> $e
  15.         ASSERT  $k <> $e
  16.  
  17.         LCLA    counter
  18. counter SETA    31
  19.  
  20. $label    RSB    $k, $k, #32
  21.     RSB    $k, $k, $k, ASL #2
  22.     ADD     $e, pc, $k, ASL #2      ;this relys on there being exactly one instruction between here & TST
  23.         MOV     pc, $e
  24.  
  25.         WHILE   counter > 1
  26.         TST     $r, #1<<counter
  27.         EOR     $r, $r, #1<<counter
  28.         BEQ     %ft0
  29. counter SETA    counter-1
  30.         WEND
  31.  
  32.         TST     $r, #1<<1
  33.         EOR     $r, $r, #1<<1
  34.         EORNE   $r, $r, #1<<0           ;nb an rbbc carry will automatically cause a wrap around back to zero
  35.  
  36. 0
  37.  
  38.         MEND
  39.  
  40.  
  41.  
  42. ; div16
  43. ; assumes abs number < 65536 * abs divisor.
  44. ; calculates integer part of 65536*number/divisor, as a signed 32 bit number.
  45. ; used in matrix inversion routine where it is necessary to divide two fixed point numbers (16 bit fraction).
  46. ; if range check on above assumption is required, the caller must perform it.
  47. ; It takes 5 parameters - all register names.
  48. ;
  49. ; $number:      number
  50. ; $divisor:     divisor - sign of divisor may be corrupted
  51. ; $num:         scratch & result - may be the same as $number
  52. ; $sign:        scratch
  53. ; $rem:         scratch
  54. ;
  55.         MACRO
  56. $label  div16   $number, $divisor, $num, $sign, $rem
  57.  
  58.         ASSERT  $num <> $divisor
  59.         ASSERT  $num <> $sign
  60.         ASSERT  $num <> $rem
  61.         ASSERT  $number <> $divisor
  62.         ASSERT  $number <> $sign
  63.         ASSERT  $number <> $rem
  64.         ASSERT  $divisor <> $sign
  65.         ASSERT  $divisor <> $rem
  66.         ASSERT  $sign <> $rem
  67.  
  68.         LCLA    counter
  69. counter SETA    32
  70.  
  71. $label  MOVS    $rem, $divisor, LSL #1
  72.         RSBCS   $divisor, $divisor, #0
  73.         ADC     $sign, $sign, $sign             ;1st 3 instructions allow setting of low bit sign as either 0
  74.                                                 ;or 1 according to sign of divisor, in only 1 instruction.
  75.         TEQ     $number, #0
  76.         RSBMI   $num, $number, #0
  77.         [       $num <> $number
  78.         MOVPL   $num, $number                   ;only assemble this case if regs different, as is otherwise a nop.
  79.         ]
  80.         EORMI   $sign, $sign, #1
  81.  
  82.         MOV     $rem, $num, LSR #16
  83.         MOV     $num, $num, LSL #16
  84.  
  85.         WHILE   counter > 0                     ;unwound loop uses about 600 bytes extra memory
  86.         MOVS    $num, $num, ASL #1              ;speed increase (ARM3) from about 1.27e-5 to .75e-5 seconds
  87.         ADC     $rem, $rem, $rem
  88.         CMP     $rem, $divisor
  89.         SUBHS   $rem, $rem, $divisor
  90.         ORRHS   $num, $num, #1
  91. counter SETA    counter-1
  92.         WEND
  93.  
  94.         CMP     $rem, $divisor, ASR #1
  95.         ADDGE   $num, $num, #1
  96.         TST     $sign, #1
  97.         RSBNE   $num, $num, #0
  98.  
  99.         MEND
  100.  
  101.  
  102.  
  103. ; mul16
  104. ; multiplication of an integer x by a 16-bit fixed point number a, with no restrictions on x or a
  105. ; other than that x*a/65536 must fit into a signed 32-bit representation.
  106. ; calculates r=x*a/65536
  107. ; It takes 6 parameters - all register names.
  108. ;
  109. ; $x:   x
  110. ; $a:   a - a will be corrupted
  111. ; $r:   result - may be the same as $x
  112. ; $u:   scratch
  113. ; $v:   scratch
  114. ; $w:   scratch
  115. ;
  116.         MACRO
  117. $label  mul16   $x, $a, $r, $u, $v, $w
  118.  
  119.         ASSERT  $r <> $a
  120.         ASSERT  $r <> $u
  121.         ASSERT  $r <> $v
  122.         ASSERT  $r <> $w
  123.         ASSERT  $x <> $a
  124.         ASSERT  $x <> $u
  125.         ASSERT  $x <> $v
  126.         ASSERT  $x <> $w
  127.         ASSERT  $a <> $u
  128.         ASSERT  $a <> $v
  129.         ASSERT  $a <> $w
  130.         ASSERT  $u <> $v
  131.         ASSERT  $u <> $w
  132.         ASSERT  $v <> $w
  133.  
  134. $label  MOVS    $w, $a, LSL #1
  135.         RSBCS   $a, $a, #0
  136.         TEQ     $x, #0
  137.         RSBMI   $r, $x, #0
  138.         [       $r <> $x
  139.         MOVPL   $r, $x
  140.         ]                       ;now have C bit set iff a<0, N bit set iff x<0
  141.         MOV     $w, pc, LSR #31
  142.         MOV     $w, $w, LSL #28 ;get w=(2^28)*N {N is b31, V is b28, C is b29 in pc/psr}
  143.         TEQP    $w, pc, LSR #1  ;EOR w into pc/psr {ie N EOR C is put into V}  - this is a way of storing
  144.                                 ;sign of result in overflow flag, saving on a register
  145.         MOV     $w, $r, LSR #16
  146.         BIC     $r, $r, $w, LSL #16
  147.         MOV     $v, $a, LSR #16
  148.         BIC     $a, $a, $v, LSL #16
  149.         MUL     $u, $a, $r
  150.         TST     $u, #1<<15      ;notice this is the only operation altering psr & that it will not corrupt V
  151.                                 ;note it will corrupt C, as immediate operand 32768 requires a shift
  152.         MOV     $u, $u, LSR #16
  153.         ADDNE   $u, $u, #1
  154.         MLA     $u, $r, $v, $u    ;6/9/94 - reverse v & r to optimise for argument a small
  155.         MLA     $u, $a, $w, $u
  156.         MUL     $r, $w, $v    ;6/9/94 - reverse v & w to optimise for argument a small
  157.         ADD     $r, $u, $r, LSL #16
  158.         RSBVS   $r, $r, #0
  159.  
  160.         MEND
  161.  
  162.  
  163.  
  164. ; mul16c
  165. ; multiplication of an integer x by a 16-bit fixed point number a, where a is a contraction
  166. ; ie calculates r=x*a/65536, assuming: abs a < 65536
  167. ; if the possibility exists that a>=65536, caller should check this & either not call routine, or set a=65535
  168. ; It takes 4 parameters - all register names.
  169. ;
  170. ; $x:   x
  171. ; $a:   a - require abs a <65536, a will be corrupted
  172. ; $r:   result - may be the same as $x
  173. ; $w:   scratch
  174. ;
  175.         MACRO
  176. $label  mul16c  $x, $a, $r, $w
  177.  
  178.         ASSERT  $r <> $a
  179.         ASSERT  $r <> $w
  180.         ASSERT  $x <> $a
  181.         ASSERT  $x <> $w
  182.         ASSERT  $a <> $w
  183.  
  184. $label  MOVS    $w, $a, LSL #1
  185.         RSBCS   $a, $a, #0
  186.         TEQ     $x, #0
  187.         RSBMI   $r, $x, #0
  188.         [       $r <> $x
  189.         MOVPL   $r, $x
  190.         ]
  191.         MOV     $w, pc, LSR #31 ;see mul16 above, for comments re this part
  192.         MOV     $w, $w, LSL #28
  193.         TEQP    $w, pc, LSR #1
  194.  
  195.         TST     $a, #1<<16      ;bodge to allow code to function if $a is upto 2*65536-1
  196.         MOVNE   $r, $r, LSL #1  ;can occur in a contractive fn due to change of coordinates to handle 1x2 pixel aspect ratio
  197.         MOVNE   $a, $a, LSR #1  ;change made 3/4/94 - estimate overhead to ria of upto 3%
  198.  
  199.         MOV     $w, $r, LSR #16
  200.         BIC     $r, $r, $w, LSL #16
  201.         MUL     $r, $a, $r
  202.         TST     $r, #1<<15      ;remember, can't use movs r,r,lsr #16, as this would corrupt V, which is storing sign of result
  203.         MOV     $r, $r, LSR #16
  204.         ADDNE   $r, $r, #1
  205.         MLA     $r, $a, $w, $r
  206.  
  207.         RSBVS   $r, $r, #0
  208.  
  209.         MEND
  210.  
  211.  
  212.  
  213. ; sqrt16
  214. ; integer square root returning 16-bit fixed point number, assuming x is 16-bit fixed point
  215. ; ie returns sqrt(x<<16)
  216. ; It takes 6 parameters - all register names
  217. ;
  218. ; $x:   x
  219. ; $r:   result - may be the same as x
  220. ; $n:   copy of x used during calculation
  221. ; $t:   transient scratch
  222. ; $d:   remainder
  223. ; $o:   constant value of 1
  224. ;
  225.         MACRO
  226. $label  sqrt16  $x, $r, $n, $t, $d, $o
  227.  
  228.         ASSERT  $x <> $n
  229.         ASSERT  $x <> $t
  230.         ASSERT  $x <> $d
  231.         ASSERT  $x <> $o
  232.         ASSERT  $n <> $t
  233.         ASSERT  $n <> $d
  234.         ASSERT  $n <> $o
  235.         ASSERT  $t <> $d
  236.         ASSERT  $t <> $o
  237.         ASSERT  $d <> $o
  238.  
  239.         LCLA    counter
  240.  
  241. $label  MOV     $n, $x
  242.         MOV     $o, #1
  243.         MOV     $x, #0
  244.         MOV     $d, #0
  245.  
  246.         AND     $t, $n, #(3<<30)
  247.         MOV     $t, $t, LSR #30
  248.         ORR     $d, $d, $t
  249.         ADD     $t, $o, $x, LSL #1
  250.         CMP     $d, $t
  251.         SUBGE   $d, $d, $t
  252.         ADDGE   $x, $x, #1
  253.  
  254. counter SETA    28
  255.         WHILE   counter < 32             ;want >= 0, however counter is unsigned, so after 0 it goes high, thus < 32 will catch it!
  256.         MOV     $x, $x, LSL #1
  257.         MOV     $d, $d, LSL #2
  258.         AND     $t, $n, #(3<<counter)
  259.         [       counter <> 0
  260.         MOV     $t, $t, LSR #counter
  261.         ]
  262.         ORR     $d, $d, $t
  263.         ADD     $t, $o, $x, LSL #1
  264.         CMP     $d, $t
  265.         SUBGE   $d, $d, $t
  266.         ADDGE   $x, $x, #1
  267. counter SETA    counter-2
  268.         WEND
  269.  
  270. counter SETA    14
  271.         WHILE   counter < 32
  272.         MOV     $x, $x, LSL #1
  273.         MOV     $d, $d, LSL #2
  274.         ADD     $t, $o, $x, LSL #1
  275.         CMP     $d, $t
  276.         SUBGE   $d, $d, $t
  277.         ADDGE   $x, $x, #1
  278. counter SETA    counter-2
  279.         WEND
  280.  
  281.         [       $r <> $x
  282.         MOV     $r, $x
  283.         ]
  284.  
  285.         MEND
  286.  
  287.  
  288.  
  289.         END
  290.