?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

  1.  ifndef included_xcos
  2.  define included_xcos
  3.  include "../common/pushpop.asm"
  4.  include "constantsx.asm"
  5.  include "xmul.asm"
  6.  include "xadd.asm"
  7.  include "xsub.asm"
  8.  include "xrsub.asm"
  9.  include "xmod1.asm"
  10.  include "xsin.asm"
  11.  
  12. cos_x=xOP1+60
  13. cos_y=xOP1+70
  14. temp=xOP1+80
  15.  
  16. xcos:
  17. ;cos(-pi/4<=x<pi/4)
  18. ;y=x*x
  19. ;1-y(.49999887-y(.041655882-y.0013591743))
  20. ;1-y(a1-y(a2-y*a3))
  21.   call pushpop
  22.   push bc
  23. ;Need to apply range reduction
  24. ; We want the input on [-pi/4,pi/4]
  25. ; First multiply by 1/(2pi)
  26.   ld de,xconst_2pi_inv
  27.   ld bc,cos_x
  28.   call xmul
  29.  
  30. ; Add .5
  31.   ld h,b
  32.   ld l,c
  33.   ld de,xconst_p5
  34.   call xadd
  35.  
  36. ;Now grab mod 1
  37.   call xmod1
  38.  
  39. ;subtract off the .5
  40.   call xsub
  41.   jr xcos_stepin
  42. xcos_readjust:
  43.   push bc
  44.   ld bc,cos_x
  45. xcos_stepin:
  46. ;now x is on [-.5,.5], but we need to evaluate cos(x*2pi) with x on [-.125,.125]
  47. ;We need to employ some identities
  48. ;  cos(-x)=cos(x)
  49. ;    make x positive
  50. ;  cos(x-pi)=-cos(x)
  51. ;   if our x is now on [.25,.5], then subtract .5 and take the absolute value,
  52. ;   and return the negative result.
  53. ;  cos(pi/2-x)=sin(x)
  54. ;    if our x is now on [.125,.25], subtract .25 and feed it to the sine routine.
  55.  
  56. ; maxe x positive
  57.   ;call absSingle
  58.   ld hl,cos_x+9
  59.   res 7,(hl)
  60.  
  61. ;Check if the exponent is -2 or more. If so, return -cos(x-pi)
  62.   dec hl
  63.   ld a,(hl)
  64.   ld h,b
  65.   ld l,c
  66.   sub $FE
  67.   jr c,xcos_small;+_
  68.   ld de,xconst_p5
  69.   call xsub
  70. ;We don't want to infinite loop if the result is .25-.5 = -.25 !
  71.   ld a,(cos_x+8)
  72.   sub $FE
  73.   jr nz,$+9
  74.   ld a,80h
  75.   ld (cos_x+8),a
  76.   jr xcos_small;+_
  77.   pop bc
  78.   call xcos_readjust
  79.   ld h,b
  80.   ld a,9
  81.   add a,c
  82.   ld l,a
  83.   jr nc,$+3
  84.   inc h
  85.   ld a,(hl)
  86.   xor $80
  87.   ld (hl),a
  88.   ret
  89. xcos_small;_:
  90. ;Check if the exponent is -3. If so, return sin(pi/2-x)
  91.   inc a
  92.   jr nz,xcos_noexpminus3;+_
  93.   ld de,xconst_p25
  94.   call xrsub
  95.   jp xsin_subroutine
  96. xcos_noexpminus3;_:
  97.   ld de,xconst_2pi
  98.   ld b,h
  99.   ld c,l
  100.   call xmul
  101.  
  102.   ld d,h
  103.   ld e,l
  104.   ld bc,cos_y
  105.   call xmul
  106.   ld h,b
  107.   ld l,c
  108.   ld de,xcos_p7
  109.   ld bc,temp
  110.   call xmul
  111.   ld hl,xcos_p6
  112.   ld d,b
  113.   ld e,c
  114.   call xadd
  115.   ld hl,cos_y
  116.   call xmul
  117.   ld hl,xcos_p5
  118.   call xadd
  119.   ld hl,cos_y
  120.   call xmul
  121.  
  122.   ld hl,xcos_p4
  123.   call xadd
  124.   ld hl,cos_y
  125.   call xmul
  126.  
  127.   ld hl,xcos_p3
  128.   call xadd
  129.   ld hl,cos_y
  130.   call xmul
  131.  
  132.   ld hl,xcos_p2
  133.   call xadd
  134.   ld hl,cos_y
  135.   call xmul
  136.  
  137.   ld hl,xcos_p1
  138.   call xadd
  139.   ld hl,cos_y
  140.   call xmul
  141.  
  142.   ld hl,xcos_p0
  143.   pop bc
  144.   jp xadd
  145. ;#undefine cos_x
  146. ;#undefine cos_y
  147. ;#undefine temp
  148.  
  149. xcos_p0:
  150.  db $FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$3F    ;0.999999999999999999964159204834411193
  151. xcos_p1:
  152.  db $F8,$FE,$FF,$FF,$FF,$FF,$FF,$FF,$FE,$BF    ;-0.4999999999999999928435986970121455617
  153. xcos_p2:
  154.  db $1B,$9A,$A9,$AA,$AA,$AA,$AA,$AA,$FB,$3F    ;.0416666666666664302574180075687333186
  155. xcos_p3:
  156.  db $8C,$65,$B1,$09,$B6,$60,$0B,$B6,$F6,$BF    ;-.001388888888885896043735590722592072382
  157. xcos_p4:
  158.  db $CB,$EF,$B3,$6B,$CD,$00,$0D,$D0,$F0,$3F    ;.00002480158728289946428496138594581111543
  159. xcos_p5:
  160.  db $B4,$48,$CE,$10,$7F,$7B,$F2,$93,$EA,$BF    ;-.000000275573128656963777686502398712343692
  161. xcos_p6:
  162.  db $C8,$97,$56,$CF,$3C,$AA,$74,$8F,$E3,$3F    ;.000000002087555514571344400405482331559490482
  163. xcos_p7:
  164.  db $90,$5C,$F1,$0F,$F8,$6A,$B5,$C7,$DB,$BF    ;-.00000000001135212320760413752073800267975510526
  165.  endif
  166.