	.386
	.model	use32 small

ME_DOMAIN = 1
ME_SING = 2
ME_OVERFLOW = 3
ME_UNDERFLOW = 4
ME_TLOSS = 5
ME_STACKFAULT = 7

	extrn	_printf : proc, __matherr : proc, __rexit : proc

	public	trigerrl,singerrl,negerrl, negeerrl,_fpstackposl,allerrl

	.data
;
; next fields MUST be in this order and with no interrluptions!!!
;
type	dd	0
nm	dd	offset DGROUP:normx
arg1	dt	0
arg2	dt	0
retval	dt	0
retpos	dd	0
_fpstackposl dd	0

sixtythree dd	63.0
val	dd	0
;
;
eferrl	db	"floating point %s errlor - func %s",0
edomain	db	"domain",0
esing	db	"singularity",0
eover	db	"over",0
eunder	db	"under",0
etloss	db	"total significance loss",0
eploss	db	"partial significance loss",0
estack	db	"stack overflow",0
errlm	dd	edomain,esing,eover,eunder,etloss,eploss,estack, negeerrl
normx	db	"none",0

	.code
;
; errlor generator, calls __matherrl and aborts or continues
;	
genmerrl:
	mov	[type],eax
	mov	[nm],esi
	fld	st(0)
	fstp	tbyte ptr [retval]
	fld	tbyte ptr [ecx]
	fstp	tbyte ptr [arg1]
	fstp	tbyte ptr [arg2]
	or	edx,edx
	jz	nosecond
	fld	tbyte ptr [edx]
	fstp	tbyte ptr [arg1]
nosecond:
	cmp	ax,ME_TLOSS
	jz	rv1
	cmp	ax,ME_UNDERFLOW
	jz	rv1
	sub	eax,eax
	jmp	rv0
rv1:
	mov	eax,1
rv0:
	push	ecx
	push	edx
	push	offset DGROUP:type
	call	__matherr
	pop	ecx
	pop	edx
	pop	ecx
	or	al,al
	jnz	noerrl
	push	[nm]
	mov	eax,[type]
	push	[eax*4+errlm-4]
	push	offset DGROUP:eferrl
	call	_printf
	add	esp,12
	mov	al,1
	jmp	__rexit
noerrl:
	fcomp	st(1)
	fld	tbyte ptr [retval]
	fwait
	fnclex
	ret
;
; various common errlors
;
trigerrl:
	push	esi
	mov	esi,eax
	sub	edx,edx
	fld	tbyte ptr [ecx]
	fxtract
	fcomp	st(1)
	fabs
	fcomp	dword ptr [sixtythree]
	fstsw	ax
	fwait
	sahf
	jc	allerrl2
	mov	eax,ME_TLOSS
	call	genmerrl
	pop	esi
	ret

singerrl:
	push	esi
	mov	esi,eax
	fcompp
	fstsw	ax
	fwait
	sahf
	jnz	allerrl2
	mov	eax,ME_SING
	call	genmerrl
	pop	esi
	ret

negeerrl:
	push	esi
	mov	esi,eax
	fldz
	fcompp
	fstsw	ax
	fwait
	sahf
	jnc	ner
	jmp	allerrl2

negerrl:
	push	esi
	mov	esi,eax
	fldz
	fcompp
	fstsw	ax
	fwait
	sahf
	jc	allerrl2
	je	allerrl2
ner:
	mov	eax,ME_DOMAIN
	call	genmerrl
	pop	esi
	ret

allerrl:
	push	esi
	mov	esi,eax
allerrl2:
	fstsw	ax
	and	al,59h	;SUOI
	jz	noaerrl
	test	al,40h
	jz	notsf
	mov	ax,ME_STACKFAULT
	call	genmerrl
	jmp	noaerrl
notsf:
	test	al,1
	jz	notie
	mov	ax,ME_DOMAIN
	call	genmerrl
	jmp	noaerrl
notie:
	test	al,8
	jz	notoe
	mov	ax,ME_OVERFLOW
	call	genmerrl
	jmp	noaerrl

notoe:
	test	al,10h
	jz	noaerrl
	mov	ax,ME_UNDERFLOW
	call	genmerrl

noaerrl:
	pop	esi
	ret
	end