Advertisement
Zeda

f8p16_abs_inv

Mar 28th, 2020
1,637
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. f8p16_abs_inv:
  2. ;| 1.0/(ix+6) | ==> HL (8.16 fixed-point)
  3. ;only computes 8 bits of fractional precision
  4. ;overflow ==> 0x7FFFFF
  5. ;underflow ==> 0x000000
  6.  
  7. ;get the denominator in DE
  8.   ld de,(ix+6)
  9.  
  10. ;make sure DE is negative
  11. ;Also, using ix+6 as scrap, so set that to 0
  12.   xor a
  13.   sbc hl,hl
  14.   ld (ix+6),hl
  15.   sbc hl,de
  16.   jp p,f8p16_abs_inv_DE_negated
  17.   ex de,hl
  18. f8p16_abs_inv_DE_negated:
  19.  
  20. ;if 1.0>DE, then output is less than 1.0
  21. ;if 1.0<DE, then output is greater than 1.0
  22. ;if 1.0=DE, then output is 1.0
  23.   ld hl,65535
  24.   adc hl,de
  25.  
  26. ;suppose input <= 1. Then carry is set
  27. ;suppose input > 1. Then carry reset
  28.   jr c,f8p16_abs_inv_big
  29. f8p16_abs_inv_small:
  30. ;DE>1.0
  31.   sbc hl,de
  32. ;HL is 1.0
  33. ;so we know the top 8 bits of the result are 0
  34.   jr f8p16_abs_inv_frac
  35.  
  36. f8p16_abs_inv_big:
  37. ;so input <= 1.0
  38.   jr z,return_1p00
  39. ;so input < 1.0
  40.  
  41.   ld hl,256   ;numerator
  42.   call f8p16_abs_inv_sub
  43.   or a
  44.   jp m,f8p16_abs_inv_overflow
  45.   ld (ix+8),a
  46.  
  47. f8p16_abs_inv_frac:
  48.   call f8p16_abs_inv_sub
  49.  
  50. ;want to round
  51.   add hl,hl
  52.   add hl,de
  53.  
  54.   ld hl,(ix+6)
  55.   ld h,a
  56.   ret nc
  57.   ld de,255
  58.   adc hl,de
  59.   ret p
  60. f8p16_abs_inv_overflow:
  61.   ld hl,$7FFFFF
  62.   ret
  63.  
  64. return_1p00:
  65.   ld hl,$010000
  66.   ret
  67.  
  68. f8p16_abs_inv_sub:
  69.   ld b,8
  70. f8p16_abs_inv_sub_loop:
  71.   add hl,hl
  72.   add hl,de
  73.   jr c,$+4
  74.   sbc hl,de
  75.   rla
  76.   djnz f8p16_abs_inv_sub_loop
  77.   ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement