⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 float.s,v

📁 TCP-IP红宝书源代码
💻 S,V
📖 第 1 页 / 共 2 页
字号:
head	1.1;
access;
symbols;
locks
	dls:1.1; strict;
comment	@# @;


1.1
date	97.09.21.19.27.40;	author dls;	state Dist;
branches;
next	;


desc
@@


1.1
log
@pre-3e code
@
text
@|J. Test	3/81
|floating point routines for Nu(MC68000)

				|offsets in internal float structure
SIGN	= 0				|sign
EXPT	= 2				|exponent (-127/+127)
MANH	= 4				|high mantissa
MANL	= 8				|low mantissa

	.data
afloat:				|internal-a floating representation
a_sign:	.word	0
a_expt:	.word	0
a_manh:	.long	0
a_manl:	.long	0

bfloat:				|internal-b floating representation
b_sign:	.word	0
b_expt:	.word	0
b_manh:	.long	0
b_manl:	.long	0

lsum:				|storage for multiply
	.word	0
	.long	0			|high part of accumulated sum
	.long	0			|low part of accumulated sum
	.word	0

	
|
|convert external float to internal format
|d0,d1 contain the external float
|a0 points to afloat or bfloat
|
	.text
etoi:
	clrw	a0@@			|clear sign
	tstl	d0			|test sign of external
	bge	1$			|set sign 0(+), 1(-)
	movw	#1,a0@@

1$:	movl	d1,a0@@(MANL)		|save low 32 bits of mantissa
	movl	d0,d1
	andl	#0x7FFFFF,d1
	orl	#0x800000,d1		|add hidden high order bit
	movl	d1,a0@@(MANH)		|save high 1+23 bits of mantissa
	swap	d0
	asrl	#7,d0
	andw	#0xFF,d0		|isolate exponent
	bne	2$
	clrl	a0@@			|zero sign, exponent,
	clrl	a0@@(MANH)		|  high mantissa, and
	clrl	a0@@(MANL)		|  low mantissa
	rts

2$:	subw	#128,d0			|convert from excess 0200
	movw	d0,a0@@(EXPT)		|store converted value
	rts				|done

|
|convert internal format to external float
|a0 points to afloat or bfloat
|external float returned in d0,d1
|
	.text
itoe:
	clrl	d0
	movw	a0@@(EXPT),d0		|get exponent
	addw	#128,d0			|convert to excess 0200
	bne	1$			|if exponent is zero
	clrl	d1			|  clear d0,d1
	rts				|  and return

1$:	tstw	a0@@			|set sign
	beq	2$			|sign bit 0(+), 1(-)
	orw	#0x100,d0

2$:	swap	d0			|align sign and exponent
	asll	#7,d0			|  in high part of d0
	movl	a0@@(MANH),d1		|get high part of mantissa
	bne	3$			|check for zero mantissa
	clrl	d0			|if zero - clear sign and
	rts				|  exponent and return

3$:	andl	#0x7FFFFF,d1		|delete high order hidden bit
	orl	d1,d0			|put high 23 bits of mantissa
	movl	a0@@(MANL),d1		|put low 32 bits of mantissa
	rts				|done

|
|normalize internal float by adjusting exponent and
|shifting  mantissa appropriately so 1/2 <= mnt < 1
|a0 points to afloat or bfloat
|
	.text
normal:
	jsr	offset			|determine amount to shift
	addw	d0,a0@@(EXPT)		|adjust exponent
	jsr	shift			|shift mantissa
	rts

|
|determine position of most significant bit of
|mantissa in relation to normalized decimal point
|a0 points to afloat or bfloat
|d0 returns offset of msb from decimal point
|
	.text
offset:
	moveq	#1,d0
	movl	a0@@(MANH),d1		|check for high order bits
	bne	2$
	movl	a0@@(MANL),d1		|check low order bits
	bne	1$
	clrw	d0			|zero shift count
	rts

1$:	subw	#32,d0			|need to shift at least 23
2$:	subqw	#1,d0			|find most significant bit
	asll	#1,d1
	bcc	2$
	addqw	#8,d0			|d0 contains exponent correction
	rts

|
|shift mantissa according to offset in d0
|a0 points to afloat or bfloat
|d0 contains shift count, <0 -> left shift, >0 -> right shift
|on return, d1 = 0, d2,d3 have shifted mantissa
|
	.text
shift:
	clrl	d1
	movl	a0@@(MANH),d2		|d2 = high part of mantissa
	movl	a0@@(MANL),d3		|d3 = low part of mantissa
	movw	d0,d1			|examine exponent correction
	bmi	2$			|shift left
	bne	1$			|shift right
	rts				|no shift - return

1$:	asrl	#1,d2			|shift entire mantissa
	roxrl	#1,d3			|  right by one bit
	subqw	#1,d1			|repeat until count
	bne	1$			|  is zero
	andl	#0xFFFFFF,d2		|zero top byte
	bra	shifte			|return

2$:	asll	#1,d3			|shift entire mantissa
	roxll	#1,d2			|  left by one bit
	addqw	#1,d1			|repeat until count
	bne	2$			|  is zero
	
shifte:	movl	d2,a0@@(MANH)		|store high part of mantissa
	movl	d3,a0@@(MANL)		|store low part of mantissa
	rts				|done
	
|
|fetch floating arguments off stack
|convert to internal format in afloat and bfloat
|on return, a0 points to afloat, a1 points to bfloat
|
getargs:
	movl	#bfloat,a1		|a1 points to bfloat
	tstw	d0			|branch to 1$ for
	bne	1$			|  indirect argument fetch
	movl	a6@@(16),d0		|b-high
	movl	a6@@(20),d1		|b-low
	movl	a1,a0			|setup a0 for conversion
	jsr	etoi			|convert b-arg to internal form
	movl	a6@@(8),d0		|a-high
	movl	a6@@(12),d1		|a-low
	movl	#afloat,a0		|a0 points to afloat
	jsr	etoi			|convert a-arg to internal form
	rts
1$:	movl	a6@@(12),d0		|b-high
	movl	a6@@(16),d1		|b-low
	movl	a1,a0			|setup a0 for conversion
	jsr	etoi			|convert b-arg to internal form
	movl	a6@@(8),a0		|a0 points to a-arg
	movl	a0@@+,d0			|a-high
	movl	a0@@,d1			|a-low
	movl	#afloat,a0		|a0 points to afloat
	jsr	etoi			|convert a-arg to internal form
	rts	

	.globl	fltused
	.text
fltused:
	rts				|simply define fltused

|
|free exponent returning fractional value
|
	.globl	frexp
	.text
frexp:
	link	a6,#0
	movl	#afloat,a0		|a0 points to afloat
	movl	a6@@(8),d0		|a-high
	movl	a6@@(12),d1		|a-low
	movl	a6@@(16),a1		|place to return exponent
	jsr	etoi			|convert to internal form
	movw	a0@@(EXPT),d0		|get unbiased exponent
	extl	d0			|  convert to long and
	movl	d0,a1@@			|  return value
	clrw	a0@@(EXPT)		|set exponent for fractional
	jsr	itoe			|  value, convert for return
	unlk	a6
	rts

|
|add/load exponent of float
|
	.globl	ldexp
	.text
ldexp:
	link	a6,#0
	movl	#afloat,a0		|a0 points to afloat
	movl	a6@@(8),d0		|a-high
	movl	a6@@(12),d1		|a-low
	jsr	etoi			|convert to internal form
	movl	a6@@(16),d0		|add argument
	addw	d0,a0@@(EXPT)		|  to exponent
	jsr	itoe			|convert and return
	unlk	a6
	rts


|
|separate integer/fractional parts of float
|
	.globl	_modf
	.text
_modf:
	link	a6,#0
	moveml	#0x3800,sp@@-		|save d2,d3,d4
	movl	a6@@(8),d0		|a-high
	movl	a6@@(12),d1		|a-low
	movl	#afloat,a0		|a0 -> afloat = fractional
	jsr	etoi			|  part on return
	movl	#bfloat,a1		|a1 -> bfloat = integer part on return
	movw	a0@@,a1@@			|copy signs
	movw	a0@@(EXPT),d4		|if exponent > 0
	bgt	1$			|  separate integer/fractional
	movw	#-128,a1@@(EXPT)		|else integer part = 0
	movl	a1,a0			|  convert integer part first
	bra	modfe			|  no need to separate

1$:	movw	d4,a1@@(EXPT)		|set integer exponent
	clrw	a0@@(EXPT)		|set fractional exponent
	cmpw	#56,d4			|if shift count is < 56
	blt	2$			|  shift mantissa
	movl	a0@@(MANL),a1@@(MANL)	|else move mantissa to integer
	movl	a0@@(MANH),a1@@(MANH)	|  part and set fractional
	movw	#-128,a0@@(EXPT)		|  part = 0
	movl	a1,a0			|convert decimal part first
	bra	modfe			|  on exit from modf

2$:	moveq	#-8,d0			|shift mantissa left
	jsr	shift			|  by 8 for alignment
	clrl	d0
3$:	asll	#1,d3			|rotate d0<--d1<--d2<--d3
	roxll	#1,d2			|  registers until shift
	roxll	#1,d1			|  count = 0
	roxll	#1,d0
	subqw	#1,d4
	bne	3$
	movl	d3,a0@@(MANL)		|save fractional components
	movl	d2,a0@@(MANH)		|  of mantissa
	movl	d1,a1@@(MANL)		|save integer components
	movl	d0,a1@@(MANH)		|  of mantissa
	jsr	normal			|align fractional part
	subqw	#8,a0@@(EXPT)		|  and adjust exponent
	movl	a1,a0			|align integer part at
	jsr	offset			|  decimal point without
	jsr	shift			|  altering exponent

modfe:	movl	a6@@(16),a1		|get pointer argument
	jsr	itoe			|convert integer part
	movl	d0,a1@@+			|  store in location
	movl	d1,a1@@			|  given in argument
	movl	#afloat,a0		|convert fractional
	jsr	itoe			|  part and return
	moveml	sp@@+,#0x1C
	unlk	a6
	rts

|
|convert floating value to fixed 32-bit integer
|
	.globl	fix
	.text
fix:
	link	a6,#0
	moveml	#0x3000,sp@@-		|save d2,d3
	movl	a6@@(8),d0		|d0 = high part of float
	movl	a6@@(12),d1		|d1 = low order part
	movl	#afloat,a0		|a0 points to afloat
	jsr	etoi			|convert to internal format
	tstw	a0@@(EXPT)		|test exponent
	bgt	1$			|if exponent is less
	clrl	d0			|  than or equal to zero
	bra	fixe			|  return zero

1$:	moveq	#-8,d0			|shift mantissa left
	jsr	shift			|  by 8 for alignment
	movw	a0@@(EXPT),d1		|(note: after shift d1 = 0)
	clrl	d0			|clear d0
2$:	asll	#1,d3			|rotate d0<--d2<--d3
	roxll	#1,d2			|  registers until exponent
	roxll	#1,d0			|  count is exhausted
	subqw	#1,d1			|resultant fixed 32-bit
	bne	2$			|  value is in d0

	tstw	a0@@			|check sign of float
	beq	fixe			|positive - d0 is ok
	negl	d0			|negative - negate d0
fixe:	moveml	sp@@+,#0xC		|pop d2,d3
	unlk	a6
	rts

|
|convert fixed 32-bit integer to floating
|
	.globl	float
	.text
float:
	link	a6,#0
	moveml	#0x3000,sp@@-		|save d2,d3
	movl	#afloat,a0		|a0 points to afloat
	clrl	a0@@(MANH)		|clear junk from mantissa
	clrw	a0@@			|clear sign
	movl	a6@@(8),d0		|d0 = 32-bit long
	bmi	1$			|negative
	bpl	2$			|positive
	movw	#-128,a0@@(EXPT)		|floating zero

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -