
                    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           D2Sing  - FuncTable
                    DC.W           S2Doub  - 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           "mathieeedoubtrans.library",0
LibId               DC.B           "MDT 14.881",0

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

                    CNOP           0,4

Pow                 MOVEM.L        D0-D3,-(SP)
                    FMOVE.D        (SP)+,FP0
                    FBOLE.W        lessi
                    FLOG2.X        FP0
                    FMUL.D         (SP),FP0
                    FTWOTOX.X      FP0
                    BRA.W          done

                    CNOP           0,4

base0               FMOVE.D        (SP)+,FP1
                    FBOGT.W        Null
                    FBNE.W         nan
                    MOVE.L         #$3FF00000,D0
                    RTS

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

                    CNOP           0,4

lessi               FBEQ.W         base0
                    FINT.D         (SP),FP1
                    FCMP.D         (SP)+,FP1
                    FBNE.W         nan
                    FNEG.X         FP0
                    FLOG2.X        FP0
                    FMUL.X         FP1,FP0
                    FTWOTOX.X      FP0
                    FMOVE.D        FP0,-(SP)
                    MOVE.L         (SP)+,D0
                    MOVE.L         (SP)+,D1
                    FREM.S         #2,FP1
                    FBEQ.W         even
                    BSET.L         #31,D0
even                RTS

                    CNOP           0,4

D2Sing              MOVEM.L        D0-D1,-(SP)
                    FMOVE.D        (SP)+,FP0
                    FMOVE.S        FP0,D0
                    RTS

                    CNOP           0,4

SinCos              MOVEM.L        D0-D1,-(SP)
                    FSINCOS.D      (SP),FP1:FP0
                    FMOVE.D        FP1,(A0)
                    BRA.S          done

                    CNOP           0,4

S2Doub              FMOVE.S        D0,FP0
                    FMOVE.D        FP0,-(SP)
                    BRA.S          save

                    CNOP           0,4

Sin                 MOVEM.L        D0-D1,-(SP)
                    FSIN.D         (SP),FP0
                    BRA.S          done

                    CNOP           0,4

Cos                 MOVEM.L        D0-D1,-(SP)
                    FCOS.D         (SP),FP0
                    BRA.S          done

                    CNOP           0,4

Tan                 MOVEM.L        D0-D1,-(SP)
                    FTAN.D         (SP),FP0
                    BRA.S          done

                    CNOP           0,4

SinH                MOVEM.L        D0-D1,-(SP)
                    FSINH.D        (SP),FP0
                    BRA.S          done

                    CNOP           0,4

CosH                MOVEM.L        D0-D1,-(SP)
                    FCOSH.D        (SP),FP0
                    BRA.S          done

                    CNOP           0,4

TanH                MOVEM.L        D0-D1,-(SP)
                    FTANH.D        (SP),FP0
done                FMOVE.D        FP0,(SP)
save                MOVE.L         (SP)+,D0
                    MOVE.L         (SP)+,D1
                    RTS

                    CNOP           0,4

Exp                 MOVEM.L        D0-D1,-(SP)
                    FETOX.D        (SP),FP0
                    BRA.S          done

                    CNOP           0,4

ASin                MOVEM.L        D0-D1,-(SP)
                    FASIN.D        (SP),FP0
                    BRA.S          done

                    CNOP           0,4

ACos                MOVEM.L        D0-D1,-(SP)
                    FACOS.D        (SP),FP0
                    BRA.S          done

                    CNOP           0,4

ATan                MOVEM.L        D0-D1,-(SP)
                    FATAN.D        (SP),FP0
                    BRA.S          done

                    CNOP           0,4

SqRt                MOVEM.L        D0-D1,-(SP)
                    FSQRT.D        (SP),FP0
                    BRA.S          done

                    CNOP           0,4

LogN                MOVEM.L        D0-D1,-(SP)
                    FLOGN.D        (SP),FP0
                    BRA.S          done

                    CNOP           0,4

Log10               MOVEM.L        D0-D1,-(SP)
                    FLOG10.D       (SP),FP0
                    BRA.S          done

                    CNOP           0,4

LibEnd

