
                    incdir         "include:"
                    include        "exec/execbase.i"
                    include        "exec/initializers.i"
                    include        "exec/resident.i"
                    include        "libraries/mathlibrary.i"
                    include        "lvo/LVOs.i"

;---------------------------------------------------------------

                    RTS

ROMTag              DC.W           RTC_MATCHWORD
                    DC.L           ROMTag
                    DC.L           LibEnd
                    DC.B           RTF_AUTOINIT
                    DC.B           45
                    DC.B           NT_LIBRARY
                    DC.B           0
                    DC.L           LibName
                    DC.L           LibId
                    DC.L           LibInit

LibInit             DC.L           MathIEEEBase_SIZE + 4    ; library + seglist
                    DC.L           FuncTable
                    DC.L           DataTable
                    DC.L           Init

FuncTable           DC.W           -1
                    DC.W           Open    - FuncTable
                    DC.W           Close   - FuncTable
                    DC.W           Expunge - FuncTable
                    DC.W           Null    - FuncTable
                    DC.W           ATan    - FuncTable
                    DC.W           Sin     - FuncTable
                    DC.W           Cos     - FuncTable
                    DC.W           Tan     - FuncTable
                    DC.W           SinCos  - FuncTable
                    DC.W           SinH    - FuncTable
                    DC.W           CosH    - FuncTable
                    DC.W           TanH    - FuncTable
                    DC.W           Exp     - FuncTable
                    DC.W           LogN    - FuncTable
                    DC.W           Pow     - FuncTable
                    DC.W           SqRt    - FuncTable
                    DC.W           return  - FuncTable
                    DC.W           return  - FuncTable
                    DC.W           ASin    - FuncTable
                    DC.W           ACos    - FuncTable
                    DC.W           Log10   - FuncTable
                    DC.W           -1

DataTable           INITBYTE       LIB_FLAGS,LIBF_SUMUSED|LIBF_CHANGED
                    INITWORD       LIB_REVISION,881
                    DC.W           0

Init                BTST.B         #AFB_68881,AttnFlags+1(A6)
                    BEQ.S          oldlib
                    MOVE.L         D0,A1                    ; libbase
                    MOVE.L         A0,MathIEEEBase_SIZE(A1) ; save segmentlist
                    RTS

oldlib              MOVE.L         D0,-(SP)                 ; libbase
                    LEA            LibName(PC),A1
                    JSR            _LVOFindResident(A6)
                    TST.L          D0                       ; resident library ?
                    BNE.S          exit
                    LEA            OldName(PC),A1           ; try nonFPU version
                    JSR            _LVOOldOpenLibrary(A6)
                    MOVEA.L        D0,A1
                    JSR            _LVOCloseLibrary(A6)
exit                MOVEA.L        (SP),A1
                    BRA.S          freemem

Open                MOVEQ          #0,D0                    ; extended precision
                    FMOVE.L        D0,FPCR                  ; round to nearest
                    ADDQ.W         #1,LIB_OPENCNT(A6)
                    BCLR.B         #LIBB_DELEXP,LIB_FLAGS(A6)
                    MOVE.L         A6,D0
                    RTS

Close               SUBQ.W         #1,LIB_OPENCNT(A6)
                    BNE.S          Null
                    BTST.B         #LIBB_DELEXP,LIB_FLAGS(A6)
                    BEQ.S          Null

Expunge             TST.W          LIB_OPENCNT(A6)
                    BEQ.S          kickout
                    BSET.B         #LIBB_DELEXP,LIB_FLAGS(A6)
Null                MOVEQ          #0,D0
                    RTS

kickout             MOVEA.L        A6,A1                    ; libbase
                    MOVE.L         A1,-(SP)
                    MOVEA.L        (A6)+,A0
                    MOVEA.L        (A6),A6
                    MOVE.L         A0,(A6)
                    MOVE.L         A6,4(A0)
                    MOVEA.L        (4).w,A6                 ; execbase
freemem             MOVEQ          #0,D0
                    MOVE.W         LIB_NEGSIZE(A1),D0
                    SUBA.W         D0,A1
                    ADD.W          LIB_POSSIZE(A1,D0.w),D0
                    JSR            _LVOFreeMem(A6)
                    MOVEA.L        (SP)+,A1                 ; libbase
                    MOVE.L         MathIEEEBase_SIZE(A1),D0 ; segmentlist or 0
                    RTS

OldName             DC.B           "NoFPU/"
LibName             DC.B           "mathieeesingtrans.library",0
LibId               DC.B           "MST 14.881",0

;-------------------------------------------------------------------------------

                    CNOP           0,4

Pow                 TST.L          D0
                    BLE.S          lessi
                    FLOG2.S        D0,FP0
                    FMUL.S         D1,FP0
                    FTWOTOX.X      FP0
                    BRA.S          done  ; PhxAss has a problem with a few CNOPs

                    CNOP           0,4

base0               FMOVE.S        D1,FP1
                    FBOGT.W        Null
                    FBNE.W         nan
                    MOVE.L         #$3F800000,D0
                    RTS

                    CNOP           0,4

lessi               FNEG.S         D0,FP0
                    FBEQ.W         base0
                    FINT.S         D1,FP1
                    FCMP.S         D1,FP1
                    FBNE.W         nan
                    FLOG2.X        FP0
                    FMUL.X         FP1,FP0
                    FTWOTOX.X      FP0
                    FMOVE.S        FP0,D0
                    FREM.S         #2,FP1
                    FBEQ.W         return
                    BSET.L         #31,D0
return              RTS

nan                 MOVEQ          #-1,D0
                    BCHG.L         D0,D0
                    RTS

                    CNOP           0,4

Sin                 FSIN.S         D0,FP0
                    BRA.S          done

                    NOP

Cos                 FCOS.S         D0,FP0
                    BRA.S          done

                    NOP

Tan                 FTAN.S         D0,FP0
                    BRA.S          done

                    NOP

SinCos              FSINCOS.S      D0,FP1:FP0
                    FMOVE.S        FP1,(A0)
done                FMOVE.S        FP0,D0
                    RTS

                    CNOP           0,4

SinH                FSINH.S        D0,FP0
                    BRA.S          done

                    CNOP           0,4

CosH                FCOSH.S        D0,FP0
                    BRA.S          done

                    CNOP           0,4

TanH                FTANH.S        D0,FP0
                    BRA.S          done

                    CNOP           0,4

ASin                FASIN.S        D0,FP0
                    BRA.S          done

                    CNOP           0,4

ACos                FACOS.S        D0,FP0
                    BRA.S          done

                    CNOP           0,4

ATan                FATAN.S        D0,FP0
                    BRA.S          done

                    CNOP           0,4

SqRt                FSQRT.S        D0,FP0
                    BRA.S          done

                    CNOP           0,4

Exp                 FETOX.S        D0,FP0
                    BRA.S          done

                    CNOP           0,4

LogN                FLOGN.S        D0,FP0
                    BRA.S          done

                    CNOP           0,4

Log10               FLOG10.S       D0,FP0
                    BRA.S          done

                    CNOP           0,4

LibEnd

