;	Mathtrans.library 06-Apr-93
;	(c) 1993 by Martin Berndt
;	optimized for MC68881

	opt p=68881
	include	asm:system.gs

VERSION	equ 37
REVISION	equ 102
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	"mathtrans.library",0
	even

IdString:	dc.b	"mathtrans 37.102 for 68881/82 (6-Apr-93)",13,10,0
	dc.b	"(c) 1993 by Martin Berndt",0
	even

intname:
	dc.b	"intuition.library",0
	even
dosname:
	dc.b	"dos.library",0
	even

EndCode:

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 SPAtan 
	dc.l SPSin
	dc.l SPCos
	dc.l SPTan
	dc.l SPSincos
	dc.l SPSinh
	dc.l SPCosh
	dc.l SPTanh
	dc.l SPExp
	dc.l SPLog
	dc.l SPPow
	dc.l SPSqrt
	dc.l SPTieee
	dc.l SPFieee
	dc.l SPAsin
	dc.l SPAcos
	dc.l SPLog10	
	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	a5,-(sp)
	move.l	d0,a5
	clr.w	LIB_OPENCNT(a5)
	move.l	a6,ml_SysLib(a5)
	move.l	a0,ml_SegList(a5)
	lea	LibList(a6),a0
	lea	dosname(pc),a1
	jsr	_LVOFindName(a6)
	move.l	d0,ml_DosLib(a5)
	move.l	a5,d0
	move.l	(sp)+,a5

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

	rts

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

Open:
	addq.w	#1,LIB_OPENCNT(a6)
	bclr	#LIBB_DELEXP,ml_Flags(a6)
	move.l	a6,d0
	rts

Close:
	moveq	#0,d0
	subq.w	#1,LIB_OPENCNT(a6)
	bne.s	Close1
	btst	#LIBB_DELEXP,ml_Flags(a6)
	beq.s	Close1
	bsr.s	Expunge
Close1:	rts

Expunge:
	movem.l	d1-d2/a0-a1/a5-a6,-(sp)
	move.l	a6,a5
	move.l	ml_SysLib(a5),a6
	tst.w	LIB_OPENCNT(a5)
	beq.s	Expunge1
	bset	#LIBB_DELEXP,ml_Flags(a5)
	moveq	#0,d0
	bra.s	Expunge2
Expunge1:
	move.l	ml_SegList(a5),d2
	move.l	a5,a1
	jsr	_LVORemove(a6)
	move.l	a5,a1
	moveq	#0,d0
	move.w	LIB_NEGSIZE(a5),d0
	sub.l	d0,a1
	add.w	LIB_POSSIZE(a5),d0
	jsr	_LVOFreeMem(a6)
	move.l	d2,d0
Expunge2:
	movem.l	(sp)+,d1-d2/a0-a1/a5-a6
	rts

SPAtan:
	fmove.s	fp0,-(sp)
	bsr.s	SPTieee	
	fatan.s	d0,fp0
	bra	DoFieee

SPSin:
	fmove.s	fp0,-(sp)
	bsr.s	SPTieee	
	fsin.s	d0,fp0
	bra	DoFieee

SPCos:
	fmove.s	fp0,-(sp)
	bsr.s	SPTieee	
	fcos.s	d0,fp0
	bra	DoFieee

SPTan:
	fmove.s	fp0,-(sp)
	bsr.s	SPTieee	
	ftan.s	d0,fp0
	bra	DoFieee

SPSincos:
	fmovem.x fp0/fp1,-(sp)
	bsr.s	SPTieee
	move.l	d1,a0
	fsincos.s d0,fp0:fp1
	fmove.s	fp0,d0
	bsr	SPFieee
	move.l	d0,(a0)
	fmove.s	fp1,d0
	fmovem.x (sp)+,fp0/fp1
	bra	SPFieee

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

SPSinh:
	fmove.s	fp0,-(sp)
	bsr.s	SPTieee	
	fsinh.s	d0,fp0
	bra	DoFieee

SPCosh:
	fmove.s	fp0,-(sp)
	bsr.s	SPTieee	
	fcosh.s	d0,fp0
	bra.s	DoFieee

SPTanh:
	fmove.s	fp0,-(sp)
	bsr.s	SPTieee	
	ftanh.s	d0,fp0
	bra.s	DoFieee

SPExp:
	fmove.s	fp0,-(sp)
	bsr.s	SPTieee	
	fetox.s	d0,fp0
	bra.s	DoFieee

SPLog:
	fmove.s	fp0,-(sp)
	bsr.s	SPTieee	
	flogn.s	d0,fp0
	bra.s	DoFieee

SPPow:
	fmove.s	fp0,-(sp)
	bsr.s	SPTieee
	fabs.s	d0,fp0
	fmove.s	fp0,d0
	flogn.s	d0,fp0
	move.l	d1,d0
	bsr.s	SPTieee
	fmul.s	d0,fp0
	fmove.s	fp0,d0
	fetox.s	d0,fp0
	bra.s	DoFieee

SPSqrt:
	fmove.s	fp0,-(sp)
	bsr	SPTieee	
	fsqrt.s	d0,fp0
	bra.s	DoFieee

SPAsin:
	fmove.s	fp0,-(sp)
	bsr	SPTieee	
	fasin.s	d0,fp0
	bra.s	DoFieee

SPAcos:
	fmove.s	fp0,-(sp)
	bsr	SPTieee	
	facos.s	d0,fp0
	bra.s	DoFieee

SPLog10:
	fmove.s	fp0,-(sp)
	bsr	SPTieee	
	flog10.s d0,fp0

DoFieee:
	fmove.s	fp0,d0
	fmove.s	(sp)+,fp0
SPFieee:
	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

alert:	dc.b	0,88,16
	dc.b	"This mathtrans.library needs a 68881/82 Math Coprocessor !",0
	dc.b	1,0,92,28
	dc.b	"Please use the library from your original Workbench disk.",0,0,0
	even

	End
