; (C) Peter Thompson 1993
; atol() and atoul() style routines for 32 bit integers.

	INCLUDE	"intdefs.i"
	INCLUDE	"to32lib.i"

	IFD	ASM_ENTRY
	INCLUDE	"intconv32.i"
	ENDIF

	IFD	STKARGS
	PUBLIC	_atou32
_atou32:			;C stkargs entry
	move.l	4(SP),a0	;C char *
	ENDIF

	IFD	REGARGS
	PUBLIC	@atou32
@atou32:			;C regargs entry
	ENDIF
a2u32:	lea	Tou32(PC),a1
	bra	to32

	IFD	STKARGS
	PUBLIC	_atoi32
_atoi32:			;C stkargs entry
	move.l	4(SP),a0	;C char *
	ENDIF

	IFD	REGARGS
	PUBLIC	@atoi32
@atoi32:			;C regargs entry
	ENDIF

a2i32:	lea	Toi32(PC),a1
to32:	push	Digit/Errno/a6
	lea	au32(PC),a6
	jsr	0(a1)
	move.l	Errno,d1
	pop	Digit/Errno/a6
	rts

	; convert base 10 ASCII string into unsigned 32-bit integer.

au32:	sub.b	#'0',Digit	; get first digit
	bcs	Derr32		;V unless not valid digit
	move.l	Digit,d0	; and initialise number to it
	cmp.b	#10,Digit	;V
	bcc	Derr32		;V unless not valid digit
au32a:
	GetCh	Digit
	sub.b	#'0',Digit
	bcs	au32x		; Less than '0',
	cmp.b	#10,Digit
	bcc	au32x		; or more than '9', so exit.
au32b:				; multiply by 10 and overflow check.
	add.l	d0,d0		; *2
	bcs	Verr32		;V
	move.l	d0,d1
	add.l	d1,d1		; *4
	bcs	Verr32		;V
	add.l	d1,d1		; *8
	bcs	Verr32		;V
	add.l	d1,d0		; *2 + *8 = *10.
	bcs	Verr32		;V
	add.l	Digit,d0	; + Digit
	bcc	au32a		;V SPECIAL - falls through if overflow.
Verr32:	move.l	#ERR_RANGE,Errno	; overflow!
au32x:	UnGetCh
	rts
Derr32:	move.l	#ERR_NONUM,Errno	; no digits in number!
	bra	au32x

	END

