*
*
*

	XREF	_p%MathBase
	XREF	_LVOSPCmp
	XREF	_LVOSPFloor
	XREF	_LVOSPDiv
	XREF	_LVOSPMul
	XREF	_LVOSPSub
	XREF	_LVOSPNeg
	XREF	_LVOSPAdd

        XDEF    _p%sin
_p%sin
        move.l  4(sp),d0          ;x to d0
        move.l  _p%MathBase,a6
        move.l  d7,-(sp)
        move.l  d0,d7
        move.l  #$C90FD943,d1     ;twopi
        jsr	_LVOSPCmp(a6)		;cmp
        bge.s   sin_toobig
        move.l  d7,d0
        move.l  #$0,d1            ;d1 holds 0
        jsr     _LVOSPCmp(a6)           ;cmp
        bge.s   sin_around
sin_toobig
        move.l  d7,d0
        move.l  #$C90FD943,d1     ;twopi
        jsr     _LVOSPDiv(a6)           ;div
        jsr     _LVOSPFloor(a6)           ;floor
        move.l  #$C90FD943,d1     ;twopi
        jsr     _LVOSPMul(a6)           ;mul
        move.l  d7,d1
        exg     d0,d1
        jsr     _LVOSPSub(a6)           ;sub
        move.l  d0,d7
sin_around
        move.l  d7,d0
        move.l  #$C90FD941,d1     ;halfpi
        jsr     _LVOSPCmp(a6)           ;cmp
        bgt.s   sin_TwoQuad
        bsr.s     GetSin
        bra.s   sin_out
sin_TwoQuad
        move.l  d7,d0
        move.l  #$C90FD942,d1     ;pi
        jsr     _LVOSPCmp(a6)           ;cmp
        bgt.s   sin_ThreeQuad
        move.l  #$C90FD942,d0     ;pi
        move.l  d7,d1
        jsr     _LVOSPSub(a6)           ;sub
        move.l  d0,d7
        bsr.s   GetSin
        bra.s   sin_out
sin_ThreeQuad
        move.l  d7,d0
        move.l  #$96CBE343,d1     ;threehalvespi
        jsr     _LVOSPCmp(a6)           ;cmp
        bgt.s   sin_FourQuad
        move.l  d7,d0
        move.l  #$C90FD942,d1     ;twopi
        jsr     _LVOSPSub(a6)           ;sub
        move.l  d0,d7
        bsr.s   GetSin
        jsr     _LVOSPNeg(a6)           ;neg
        bra.s   sin_out
sin_FourQuad
        move.l  #$C90FD943,d0     ;twopi
        move.l  d7,d1
        jsr     _LVOSPSub(a6)           ;sub
        move.l  d0,d7
        bsr.s   GetSin
        jsr     _LVOSPNeg(a6)           ;neg
sin_out     move.l  (sp)+,d7
        rts


GetSin                            ;a4x^4+a3x^3+a2x^2+a1x+a0
        move.l  d7,d0             ;d7 holds x
        move.l  #$E8626F3B,d1     ;a4
        jsr     _LVOSPMul(a6)           ;mul
        move.l  #$CFD12F3E,d1     ;a3
        jsr     _LVOSPSub(a6)           ;sub   a3 is negative
        move.l  d7,d1
        jsr     _LVOSPMul(a6)           ;mul
        move.l  #$A350863B,d1     ;a2
        jsr     _LVOSPAdd(a6)           ;add
        move.l  d7,d1
        jsr     _LVOSPMul(a6)           ;mul
        move.l  #$FEEF6E40,d1     ;a1
        jsr     _LVOSPAdd(a6)           ;add
        move.l  d7,d1
        jsr     _LVOSPMul(a6)           ;mul
        move.l  #$CB805734,d1     ;a0
        jsr     _LVOSPAdd(a6)           ;add
        rts

        XDEF    _p%cos
_p%cos
        move.l  4(sp),d0          ;x to d0
        move.l  _p%MathBase,a6
        move.l  d7,-(sp)
        move.l  d0,d7
        move.l  #$C90FD943,d1     ;twopi
        jsr     _LVOSPCmp(a6)           ;cmp
        bge.s   cos_toobig
        move.l  d7,d0
        move.l  #$0,d1            ;d1 holds 0
        jsr     _LVOSPCmp(a6)           ;cmp
        bge.s   cos_around
cos_toobig
        move.l  d7,d0
        move.l  #$C90FD943,d1     ;twopi
        jsr     _LVOSPDiv(a6)           ;div
        jsr     _LVOSPFloor(a6)           ;floor
        move.l  #$C90FD943,d1     ;twopi
        jsr     _LVOSPMul(a6)           ;mul
        move.l  d7,d1
        exg     d0,d1
        jsr     _LVOSPSub(a6)           ;sub
        move.l  d0,d7
cos_around
        move.l  d7,d0
        move.l  #$C90FD941,d1     ;halfpi
        jsr     _LVOSPCmp(a6)           ;cmp
        bgt.s   cos_TwoQuad
        bsr.s   GetCos
        bra.s   cos_out
cos_TwoQuad
        move.l  d7,d0
        move.l  #$C90FD942,d1     ;pi
        jsr     _LVOSPCmp(a6)           ;cmp
        bgt.s   cos_ThreeQuad
        move.l  #$C90FD942,d0     ;pi
        move.l  d7,d1
        jsr     _LVOSPSub(a6)           ;sub
        move.l  d0,d7
        bsr.s   GetCos
        jsr     _LVOSPNeg(a6)           ;neg
        bra.s   cos_out
cos_ThreeQuad
        move.l  d7,d0
        move.l  #$96CBE343,d1     ;threehalvespi
        jsr     _LVOSPCmp(a6)           ;cmp
        bgt.s   cos_FourQuad
        move.l  d7,d0
        move.l  #$C90FD942,d1     ;pi
        jsr     _LVOSPSub(a6)           ;sub
        move.l  d0,d7
        bsr.s   GetCos
        jsr     _LVOSPNeg(a6)           ;neg
        bra.s   cos_out
cos_FourQuad
        move.l  #$C90FD943,d0     ;twopi
        move.l  d7,d1
        jsr     _LVOSPSub(a6)           ;sub
        move.l  d0,d7
        bsr.s   GetCos
cos_out     move.l  (sp)+,d7
        rts


GetCos                            ;a4x^4+a3x^3+a2x^2+a1x+a0
        move.l  d7,d0             ;d7 holds x
        move.l  #$F570BF3B,d1     ;a4
        jsr     _LVOSPMul(a6)           ;mul
        move.l  #$A1650A3B,d1     ;a3
        jsr     _LVOSPAdd(a6)           ;add
        move.l  d7,d1
        jsr     _LVOSPMul(a6)           ;mul
        move.l  #$82E90140,d1     ;a2
        jsr     _LVOSPSub(a6)           ;sub   a2 is negative
        move.l  d7,d1
        jsr     _LVOSPMul(a6)           ;mul
        move.l  #$8E477E38,d1     ;a1
        jsr     _LVOSPAdd(a6)           ;add
        move.l  d7,d1
        jsr     _LVOSPMul(a6)           ;mul
        move.l  #$FFFB5540,d1     ;a0
        jsr     _LVOSPAdd(a6)           ;add
        rts

        XDEF    _p%tan
_p%tan
        move.l  4(sp),d0          ;x to d0
        move.l  _p%MathBase,a6
        movem.l  d6-d7,-(sp)
        move.l  d0,d7
        move.l  #$C90FD943,d1     ;twopi
        jsr     _LVOSPCmp(a6)           ;cmp
        bge.s   tan_toobig
        move.l  d7,d0
        move.l  #$0,d1            ;d1 holds 0
        jsr     _LVOSPCmp(a6)           ;cmp
        bge.s   tan_around
tan_toobig
        move.l  d7,d0
        move.l  #$C90FD943,d1     ;twopi
        jsr     _LVOSPDiv(a6)           ;div
        jsr     _LVOSPFloor(a6)           ;floor
        move.l  #$C90FD943,d1     ;twopi
        jsr     _LVOSPMul(a6)           ;mul
        move.l  d7,d1
        exg     d0,d1
        jsr     _LVOSPSub(a6)           ;sub
        move.l  d0,d7
tan_around
        move.l  d7,d0
        move.l  #$C90FD941,d1     ;halfpi
        jsr     _LVOSPCmp(a6)           ;cmp
        bgt.s   tan_TwoQuad
        bsr.s     GetTan
        bra.s   tan_out
tan_TwoQuad
        move.l  d7,d0
        move.l  #$C90FD942,d1     ;pi
        jsr     _LVOSPCmp(a6)           ;cmp
        bgt.s   tan_ThreeQuad
        move.l  #$C90FD942,d0     ;pi
        move.l  d7,d1
        jsr     _LVOSPSub(a6)           ;sub
        move.l  d0,d7
        bsr.s   GetTan
        jsr     _LVOSPNeg(a6)           ;neg
        bra.s   tan_out
tan_ThreeQuad
        move.l  d7,d0
        move.l  #$96CBE343,d1     ;threehalvespi
        jsr     _LVOSPCmp(a6)           ;cmp
        bgt.s   tan_FourQuad
        move.l  d7,d0
        move.l  #$C90FD942,d1     ;pi
        jsr     _LVOSPSub(a6)           ;sub
        move.l  d0,d7
        bsr.s   GetTan
        bra.s   tan_out
tan_FourQuad
        move.l  #$C90FD943,d0     ;twopi
        move.l  d7,d1
        jsr     _LVOSPSub(a6)           ;sub
        move.l  d0,d7
        bsr.s   GetTan
        jsr     _LVOSPNeg(a6)           ;neg
tan_out     movem.l  (sp)+,d6-d7
        rts

GetTan
        move.l  d7,d0
        move.l  #$C90FD940,d1     ;fourthpi
        jsr     _LVOSPCmp(a6)           ;cmp
        sgt     d6                ;d6 is flag for angle>fourthpi
        ble.s   tan_small
        move.l  #$C90FD941,d0     ;halfpi
        move.l  d7,d1
        jsr     _LVOSPSub(a6)           ;sub
        move.l  d0,d7

tan_small                         ;a4x^4+a3x^3+a2x^2+a1x+a0
        move.l  d7,d0             ;d7 holds x
        move.l  #$FE621B3F,d1     ;a4
        jsr     _LVOSPMul(a6)           ;mul
        move.l  #$DD40913D,d1     ;a3
        jsr     _LVOSPSub(a6)           ;sub   a3 is negative
        move.l  d7,d1
        jsr     _LVOSPMul(a6)           ;mul
        move.l  #$96E19C3E,d1     ;a2
        jsr     _LVOSPAdd(a6)           ;add
        move.l  d7,d1
        jsr     _LVOSPMul(a6)           ;mul
        move.l  #$FB844840,d1     ;a1
        jsr     _LVOSPAdd(a6)           ;add
        move.l  d7,d1
        jsr     _LVOSPMul(a6)           ;mul
        move.l  #$E60CA535,d1     ;a0
        jsr     _LVOSPAdd(a6)           ;add
        tst.s   d6
        beq.s   tan_small_out
        move.l  d0,d1
        move.l  #$80000041,d0
        jsr     _LVOSPDiv(a6)           ;div       take reciprocal of tangent
tan_small_out
        rts

        XDEF    _p%atn
_p%atn
        move.l  4(sp),d0          ;x to d0
        move.l  _p%MathBase,a6
        movem.l  d5-d7,-(sp)
        move.l  d0,d7
        move.l  #$0,d1            ;d1 holds 0
        jsr     _LVOSPCmp(a6)           ;cmp
        sle     d6                ;set flag for negative angle
        bge.s   atn_posang
        move.l  d7,d0
        jsr     _LVOSPNeg(a6)           ;neg
        move.l  d0,d7
atn_posang
        move.l  d7,d0
        move.l  #$80000041,d1           ; one
        jsr     _LVOSPCmp(a6)           ;cmp
        sge     d5                ;d5 flags angle > fourthpi
        blt.s   GetAtn
        move.l  d7,d1
        move.l  #$80000041,d0
        jsr     _LVOSPDiv(a6)           ;take reciprocal
        move.l  d0,d7
GetAtn                            ;a4x^4+a3x^3+a2x^2+a1x+a0
        move.l  d7,d0             ;d7 holds x
        move.l  #$8D9AED3E,d1     ;a4
        jsr     _LVOSPMul(a6)           ;mul
        move.l  #$AC901A3F,d1     ;a3
        jsr     _LVOSPSub(a6)           ;sub   a3 is negative
        move.l  d7,d1
        jsr     _LVOSPMul(a6)           ;mul
        move.l  #$A4FC033B,d1     ;a2
        jsr     _LVOSPSub(a6)           ;sub    a2 is negative
        move.l  d7,d1
        jsr     _LVOSPMul(a6)           ;mul
        move.l  #$8098CB41,d1     ;a1
        jsr     _LVOSPAdd(a6)           ;add
        move.l  d7,d1
        jsr     _LVOSPMul(a6)           ;mul
        move.l  #$C93A4B34,d1     ;a0
        jsr     _LVOSPSub(a6)           ;sub    a0 is negative
        tst.b   d5
        beq.s   atn_pos
        move.l  d0,d1
        move.l  #$C90FD941,d0
        jsr     _LVOSPSub(a6)           ;sub

atn_pos tst.b    d6
        beq.s   atn_out
        jsr     _LVOSPNeg(a6)           ;neg angle
atn_out movem.l  (sp)+,d5-d7
        rts

        END
