;
;  COMPARE.MAC - Comparison Functions
;
;  0=  0<>  =  0<  0>  <  >  U<  U>  MAX  MIN  UMAX  UMIN
;  WITHIN  BETWEEN  D0=  D=  D0<  D<  DU<  DMIN  DMAX
;

;	0=  ( x -- flag )

	hdr	1,'0='
zequ:	pop	hl
zequ1:	ld	a,l
	or	h
	jp	z,true
	jp	false

;	0<>  ( x -- flag )

	hdr	1,'0<>'
zneq:	pop	hl
zneq1:	ld	a,l
	or	h
	jp	z,false
	jp	true

;	=  ( x1 x2 -- flag )	- 0=

	hdr	1,'='
equal:	pop	de
	pop	hl
equal1:	call	cmpu
	jp	z,true
	jp	false

;	<>  ( x1 x2 -- flag )	- 0= 0=

	hdr	1,'<>'
nequ:	pop	de
	pop	hl
	call	cmpu
	jp	z,false
	jp	true

;	0<  ( n -- flag )

	hdr	1,'0<'
zless:	pop	hl
zless1:	add	hl,hl
zless2:	jp	c,true
	jp	false

;	0>  ( n -- flag )

	hdr	1,'0>'
zgrea:	pop	de
	ld	hl,0
zgrea1:	call	cmps
	jp	c,true
	jp	false

;	<  ( n1 n2 -- flag )

	hdr	1,'<'
less:	pop	de
	pop	hl
	jp	zgrea1

;	>  ( n1 n2 -- flag )

	hdr	1,'>'
great:	pop	hl
	pop	de
	jp	zgrea1

;	U<  ( u1 u2 -- flag )

	hdr	1,'U<'
uless:	pop	de
	pop	hl
uless1:	call	cmpu
	jp	c,true
	jp	false

;	U>  ( u1 u2 -- flag )

	hdr	1,'U>'
ugrea:	pop	hl
ugrea1:	pop	de
	jp	uless1

;	MAX  ( n1 n2 -- n1 | n2 )  2dup < if swap then drop

	hdr	1,'MAX'
max:	pop	de
	pop	hl
	call	cmps
max1:	ccf
max2:	jp	c,hpush
	ex	de,hl
	jp	hpush

;	MIN  ( n1 n2 -- n1 | n2 )  2dup > if swap then drop

	hdr	1,'MIN'
min:	pop	de
	pop	hl
	call	cmps
	jp	max2

;	UMAX  ( u1 u2 -- u1 | u2 )  2dup u< if swap then drop

	hdr	1,'UMAX'
umax:	pop	de
	pop	hl
	call	cmpu
	jp	max1

;	UMIN  ( u1 u2 -- u1 | u2 )  2dup u> if swap then drop

	hdr	1,'UMIN'
umin:	pop	de
	pop	hl
	call	cmpu
	jp	max2

;	WITHIN  ( n1|u1 n2|u2 n3|u3 -- flag )  over - >r - r> u<

	hdr	1,'WITHIN'	; ANS
within:	pop	hl
	pop	de
	call	ssub
	ex	(sp),hl
	call	ssub
	jp	ugrea1

;	BETWEEN  ( n1|u1 n2|u2 n3|u3 -- flag )  over - -rot - u< 0=

	hdr	1,'BETWEEN'
betw:	pop	hl
	pop	de
	call	ssub
	ex	(sp),hl
	call	ssub
	pop	de
	call	cmpu
	jp	z,true
	jp	zless2

;	D0=  ( d -- flag )	or 0=

	hdr	1,'D0='
dzequ:	call	docol
	dw	orr,zequ
	dw	exit

;	D=  ( d1 d2 -- flag )

	hdr	1,'D='
dequ:	pop	de
	pop	hl
	ex	(sp),hl
	call	cmpu
	pop	de
	pop	hl
	jp	nz,false
	jp	equal1

;	D0<  ( d -- flag )	swap drop 0<

	hdr	1,'D0<'		; ANS
dzle:	pop	hl
	pop	de
	jp	zless1

;	D<  ( d1 d2 -- flag )	rot 2dup = if 2drop u< exit then 2swap 2drop >

	hdr	1,'D<'
dless:	pop	de
	pop	hl
	ex	(sp),hl
	call	cmpu
	jp	z,uless
	inc	sp
	inc	sp
	inc	sp
	inc	sp
	jp	zgrea1

;	DU<  ( ud1 ud2 -- flag )  rot swap 2dup u< if 2drop 2drop -1 exit
;				  then - if 2drop 0 exit then u<

	hdr	1,'DU<'
dules:	pop	de
	pop	hl
	ex	(sp),hl
	call	cmpu
	pop	de
	pop	hl
	jp	c,true
	jp	nz,false
	jp	uless1

;	DMIN  ( d1 d2 -- d1 | d2 )  2over 2over d< 0= if 2swap then 2drop

	hdr	1,'DMIN'
dmin:	call	docol
	dw	tover,tover
	dw	dless,zequ
dmin1:	dw	zbran,dmin2
	dw	tswap
dmin2:	dw	tdrop
	dw	exit

;	DMAX  ( d1 d2 -- d1 | d2 )  2over 2over d< if 2swap then 2drop

	hdr	1,'DMAX'
dmax:	call	docol
	dw	tover,tover
	dw	dless
	dw	bran,dmin1

;	end

