;	MathFFP.library 16-Sep-91
;	(c) 1991 by Martin Berndt
;	optimized for MC68881
;	for ADAPT68 Assembler

	opt p=68881
	exeobj
	optimon
	objfile	"ram:mathffp.library"
	errfile	"ram:mathffp.err"
	lisfile	"ram:mathffp.lst"
	nolist

	incdir	"includes:"
	include	"exec/funcdef.i"
	include	"exec/types.i"
	include	"exec/initializers.i"
	include	"exec/resident.i"
	include	"exec/libraries.i"
	include	"exec/execbase.i"
	include	"exec/exec_lib.i"
	include	"intuition/intuition_lib.i"

	list

VERSION		equ 37
REVISION	equ 100
MYPRI		equ 0

	STRUCTURE MyLib,LIB_SIZE
	* ---- STRUCTURE LN,0
	ULONG ml_SysLib
	ULONG ml_DosLib
	ULONG ml_SegList
	UBYTE ml_Flags
	UBYTE ml_Pad
	LABEL MyLib_Sizeof

FirstAddress:
	moveq	#0,d0
	rts

InitDescription:
	dc.w	RTC_MATCHWORD
	dc.l	InitDescription
	dc.l	EndCode
	dc.b	RTF_AUTOINIT
	dc.b	VERSION
	dc.b	NT_LIBRARY
	dc.b	MYPRI
	dc.l	LibName
	dc.l	IdString
	dc.l	InitTab

LibName:	dc.b	"mathffp.library",0
		cnop	0,2

IdString:	dc.b	"MC68881/82 Version 37.100 (16-Sep-91)",13,10,0
		dc.b	"(c) 1991 by Martin Berndt",0
		cnop	0,2

InitTab:	dc.l MyLib_Sizeof
		dc.l FuncTable
		dc.l DataTable
		dc.l InitLibrary

FuncTable:	dc.l Open
		dc.l Close
		dc.l Expunge
		dc.l FirstAddress

		dc.l SPFix
		dc.l SPFlt
		dc.l SPCmp
		dc.l SPTst
		dc.l SPAbs
		dc.l SPNeg
		dc.l SPAdd
		dc.l SPSub
		dc.l SPMul
		dc.l SPDiv
		dc.l SPFloor
		dc.l SPCeil
		dc.l -1

DataTable:	INITBYTE LN_TYPE,NT_LIBRARY
		INITLONG LN_NAME,LibName
		INITBYTE LIB_FLAGS,LIBF_SUMUSED|LIBF_CHANGED
		INITWORD LIB_VERSION,VERSION
		INITWORD LIB_REVISION,REVISION
		INITLONG LIB_IDSTRING,IdString
		dc.l 0

InitLibrary:
	move.l	d0,a1
	move.l	a6,34(a1)
	move.l	a0,38(a1)

	btst	#4,AttnFlags+1(a6)
	beq.s	do_alert

	move.w	#1,32(a1)
	move.l	a1,d0
	rts

do_alert:
	movem.l	d1-d7/a0-a6,-(sp)
	lea	intname,a1
	jsr	_LVOOldOpenLibrary(a6)
	move.l	a6,a5	
	move.l	d0,a6
	moveq	#40,d1
	moveq	#0,d0
	lea	alert,a0
	jsr	_LVODisplayAlert(a6)
	move.l	a6,a1
	move.l	a5,a6
	jsr	_LVOCloseLibrary(a6)
	movem.l	(sp)+,d1-d7/a0-a6

Expunge:
	moveq	#0,d0
	rts

Open:
	addq.w	#1,32(a6)
	bclr.b	#3,42(a6)
	move.l	a6,d0
	rts

Close:
	moveq	#0,d0
	subq.w	#1,32(a6)
	bmi.s	Close1
	rts
Close1:
	clr.w	32(a6)
	rts       

	cnop	0,4

SPFix:	
	move.b	d0,d1
	bmi.s	SPF4
	beq.s	SPF1
	clr.b	d0
	subi.b	#$41,d1
	bmi.s	SPF3
	subi.b	#$1f,d1
	bpl.s	SPF2
	neg.b	d1
	lsr.l	d1,d0
SPF1:	rts
SPF2:	moveq	#-1,d0
	lsr.l	#1,d0
	ori	#2,ccr
	rts
SPF3:	moveq	#0,d0
	rts
SPF4:	clr.b	d0
	subi.b	#$c1,d1
	bmi.s	SPF3
	subi.b	#$1f,d1
	bpl.s	SPF5
	neg.b	d1
	lsr.l	d1,d0
	neg.l	d0
	rts
SPF5:	bne.s	SPF6
	neg.l	d0
	tst.l	d0
	bmi.s	SPF1
SPF6:	moveq	#0,d0
	bset	#$1f,d0
	ori	#2,ccr
	rts

	cnop	0,4

SPFlt:
	moveq	#$5f,d1
	tst.l	d0
	beq.s	SPF10
	bpl.s	SPF7
	moveq	#$e0,d1
	neg.l	d0
	bvs.s	SPF9
	subq.b	#1,d1
SPF7:	cmpi.l	#$7fff,d0
	bhi.s	SPF8
	swap	d0
	subi.b	#$10,d1
SPF8:	add.l	d0,d0
	dbmi	d1,SPF8
	tst.b	d0
	bpl.s	SPF9
	addi.l	#$100,d0
	bcc.s	SPF9
	roxr.l	#1,d0
	addq.b	#1,d1
SPF9:	move.b	d1,d0
SPF10:	rts

	cnop	0,4

SPCmp:
	tst.b	d1
	bpl.s	SPC1
	tst.b	d0
	bpl.s	SPC1
	cmp.b	d0,d1
	bne.s	SPC2
	cmp.l	d0,d1
	bra.s	SPC2
SPC1:	cmp.b	d1,d0
	bne.s	SPC2
	cmp.l	d1,d0
SPC2:	movem.l	a6,-(sp)
	move.l	$4.w,a6
	jsr	_LVOGetCC(a6)
	move.l	(sp)+,a6
	move.w	d0,d1
	moveq	#0,d0
	move.w	d1,ccr
	blt.s	SPC4
	bgt.s	SPC3
	move.w	d1,ccr
	rts
SPC3:	subq.l	#1,d0
	move.w	d1,ccr
	rts
SPC4:	addq.l	#1,d0
	move.w	d1,ccr
	rts

	cnop	0,4

SPTst:
	tst.b	d1
	bra.s	SPC2	

	cnop	0,4

SPAbs:
	andi.b	#$7f,d0
	rts

	cnop	0,4

SPCeil:
	bsr.s	SPNeg
	bsr.s	SPFloor

SPNeg:
	tst.b	d0
	beq.s	SPN1
	eori.b	#$80,d0
SPN1:	rts

	cnop	0,4

SPTieee:
	add.l	d0,d0
	beq.s	Tieee1
	eori.b	#128,d0
	asr.b	#1,d0
	subi.b	#130,d0
	swap	d0
	rol.l	#7,d0	
Tieee1:	rts

	cnop	0,4

SPFloor:
	fmove.s	fp0,-(sp)
	bsr.s	SPTieee
	fintrz.s d0,fp0
	bra.s	SPFieee

	cnop	0,4

SPAdd:
	fmove.s	fp0,-(sp)
	bsr.s	SPTieee
	fmove.s	d0,fp0
	move.l	d1,d0
	bsr.s	SPTieee
	fadd.s	d0,fp0
	bra.s	SPFieee

	cnop	0,4

SPSub:
	fmove.s	fp0,-(sp)
	bsr.s	SPTieee
	fmove.s	d0,fp0
	move.l	d1,d0
	bsr.s	SPTieee
	fsub.s	d0,fp0
	bra.s	SPFieee

	cnop	0,4

SPMul:
	fmove.s	fp0,-(sp)
	bsr.s	SPTieee
	fmove.s	d0,fp0
	move.l	d1,d0
	bsr.s	SPTieee
	fmul.s	d0,fp0
	bra.s	SPFieee

	cnop	0,4

SPDiv:
	fmove.s	fp0,-(sp)
	bsr.s	SPTieee
	fmove.s	d0,fp0
	move.l	d1,d0
	bsr.s	SPTieee
	fdiv.s	d0,fp0

	cnop	0,4

SPFieee:
	fmove.s	fp0,d0
	fmove.s	(sp)+,fp0
	swap	d0
	ror.l	#7,d0
	eori.b	#128,d0
	add.b	d0,d0
	bvs.s	Fieee2
	addq.b	#5,d0
	bvs.s	Fieee6
	eori.b	#128,d0
	ror.l	#1,d0
Fieee1:	rts
Fieee2:	bcc.s	Fieee5
	cmpi.b	#124,d0
	beq.s	Fieee3
	cmpi.b	#126,d0
	bne.s	Fieee4
Fieee3:	addi.b	#133,d0
	ror.l	#1,d0
	tst.b	d0
	bne.s	Fieee1
	moveq	#0,d0
	rts
Fieee4:	andi.w	#65279,d0
	tst.l	d0
	beq.s	Fieee1
	moveq	#0,d0
	rts
Fieee5:	cmpi.b	#254,d0
	bne.s	Fieee6
	lsr.l	#8,d0
	lsr.l	#1,d0
	beq.s	Fieee7
	moveq	#0,d0
	rts
Fieee6:	lsl.w	#8,d0
Fieee7:	moveq	#-1,d0
	roxr.b	#1,d0
	ori.b	#2,ccr
	rts

	section	Text,DATA

intname:
	dc.b	"intuition.library",0
	cnop	0,2

alert:	dc.b	0,96,16
	dc.b	"This mathffp.library needs a 68881/82 Math Coprocessor !",0
	dc.b	1,0,170,28
	dc.b	"Please use your original ROM library.",0,0,0
	cnop	0,2
EndCode:	
	End
