
                    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           Fix     - FuncTable
                    DC.W           Flt     - FuncTable
                    DC.W           Cmp     - FuncTable
                    DC.W           Tst     - FuncTable
                    DC.W           Abs     - FuncTable
                    DC.W           Neg     - FuncTable
                    DC.W           Add     - FuncTable
                    DC.W           Sub     - FuncTable
                    DC.W           Mul     - FuncTable
                    DC.W           Div     - FuncTable
                    DC.W           Floor   - FuncTable
                    DC.W           Ceil    - 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           "mathieeedoubbas.library",0
LibId               DC.B           "MDB 14.881",0

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

                    CNOP           0,4

Cmp                 TST.L          D2
                    BMI.S          minus
                    BNE.S          plus
                    TST.L          D3
                    BEQ.S          Tst       ; D2/D3=0 -> Tst
plus                SUB.L          D2,D0
                    BLT.S          less
                    BNE.S          more
                    CMP.L          D3,D1
                    BHI.S          more
                    BNE.S          less
                    RTS

                    CNOP           0,4

minus               TST.L          D0
                    BNE.S          skip
                    OR.L           D3,D1
                    BNE.S          more
                    BSET.L         #31,D0    ; D0/D1=0 -> -0
skip                SUB.L          D2,D0
                    BCS.S          more
                    BNE.S          less
                    CMP.L          D3,D1
                    BHI.S          less
                    BEQ.S          same
more                MOVEQ          #1,D0
                    RTS

less                MOVEQ          #-1,D0
                    RTS

                    CNOP           0,4

Tst                 ADD.L          D0,D0
                    BCS.S          lessi
                    OR.L           D1,D0
                    BNE.S          more
same                RTS

lessi               OR.L           D1,D0
                    BNE.S          less
                    RTS

                    CNOP           0,8

Fix                 MOVEM.L        D0-D1,-(SP)
                    FMOVE.D        (SP)+,FP0
                    MOVEQ          #$10,D0   ; extended precision
                    FMOVE.L        D0,FPCR   ; round to zero
                    FMOVE.L        FP0,D0    ; double to long int
                    MOVEQ          #0,D1     ; extended precision
                    FMOVE.L        D1,FPCR   ; round to nearest
                    RTS

Abs                 BCLR.L         #31,D0
                    RTS

                    CNOP           0,8

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

Neg                 BCHG.L         #31,D0
                    RTS

                    CNOP           0,8

Add                 MOVEM.L        D0-D3,-(SP)
                    FMOVE.D        (SP)+,FP0
                    FADD.D         (SP),FP0
                    BRA.S          done

                    CNOP           0,8

Sub                 MOVEM.L        D0-D3,-(SP)
                    FMOVE.D        (SP)+,FP0
                    FSUB.D         (SP),FP0
                    BRA.S          done

                    CNOP           0,8

Mul                 MOVEM.L        D0-D3,-(SP)
                    FMOVE.D        (SP)+,FP0
                    FMUL.D         (SP),FP0
                    BRA.S          done

                    CNOP           0,8

Div                 MOVEM.L        D0-D3,-(SP)
                    FMOVE.D        (SP)+,FP0
                    FDIV.D         (SP),FP0
done                FMOVE.D        FP0,(SP)
save                MOVE.L         (SP)+,D0
                    MOVE.L         (SP)+,D1
                    RTS

                    CNOP           0,8

Floor               MOVEM.L        D0-D1,-(SP)
                    FINTRZ.D       (SP),FP0
                    TST.L          D0
                    FMOVE.D        FP0,(SP)
                    BPL.S          save
                    CMP.L          (SP),D0
                    BNE.S          sub1
                    CMP.L          4(SP),D1
                    BEQ.S          save
sub1                FSUB.S         #1,FP0
                    BRA.S          done

                    CNOP           0,4

Ceil                MOVEM.L        D0-D1,-(SP)
                    FINTRZ.D       (SP),FP0
                    TST.L          D0
                    FMOVE.D        FP0,(SP)
                    BMI.S          save
                    CMP.L          (SP),D0
                    BNE.S          add1
                    CMP.L          4(SP),D1
                    BEQ.S          save
add1                FADD.S         #1,FP0
                    BRA.S          done

                    CNOP           0,4

LibEnd

