?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

  1. #ifndef included_xpow2
  2. #define included_xpow2
  3. #include "pushpop.z80"
  4. #include "mov.z80"
  5. #include "routines/sla64.z80"
  6. #include "routines/normalizexOP1.z80"
  7. #include "constantsx.z80"
  8. #include "xfma.z80"
  9.  
  10. ;This is just the same algorithm as the other pow2 routine, but using the FMA routine.
  11. ;It's three-bytes larger, probably slower, and isn't giving better accuracy, so not worth it.
  12.  
  13. #define xexp_x xOP3+42
  14. ;use range reduction to get x on [0,1]
  15. ;Then use the formula:
  16. ;2^x ~ 1+x(p1+x(p2+...+x(p11+x*p12)...))
  17. xpow2:
  18.   call pushpop
  19.   push bc
  20.   ld de,xOP1
  21.   call mov10
  22.   ld hl,(xOP1+8)
  23.   res 7,h
  24.   ld a,h
  25.   or l
  26.   jp z,caseexp
  27.   ;If HL<3FF3 or HL>=400E, then magnitudes are too high
  28.   ld bc,-$3EF3
  29.   add hl,bc
  30.   dec h
  31.   jp nz,exp_too_big
  32.   ld a,l
  33.   cp 27
  34.   jp nc,exp_too_big
  35. ;Now we know the exponent isn't too big
  36. ;
  37. ;Range Reduction phase
  38. ;e=int(x+.5)
  39. ;x-=e
  40. ;now we compute 2^e*2^x, where e is an integer
  41.   ld de,0
  42.   sub 13
  43.   inc a
  44.   jr c,xexp_ipart0
  45.   ld b,a
  46.   ;now we need to shift bits in from the mantissa!
  47. _:
  48.   call sla64_xOP1
  49.   rl e \ rl d
  50.   djnz -_
  51.   ld a,(xOP1+9)
  52.   add a,a
  53.   ld hl,$3FFF
  54.   jr nc,+_
  55.   xor a \ sub e \ ld e,a
  56.   sbc a,a \ sub d \ ld d,a
  57.   ld h,$BF
  58. _:
  59.   ld (xOP1+8),hl
  60.   call normalizexOP1
  61. xexp_ipart0:
  62. ;!!!! Future Zeda, investigate! I think this should actually be fine on (-1,1) :O
  63. ;!!!! If not, we'll need to check if the input is negative, add 1, decrement DE
  64.   push de
  65.   ld hl,xOP1
  66.   ld de,xexp_x
  67.   call mov10
  68.  
  69.   ld hl,xOP1 \ ld b,h \ ld c,l \ ld de,xexp_p12 \ ld ix,xexp_p11 \ call xfma
  70.   ld de,xexp_x \ ld ix,xexp_p10 \ call xfma
  71.   ld ix,xexp_p9 \ call xfma
  72.   ld ix,xexp_p8 \ call xfma
  73.   ld ix,xexp_p7 \ call xfma
  74.   ld ix,xexp_p6 \ call xfma
  75.   ld ix,xexp_p5\ call xfma
  76.   ld ix,xexp_p4 \ call xfma
  77.   ld ix,xexp_p3 \ call xfma
  78.   ld ix,xexp_p2 \ call xfma
  79.   ld ix,xexp_p1 \ call xfma
  80.   ld ix,xconst_1 \ call xfma
  81.  
  82.   pop de
  83.   ;now I need to add DE as a signed integer to the exponent
  84.   ld hl,(xOP1+8)
  85.   add hl,de
  86.   ld (xOP1+8),hl
  87.   pop de
  88.   ld h,b
  89.   ld l,c
  90.   jp mov10
  91. caseexp:
  92.   pop de
  93.   ret
  94. exp_too_big:
  95.   pop de
  96.   ret
  97. xexp_p1:
  98. xexp_p2 = xexp_p1+10
  99. xexp_p3 = xexp_p1+20
  100. xexp_p4 = xexp_p1+30
  101. xexp_p5 = xexp_p1+40
  102. xexp_p6 = xexp_p1+50
  103. xexp_p7 = xexp_p1+60
  104. xexp_p8 = xexp_p1+70
  105. xexp_p9 = xexp_p1+80
  106. xexp_p10= xexp_p1+90
  107. xexp_p11= xexp_p1+100
  108. xexp_p12= xexp_p1+110
  109. .db $A2,$78,$CF,$D1,$F7,$17,$72,$B1,$FF,$3F    ;0.6931471805599452949907356518859092751897692718065
  110. .db $99,$67,$2D,$16,$FC,$EF,$FD,$F5,$FD,$3F    ;0.240226506959101553103846007431060297021429734606215
  111. .db $10,$CD,$AF,$24,$B8,$46,$58,$E3,$FB,$3F    ;5.55041086648024431020190322727610690536837023045e-2
  112. .db $F8,$78,$4B,$E2,$7D,$5B,$95,$9D,$F9,$3F    ;9.6181291078536164406046473880520178751949899000523e-3
  113. .db $5B,$9D,$F3,$DF,$38,$FF,$C3,$AE,$F6,$3F    ;1.33335581307383609544222238286864444825050937393667e-3
  114. .db $A4,$71,$3C,$26,$F7,$89,$84,$A1,$F3,$3F    ;1.540353109220040892607797490525175686435054921034e-4
  115. .db $BC,$AB,$25,$27,$57,$E7,$E5,$FF,$EF,$3F    ;1.5252713038026820986694573437398168701085442050318e-5
  116. .db $66,$AE,$E1,$25,$E7,$82,$61,$B1,$EC,$3F    ;1.32159071914904607697946887057043728604916889120466e-6
  117. .db $FA,$40,$26,$7A,$19,$AD,$72,$DA,$E8,$3F    ;1.01722755371157108831058054858407530634720014307972e-7
  118. .db $5F,$A3,$56,$C8,$B3,$E2,$40,$F4,$E4,$3F    ;7.1087112650916814301740895279260149232576576809875e-9
  119. .db $8C,$EE,$B8,$06,$1D,$21,$F6,$E2,$E0,$3F    ;4.1284045941236206883307640217911126100813512882446e-10
  120. .db $3E,$73,$CA,$31,$87,$36,$5F,$9F,$DD,$3F    ;3.6236980527707885031220529305671221862606493025011e-11
  121.  
  122. #undefine xexp_x
  123. #endif
  124.