162306a36Sopenharmony_ci|
262306a36Sopenharmony_ci|	bindec.sa 3.4 1/3/91
362306a36Sopenharmony_ci|
462306a36Sopenharmony_ci|	bindec
562306a36Sopenharmony_ci|
662306a36Sopenharmony_ci|	Description:
762306a36Sopenharmony_ci|		Converts an input in extended precision format
862306a36Sopenharmony_ci|		to bcd format.
962306a36Sopenharmony_ci|
1062306a36Sopenharmony_ci|	Input:
1162306a36Sopenharmony_ci|		a0 points to the input extended precision value
1262306a36Sopenharmony_ci|		value in memory; d0 contains the k-factor sign-extended
1362306a36Sopenharmony_ci|		to 32-bits.  The input may be either normalized,
1462306a36Sopenharmony_ci|		unnormalized, or denormalized.
1562306a36Sopenharmony_ci|
1662306a36Sopenharmony_ci|	Output:	result in the FP_SCR1 space on the stack.
1762306a36Sopenharmony_ci|
1862306a36Sopenharmony_ci|	Saves and Modifies: D2-D7,A2,FP2
1962306a36Sopenharmony_ci|
2062306a36Sopenharmony_ci|	Algorithm:
2162306a36Sopenharmony_ci|
2262306a36Sopenharmony_ci|	A1.	Set RM and size ext;  Set SIGMA = sign of input.
2362306a36Sopenharmony_ci|		The k-factor is saved for use in d7. Clear the
2462306a36Sopenharmony_ci|		BINDEC_FLG for separating normalized/denormalized
2562306a36Sopenharmony_ci|		input.  If input is unnormalized or denormalized,
2662306a36Sopenharmony_ci|		normalize it.
2762306a36Sopenharmony_ci|
2862306a36Sopenharmony_ci|	A2.	Set X = abs(input).
2962306a36Sopenharmony_ci|
3062306a36Sopenharmony_ci|	A3.	Compute ILOG.
3162306a36Sopenharmony_ci|		ILOG is the log base 10 of the input value.  It is
3262306a36Sopenharmony_ci|		approximated by adding e + 0.f when the original
3362306a36Sopenharmony_ci|		value is viewed as 2^^e * 1.f in extended precision.
3462306a36Sopenharmony_ci|		This value is stored in d6.
3562306a36Sopenharmony_ci|
3662306a36Sopenharmony_ci|	A4.	Clr INEX bit.
3762306a36Sopenharmony_ci|		The operation in A3 above may have set INEX2.
3862306a36Sopenharmony_ci|
3962306a36Sopenharmony_ci|	A5.	Set ICTR = 0;
4062306a36Sopenharmony_ci|		ICTR is a flag used in A13.  It must be set before the
4162306a36Sopenharmony_ci|		loop entry A6.
4262306a36Sopenharmony_ci|
4362306a36Sopenharmony_ci|	A6.	Calculate LEN.
4462306a36Sopenharmony_ci|		LEN is the number of digits to be displayed.  The
4562306a36Sopenharmony_ci|		k-factor can dictate either the total number of digits,
4662306a36Sopenharmony_ci|		if it is a positive number, or the number of digits
4762306a36Sopenharmony_ci|		after the decimal point which are to be included as
4862306a36Sopenharmony_ci|		significant.  See the 68882 manual for examples.
4962306a36Sopenharmony_ci|		If LEN is computed to be greater than 17, set OPERR in
5062306a36Sopenharmony_ci|		USER_FPSR.  LEN is stored in d4.
5162306a36Sopenharmony_ci|
5262306a36Sopenharmony_ci|	A7.	Calculate SCALE.
5362306a36Sopenharmony_ci|		SCALE is equal to 10^ISCALE, where ISCALE is the number
5462306a36Sopenharmony_ci|		of decimal places needed to insure LEN integer digits
5562306a36Sopenharmony_ci|		in the output before conversion to bcd. LAMBDA is the
5662306a36Sopenharmony_ci|		sign of ISCALE, used in A9. Fp1 contains
5762306a36Sopenharmony_ci|		10^^(abs(ISCALE)) using a rounding mode which is a
5862306a36Sopenharmony_ci|		function of the original rounding mode and the signs
5962306a36Sopenharmony_ci|		of ISCALE and X.  A table is given in the code.
6062306a36Sopenharmony_ci|
6162306a36Sopenharmony_ci|	A8.	Clr INEX; Force RZ.
6262306a36Sopenharmony_ci|		The operation in A3 above may have set INEX2.
6362306a36Sopenharmony_ci|		RZ mode is forced for the scaling operation to insure
6462306a36Sopenharmony_ci|		only one rounding error.  The grs bits are collected in
6562306a36Sopenharmony_ci|		the INEX flag for use in A10.
6662306a36Sopenharmony_ci|
6762306a36Sopenharmony_ci|	A9.	Scale X -> Y.
6862306a36Sopenharmony_ci|		The mantissa is scaled to the desired number of
6962306a36Sopenharmony_ci|		significant digits.  The excess digits are collected
7062306a36Sopenharmony_ci|		in INEX2.
7162306a36Sopenharmony_ci|
7262306a36Sopenharmony_ci|	A10.	Or in INEX.
7362306a36Sopenharmony_ci|		If INEX is set, round error occurred.  This is
7462306a36Sopenharmony_ci|		compensated for by 'or-ing' in the INEX2 flag to
7562306a36Sopenharmony_ci|		the lsb of Y.
7662306a36Sopenharmony_ci|
7762306a36Sopenharmony_ci|	A11.	Restore original FPCR; set size ext.
7862306a36Sopenharmony_ci|		Perform FINT operation in the user's rounding mode.
7962306a36Sopenharmony_ci|		Keep the size to extended.
8062306a36Sopenharmony_ci|
8162306a36Sopenharmony_ci|	A12.	Calculate YINT = FINT(Y) according to user's rounding
8262306a36Sopenharmony_ci|		mode.  The FPSP routine sintd0 is used.  The output
8362306a36Sopenharmony_ci|		is in fp0.
8462306a36Sopenharmony_ci|
8562306a36Sopenharmony_ci|	A13.	Check for LEN digits.
8662306a36Sopenharmony_ci|		If the int operation results in more than LEN digits,
8762306a36Sopenharmony_ci|		or less than LEN -1 digits, adjust ILOG and repeat from
8862306a36Sopenharmony_ci|		A6.  This test occurs only on the first pass.  If the
8962306a36Sopenharmony_ci|		result is exactly 10^LEN, decrement ILOG and divide
9062306a36Sopenharmony_ci|		the mantissa by 10.
9162306a36Sopenharmony_ci|
9262306a36Sopenharmony_ci|	A14.	Convert the mantissa to bcd.
9362306a36Sopenharmony_ci|		The binstr routine is used to convert the LEN digit
9462306a36Sopenharmony_ci|		mantissa to bcd in memory.  The input to binstr is
9562306a36Sopenharmony_ci|		to be a fraction; i.e. (mantissa)/10^LEN and adjusted
9662306a36Sopenharmony_ci|		such that the decimal point is to the left of bit 63.
9762306a36Sopenharmony_ci|		The bcd digits are stored in the correct position in
9862306a36Sopenharmony_ci|		the final string area in memory.
9962306a36Sopenharmony_ci|
10062306a36Sopenharmony_ci|	A15.	Convert the exponent to bcd.
10162306a36Sopenharmony_ci|		As in A14 above, the exp is converted to bcd and the
10262306a36Sopenharmony_ci|		digits are stored in the final string.
10362306a36Sopenharmony_ci|		Test the length of the final exponent string.  If the
10462306a36Sopenharmony_ci|		length is 4, set operr.
10562306a36Sopenharmony_ci|
10662306a36Sopenharmony_ci|	A16.	Write sign bits to final string.
10762306a36Sopenharmony_ci|
10862306a36Sopenharmony_ci|	Implementation Notes:
10962306a36Sopenharmony_ci|
11062306a36Sopenharmony_ci|	The registers are used as follows:
11162306a36Sopenharmony_ci|
11262306a36Sopenharmony_ci|		d0: scratch; LEN input to binstr
11362306a36Sopenharmony_ci|		d1: scratch
11462306a36Sopenharmony_ci|		d2: upper 32-bits of mantissa for binstr
11562306a36Sopenharmony_ci|		d3: scratch;lower 32-bits of mantissa for binstr
11662306a36Sopenharmony_ci|		d4: LEN
11762306a36Sopenharmony_ci|		d5: LAMBDA/ICTR
11862306a36Sopenharmony_ci|		d6: ILOG
11962306a36Sopenharmony_ci|		d7: k-factor
12062306a36Sopenharmony_ci|		a0: ptr for original operand/final result
12162306a36Sopenharmony_ci|		a1: scratch pointer
12262306a36Sopenharmony_ci|		a2: pointer to FP_X; abs(original value) in ext
12362306a36Sopenharmony_ci|		fp0: scratch
12462306a36Sopenharmony_ci|		fp1: scratch
12562306a36Sopenharmony_ci|		fp2: scratch
12662306a36Sopenharmony_ci|		F_SCR1:
12762306a36Sopenharmony_ci|		F_SCR2:
12862306a36Sopenharmony_ci|		L_SCR1:
12962306a36Sopenharmony_ci|		L_SCR2:
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ci|		Copyright (C) Motorola, Inc. 1990
13262306a36Sopenharmony_ci|			All Rights Reserved
13362306a36Sopenharmony_ci|
13462306a36Sopenharmony_ci|       For details on the license for this file, please see the
13562306a36Sopenharmony_ci|       file, README, in this same directory.
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci|BINDEC    idnt    2,1 | Motorola 040 Floating Point Software Package
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci#include "fpsp.h"
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci	|section	8
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci| Constants in extended precision
14462306a36Sopenharmony_ciLOG2:	.long	0x3FFD0000,0x9A209A84,0xFBCFF798,0x00000000
14562306a36Sopenharmony_ciLOG2UP1:	.long	0x3FFD0000,0x9A209A84,0xFBCFF799,0x00000000
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ci| Constants in single precision
14862306a36Sopenharmony_ciFONE:	.long	0x3F800000,0x00000000,0x00000000,0x00000000
14962306a36Sopenharmony_ciFTWO:	.long	0x40000000,0x00000000,0x00000000,0x00000000
15062306a36Sopenharmony_ciFTEN:	.long	0x41200000,0x00000000,0x00000000,0x00000000
15162306a36Sopenharmony_ciF4933:	.long	0x459A2800,0x00000000,0x00000000,0x00000000
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ciRBDTBL:	.byte	0,0,0,0
15462306a36Sopenharmony_ci	.byte	3,3,2,2
15562306a36Sopenharmony_ci	.byte	3,2,2,3
15662306a36Sopenharmony_ci	.byte	2,3,3,2
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci	|xref	binstr
15962306a36Sopenharmony_ci	|xref	sintdo
16062306a36Sopenharmony_ci	|xref	ptenrn,ptenrm,ptenrp
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ci	.global	bindec
16362306a36Sopenharmony_ci	.global	sc_mul
16462306a36Sopenharmony_cibindec:
16562306a36Sopenharmony_ci	moveml	%d2-%d7/%a2,-(%a7)
16662306a36Sopenharmony_ci	fmovemx %fp0-%fp2,-(%a7)
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ci| A1. Set RM and size ext. Set SIGMA = sign input;
16962306a36Sopenharmony_ci|     The k-factor is saved for use in d7.  Clear BINDEC_FLG for
17062306a36Sopenharmony_ci|     separating  normalized/denormalized input.  If the input
17162306a36Sopenharmony_ci|     is a denormalized number, set the BINDEC_FLG memory word
17262306a36Sopenharmony_ci|     to signal denorm.  If the input is unnormalized, normalize
17362306a36Sopenharmony_ci|     the input and test for denormalized result.
17462306a36Sopenharmony_ci|
17562306a36Sopenharmony_ci	fmovel	#rm_mode,%FPCR	|set RM and ext
17662306a36Sopenharmony_ci	movel	(%a0),L_SCR2(%a6)	|save exponent for sign check
17762306a36Sopenharmony_ci	movel	%d0,%d7		|move k-factor to d7
17862306a36Sopenharmony_ci	clrb	BINDEC_FLG(%a6)	|clr norm/denorm flag
17962306a36Sopenharmony_ci	movew	STAG(%a6),%d0	|get stag
18062306a36Sopenharmony_ci	andiw	#0xe000,%d0	|isolate stag bits
18162306a36Sopenharmony_ci	beq	A2_str		|if zero, input is norm
18262306a36Sopenharmony_ci|
18362306a36Sopenharmony_ci| Normalize the denorm
18462306a36Sopenharmony_ci|
18562306a36Sopenharmony_ciun_de_norm:
18662306a36Sopenharmony_ci	movew	(%a0),%d0
18762306a36Sopenharmony_ci	andiw	#0x7fff,%d0	|strip sign of normalized exp
18862306a36Sopenharmony_ci	movel	4(%a0),%d1
18962306a36Sopenharmony_ci	movel	8(%a0),%d2
19062306a36Sopenharmony_cinorm_loop:
19162306a36Sopenharmony_ci	subw	#1,%d0
19262306a36Sopenharmony_ci	lsll	#1,%d2
19362306a36Sopenharmony_ci	roxll	#1,%d1
19462306a36Sopenharmony_ci	tstl	%d1
19562306a36Sopenharmony_ci	bges	norm_loop
19662306a36Sopenharmony_ci|
19762306a36Sopenharmony_ci| Test if the normalized input is denormalized
19862306a36Sopenharmony_ci|
19962306a36Sopenharmony_ci	tstw	%d0
20062306a36Sopenharmony_ci	bgts	pos_exp		|if greater than zero, it is a norm
20162306a36Sopenharmony_ci	st	BINDEC_FLG(%a6)	|set flag for denorm
20262306a36Sopenharmony_cipos_exp:
20362306a36Sopenharmony_ci	andiw	#0x7fff,%d0	|strip sign of normalized exp
20462306a36Sopenharmony_ci	movew	%d0,(%a0)
20562306a36Sopenharmony_ci	movel	%d1,4(%a0)
20662306a36Sopenharmony_ci	movel	%d2,8(%a0)
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_ci| A2. Set X = abs(input).
20962306a36Sopenharmony_ci|
21062306a36Sopenharmony_ciA2_str:
21162306a36Sopenharmony_ci	movel	(%a0),FP_SCR2(%a6) | move input to work space
21262306a36Sopenharmony_ci	movel	4(%a0),FP_SCR2+4(%a6) | move input to work space
21362306a36Sopenharmony_ci	movel	8(%a0),FP_SCR2+8(%a6) | move input to work space
21462306a36Sopenharmony_ci	andil	#0x7fffffff,FP_SCR2(%a6) |create abs(X)
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ci| A3. Compute ILOG.
21762306a36Sopenharmony_ci|     ILOG is the log base 10 of the input value.  It is approx-
21862306a36Sopenharmony_ci|     imated by adding e + 0.f when the original value is viewed
21962306a36Sopenharmony_ci|     as 2^^e * 1.f in extended precision.  This value is stored
22062306a36Sopenharmony_ci|     in d6.
22162306a36Sopenharmony_ci|
22262306a36Sopenharmony_ci| Register usage:
22362306a36Sopenharmony_ci|	Input/Output
22462306a36Sopenharmony_ci|	d0: k-factor/exponent
22562306a36Sopenharmony_ci|	d2: x/x
22662306a36Sopenharmony_ci|	d3: x/x
22762306a36Sopenharmony_ci|	d4: x/x
22862306a36Sopenharmony_ci|	d5: x/x
22962306a36Sopenharmony_ci|	d6: x/ILOG
23062306a36Sopenharmony_ci|	d7: k-factor/Unchanged
23162306a36Sopenharmony_ci|	a0: ptr for original operand/final result
23262306a36Sopenharmony_ci|	a1: x/x
23362306a36Sopenharmony_ci|	a2: x/x
23462306a36Sopenharmony_ci|	fp0: x/float(ILOG)
23562306a36Sopenharmony_ci|	fp1: x/x
23662306a36Sopenharmony_ci|	fp2: x/x
23762306a36Sopenharmony_ci|	F_SCR1:x/x
23862306a36Sopenharmony_ci|	F_SCR2:Abs(X)/Abs(X) with $3fff exponent
23962306a36Sopenharmony_ci|	L_SCR1:x/x
24062306a36Sopenharmony_ci|	L_SCR2:first word of X packed/Unchanged
24162306a36Sopenharmony_ci
24262306a36Sopenharmony_ci	tstb	BINDEC_FLG(%a6)	|check for denorm
24362306a36Sopenharmony_ci	beqs	A3_cont		|if clr, continue with norm
24462306a36Sopenharmony_ci	movel	#-4933,%d6	|force ILOG = -4933
24562306a36Sopenharmony_ci	bras	A4_str
24662306a36Sopenharmony_ciA3_cont:
24762306a36Sopenharmony_ci	movew	FP_SCR2(%a6),%d0	|move exp to d0
24862306a36Sopenharmony_ci	movew	#0x3fff,FP_SCR2(%a6) |replace exponent with 0x3fff
24962306a36Sopenharmony_ci	fmovex	FP_SCR2(%a6),%fp0	|now fp0 has 1.f
25062306a36Sopenharmony_ci	subw	#0x3fff,%d0	|strip off bias
25162306a36Sopenharmony_ci	faddw	%d0,%fp0		|add in exp
25262306a36Sopenharmony_ci	fsubs	FONE,%fp0	|subtract off 1.0
25362306a36Sopenharmony_ci	fbge	pos_res		|if pos, branch
25462306a36Sopenharmony_ci	fmulx	LOG2UP1,%fp0	|if neg, mul by LOG2UP1
25562306a36Sopenharmony_ci	fmovel	%fp0,%d6		|put ILOG in d6 as a lword
25662306a36Sopenharmony_ci	bras	A4_str		|go move out ILOG
25762306a36Sopenharmony_cipos_res:
25862306a36Sopenharmony_ci	fmulx	LOG2,%fp0	|if pos, mul by LOG2
25962306a36Sopenharmony_ci	fmovel	%fp0,%d6		|put ILOG in d6 as a lword
26062306a36Sopenharmony_ci
26162306a36Sopenharmony_ci
26262306a36Sopenharmony_ci| A4. Clr INEX bit.
26362306a36Sopenharmony_ci|     The operation in A3 above may have set INEX2.
26462306a36Sopenharmony_ci
26562306a36Sopenharmony_ciA4_str:
26662306a36Sopenharmony_ci	fmovel	#0,%FPSR		|zero all of fpsr - nothing needed
26762306a36Sopenharmony_ci
26862306a36Sopenharmony_ci
26962306a36Sopenharmony_ci| A5. Set ICTR = 0;
27062306a36Sopenharmony_ci|     ICTR is a flag used in A13.  It must be set before the
27162306a36Sopenharmony_ci|     loop entry A6. The lower word of d5 is used for ICTR.
27262306a36Sopenharmony_ci
27362306a36Sopenharmony_ci	clrw	%d5		|clear ICTR
27462306a36Sopenharmony_ci
27562306a36Sopenharmony_ci
27662306a36Sopenharmony_ci| A6. Calculate LEN.
27762306a36Sopenharmony_ci|     LEN is the number of digits to be displayed.  The k-factor
27862306a36Sopenharmony_ci|     can dictate either the total number of digits, if it is
27962306a36Sopenharmony_ci|     a positive number, or the number of digits after the
28062306a36Sopenharmony_ci|     original decimal point which are to be included as
28162306a36Sopenharmony_ci|     significant.  See the 68882 manual for examples.
28262306a36Sopenharmony_ci|     If LEN is computed to be greater than 17, set OPERR in
28362306a36Sopenharmony_ci|     USER_FPSR.  LEN is stored in d4.
28462306a36Sopenharmony_ci|
28562306a36Sopenharmony_ci| Register usage:
28662306a36Sopenharmony_ci|	Input/Output
28762306a36Sopenharmony_ci|	d0: exponent/Unchanged
28862306a36Sopenharmony_ci|	d2: x/x/scratch
28962306a36Sopenharmony_ci|	d3: x/x
29062306a36Sopenharmony_ci|	d4: exc picture/LEN
29162306a36Sopenharmony_ci|	d5: ICTR/Unchanged
29262306a36Sopenharmony_ci|	d6: ILOG/Unchanged
29362306a36Sopenharmony_ci|	d7: k-factor/Unchanged
29462306a36Sopenharmony_ci|	a0: ptr for original operand/final result
29562306a36Sopenharmony_ci|	a1: x/x
29662306a36Sopenharmony_ci|	a2: x/x
29762306a36Sopenharmony_ci|	fp0: float(ILOG)/Unchanged
29862306a36Sopenharmony_ci|	fp1: x/x
29962306a36Sopenharmony_ci|	fp2: x/x
30062306a36Sopenharmony_ci|	F_SCR1:x/x
30162306a36Sopenharmony_ci|	F_SCR2:Abs(X) with $3fff exponent/Unchanged
30262306a36Sopenharmony_ci|	L_SCR1:x/x
30362306a36Sopenharmony_ci|	L_SCR2:first word of X packed/Unchanged
30462306a36Sopenharmony_ci
30562306a36Sopenharmony_ciA6_str:
30662306a36Sopenharmony_ci	tstl	%d7		|branch on sign of k
30762306a36Sopenharmony_ci	bles	k_neg		|if k <= 0, LEN = ILOG + 1 - k
30862306a36Sopenharmony_ci	movel	%d7,%d4		|if k > 0, LEN = k
30962306a36Sopenharmony_ci	bras	len_ck		|skip to LEN check
31062306a36Sopenharmony_cik_neg:
31162306a36Sopenharmony_ci	movel	%d6,%d4		|first load ILOG to d4
31262306a36Sopenharmony_ci	subl	%d7,%d4		|subtract off k
31362306a36Sopenharmony_ci	addql	#1,%d4		|add in the 1
31462306a36Sopenharmony_cilen_ck:
31562306a36Sopenharmony_ci	tstl	%d4		|LEN check: branch on sign of LEN
31662306a36Sopenharmony_ci	bles	LEN_ng		|if neg, set LEN = 1
31762306a36Sopenharmony_ci	cmpl	#17,%d4		|test if LEN > 17
31862306a36Sopenharmony_ci	bles	A7_str		|if not, forget it
31962306a36Sopenharmony_ci	movel	#17,%d4		|set max LEN = 17
32062306a36Sopenharmony_ci	tstl	%d7		|if negative, never set OPERR
32162306a36Sopenharmony_ci	bles	A7_str		|if positive, continue
32262306a36Sopenharmony_ci	orl	#opaop_mask,USER_FPSR(%a6) |set OPERR & AIOP in USER_FPSR
32362306a36Sopenharmony_ci	bras	A7_str		|finished here
32462306a36Sopenharmony_ciLEN_ng:
32562306a36Sopenharmony_ci	moveql	#1,%d4		|min LEN is 1
32662306a36Sopenharmony_ci
32762306a36Sopenharmony_ci
32862306a36Sopenharmony_ci| A7. Calculate SCALE.
32962306a36Sopenharmony_ci|     SCALE is equal to 10^ISCALE, where ISCALE is the number
33062306a36Sopenharmony_ci|     of decimal places needed to insure LEN integer digits
33162306a36Sopenharmony_ci|     in the output before conversion to bcd. LAMBDA is the sign
33262306a36Sopenharmony_ci|     of ISCALE, used in A9.  Fp1 contains 10^^(abs(ISCALE)) using
33362306a36Sopenharmony_ci|     the rounding mode as given in the following table (see
33462306a36Sopenharmony_ci|     Coonen, p. 7.23 as ref.; however, the SCALE variable is
33562306a36Sopenharmony_ci|     of opposite sign in bindec.sa from Coonen).
33662306a36Sopenharmony_ci|
33762306a36Sopenharmony_ci|	Initial					USE
33862306a36Sopenharmony_ci|	FPCR[6:5]	LAMBDA	SIGN(X)		FPCR[6:5]
33962306a36Sopenharmony_ci|	----------------------------------------------
34062306a36Sopenharmony_ci|	 RN	00	   0	   0		00/0	RN
34162306a36Sopenharmony_ci|	 RN	00	   0	   1		00/0	RN
34262306a36Sopenharmony_ci|	 RN	00	   1	   0		00/0	RN
34362306a36Sopenharmony_ci|	 RN	00	   1	   1		00/0	RN
34462306a36Sopenharmony_ci|	 RZ	01	   0	   0		11/3	RP
34562306a36Sopenharmony_ci|	 RZ	01	   0	   1		11/3	RP
34662306a36Sopenharmony_ci|	 RZ	01	   1	   0		10/2	RM
34762306a36Sopenharmony_ci|	 RZ	01	   1	   1		10/2	RM
34862306a36Sopenharmony_ci|	 RM	10	   0	   0		11/3	RP
34962306a36Sopenharmony_ci|	 RM	10	   0	   1		10/2	RM
35062306a36Sopenharmony_ci|	 RM	10	   1	   0		10/2	RM
35162306a36Sopenharmony_ci|	 RM	10	   1	   1		11/3	RP
35262306a36Sopenharmony_ci|	 RP	11	   0	   0		10/2	RM
35362306a36Sopenharmony_ci|	 RP	11	   0	   1		11/3	RP
35462306a36Sopenharmony_ci|	 RP	11	   1	   0		11/3	RP
35562306a36Sopenharmony_ci|	 RP	11	   1	   1		10/2	RM
35662306a36Sopenharmony_ci|
35762306a36Sopenharmony_ci| Register usage:
35862306a36Sopenharmony_ci|	Input/Output
35962306a36Sopenharmony_ci|	d0: exponent/scratch - final is 0
36062306a36Sopenharmony_ci|	d2: x/0 or 24 for A9
36162306a36Sopenharmony_ci|	d3: x/scratch - offset ptr into PTENRM array
36262306a36Sopenharmony_ci|	d4: LEN/Unchanged
36362306a36Sopenharmony_ci|	d5: 0/ICTR:LAMBDA
36462306a36Sopenharmony_ci|	d6: ILOG/ILOG or k if ((k<=0)&(ILOG<k))
36562306a36Sopenharmony_ci|	d7: k-factor/Unchanged
36662306a36Sopenharmony_ci|	a0: ptr for original operand/final result
36762306a36Sopenharmony_ci|	a1: x/ptr to PTENRM array
36862306a36Sopenharmony_ci|	a2: x/x
36962306a36Sopenharmony_ci|	fp0: float(ILOG)/Unchanged
37062306a36Sopenharmony_ci|	fp1: x/10^ISCALE
37162306a36Sopenharmony_ci|	fp2: x/x
37262306a36Sopenharmony_ci|	F_SCR1:x/x
37362306a36Sopenharmony_ci|	F_SCR2:Abs(X) with $3fff exponent/Unchanged
37462306a36Sopenharmony_ci|	L_SCR1:x/x
37562306a36Sopenharmony_ci|	L_SCR2:first word of X packed/Unchanged
37662306a36Sopenharmony_ci
37762306a36Sopenharmony_ciA7_str:
37862306a36Sopenharmony_ci	tstl	%d7		|test sign of k
37962306a36Sopenharmony_ci	bgts	k_pos		|if pos and > 0, skip this
38062306a36Sopenharmony_ci	cmpl	%d6,%d7		|test k - ILOG
38162306a36Sopenharmony_ci	blts	k_pos		|if ILOG >= k, skip this
38262306a36Sopenharmony_ci	movel	%d7,%d6		|if ((k<0) & (ILOG < k)) ILOG = k
38362306a36Sopenharmony_cik_pos:
38462306a36Sopenharmony_ci	movel	%d6,%d0		|calc ILOG + 1 - LEN in d0
38562306a36Sopenharmony_ci	addql	#1,%d0		|add the 1
38662306a36Sopenharmony_ci	subl	%d4,%d0		|sub off LEN
38762306a36Sopenharmony_ci	swap	%d5		|use upper word of d5 for LAMBDA
38862306a36Sopenharmony_ci	clrw	%d5		|set it zero initially
38962306a36Sopenharmony_ci	clrw	%d2		|set up d2 for very small case
39062306a36Sopenharmony_ci	tstl	%d0		|test sign of ISCALE
39162306a36Sopenharmony_ci	bges	iscale		|if pos, skip next inst
39262306a36Sopenharmony_ci	addqw	#1,%d5		|if neg, set LAMBDA true
39362306a36Sopenharmony_ci	cmpl	#0xffffecd4,%d0	|test iscale <= -4908
39462306a36Sopenharmony_ci	bgts	no_inf		|if false, skip rest
39562306a36Sopenharmony_ci	addil	#24,%d0		|add in 24 to iscale
39662306a36Sopenharmony_ci	movel	#24,%d2		|put 24 in d2 for A9
39762306a36Sopenharmony_cino_inf:
39862306a36Sopenharmony_ci	negl	%d0		|and take abs of ISCALE
39962306a36Sopenharmony_ciiscale:
40062306a36Sopenharmony_ci	fmoves	FONE,%fp1	|init fp1 to 1
40162306a36Sopenharmony_ci	bfextu	USER_FPCR(%a6){#26:#2},%d1 |get initial rmode bits
40262306a36Sopenharmony_ci	lslw	#1,%d1		|put them in bits 2:1
40362306a36Sopenharmony_ci	addw	%d5,%d1		|add in LAMBDA
40462306a36Sopenharmony_ci	lslw	#1,%d1		|put them in bits 3:1
40562306a36Sopenharmony_ci	tstl	L_SCR2(%a6)	|test sign of original x
40662306a36Sopenharmony_ci	bges	x_pos		|if pos, don't set bit 0
40762306a36Sopenharmony_ci	addql	#1,%d1		|if neg, set bit 0
40862306a36Sopenharmony_cix_pos:
40962306a36Sopenharmony_ci	leal	RBDTBL,%a2	|load rbdtbl base
41062306a36Sopenharmony_ci	moveb	(%a2,%d1),%d3	|load d3 with new rmode
41162306a36Sopenharmony_ci	lsll	#4,%d3		|put bits in proper position
41262306a36Sopenharmony_ci	fmovel	%d3,%fpcr		|load bits into fpu
41362306a36Sopenharmony_ci	lsrl	#4,%d3		|put bits in proper position
41462306a36Sopenharmony_ci	tstb	%d3		|decode new rmode for pten table
41562306a36Sopenharmony_ci	bnes	not_rn		|if zero, it is RN
41662306a36Sopenharmony_ci	leal	PTENRN,%a1	|load a1 with RN table base
41762306a36Sopenharmony_ci	bras	rmode		|exit decode
41862306a36Sopenharmony_cinot_rn:
41962306a36Sopenharmony_ci	lsrb	#1,%d3		|get lsb in carry
42062306a36Sopenharmony_ci	bccs	not_rp		|if carry clear, it is RM
42162306a36Sopenharmony_ci	leal	PTENRP,%a1	|load a1 with RP table base
42262306a36Sopenharmony_ci	bras	rmode		|exit decode
42362306a36Sopenharmony_cinot_rp:
42462306a36Sopenharmony_ci	leal	PTENRM,%a1	|load a1 with RM table base
42562306a36Sopenharmony_cirmode:
42662306a36Sopenharmony_ci	clrl	%d3		|clr table index
42762306a36Sopenharmony_cie_loop:
42862306a36Sopenharmony_ci	lsrl	#1,%d0		|shift next bit into carry
42962306a36Sopenharmony_ci	bccs	e_next		|if zero, skip the mul
43062306a36Sopenharmony_ci	fmulx	(%a1,%d3),%fp1	|mul by 10**(d3_bit_no)
43162306a36Sopenharmony_cie_next:
43262306a36Sopenharmony_ci	addl	#12,%d3		|inc d3 to next pwrten table entry
43362306a36Sopenharmony_ci	tstl	%d0		|test if ISCALE is zero
43462306a36Sopenharmony_ci	bnes	e_loop		|if not, loop
43562306a36Sopenharmony_ci
43662306a36Sopenharmony_ci
43762306a36Sopenharmony_ci| A8. Clr INEX; Force RZ.
43862306a36Sopenharmony_ci|     The operation in A3 above may have set INEX2.
43962306a36Sopenharmony_ci|     RZ mode is forced for the scaling operation to insure
44062306a36Sopenharmony_ci|     only one rounding error.  The grs bits are collected in
44162306a36Sopenharmony_ci|     the INEX flag for use in A10.
44262306a36Sopenharmony_ci|
44362306a36Sopenharmony_ci| Register usage:
44462306a36Sopenharmony_ci|	Input/Output
44562306a36Sopenharmony_ci
44662306a36Sopenharmony_ci	fmovel	#0,%FPSR		|clr INEX
44762306a36Sopenharmony_ci	fmovel	#rz_mode,%FPCR	|set RZ rounding mode
44862306a36Sopenharmony_ci
44962306a36Sopenharmony_ci
45062306a36Sopenharmony_ci| A9. Scale X -> Y.
45162306a36Sopenharmony_ci|     The mantissa is scaled to the desired number of significant
45262306a36Sopenharmony_ci|     digits.  The excess digits are collected in INEX2. If mul,
45362306a36Sopenharmony_ci|     Check d2 for excess 10 exponential value.  If not zero,
45462306a36Sopenharmony_ci|     the iscale value would have caused the pwrten calculation
45562306a36Sopenharmony_ci|     to overflow.  Only a negative iscale can cause this, so
45662306a36Sopenharmony_ci|     multiply by 10^(d2), which is now only allowed to be 24,
45762306a36Sopenharmony_ci|     with a multiply by 10^8 and 10^16, which is exact since
45862306a36Sopenharmony_ci|     10^24 is exact.  If the input was denormalized, we must
45962306a36Sopenharmony_ci|     create a busy stack frame with the mul command and the
46062306a36Sopenharmony_ci|     two operands, and allow the fpu to complete the multiply.
46162306a36Sopenharmony_ci|
46262306a36Sopenharmony_ci| Register usage:
46362306a36Sopenharmony_ci|	Input/Output
46462306a36Sopenharmony_ci|	d0: FPCR with RZ mode/Unchanged
46562306a36Sopenharmony_ci|	d2: 0 or 24/unchanged
46662306a36Sopenharmony_ci|	d3: x/x
46762306a36Sopenharmony_ci|	d4: LEN/Unchanged
46862306a36Sopenharmony_ci|	d5: ICTR:LAMBDA
46962306a36Sopenharmony_ci|	d6: ILOG/Unchanged
47062306a36Sopenharmony_ci|	d7: k-factor/Unchanged
47162306a36Sopenharmony_ci|	a0: ptr for original operand/final result
47262306a36Sopenharmony_ci|	a1: ptr to PTENRM array/Unchanged
47362306a36Sopenharmony_ci|	a2: x/x
47462306a36Sopenharmony_ci|	fp0: float(ILOG)/X adjusted for SCALE (Y)
47562306a36Sopenharmony_ci|	fp1: 10^ISCALE/Unchanged
47662306a36Sopenharmony_ci|	fp2: x/x
47762306a36Sopenharmony_ci|	F_SCR1:x/x
47862306a36Sopenharmony_ci|	F_SCR2:Abs(X) with $3fff exponent/Unchanged
47962306a36Sopenharmony_ci|	L_SCR1:x/x
48062306a36Sopenharmony_ci|	L_SCR2:first word of X packed/Unchanged
48162306a36Sopenharmony_ci
48262306a36Sopenharmony_ciA9_str:
48362306a36Sopenharmony_ci	fmovex	(%a0),%fp0	|load X from memory
48462306a36Sopenharmony_ci	fabsx	%fp0		|use abs(X)
48562306a36Sopenharmony_ci	tstw	%d5		|LAMBDA is in lower word of d5
48662306a36Sopenharmony_ci	bne	sc_mul		|if neg (LAMBDA = 1), scale by mul
48762306a36Sopenharmony_ci	fdivx	%fp1,%fp0		|calculate X / SCALE -> Y to fp0
48862306a36Sopenharmony_ci	bras	A10_st		|branch to A10
48962306a36Sopenharmony_ci
49062306a36Sopenharmony_cisc_mul:
49162306a36Sopenharmony_ci	tstb	BINDEC_FLG(%a6)	|check for denorm
49262306a36Sopenharmony_ci	beqs	A9_norm		|if norm, continue with mul
49362306a36Sopenharmony_ci	fmovemx %fp1-%fp1,-(%a7)	|load ETEMP with 10^ISCALE
49462306a36Sopenharmony_ci	movel	8(%a0),-(%a7)	|load FPTEMP with input arg
49562306a36Sopenharmony_ci	movel	4(%a0),-(%a7)
49662306a36Sopenharmony_ci	movel	(%a0),-(%a7)
49762306a36Sopenharmony_ci	movel	#18,%d3		|load count for busy stack
49862306a36Sopenharmony_ciA9_loop:
49962306a36Sopenharmony_ci	clrl	-(%a7)		|clear lword on stack
50062306a36Sopenharmony_ci	dbf	%d3,A9_loop
50162306a36Sopenharmony_ci	moveb	VER_TMP(%a6),(%a7) |write current version number
50262306a36Sopenharmony_ci	moveb	#BUSY_SIZE-4,1(%a7) |write current busy size
50362306a36Sopenharmony_ci	moveb	#0x10,0x44(%a7)	|set fcefpte[15] bit
50462306a36Sopenharmony_ci	movew	#0x0023,0x40(%a7)	|load cmdreg1b with mul command
50562306a36Sopenharmony_ci	moveb	#0xfe,0x8(%a7)	|load all 1s to cu savepc
50662306a36Sopenharmony_ci	frestore (%a7)+		|restore frame to fpu for completion
50762306a36Sopenharmony_ci	fmulx	36(%a1),%fp0	|multiply fp0 by 10^8
50862306a36Sopenharmony_ci	fmulx	48(%a1),%fp0	|multiply fp0 by 10^16
50962306a36Sopenharmony_ci	bras	A10_st
51062306a36Sopenharmony_ciA9_norm:
51162306a36Sopenharmony_ci	tstw	%d2		|test for small exp case
51262306a36Sopenharmony_ci	beqs	A9_con		|if zero, continue as normal
51362306a36Sopenharmony_ci	fmulx	36(%a1),%fp0	|multiply fp0 by 10^8
51462306a36Sopenharmony_ci	fmulx	48(%a1),%fp0	|multiply fp0 by 10^16
51562306a36Sopenharmony_ciA9_con:
51662306a36Sopenharmony_ci	fmulx	%fp1,%fp0		|calculate X * SCALE -> Y to fp0
51762306a36Sopenharmony_ci
51862306a36Sopenharmony_ci
51962306a36Sopenharmony_ci| A10. Or in INEX.
52062306a36Sopenharmony_ci|      If INEX is set, round error occurred.  This is compensated
52162306a36Sopenharmony_ci|      for by 'or-ing' in the INEX2 flag to the lsb of Y.
52262306a36Sopenharmony_ci|
52362306a36Sopenharmony_ci| Register usage:
52462306a36Sopenharmony_ci|	Input/Output
52562306a36Sopenharmony_ci|	d0: FPCR with RZ mode/FPSR with INEX2 isolated
52662306a36Sopenharmony_ci|	d2: x/x
52762306a36Sopenharmony_ci|	d3: x/x
52862306a36Sopenharmony_ci|	d4: LEN/Unchanged
52962306a36Sopenharmony_ci|	d5: ICTR:LAMBDA
53062306a36Sopenharmony_ci|	d6: ILOG/Unchanged
53162306a36Sopenharmony_ci|	d7: k-factor/Unchanged
53262306a36Sopenharmony_ci|	a0: ptr for original operand/final result
53362306a36Sopenharmony_ci|	a1: ptr to PTENxx array/Unchanged
53462306a36Sopenharmony_ci|	a2: x/ptr to FP_SCR2(a6)
53562306a36Sopenharmony_ci|	fp0: Y/Y with lsb adjusted
53662306a36Sopenharmony_ci|	fp1: 10^ISCALE/Unchanged
53762306a36Sopenharmony_ci|	fp2: x/x
53862306a36Sopenharmony_ci
53962306a36Sopenharmony_ciA10_st:
54062306a36Sopenharmony_ci	fmovel	%FPSR,%d0		|get FPSR
54162306a36Sopenharmony_ci	fmovex	%fp0,FP_SCR2(%a6)	|move Y to memory
54262306a36Sopenharmony_ci	leal	FP_SCR2(%a6),%a2	|load a2 with ptr to FP_SCR2
54362306a36Sopenharmony_ci	btstl	#9,%d0		|check if INEX2 set
54462306a36Sopenharmony_ci	beqs	A11_st		|if clear, skip rest
54562306a36Sopenharmony_ci	oril	#1,8(%a2)	|or in 1 to lsb of mantissa
54662306a36Sopenharmony_ci	fmovex	FP_SCR2(%a6),%fp0	|write adjusted Y back to fpu
54762306a36Sopenharmony_ci
54862306a36Sopenharmony_ci
54962306a36Sopenharmony_ci| A11. Restore original FPCR; set size ext.
55062306a36Sopenharmony_ci|      Perform FINT operation in the user's rounding mode.  Keep
55162306a36Sopenharmony_ci|      the size to extended.  The sintdo entry point in the sint
55262306a36Sopenharmony_ci|      routine expects the FPCR value to be in USER_FPCR for
55362306a36Sopenharmony_ci|      mode and precision.  The original FPCR is saved in L_SCR1.
55462306a36Sopenharmony_ci
55562306a36Sopenharmony_ciA11_st:
55662306a36Sopenharmony_ci	movel	USER_FPCR(%a6),L_SCR1(%a6) |save it for later
55762306a36Sopenharmony_ci	andil	#0x00000030,USER_FPCR(%a6) |set size to ext,
55862306a36Sopenharmony_ci|					;block exceptions
55962306a36Sopenharmony_ci
56062306a36Sopenharmony_ci
56162306a36Sopenharmony_ci| A12. Calculate YINT = FINT(Y) according to user's rounding mode.
56262306a36Sopenharmony_ci|      The FPSP routine sintd0 is used.  The output is in fp0.
56362306a36Sopenharmony_ci|
56462306a36Sopenharmony_ci| Register usage:
56562306a36Sopenharmony_ci|	Input/Output
56662306a36Sopenharmony_ci|	d0: FPSR with AINEX cleared/FPCR with size set to ext
56762306a36Sopenharmony_ci|	d2: x/x/scratch
56862306a36Sopenharmony_ci|	d3: x/x
56962306a36Sopenharmony_ci|	d4: LEN/Unchanged
57062306a36Sopenharmony_ci|	d5: ICTR:LAMBDA/Unchanged
57162306a36Sopenharmony_ci|	d6: ILOG/Unchanged
57262306a36Sopenharmony_ci|	d7: k-factor/Unchanged
57362306a36Sopenharmony_ci|	a0: ptr for original operand/src ptr for sintdo
57462306a36Sopenharmony_ci|	a1: ptr to PTENxx array/Unchanged
57562306a36Sopenharmony_ci|	a2: ptr to FP_SCR2(a6)/Unchanged
57662306a36Sopenharmony_ci|	a6: temp pointer to FP_SCR2(a6) - orig value saved and restored
57762306a36Sopenharmony_ci|	fp0: Y/YINT
57862306a36Sopenharmony_ci|	fp1: 10^ISCALE/Unchanged
57962306a36Sopenharmony_ci|	fp2: x/x
58062306a36Sopenharmony_ci|	F_SCR1:x/x
58162306a36Sopenharmony_ci|	F_SCR2:Y adjusted for inex/Y with original exponent
58262306a36Sopenharmony_ci|	L_SCR1:x/original USER_FPCR
58362306a36Sopenharmony_ci|	L_SCR2:first word of X packed/Unchanged
58462306a36Sopenharmony_ci
58562306a36Sopenharmony_ciA12_st:
58662306a36Sopenharmony_ci	moveml	%d0-%d1/%a0-%a1,-(%a7)	|save regs used by sintd0
58762306a36Sopenharmony_ci	movel	L_SCR1(%a6),-(%a7)
58862306a36Sopenharmony_ci	movel	L_SCR2(%a6),-(%a7)
58962306a36Sopenharmony_ci	leal	FP_SCR2(%a6),%a0		|a0 is ptr to F_SCR2(a6)
59062306a36Sopenharmony_ci	fmovex	%fp0,(%a0)		|move Y to memory at FP_SCR2(a6)
59162306a36Sopenharmony_ci	tstl	L_SCR2(%a6)		|test sign of original operand
59262306a36Sopenharmony_ci	bges	do_fint			|if pos, use Y
59362306a36Sopenharmony_ci	orl	#0x80000000,(%a0)		|if neg, use -Y
59462306a36Sopenharmony_cido_fint:
59562306a36Sopenharmony_ci	movel	USER_FPSR(%a6),-(%a7)
59662306a36Sopenharmony_ci	bsr	sintdo			|sint routine returns int in fp0
59762306a36Sopenharmony_ci	moveb	(%a7),USER_FPSR(%a6)
59862306a36Sopenharmony_ci	addl	#4,%a7
59962306a36Sopenharmony_ci	movel	(%a7)+,L_SCR2(%a6)
60062306a36Sopenharmony_ci	movel	(%a7)+,L_SCR1(%a6)
60162306a36Sopenharmony_ci	moveml	(%a7)+,%d0-%d1/%a0-%a1	|restore regs used by sint
60262306a36Sopenharmony_ci	movel	L_SCR2(%a6),FP_SCR2(%a6)	|restore original exponent
60362306a36Sopenharmony_ci	movel	L_SCR1(%a6),USER_FPCR(%a6) |restore user's FPCR
60462306a36Sopenharmony_ci
60562306a36Sopenharmony_ci
60662306a36Sopenharmony_ci| A13. Check for LEN digits.
60762306a36Sopenharmony_ci|      If the int operation results in more than LEN digits,
60862306a36Sopenharmony_ci|      or less than LEN -1 digits, adjust ILOG and repeat from
60962306a36Sopenharmony_ci|      A6.  This test occurs only on the first pass.  If the
61062306a36Sopenharmony_ci|      result is exactly 10^LEN, decrement ILOG and divide
61162306a36Sopenharmony_ci|      the mantissa by 10.  The calculation of 10^LEN cannot
61262306a36Sopenharmony_ci|      be inexact, since all powers of ten up to 10^27 are exact
61362306a36Sopenharmony_ci|      in extended precision, so the use of a previous power-of-ten
61462306a36Sopenharmony_ci|      table will introduce no error.
61562306a36Sopenharmony_ci|
61662306a36Sopenharmony_ci|
61762306a36Sopenharmony_ci| Register usage:
61862306a36Sopenharmony_ci|	Input/Output
61962306a36Sopenharmony_ci|	d0: FPCR with size set to ext/scratch final = 0
62062306a36Sopenharmony_ci|	d2: x/x
62162306a36Sopenharmony_ci|	d3: x/scratch final = x
62262306a36Sopenharmony_ci|	d4: LEN/LEN adjusted
62362306a36Sopenharmony_ci|	d5: ICTR:LAMBDA/LAMBDA:ICTR
62462306a36Sopenharmony_ci|	d6: ILOG/ILOG adjusted
62562306a36Sopenharmony_ci|	d7: k-factor/Unchanged
62662306a36Sopenharmony_ci|	a0: pointer into memory for packed bcd string formation
62762306a36Sopenharmony_ci|	a1: ptr to PTENxx array/Unchanged
62862306a36Sopenharmony_ci|	a2: ptr to FP_SCR2(a6)/Unchanged
62962306a36Sopenharmony_ci|	fp0: int portion of Y/abs(YINT) adjusted
63062306a36Sopenharmony_ci|	fp1: 10^ISCALE/Unchanged
63162306a36Sopenharmony_ci|	fp2: x/10^LEN
63262306a36Sopenharmony_ci|	F_SCR1:x/x
63362306a36Sopenharmony_ci|	F_SCR2:Y with original exponent/Unchanged
63462306a36Sopenharmony_ci|	L_SCR1:original USER_FPCR/Unchanged
63562306a36Sopenharmony_ci|	L_SCR2:first word of X packed/Unchanged
63662306a36Sopenharmony_ci
63762306a36Sopenharmony_ciA13_st:
63862306a36Sopenharmony_ci	swap	%d5		|put ICTR in lower word of d5
63962306a36Sopenharmony_ci	tstw	%d5		|check if ICTR = 0
64062306a36Sopenharmony_ci	bne	not_zr		|if non-zero, go to second test
64162306a36Sopenharmony_ci|
64262306a36Sopenharmony_ci| Compute 10^(LEN-1)
64362306a36Sopenharmony_ci|
64462306a36Sopenharmony_ci	fmoves	FONE,%fp2	|init fp2 to 1.0
64562306a36Sopenharmony_ci	movel	%d4,%d0		|put LEN in d0
64662306a36Sopenharmony_ci	subql	#1,%d0		|d0 = LEN -1
64762306a36Sopenharmony_ci	clrl	%d3		|clr table index
64862306a36Sopenharmony_cil_loop:
64962306a36Sopenharmony_ci	lsrl	#1,%d0		|shift next bit into carry
65062306a36Sopenharmony_ci	bccs	l_next		|if zero, skip the mul
65162306a36Sopenharmony_ci	fmulx	(%a1,%d3),%fp2	|mul by 10**(d3_bit_no)
65262306a36Sopenharmony_cil_next:
65362306a36Sopenharmony_ci	addl	#12,%d3		|inc d3 to next pwrten table entry
65462306a36Sopenharmony_ci	tstl	%d0		|test if LEN is zero
65562306a36Sopenharmony_ci	bnes	l_loop		|if not, loop
65662306a36Sopenharmony_ci|
65762306a36Sopenharmony_ci| 10^LEN-1 is computed for this test and A14.  If the input was
65862306a36Sopenharmony_ci| denormalized, check only the case in which YINT > 10^LEN.
65962306a36Sopenharmony_ci|
66062306a36Sopenharmony_ci	tstb	BINDEC_FLG(%a6)	|check if input was norm
66162306a36Sopenharmony_ci	beqs	A13_con		|if norm, continue with checking
66262306a36Sopenharmony_ci	fabsx	%fp0		|take abs of YINT
66362306a36Sopenharmony_ci	bra	test_2
66462306a36Sopenharmony_ci|
66562306a36Sopenharmony_ci| Compare abs(YINT) to 10^(LEN-1) and 10^LEN
66662306a36Sopenharmony_ci|
66762306a36Sopenharmony_ciA13_con:
66862306a36Sopenharmony_ci	fabsx	%fp0		|take abs of YINT
66962306a36Sopenharmony_ci	fcmpx	%fp2,%fp0		|compare abs(YINT) with 10^(LEN-1)
67062306a36Sopenharmony_ci	fbge	test_2		|if greater, do next test
67162306a36Sopenharmony_ci	subql	#1,%d6		|subtract 1 from ILOG
67262306a36Sopenharmony_ci	movew	#1,%d5		|set ICTR
67362306a36Sopenharmony_ci	fmovel	#rm_mode,%FPCR	|set rmode to RM
67462306a36Sopenharmony_ci	fmuls	FTEN,%fp2	|compute 10^LEN
67562306a36Sopenharmony_ci	bra	A6_str		|return to A6 and recompute YINT
67662306a36Sopenharmony_citest_2:
67762306a36Sopenharmony_ci	fmuls	FTEN,%fp2	|compute 10^LEN
67862306a36Sopenharmony_ci	fcmpx	%fp2,%fp0		|compare abs(YINT) with 10^LEN
67962306a36Sopenharmony_ci	fblt	A14_st		|if less, all is ok, go to A14
68062306a36Sopenharmony_ci	fbgt	fix_ex		|if greater, fix and redo
68162306a36Sopenharmony_ci	fdivs	FTEN,%fp0	|if equal, divide by 10
68262306a36Sopenharmony_ci	addql	#1,%d6		| and inc ILOG
68362306a36Sopenharmony_ci	bras	A14_st		| and continue elsewhere
68462306a36Sopenharmony_cifix_ex:
68562306a36Sopenharmony_ci	addql	#1,%d6		|increment ILOG by 1
68662306a36Sopenharmony_ci	movew	#1,%d5		|set ICTR
68762306a36Sopenharmony_ci	fmovel	#rm_mode,%FPCR	|set rmode to RM
68862306a36Sopenharmony_ci	bra	A6_str		|return to A6 and recompute YINT
68962306a36Sopenharmony_ci|
69062306a36Sopenharmony_ci| Since ICTR <> 0, we have already been through one adjustment,
69162306a36Sopenharmony_ci| and shouldn't have another; this is to check if abs(YINT) = 10^LEN
69262306a36Sopenharmony_ci| 10^LEN is again computed using whatever table is in a1 since the
69362306a36Sopenharmony_ci| value calculated cannot be inexact.
69462306a36Sopenharmony_ci|
69562306a36Sopenharmony_cinot_zr:
69662306a36Sopenharmony_ci	fmoves	FONE,%fp2	|init fp2 to 1.0
69762306a36Sopenharmony_ci	movel	%d4,%d0		|put LEN in d0
69862306a36Sopenharmony_ci	clrl	%d3		|clr table index
69962306a36Sopenharmony_ciz_loop:
70062306a36Sopenharmony_ci	lsrl	#1,%d0		|shift next bit into carry
70162306a36Sopenharmony_ci	bccs	z_next		|if zero, skip the mul
70262306a36Sopenharmony_ci	fmulx	(%a1,%d3),%fp2	|mul by 10**(d3_bit_no)
70362306a36Sopenharmony_ciz_next:
70462306a36Sopenharmony_ci	addl	#12,%d3		|inc d3 to next pwrten table entry
70562306a36Sopenharmony_ci	tstl	%d0		|test if LEN is zero
70662306a36Sopenharmony_ci	bnes	z_loop		|if not, loop
70762306a36Sopenharmony_ci	fabsx	%fp0		|get abs(YINT)
70862306a36Sopenharmony_ci	fcmpx	%fp2,%fp0		|check if abs(YINT) = 10^LEN
70962306a36Sopenharmony_ci	fbne	A14_st		|if not, skip this
71062306a36Sopenharmony_ci	fdivs	FTEN,%fp0	|divide abs(YINT) by 10
71162306a36Sopenharmony_ci	addql	#1,%d6		|and inc ILOG by 1
71262306a36Sopenharmony_ci	addql	#1,%d4		| and inc LEN
71362306a36Sopenharmony_ci	fmuls	FTEN,%fp2	| if LEN++, the get 10^^LEN
71462306a36Sopenharmony_ci
71562306a36Sopenharmony_ci
71662306a36Sopenharmony_ci| A14. Convert the mantissa to bcd.
71762306a36Sopenharmony_ci|      The binstr routine is used to convert the LEN digit
71862306a36Sopenharmony_ci|      mantissa to bcd in memory.  The input to binstr is
71962306a36Sopenharmony_ci|      to be a fraction; i.e. (mantissa)/10^LEN and adjusted
72062306a36Sopenharmony_ci|      such that the decimal point is to the left of bit 63.
72162306a36Sopenharmony_ci|      The bcd digits are stored in the correct position in
72262306a36Sopenharmony_ci|      the final string area in memory.
72362306a36Sopenharmony_ci|
72462306a36Sopenharmony_ci|
72562306a36Sopenharmony_ci| Register usage:
72662306a36Sopenharmony_ci|	Input/Output
72762306a36Sopenharmony_ci|	d0: x/LEN call to binstr - final is 0
72862306a36Sopenharmony_ci|	d1: x/0
72962306a36Sopenharmony_ci|	d2: x/ms 32-bits of mant of abs(YINT)
73062306a36Sopenharmony_ci|	d3: x/ls 32-bits of mant of abs(YINT)
73162306a36Sopenharmony_ci|	d4: LEN/Unchanged
73262306a36Sopenharmony_ci|	d5: ICTR:LAMBDA/LAMBDA:ICTR
73362306a36Sopenharmony_ci|	d6: ILOG
73462306a36Sopenharmony_ci|	d7: k-factor/Unchanged
73562306a36Sopenharmony_ci|	a0: pointer into memory for packed bcd string formation
73662306a36Sopenharmony_ci|	    /ptr to first mantissa byte in result string
73762306a36Sopenharmony_ci|	a1: ptr to PTENxx array/Unchanged
73862306a36Sopenharmony_ci|	a2: ptr to FP_SCR2(a6)/Unchanged
73962306a36Sopenharmony_ci|	fp0: int portion of Y/abs(YINT) adjusted
74062306a36Sopenharmony_ci|	fp1: 10^ISCALE/Unchanged
74162306a36Sopenharmony_ci|	fp2: 10^LEN/Unchanged
74262306a36Sopenharmony_ci|	F_SCR1:x/Work area for final result
74362306a36Sopenharmony_ci|	F_SCR2:Y with original exponent/Unchanged
74462306a36Sopenharmony_ci|	L_SCR1:original USER_FPCR/Unchanged
74562306a36Sopenharmony_ci|	L_SCR2:first word of X packed/Unchanged
74662306a36Sopenharmony_ci
74762306a36Sopenharmony_ciA14_st:
74862306a36Sopenharmony_ci	fmovel	#rz_mode,%FPCR	|force rz for conversion
74962306a36Sopenharmony_ci	fdivx	%fp2,%fp0		|divide abs(YINT) by 10^LEN
75062306a36Sopenharmony_ci	leal	FP_SCR1(%a6),%a0
75162306a36Sopenharmony_ci	fmovex	%fp0,(%a0)	|move abs(YINT)/10^LEN to memory
75262306a36Sopenharmony_ci	movel	4(%a0),%d2	|move 2nd word of FP_RES to d2
75362306a36Sopenharmony_ci	movel	8(%a0),%d3	|move 3rd word of FP_RES to d3
75462306a36Sopenharmony_ci	clrl	4(%a0)		|zero word 2 of FP_RES
75562306a36Sopenharmony_ci	clrl	8(%a0)		|zero word 3 of FP_RES
75662306a36Sopenharmony_ci	movel	(%a0),%d0		|move exponent to d0
75762306a36Sopenharmony_ci	swap	%d0		|put exponent in lower word
75862306a36Sopenharmony_ci	beqs	no_sft		|if zero, don't shift
75962306a36Sopenharmony_ci	subil	#0x3ffd,%d0	|sub bias less 2 to make fract
76062306a36Sopenharmony_ci	tstl	%d0		|check if > 1
76162306a36Sopenharmony_ci	bgts	no_sft		|if so, don't shift
76262306a36Sopenharmony_ci	negl	%d0		|make exp positive
76362306a36Sopenharmony_cim_loop:
76462306a36Sopenharmony_ci	lsrl	#1,%d2		|shift d2:d3 right, add 0s
76562306a36Sopenharmony_ci	roxrl	#1,%d3		|the number of places
76662306a36Sopenharmony_ci	dbf	%d0,m_loop	|given in d0
76762306a36Sopenharmony_cino_sft:
76862306a36Sopenharmony_ci	tstl	%d2		|check for mantissa of zero
76962306a36Sopenharmony_ci	bnes	no_zr		|if not, go on
77062306a36Sopenharmony_ci	tstl	%d3		|continue zero check
77162306a36Sopenharmony_ci	beqs	zer_m		|if zero, go directly to binstr
77262306a36Sopenharmony_cino_zr:
77362306a36Sopenharmony_ci	clrl	%d1		|put zero in d1 for addx
77462306a36Sopenharmony_ci	addil	#0x00000080,%d3	|inc at bit 7
77562306a36Sopenharmony_ci	addxl	%d1,%d2		|continue inc
77662306a36Sopenharmony_ci	andil	#0xffffff80,%d3	|strip off lsb not used by 882
77762306a36Sopenharmony_cizer_m:
77862306a36Sopenharmony_ci	movel	%d4,%d0		|put LEN in d0 for binstr call
77962306a36Sopenharmony_ci	addql	#3,%a0		|a0 points to M16 byte in result
78062306a36Sopenharmony_ci	bsr	binstr		|call binstr to convert mant
78162306a36Sopenharmony_ci
78262306a36Sopenharmony_ci
78362306a36Sopenharmony_ci| A15. Convert the exponent to bcd.
78462306a36Sopenharmony_ci|      As in A14 above, the exp is converted to bcd and the
78562306a36Sopenharmony_ci|      digits are stored in the final string.
78662306a36Sopenharmony_ci|
78762306a36Sopenharmony_ci|      Digits are stored in L_SCR1(a6) on return from BINDEC as:
78862306a36Sopenharmony_ci|
78962306a36Sopenharmony_ci|	 32               16 15                0
79062306a36Sopenharmony_ci|	-----------------------------------------
79162306a36Sopenharmony_ci|	|  0 | e3 | e2 | e1 | e4 |  X |  X |  X |
79262306a36Sopenharmony_ci|	-----------------------------------------
79362306a36Sopenharmony_ci|
79462306a36Sopenharmony_ci| And are moved into their proper places in FP_SCR1.  If digit e4
79562306a36Sopenharmony_ci| is non-zero, OPERR is signaled.  In all cases, all 4 digits are
79662306a36Sopenharmony_ci| written as specified in the 881/882 manual for packed decimal.
79762306a36Sopenharmony_ci|
79862306a36Sopenharmony_ci| Register usage:
79962306a36Sopenharmony_ci|	Input/Output
80062306a36Sopenharmony_ci|	d0: x/LEN call to binstr - final is 0
80162306a36Sopenharmony_ci|	d1: x/scratch (0);shift count for final exponent packing
80262306a36Sopenharmony_ci|	d2: x/ms 32-bits of exp fraction/scratch
80362306a36Sopenharmony_ci|	d3: x/ls 32-bits of exp fraction
80462306a36Sopenharmony_ci|	d4: LEN/Unchanged
80562306a36Sopenharmony_ci|	d5: ICTR:LAMBDA/LAMBDA:ICTR
80662306a36Sopenharmony_ci|	d6: ILOG
80762306a36Sopenharmony_ci|	d7: k-factor/Unchanged
80862306a36Sopenharmony_ci|	a0: ptr to result string/ptr to L_SCR1(a6)
80962306a36Sopenharmony_ci|	a1: ptr to PTENxx array/Unchanged
81062306a36Sopenharmony_ci|	a2: ptr to FP_SCR2(a6)/Unchanged
81162306a36Sopenharmony_ci|	fp0: abs(YINT) adjusted/float(ILOG)
81262306a36Sopenharmony_ci|	fp1: 10^ISCALE/Unchanged
81362306a36Sopenharmony_ci|	fp2: 10^LEN/Unchanged
81462306a36Sopenharmony_ci|	F_SCR1:Work area for final result/BCD result
81562306a36Sopenharmony_ci|	F_SCR2:Y with original exponent/ILOG/10^4
81662306a36Sopenharmony_ci|	L_SCR1:original USER_FPCR/Exponent digits on return from binstr
81762306a36Sopenharmony_ci|	L_SCR2:first word of X packed/Unchanged
81862306a36Sopenharmony_ci
81962306a36Sopenharmony_ciA15_st:
82062306a36Sopenharmony_ci	tstb	BINDEC_FLG(%a6)	|check for denorm
82162306a36Sopenharmony_ci	beqs	not_denorm
82262306a36Sopenharmony_ci	ftstx	%fp0		|test for zero
82362306a36Sopenharmony_ci	fbeq	den_zero	|if zero, use k-factor or 4933
82462306a36Sopenharmony_ci	fmovel	%d6,%fp0		|float ILOG
82562306a36Sopenharmony_ci	fabsx	%fp0		|get abs of ILOG
82662306a36Sopenharmony_ci	bras	convrt
82762306a36Sopenharmony_ciden_zero:
82862306a36Sopenharmony_ci	tstl	%d7		|check sign of the k-factor
82962306a36Sopenharmony_ci	blts	use_ilog	|if negative, use ILOG
83062306a36Sopenharmony_ci	fmoves	F4933,%fp0	|force exponent to 4933
83162306a36Sopenharmony_ci	bras	convrt		|do it
83262306a36Sopenharmony_ciuse_ilog:
83362306a36Sopenharmony_ci	fmovel	%d6,%fp0		|float ILOG
83462306a36Sopenharmony_ci	fabsx	%fp0		|get abs of ILOG
83562306a36Sopenharmony_ci	bras	convrt
83662306a36Sopenharmony_cinot_denorm:
83762306a36Sopenharmony_ci	ftstx	%fp0		|test for zero
83862306a36Sopenharmony_ci	fbne	not_zero	|if zero, force exponent
83962306a36Sopenharmony_ci	fmoves	FONE,%fp0	|force exponent to 1
84062306a36Sopenharmony_ci	bras	convrt		|do it
84162306a36Sopenharmony_cinot_zero:
84262306a36Sopenharmony_ci	fmovel	%d6,%fp0		|float ILOG
84362306a36Sopenharmony_ci	fabsx	%fp0		|get abs of ILOG
84462306a36Sopenharmony_ciconvrt:
84562306a36Sopenharmony_ci	fdivx	24(%a1),%fp0	|compute ILOG/10^4
84662306a36Sopenharmony_ci	fmovex	%fp0,FP_SCR2(%a6)	|store fp0 in memory
84762306a36Sopenharmony_ci	movel	4(%a2),%d2	|move word 2 to d2
84862306a36Sopenharmony_ci	movel	8(%a2),%d3	|move word 3 to d3
84962306a36Sopenharmony_ci	movew	(%a2),%d0		|move exp to d0
85062306a36Sopenharmony_ci	beqs	x_loop_fin	|if zero, skip the shift
85162306a36Sopenharmony_ci	subiw	#0x3ffd,%d0	|subtract off bias
85262306a36Sopenharmony_ci	negw	%d0		|make exp positive
85362306a36Sopenharmony_cix_loop:
85462306a36Sopenharmony_ci	lsrl	#1,%d2		|shift d2:d3 right
85562306a36Sopenharmony_ci	roxrl	#1,%d3		|the number of places
85662306a36Sopenharmony_ci	dbf	%d0,x_loop	|given in d0
85762306a36Sopenharmony_cix_loop_fin:
85862306a36Sopenharmony_ci	clrl	%d1		|put zero in d1 for addx
85962306a36Sopenharmony_ci	addil	#0x00000080,%d3	|inc at bit 6
86062306a36Sopenharmony_ci	addxl	%d1,%d2		|continue inc
86162306a36Sopenharmony_ci	andil	#0xffffff80,%d3	|strip off lsb not used by 882
86262306a36Sopenharmony_ci	movel	#4,%d0		|put 4 in d0 for binstr call
86362306a36Sopenharmony_ci	leal	L_SCR1(%a6),%a0	|a0 is ptr to L_SCR1 for exp digits
86462306a36Sopenharmony_ci	bsr	binstr		|call binstr to convert exp
86562306a36Sopenharmony_ci	movel	L_SCR1(%a6),%d0	|load L_SCR1 lword to d0
86662306a36Sopenharmony_ci	movel	#12,%d1		|use d1 for shift count
86762306a36Sopenharmony_ci	lsrl	%d1,%d0		|shift d0 right by 12
86862306a36Sopenharmony_ci	bfins	%d0,FP_SCR1(%a6){#4:#12} |put e3:e2:e1 in FP_SCR1
86962306a36Sopenharmony_ci	lsrl	%d1,%d0		|shift d0 right by 12
87062306a36Sopenharmony_ci	bfins	%d0,FP_SCR1(%a6){#16:#4} |put e4 in FP_SCR1
87162306a36Sopenharmony_ci	tstb	%d0		|check if e4 is zero
87262306a36Sopenharmony_ci	beqs	A16_st		|if zero, skip rest
87362306a36Sopenharmony_ci	orl	#opaop_mask,USER_FPSR(%a6) |set OPERR & AIOP in USER_FPSR
87462306a36Sopenharmony_ci
87562306a36Sopenharmony_ci
87662306a36Sopenharmony_ci| A16. Write sign bits to final string.
87762306a36Sopenharmony_ci|	   Sigma is bit 31 of initial value; RHO is bit 31 of d6 (ILOG).
87862306a36Sopenharmony_ci|
87962306a36Sopenharmony_ci| Register usage:
88062306a36Sopenharmony_ci|	Input/Output
88162306a36Sopenharmony_ci|	d0: x/scratch - final is x
88262306a36Sopenharmony_ci|	d2: x/x
88362306a36Sopenharmony_ci|	d3: x/x
88462306a36Sopenharmony_ci|	d4: LEN/Unchanged
88562306a36Sopenharmony_ci|	d5: ICTR:LAMBDA/LAMBDA:ICTR
88662306a36Sopenharmony_ci|	d6: ILOG/ILOG adjusted
88762306a36Sopenharmony_ci|	d7: k-factor/Unchanged
88862306a36Sopenharmony_ci|	a0: ptr to L_SCR1(a6)/Unchanged
88962306a36Sopenharmony_ci|	a1: ptr to PTENxx array/Unchanged
89062306a36Sopenharmony_ci|	a2: ptr to FP_SCR2(a6)/Unchanged
89162306a36Sopenharmony_ci|	fp0: float(ILOG)/Unchanged
89262306a36Sopenharmony_ci|	fp1: 10^ISCALE/Unchanged
89362306a36Sopenharmony_ci|	fp2: 10^LEN/Unchanged
89462306a36Sopenharmony_ci|	F_SCR1:BCD result with correct signs
89562306a36Sopenharmony_ci|	F_SCR2:ILOG/10^4
89662306a36Sopenharmony_ci|	L_SCR1:Exponent digits on return from binstr
89762306a36Sopenharmony_ci|	L_SCR2:first word of X packed/Unchanged
89862306a36Sopenharmony_ci
89962306a36Sopenharmony_ciA16_st:
90062306a36Sopenharmony_ci	clrl	%d0		|clr d0 for collection of signs
90162306a36Sopenharmony_ci	andib	#0x0f,FP_SCR1(%a6) |clear first nibble of FP_SCR1
90262306a36Sopenharmony_ci	tstl	L_SCR2(%a6)	|check sign of original mantissa
90362306a36Sopenharmony_ci	bges	mant_p		|if pos, don't set SM
90462306a36Sopenharmony_ci	moveql	#2,%d0		|move 2 in to d0 for SM
90562306a36Sopenharmony_cimant_p:
90662306a36Sopenharmony_ci	tstl	%d6		|check sign of ILOG
90762306a36Sopenharmony_ci	bges	wr_sgn		|if pos, don't set SE
90862306a36Sopenharmony_ci	addql	#1,%d0		|set bit 0 in d0 for SE
90962306a36Sopenharmony_ciwr_sgn:
91062306a36Sopenharmony_ci	bfins	%d0,FP_SCR1(%a6){#0:#2} |insert SM and SE into FP_SCR1
91162306a36Sopenharmony_ci
91262306a36Sopenharmony_ci| Clean up and restore all registers used.
91362306a36Sopenharmony_ci
91462306a36Sopenharmony_ci	fmovel	#0,%FPSR		|clear possible inex2/ainex bits
91562306a36Sopenharmony_ci	fmovemx (%a7)+,%fp0-%fp2
91662306a36Sopenharmony_ci	moveml	(%a7)+,%d2-%d7/%a2
91762306a36Sopenharmony_ci	rts
91862306a36Sopenharmony_ci
91962306a36Sopenharmony_ci	|end
920