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

	opt p=68881
	exeobj
	optimon
	objfile	"ram:mathtrans.library"
	errfile	"ram:mathtrans.err"
	lisfile	"ram:mathtrans.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	"mathtrans.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 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	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
	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)
	bne.s	Close1
	btst.b	#3,42(a6)
	beq.s	Close1
	bsr.s	Expunge
Close1:	rts

Expunge:
	movem.l	d2/a5-a6,-(sp)
	move.l	a6,a5
	move.l	34(a5),a6
	tst.w	32(a5)
	beq.s	Expunge1
	bset.b	#3,42(a5)
	moveq	#0,d0
	bra.s	Expunge2
Expunge1:
	move.l	38(a5),d2
	move.l	a5,a1
	jsr	_LVORemove(a6)
	move.l	a5,a1
	moveq	#0,d0
	moveq	#0,d1
	move.w	16(a5),d1
	sub.l	d1,a1
	move.w	18(a5),d0
	add.l	d1,d0
	jsr	_LVOFreeMem(a6)
	move.l	d2,d0
Expunge2:
	movem.l	(sp)+,d2/a5-a6
	rts

	cnop	0,4

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

	cnop	0,4

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

	cnop	0,4

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

	cnop	0,4

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

	cnop	0,4

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

	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

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

	cnop	0,4

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

	cnop	0,4

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

	cnop	0,4

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

	cnop	0,4

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

	cnop	0,4

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

	cnop	0,4

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

	cnop	0,4

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

	cnop	0,4

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

	cnop	0,4

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

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

	section	Text,DATA

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

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
	cnop	0,2
EndCode:	
	End
