xref: /openbmc/linux/arch/m68k/fpsp040/bindec.S (revision e5451c8f8330e03ad3cfa16048b4daf961af434f)
11da177e4SLinus Torvalds|
21da177e4SLinus Torvalds|	bindec.sa 3.4 1/3/91
31da177e4SLinus Torvalds|
41da177e4SLinus Torvalds|	bindec
51da177e4SLinus Torvalds|
61da177e4SLinus Torvalds|	Description:
71da177e4SLinus Torvalds|		Converts an input in extended precision format
81da177e4SLinus Torvalds|		to bcd format.
91da177e4SLinus Torvalds|
101da177e4SLinus Torvalds|	Input:
111da177e4SLinus Torvalds|		a0 points to the input extended precision value
121da177e4SLinus Torvalds|		value in memory; d0 contains the k-factor sign-extended
131da177e4SLinus Torvalds|		to 32-bits.  The input may be either normalized,
141da177e4SLinus Torvalds|		unnormalized, or denormalized.
151da177e4SLinus Torvalds|
161da177e4SLinus Torvalds|	Output:	result in the FP_SCR1 space on the stack.
171da177e4SLinus Torvalds|
181da177e4SLinus Torvalds|	Saves and Modifies: D2-D7,A2,FP2
191da177e4SLinus Torvalds|
201da177e4SLinus Torvalds|	Algorithm:
211da177e4SLinus Torvalds|
221da177e4SLinus Torvalds|	A1.	Set RM and size ext;  Set SIGMA = sign of input.
231da177e4SLinus Torvalds|		The k-factor is saved for use in d7. Clear the
241da177e4SLinus Torvalds|		BINDEC_FLG for separating normalized/denormalized
251da177e4SLinus Torvalds|		input.  If input is unnormalized or denormalized,
261da177e4SLinus Torvalds|		normalize it.
271da177e4SLinus Torvalds|
281da177e4SLinus Torvalds|	A2.	Set X = abs(input).
291da177e4SLinus Torvalds|
301da177e4SLinus Torvalds|	A3.	Compute ILOG.
311da177e4SLinus Torvalds|		ILOG is the log base 10 of the input value.  It is
321da177e4SLinus Torvalds|		approximated by adding e + 0.f when the original
331da177e4SLinus Torvalds|		value is viewed as 2^^e * 1.f in extended precision.
341da177e4SLinus Torvalds|		This value is stored in d6.
351da177e4SLinus Torvalds|
361da177e4SLinus Torvalds|	A4.	Clr INEX bit.
371da177e4SLinus Torvalds|		The operation in A3 above may have set INEX2.
381da177e4SLinus Torvalds|
391da177e4SLinus Torvalds|	A5.	Set ICTR = 0;
401da177e4SLinus Torvalds|		ICTR is a flag used in A13.  It must be set before the
411da177e4SLinus Torvalds|		loop entry A6.
421da177e4SLinus Torvalds|
431da177e4SLinus Torvalds|	A6.	Calculate LEN.
441da177e4SLinus Torvalds|		LEN is the number of digits to be displayed.  The
451da177e4SLinus Torvalds|		k-factor can dictate either the total number of digits,
461da177e4SLinus Torvalds|		if it is a positive number, or the number of digits
471da177e4SLinus Torvalds|		after the decimal point which are to be included as
481da177e4SLinus Torvalds|		significant.  See the 68882 manual for examples.
491da177e4SLinus Torvalds|		If LEN is computed to be greater than 17, set OPERR in
501da177e4SLinus Torvalds|		USER_FPSR.  LEN is stored in d4.
511da177e4SLinus Torvalds|
521da177e4SLinus Torvalds|	A7.	Calculate SCALE.
531da177e4SLinus Torvalds|		SCALE is equal to 10^ISCALE, where ISCALE is the number
541da177e4SLinus Torvalds|		of decimal places needed to insure LEN integer digits
551da177e4SLinus Torvalds|		in the output before conversion to bcd. LAMBDA is the
561da177e4SLinus Torvalds|		sign of ISCALE, used in A9. Fp1 contains
571da177e4SLinus Torvalds|		10^^(abs(ISCALE)) using a rounding mode which is a
581da177e4SLinus Torvalds|		function of the original rounding mode and the signs
591da177e4SLinus Torvalds|		of ISCALE and X.  A table is given in the code.
601da177e4SLinus Torvalds|
611da177e4SLinus Torvalds|	A8.	Clr INEX; Force RZ.
621da177e4SLinus Torvalds|		The operation in A3 above may have set INEX2.
631da177e4SLinus Torvalds|		RZ mode is forced for the scaling operation to insure
641da177e4SLinus Torvalds|		only one rounding error.  The grs bits are collected in
651da177e4SLinus Torvalds|		the INEX flag for use in A10.
661da177e4SLinus Torvalds|
671da177e4SLinus Torvalds|	A9.	Scale X -> Y.
681da177e4SLinus Torvalds|		The mantissa is scaled to the desired number of
691da177e4SLinus Torvalds|		significant digits.  The excess digits are collected
701da177e4SLinus Torvalds|		in INEX2.
711da177e4SLinus Torvalds|
721da177e4SLinus Torvalds|	A10.	Or in INEX.
731da177e4SLinus Torvalds|		If INEX is set, round error occurred.  This is
741da177e4SLinus Torvalds|		compensated for by 'or-ing' in the INEX2 flag to
751da177e4SLinus Torvalds|		the lsb of Y.
761da177e4SLinus Torvalds|
771da177e4SLinus Torvalds|	A11.	Restore original FPCR; set size ext.
781da177e4SLinus Torvalds|		Perform FINT operation in the user's rounding mode.
791da177e4SLinus Torvalds|		Keep the size to extended.
801da177e4SLinus Torvalds|
811da177e4SLinus Torvalds|	A12.	Calculate YINT = FINT(Y) according to user's rounding
821da177e4SLinus Torvalds|		mode.  The FPSP routine sintd0 is used.  The output
831da177e4SLinus Torvalds|		is in fp0.
841da177e4SLinus Torvalds|
851da177e4SLinus Torvalds|	A13.	Check for LEN digits.
861da177e4SLinus Torvalds|		If the int operation results in more than LEN digits,
871da177e4SLinus Torvalds|		or less than LEN -1 digits, adjust ILOG and repeat from
881da177e4SLinus Torvalds|		A6.  This test occurs only on the first pass.  If the
891da177e4SLinus Torvalds|		result is exactly 10^LEN, decrement ILOG and divide
901da177e4SLinus Torvalds|		the mantissa by 10.
911da177e4SLinus Torvalds|
921da177e4SLinus Torvalds|	A14.	Convert the mantissa to bcd.
931da177e4SLinus Torvalds|		The binstr routine is used to convert the LEN digit
941da177e4SLinus Torvalds|		mantissa to bcd in memory.  The input to binstr is
951da177e4SLinus Torvalds|		to be a fraction; i.e. (mantissa)/10^LEN and adjusted
961da177e4SLinus Torvalds|		such that the decimal point is to the left of bit 63.
971da177e4SLinus Torvalds|		The bcd digits are stored in the correct position in
981da177e4SLinus Torvalds|		the final string area in memory.
991da177e4SLinus Torvalds|
1001da177e4SLinus Torvalds|	A15.	Convert the exponent to bcd.
1011da177e4SLinus Torvalds|		As in A14 above, the exp is converted to bcd and the
1021da177e4SLinus Torvalds|		digits are stored in the final string.
1031da177e4SLinus Torvalds|		Test the length of the final exponent string.  If the
1041da177e4SLinus Torvalds|		length is 4, set operr.
1051da177e4SLinus Torvalds|
1061da177e4SLinus Torvalds|	A16.	Write sign bits to final string.
1071da177e4SLinus Torvalds|
1081da177e4SLinus Torvalds|	Implementation Notes:
1091da177e4SLinus Torvalds|
1101da177e4SLinus Torvalds|	The registers are used as follows:
1111da177e4SLinus Torvalds|
1121da177e4SLinus Torvalds|		d0: scratch; LEN input to binstr
1131da177e4SLinus Torvalds|		d1: scratch
1141da177e4SLinus Torvalds|		d2: upper 32-bits of mantissa for binstr
1151da177e4SLinus Torvalds|		d3: scratch;lower 32-bits of mantissa for binstr
1161da177e4SLinus Torvalds|		d4: LEN
1171da177e4SLinus Torvalds|		d5: LAMBDA/ICTR
1181da177e4SLinus Torvalds|		d6: ILOG
1191da177e4SLinus Torvalds|		d7: k-factor
1201da177e4SLinus Torvalds|		a0: ptr for original operand/final result
1211da177e4SLinus Torvalds|		a1: scratch pointer
1221da177e4SLinus Torvalds|		a2: pointer to FP_X; abs(original value) in ext
1231da177e4SLinus Torvalds|		fp0: scratch
1241da177e4SLinus Torvalds|		fp1: scratch
1251da177e4SLinus Torvalds|		fp2: scratch
1261da177e4SLinus Torvalds|		F_SCR1:
1271da177e4SLinus Torvalds|		F_SCR2:
1281da177e4SLinus Torvalds|		L_SCR1:
1291da177e4SLinus Torvalds|		L_SCR2:
1301da177e4SLinus Torvalds
1311da177e4SLinus Torvalds|		Copyright (C) Motorola, Inc. 1990
1321da177e4SLinus Torvalds|			All Rights Reserved
1331da177e4SLinus Torvalds|
134*e00d82d0SMatt Waddel|       For details on the license for this file, please see the
135*e00d82d0SMatt Waddel|       file, README, in this same directory.
1361da177e4SLinus Torvalds
1371da177e4SLinus Torvalds|BINDEC    idnt    2,1 | Motorola 040 Floating Point Software Package
1381da177e4SLinus Torvalds
1391da177e4SLinus Torvalds#include "fpsp.h"
1401da177e4SLinus Torvalds
1411da177e4SLinus Torvalds	|section	8
1421da177e4SLinus Torvalds
1431da177e4SLinus Torvalds| Constants in extended precision
1441da177e4SLinus TorvaldsLOG2:	.long	0x3FFD0000,0x9A209A84,0xFBCFF798,0x00000000
1451da177e4SLinus TorvaldsLOG2UP1:	.long	0x3FFD0000,0x9A209A84,0xFBCFF799,0x00000000
1461da177e4SLinus Torvalds
1471da177e4SLinus Torvalds| Constants in single precision
1481da177e4SLinus TorvaldsFONE:	.long	0x3F800000,0x00000000,0x00000000,0x00000000
1491da177e4SLinus TorvaldsFTWO:	.long	0x40000000,0x00000000,0x00000000,0x00000000
1501da177e4SLinus TorvaldsFTEN:	.long	0x41200000,0x00000000,0x00000000,0x00000000
1511da177e4SLinus TorvaldsF4933:	.long	0x459A2800,0x00000000,0x00000000,0x00000000
1521da177e4SLinus Torvalds
1531da177e4SLinus TorvaldsRBDTBL:	.byte	0,0,0,0
1541da177e4SLinus Torvalds	.byte	3,3,2,2
1551da177e4SLinus Torvalds	.byte	3,2,2,3
1561da177e4SLinus Torvalds	.byte	2,3,3,2
1571da177e4SLinus Torvalds
1581da177e4SLinus Torvalds	|xref	binstr
1591da177e4SLinus Torvalds	|xref	sintdo
1601da177e4SLinus Torvalds	|xref	ptenrn,ptenrm,ptenrp
1611da177e4SLinus Torvalds
1621da177e4SLinus Torvalds	.global	bindec
1631da177e4SLinus Torvalds	.global	sc_mul
1641da177e4SLinus Torvaldsbindec:
1651da177e4SLinus Torvalds	moveml	%d2-%d7/%a2,-(%a7)
1661da177e4SLinus Torvalds	fmovemx %fp0-%fp2,-(%a7)
1671da177e4SLinus Torvalds
1681da177e4SLinus Torvalds| A1. Set RM and size ext. Set SIGMA = sign input;
1691da177e4SLinus Torvalds|     The k-factor is saved for use in d7.  Clear BINDEC_FLG for
1701da177e4SLinus Torvalds|     separating  normalized/denormalized input.  If the input
1711da177e4SLinus Torvalds|     is a denormalized number, set the BINDEC_FLG memory word
1721da177e4SLinus Torvalds|     to signal denorm.  If the input is unnormalized, normalize
1731da177e4SLinus Torvalds|     the input and test for denormalized result.
1741da177e4SLinus Torvalds|
1751da177e4SLinus Torvalds	fmovel	#rm_mode,%FPCR	|set RM and ext
1761da177e4SLinus Torvalds	movel	(%a0),L_SCR2(%a6)	|save exponent for sign check
1771da177e4SLinus Torvalds	movel	%d0,%d7		|move k-factor to d7
1781da177e4SLinus Torvalds	clrb	BINDEC_FLG(%a6)	|clr norm/denorm flag
1791da177e4SLinus Torvalds	movew	STAG(%a6),%d0	|get stag
1801da177e4SLinus Torvalds	andiw	#0xe000,%d0	|isolate stag bits
1811da177e4SLinus Torvalds	beq	A2_str		|if zero, input is norm
1821da177e4SLinus Torvalds|
1831da177e4SLinus Torvalds| Normalize the denorm
1841da177e4SLinus Torvalds|
1851da177e4SLinus Torvaldsun_de_norm:
1861da177e4SLinus Torvalds	movew	(%a0),%d0
1871da177e4SLinus Torvalds	andiw	#0x7fff,%d0	|strip sign of normalized exp
1881da177e4SLinus Torvalds	movel	4(%a0),%d1
1891da177e4SLinus Torvalds	movel	8(%a0),%d2
1901da177e4SLinus Torvaldsnorm_loop:
1911da177e4SLinus Torvalds	subw	#1,%d0
1921da177e4SLinus Torvalds	lsll	#1,%d2
1931da177e4SLinus Torvalds	roxll	#1,%d1
1941da177e4SLinus Torvalds	tstl	%d1
1951da177e4SLinus Torvalds	bges	norm_loop
1961da177e4SLinus Torvalds|
1971da177e4SLinus Torvalds| Test if the normalized input is denormalized
1981da177e4SLinus Torvalds|
1991da177e4SLinus Torvalds	tstw	%d0
2001da177e4SLinus Torvalds	bgts	pos_exp		|if greater than zero, it is a norm
2011da177e4SLinus Torvalds	st	BINDEC_FLG(%a6)	|set flag for denorm
2021da177e4SLinus Torvaldspos_exp:
2031da177e4SLinus Torvalds	andiw	#0x7fff,%d0	|strip sign of normalized exp
2041da177e4SLinus Torvalds	movew	%d0,(%a0)
2051da177e4SLinus Torvalds	movel	%d1,4(%a0)
2061da177e4SLinus Torvalds	movel	%d2,8(%a0)
2071da177e4SLinus Torvalds
2081da177e4SLinus Torvalds| A2. Set X = abs(input).
2091da177e4SLinus Torvalds|
2101da177e4SLinus TorvaldsA2_str:
2111da177e4SLinus Torvalds	movel	(%a0),FP_SCR2(%a6) | move input to work space
2121da177e4SLinus Torvalds	movel	4(%a0),FP_SCR2+4(%a6) | move input to work space
2131da177e4SLinus Torvalds	movel	8(%a0),FP_SCR2+8(%a6) | move input to work space
2141da177e4SLinus Torvalds	andil	#0x7fffffff,FP_SCR2(%a6) |create abs(X)
2151da177e4SLinus Torvalds
2161da177e4SLinus Torvalds| A3. Compute ILOG.
2171da177e4SLinus Torvalds|     ILOG is the log base 10 of the input value.  It is approx-
2181da177e4SLinus Torvalds|     imated by adding e + 0.f when the original value is viewed
2191da177e4SLinus Torvalds|     as 2^^e * 1.f in extended precision.  This value is stored
2201da177e4SLinus Torvalds|     in d6.
2211da177e4SLinus Torvalds|
2221da177e4SLinus Torvalds| Register usage:
2231da177e4SLinus Torvalds|	Input/Output
2241da177e4SLinus Torvalds|	d0: k-factor/exponent
2251da177e4SLinus Torvalds|	d2: x/x
2261da177e4SLinus Torvalds|	d3: x/x
2271da177e4SLinus Torvalds|	d4: x/x
2281da177e4SLinus Torvalds|	d5: x/x
2291da177e4SLinus Torvalds|	d6: x/ILOG
2301da177e4SLinus Torvalds|	d7: k-factor/Unchanged
2311da177e4SLinus Torvalds|	a0: ptr for original operand/final result
2321da177e4SLinus Torvalds|	a1: x/x
2331da177e4SLinus Torvalds|	a2: x/x
2341da177e4SLinus Torvalds|	fp0: x/float(ILOG)
2351da177e4SLinus Torvalds|	fp1: x/x
2361da177e4SLinus Torvalds|	fp2: x/x
2371da177e4SLinus Torvalds|	F_SCR1:x/x
2381da177e4SLinus Torvalds|	F_SCR2:Abs(X)/Abs(X) with $3fff exponent
2391da177e4SLinus Torvalds|	L_SCR1:x/x
2401da177e4SLinus Torvalds|	L_SCR2:first word of X packed/Unchanged
2411da177e4SLinus Torvalds
2421da177e4SLinus Torvalds	tstb	BINDEC_FLG(%a6)	|check for denorm
2431da177e4SLinus Torvalds	beqs	A3_cont		|if clr, continue with norm
2441da177e4SLinus Torvalds	movel	#-4933,%d6	|force ILOG = -4933
2451da177e4SLinus Torvalds	bras	A4_str
2461da177e4SLinus TorvaldsA3_cont:
2471da177e4SLinus Torvalds	movew	FP_SCR2(%a6),%d0	|move exp to d0
2481da177e4SLinus Torvalds	movew	#0x3fff,FP_SCR2(%a6) |replace exponent with 0x3fff
2491da177e4SLinus Torvalds	fmovex	FP_SCR2(%a6),%fp0	|now fp0 has 1.f
2501da177e4SLinus Torvalds	subw	#0x3fff,%d0	|strip off bias
2511da177e4SLinus Torvalds	faddw	%d0,%fp0		|add in exp
2521da177e4SLinus Torvalds	fsubs	FONE,%fp0	|subtract off 1.0
2531da177e4SLinus Torvalds	fbge	pos_res		|if pos, branch
2541da177e4SLinus Torvalds	fmulx	LOG2UP1,%fp0	|if neg, mul by LOG2UP1
2551da177e4SLinus Torvalds	fmovel	%fp0,%d6		|put ILOG in d6 as a lword
2561da177e4SLinus Torvalds	bras	A4_str		|go move out ILOG
2571da177e4SLinus Torvaldspos_res:
2581da177e4SLinus Torvalds	fmulx	LOG2,%fp0	|if pos, mul by LOG2
2591da177e4SLinus Torvalds	fmovel	%fp0,%d6		|put ILOG in d6 as a lword
2601da177e4SLinus Torvalds
2611da177e4SLinus Torvalds
2621da177e4SLinus Torvalds| A4. Clr INEX bit.
2631da177e4SLinus Torvalds|     The operation in A3 above may have set INEX2.
2641da177e4SLinus Torvalds
2651da177e4SLinus TorvaldsA4_str:
2661da177e4SLinus Torvalds	fmovel	#0,%FPSR		|zero all of fpsr - nothing needed
2671da177e4SLinus Torvalds
2681da177e4SLinus Torvalds
2691da177e4SLinus Torvalds| A5. Set ICTR = 0;
2701da177e4SLinus Torvalds|     ICTR is a flag used in A13.  It must be set before the
2711da177e4SLinus Torvalds|     loop entry A6. The lower word of d5 is used for ICTR.
2721da177e4SLinus Torvalds
2731da177e4SLinus Torvalds	clrw	%d5		|clear ICTR
2741da177e4SLinus Torvalds
2751da177e4SLinus Torvalds
2761da177e4SLinus Torvalds| A6. Calculate LEN.
2771da177e4SLinus Torvalds|     LEN is the number of digits to be displayed.  The k-factor
2781da177e4SLinus Torvalds|     can dictate either the total number of digits, if it is
2791da177e4SLinus Torvalds|     a positive number, or the number of digits after the
2801da177e4SLinus Torvalds|     original decimal point which are to be included as
2811da177e4SLinus Torvalds|     significant.  See the 68882 manual for examples.
2821da177e4SLinus Torvalds|     If LEN is computed to be greater than 17, set OPERR in
2831da177e4SLinus Torvalds|     USER_FPSR.  LEN is stored in d4.
2841da177e4SLinus Torvalds|
2851da177e4SLinus Torvalds| Register usage:
2861da177e4SLinus Torvalds|	Input/Output
2871da177e4SLinus Torvalds|	d0: exponent/Unchanged
2881da177e4SLinus Torvalds|	d2: x/x/scratch
2891da177e4SLinus Torvalds|	d3: x/x
2901da177e4SLinus Torvalds|	d4: exc picture/LEN
2911da177e4SLinus Torvalds|	d5: ICTR/Unchanged
2921da177e4SLinus Torvalds|	d6: ILOG/Unchanged
2931da177e4SLinus Torvalds|	d7: k-factor/Unchanged
2941da177e4SLinus Torvalds|	a0: ptr for original operand/final result
2951da177e4SLinus Torvalds|	a1: x/x
2961da177e4SLinus Torvalds|	a2: x/x
2971da177e4SLinus Torvalds|	fp0: float(ILOG)/Unchanged
2981da177e4SLinus Torvalds|	fp1: x/x
2991da177e4SLinus Torvalds|	fp2: x/x
3001da177e4SLinus Torvalds|	F_SCR1:x/x
3011da177e4SLinus Torvalds|	F_SCR2:Abs(X) with $3fff exponent/Unchanged
3021da177e4SLinus Torvalds|	L_SCR1:x/x
3031da177e4SLinus Torvalds|	L_SCR2:first word of X packed/Unchanged
3041da177e4SLinus Torvalds
3051da177e4SLinus TorvaldsA6_str:
3061da177e4SLinus Torvalds	tstl	%d7		|branch on sign of k
3071da177e4SLinus Torvalds	bles	k_neg		|if k <= 0, LEN = ILOG + 1 - k
3081da177e4SLinus Torvalds	movel	%d7,%d4		|if k > 0, LEN = k
3091da177e4SLinus Torvalds	bras	len_ck		|skip to LEN check
3101da177e4SLinus Torvaldsk_neg:
3111da177e4SLinus Torvalds	movel	%d6,%d4		|first load ILOG to d4
3121da177e4SLinus Torvalds	subl	%d7,%d4		|subtract off k
3131da177e4SLinus Torvalds	addql	#1,%d4		|add in the 1
3141da177e4SLinus Torvaldslen_ck:
3151da177e4SLinus Torvalds	tstl	%d4		|LEN check: branch on sign of LEN
3161da177e4SLinus Torvalds	bles	LEN_ng		|if neg, set LEN = 1
3171da177e4SLinus Torvalds	cmpl	#17,%d4		|test if LEN > 17
3181da177e4SLinus Torvalds	bles	A7_str		|if not, forget it
3191da177e4SLinus Torvalds	movel	#17,%d4		|set max LEN = 17
3201da177e4SLinus Torvalds	tstl	%d7		|if negative, never set OPERR
3211da177e4SLinus Torvalds	bles	A7_str		|if positive, continue
3221da177e4SLinus Torvalds	orl	#opaop_mask,USER_FPSR(%a6) |set OPERR & AIOP in USER_FPSR
3231da177e4SLinus Torvalds	bras	A7_str		|finished here
3241da177e4SLinus TorvaldsLEN_ng:
3251da177e4SLinus Torvalds	moveql	#1,%d4		|min LEN is 1
3261da177e4SLinus Torvalds
3271da177e4SLinus Torvalds
3281da177e4SLinus Torvalds| A7. Calculate SCALE.
3291da177e4SLinus Torvalds|     SCALE is equal to 10^ISCALE, where ISCALE is the number
3301da177e4SLinus Torvalds|     of decimal places needed to insure LEN integer digits
3311da177e4SLinus Torvalds|     in the output before conversion to bcd. LAMBDA is the sign
3321da177e4SLinus Torvalds|     of ISCALE, used in A9.  Fp1 contains 10^^(abs(ISCALE)) using
3331da177e4SLinus Torvalds|     the rounding mode as given in the following table (see
3341da177e4SLinus Torvalds|     Coonen, p. 7.23 as ref.; however, the SCALE variable is
3351da177e4SLinus Torvalds|     of opposite sign in bindec.sa from Coonen).
3361da177e4SLinus Torvalds|
3371da177e4SLinus Torvalds|	Initial					USE
3381da177e4SLinus Torvalds|	FPCR[6:5]	LAMBDA	SIGN(X)		FPCR[6:5]
3391da177e4SLinus Torvalds|	----------------------------------------------
3401da177e4SLinus Torvalds|	 RN	00	   0	   0		00/0	RN
3411da177e4SLinus Torvalds|	 RN	00	   0	   1		00/0	RN
3421da177e4SLinus Torvalds|	 RN	00	   1	   0		00/0	RN
3431da177e4SLinus Torvalds|	 RN	00	   1	   1		00/0	RN
3441da177e4SLinus Torvalds|	 RZ	01	   0	   0		11/3	RP
3451da177e4SLinus Torvalds|	 RZ	01	   0	   1		11/3	RP
3461da177e4SLinus Torvalds|	 RZ	01	   1	   0		10/2	RM
3471da177e4SLinus Torvalds|	 RZ	01	   1	   1		10/2	RM
3481da177e4SLinus Torvalds|	 RM	10	   0	   0		11/3	RP
3491da177e4SLinus Torvalds|	 RM	10	   0	   1		10/2	RM
3501da177e4SLinus Torvalds|	 RM	10	   1	   0		10/2	RM
3511da177e4SLinus Torvalds|	 RM	10	   1	   1		11/3	RP
3521da177e4SLinus Torvalds|	 RP	11	   0	   0		10/2	RM
3531da177e4SLinus Torvalds|	 RP	11	   0	   1		11/3	RP
3541da177e4SLinus Torvalds|	 RP	11	   1	   0		11/3	RP
3551da177e4SLinus Torvalds|	 RP	11	   1	   1		10/2	RM
3561da177e4SLinus Torvalds|
3571da177e4SLinus Torvalds| Register usage:
3581da177e4SLinus Torvalds|	Input/Output
3591da177e4SLinus Torvalds|	d0: exponent/scratch - final is 0
3601da177e4SLinus Torvalds|	d2: x/0 or 24 for A9
3611da177e4SLinus Torvalds|	d3: x/scratch - offset ptr into PTENRM array
3621da177e4SLinus Torvalds|	d4: LEN/Unchanged
3631da177e4SLinus Torvalds|	d5: 0/ICTR:LAMBDA
3641da177e4SLinus Torvalds|	d6: ILOG/ILOG or k if ((k<=0)&(ILOG<k))
3651da177e4SLinus Torvalds|	d7: k-factor/Unchanged
3661da177e4SLinus Torvalds|	a0: ptr for original operand/final result
3671da177e4SLinus Torvalds|	a1: x/ptr to PTENRM array
3681da177e4SLinus Torvalds|	a2: x/x
3691da177e4SLinus Torvalds|	fp0: float(ILOG)/Unchanged
3701da177e4SLinus Torvalds|	fp1: x/10^ISCALE
3711da177e4SLinus Torvalds|	fp2: x/x
3721da177e4SLinus Torvalds|	F_SCR1:x/x
3731da177e4SLinus Torvalds|	F_SCR2:Abs(X) with $3fff exponent/Unchanged
3741da177e4SLinus Torvalds|	L_SCR1:x/x
3751da177e4SLinus Torvalds|	L_SCR2:first word of X packed/Unchanged
3761da177e4SLinus Torvalds
3771da177e4SLinus TorvaldsA7_str:
3781da177e4SLinus Torvalds	tstl	%d7		|test sign of k
3791da177e4SLinus Torvalds	bgts	k_pos		|if pos and > 0, skip this
3801da177e4SLinus Torvalds	cmpl	%d6,%d7		|test k - ILOG
3811da177e4SLinus Torvalds	blts	k_pos		|if ILOG >= k, skip this
3821da177e4SLinus Torvalds	movel	%d7,%d6		|if ((k<0) & (ILOG < k)) ILOG = k
3831da177e4SLinus Torvaldsk_pos:
3841da177e4SLinus Torvalds	movel	%d6,%d0		|calc ILOG + 1 - LEN in d0
3851da177e4SLinus Torvalds	addql	#1,%d0		|add the 1
3861da177e4SLinus Torvalds	subl	%d4,%d0		|sub off LEN
3871da177e4SLinus Torvalds	swap	%d5		|use upper word of d5 for LAMBDA
3881da177e4SLinus Torvalds	clrw	%d5		|set it zero initially
3891da177e4SLinus Torvalds	clrw	%d2		|set up d2 for very small case
3901da177e4SLinus Torvalds	tstl	%d0		|test sign of ISCALE
3911da177e4SLinus Torvalds	bges	iscale		|if pos, skip next inst
3921da177e4SLinus Torvalds	addqw	#1,%d5		|if neg, set LAMBDA true
3931da177e4SLinus Torvalds	cmpl	#0xffffecd4,%d0	|test iscale <= -4908
3941da177e4SLinus Torvalds	bgts	no_inf		|if false, skip rest
3951da177e4SLinus Torvalds	addil	#24,%d0		|add in 24 to iscale
3961da177e4SLinus Torvalds	movel	#24,%d2		|put 24 in d2 for A9
3971da177e4SLinus Torvaldsno_inf:
3981da177e4SLinus Torvalds	negl	%d0		|and take abs of ISCALE
3991da177e4SLinus Torvaldsiscale:
4001da177e4SLinus Torvalds	fmoves	FONE,%fp1	|init fp1 to 1
4011da177e4SLinus Torvalds	bfextu	USER_FPCR(%a6){#26:#2},%d1 |get initial rmode bits
4021da177e4SLinus Torvalds	lslw	#1,%d1		|put them in bits 2:1
4031da177e4SLinus Torvalds	addw	%d5,%d1		|add in LAMBDA
4041da177e4SLinus Torvalds	lslw	#1,%d1		|put them in bits 3:1
4051da177e4SLinus Torvalds	tstl	L_SCR2(%a6)	|test sign of original x
4061da177e4SLinus Torvalds	bges	x_pos		|if pos, don't set bit 0
4071da177e4SLinus Torvalds	addql	#1,%d1		|if neg, set bit 0
4081da177e4SLinus Torvaldsx_pos:
4091da177e4SLinus Torvalds	leal	RBDTBL,%a2	|load rbdtbl base
4101da177e4SLinus Torvalds	moveb	(%a2,%d1),%d3	|load d3 with new rmode
4111da177e4SLinus Torvalds	lsll	#4,%d3		|put bits in proper position
4121da177e4SLinus Torvalds	fmovel	%d3,%fpcr		|load bits into fpu
4131da177e4SLinus Torvalds	lsrl	#4,%d3		|put bits in proper position
4141da177e4SLinus Torvalds	tstb	%d3		|decode new rmode for pten table
4151da177e4SLinus Torvalds	bnes	not_rn		|if zero, it is RN
4161da177e4SLinus Torvalds	leal	PTENRN,%a1	|load a1 with RN table base
4171da177e4SLinus Torvalds	bras	rmode		|exit decode
4181da177e4SLinus Torvaldsnot_rn:
4191da177e4SLinus Torvalds	lsrb	#1,%d3		|get lsb in carry
4201da177e4SLinus Torvalds	bccs	not_rp		|if carry clear, it is RM
4211da177e4SLinus Torvalds	leal	PTENRP,%a1	|load a1 with RP table base
4221da177e4SLinus Torvalds	bras	rmode		|exit decode
4231da177e4SLinus Torvaldsnot_rp:
4241da177e4SLinus Torvalds	leal	PTENRM,%a1	|load a1 with RM table base
4251da177e4SLinus Torvaldsrmode:
4261da177e4SLinus Torvalds	clrl	%d3		|clr table index
4271da177e4SLinus Torvaldse_loop:
4281da177e4SLinus Torvalds	lsrl	#1,%d0		|shift next bit into carry
4291da177e4SLinus Torvalds	bccs	e_next		|if zero, skip the mul
4301da177e4SLinus Torvalds	fmulx	(%a1,%d3),%fp1	|mul by 10**(d3_bit_no)
4311da177e4SLinus Torvaldse_next:
4321da177e4SLinus Torvalds	addl	#12,%d3		|inc d3 to next pwrten table entry
4331da177e4SLinus Torvalds	tstl	%d0		|test if ISCALE is zero
4341da177e4SLinus Torvalds	bnes	e_loop		|if not, loop
4351da177e4SLinus Torvalds
4361da177e4SLinus Torvalds
4371da177e4SLinus Torvalds| A8. Clr INEX; Force RZ.
4381da177e4SLinus Torvalds|     The operation in A3 above may have set INEX2.
4391da177e4SLinus Torvalds|     RZ mode is forced for the scaling operation to insure
4401da177e4SLinus Torvalds|     only one rounding error.  The grs bits are collected in
4411da177e4SLinus Torvalds|     the INEX flag for use in A10.
4421da177e4SLinus Torvalds|
4431da177e4SLinus Torvalds| Register usage:
4441da177e4SLinus Torvalds|	Input/Output
4451da177e4SLinus Torvalds
4461da177e4SLinus Torvalds	fmovel	#0,%FPSR		|clr INEX
4471da177e4SLinus Torvalds	fmovel	#rz_mode,%FPCR	|set RZ rounding mode
4481da177e4SLinus Torvalds
4491da177e4SLinus Torvalds
4501da177e4SLinus Torvalds| A9. Scale X -> Y.
4511da177e4SLinus Torvalds|     The mantissa is scaled to the desired number of significant
4521da177e4SLinus Torvalds|     digits.  The excess digits are collected in INEX2. If mul,
4531da177e4SLinus Torvalds|     Check d2 for excess 10 exponential value.  If not zero,
4541da177e4SLinus Torvalds|     the iscale value would have caused the pwrten calculation
4551da177e4SLinus Torvalds|     to overflow.  Only a negative iscale can cause this, so
4561da177e4SLinus Torvalds|     multiply by 10^(d2), which is now only allowed to be 24,
4571da177e4SLinus Torvalds|     with a multiply by 10^8 and 10^16, which is exact since
4581da177e4SLinus Torvalds|     10^24 is exact.  If the input was denormalized, we must
4591da177e4SLinus Torvalds|     create a busy stack frame with the mul command and the
4601da177e4SLinus Torvalds|     two operands, and allow the fpu to complete the multiply.
4611da177e4SLinus Torvalds|
4621da177e4SLinus Torvalds| Register usage:
4631da177e4SLinus Torvalds|	Input/Output
4641da177e4SLinus Torvalds|	d0: FPCR with RZ mode/Unchanged
4651da177e4SLinus Torvalds|	d2: 0 or 24/unchanged
4661da177e4SLinus Torvalds|	d3: x/x
4671da177e4SLinus Torvalds|	d4: LEN/Unchanged
4681da177e4SLinus Torvalds|	d5: ICTR:LAMBDA
4691da177e4SLinus Torvalds|	d6: ILOG/Unchanged
4701da177e4SLinus Torvalds|	d7: k-factor/Unchanged
4711da177e4SLinus Torvalds|	a0: ptr for original operand/final result
4721da177e4SLinus Torvalds|	a1: ptr to PTENRM array/Unchanged
4731da177e4SLinus Torvalds|	a2: x/x
4741da177e4SLinus Torvalds|	fp0: float(ILOG)/X adjusted for SCALE (Y)
4751da177e4SLinus Torvalds|	fp1: 10^ISCALE/Unchanged
4761da177e4SLinus Torvalds|	fp2: x/x
4771da177e4SLinus Torvalds|	F_SCR1:x/x
4781da177e4SLinus Torvalds|	F_SCR2:Abs(X) with $3fff exponent/Unchanged
4791da177e4SLinus Torvalds|	L_SCR1:x/x
4801da177e4SLinus Torvalds|	L_SCR2:first word of X packed/Unchanged
4811da177e4SLinus Torvalds
4821da177e4SLinus TorvaldsA9_str:
4831da177e4SLinus Torvalds	fmovex	(%a0),%fp0	|load X from memory
4841da177e4SLinus Torvalds	fabsx	%fp0		|use abs(X)
4851da177e4SLinus Torvalds	tstw	%d5		|LAMBDA is in lower word of d5
4861da177e4SLinus Torvalds	bne	sc_mul		|if neg (LAMBDA = 1), scale by mul
4871da177e4SLinus Torvalds	fdivx	%fp1,%fp0		|calculate X / SCALE -> Y to fp0
4881da177e4SLinus Torvalds	bras	A10_st		|branch to A10
4891da177e4SLinus Torvalds
4901da177e4SLinus Torvaldssc_mul:
4911da177e4SLinus Torvalds	tstb	BINDEC_FLG(%a6)	|check for denorm
4921da177e4SLinus Torvalds	beqs	A9_norm		|if norm, continue with mul
4931da177e4SLinus Torvalds	fmovemx %fp1-%fp1,-(%a7)	|load ETEMP with 10^ISCALE
4941da177e4SLinus Torvalds	movel	8(%a0),-(%a7)	|load FPTEMP with input arg
4951da177e4SLinus Torvalds	movel	4(%a0),-(%a7)
4961da177e4SLinus Torvalds	movel	(%a0),-(%a7)
4971da177e4SLinus Torvalds	movel	#18,%d3		|load count for busy stack
4981da177e4SLinus TorvaldsA9_loop:
4991da177e4SLinus Torvalds	clrl	-(%a7)		|clear lword on stack
5001da177e4SLinus Torvalds	dbf	%d3,A9_loop
5011da177e4SLinus Torvalds	moveb	VER_TMP(%a6),(%a7) |write current version number
5021da177e4SLinus Torvalds	moveb	#BUSY_SIZE-4,1(%a7) |write current busy size
5031da177e4SLinus Torvalds	moveb	#0x10,0x44(%a7)	|set fcefpte[15] bit
5041da177e4SLinus Torvalds	movew	#0x0023,0x40(%a7)	|load cmdreg1b with mul command
5051da177e4SLinus Torvalds	moveb	#0xfe,0x8(%a7)	|load all 1s to cu savepc
5061da177e4SLinus Torvalds	frestore (%a7)+		|restore frame to fpu for completion
5071da177e4SLinus Torvalds	fmulx	36(%a1),%fp0	|multiply fp0 by 10^8
5081da177e4SLinus Torvalds	fmulx	48(%a1),%fp0	|multiply fp0 by 10^16
5091da177e4SLinus Torvalds	bras	A10_st
5101da177e4SLinus TorvaldsA9_norm:
5111da177e4SLinus Torvalds	tstw	%d2		|test for small exp case
5121da177e4SLinus Torvalds	beqs	A9_con		|if zero, continue as normal
5131da177e4SLinus Torvalds	fmulx	36(%a1),%fp0	|multiply fp0 by 10^8
5141da177e4SLinus Torvalds	fmulx	48(%a1),%fp0	|multiply fp0 by 10^16
5151da177e4SLinus TorvaldsA9_con:
5161da177e4SLinus Torvalds	fmulx	%fp1,%fp0		|calculate X * SCALE -> Y to fp0
5171da177e4SLinus Torvalds
5181da177e4SLinus Torvalds
5191da177e4SLinus Torvalds| A10. Or in INEX.
5201da177e4SLinus Torvalds|      If INEX is set, round error occurred.  This is compensated
5211da177e4SLinus Torvalds|      for by 'or-ing' in the INEX2 flag to the lsb of Y.
5221da177e4SLinus Torvalds|
5231da177e4SLinus Torvalds| Register usage:
5241da177e4SLinus Torvalds|	Input/Output
5251da177e4SLinus Torvalds|	d0: FPCR with RZ mode/FPSR with INEX2 isolated
5261da177e4SLinus Torvalds|	d2: x/x
5271da177e4SLinus Torvalds|	d3: x/x
5281da177e4SLinus Torvalds|	d4: LEN/Unchanged
5291da177e4SLinus Torvalds|	d5: ICTR:LAMBDA
5301da177e4SLinus Torvalds|	d6: ILOG/Unchanged
5311da177e4SLinus Torvalds|	d7: k-factor/Unchanged
5321da177e4SLinus Torvalds|	a0: ptr for original operand/final result
5331da177e4SLinus Torvalds|	a1: ptr to PTENxx array/Unchanged
5341da177e4SLinus Torvalds|	a2: x/ptr to FP_SCR2(a6)
5351da177e4SLinus Torvalds|	fp0: Y/Y with lsb adjusted
5361da177e4SLinus Torvalds|	fp1: 10^ISCALE/Unchanged
5371da177e4SLinus Torvalds|	fp2: x/x
5381da177e4SLinus Torvalds
5391da177e4SLinus TorvaldsA10_st:
5401da177e4SLinus Torvalds	fmovel	%FPSR,%d0		|get FPSR
5411da177e4SLinus Torvalds	fmovex	%fp0,FP_SCR2(%a6)	|move Y to memory
5421da177e4SLinus Torvalds	leal	FP_SCR2(%a6),%a2	|load a2 with ptr to FP_SCR2
5431da177e4SLinus Torvalds	btstl	#9,%d0		|check if INEX2 set
5441da177e4SLinus Torvalds	beqs	A11_st		|if clear, skip rest
5451da177e4SLinus Torvalds	oril	#1,8(%a2)	|or in 1 to lsb of mantissa
5461da177e4SLinus Torvalds	fmovex	FP_SCR2(%a6),%fp0	|write adjusted Y back to fpu
5471da177e4SLinus Torvalds
5481da177e4SLinus Torvalds
5491da177e4SLinus Torvalds| A11. Restore original FPCR; set size ext.
5501da177e4SLinus Torvalds|      Perform FINT operation in the user's rounding mode.  Keep
5511da177e4SLinus Torvalds|      the size to extended.  The sintdo entry point in the sint
5521da177e4SLinus Torvalds|      routine expects the FPCR value to be in USER_FPCR for
5531da177e4SLinus Torvalds|      mode and precision.  The original FPCR is saved in L_SCR1.
5541da177e4SLinus Torvalds
5551da177e4SLinus TorvaldsA11_st:
5561da177e4SLinus Torvalds	movel	USER_FPCR(%a6),L_SCR1(%a6) |save it for later
5571da177e4SLinus Torvalds	andil	#0x00000030,USER_FPCR(%a6) |set size to ext,
5581da177e4SLinus Torvalds|					;block exceptions
5591da177e4SLinus Torvalds
5601da177e4SLinus Torvalds
5611da177e4SLinus Torvalds| A12. Calculate YINT = FINT(Y) according to user's rounding mode.
5621da177e4SLinus Torvalds|      The FPSP routine sintd0 is used.  The output is in fp0.
5631da177e4SLinus Torvalds|
5641da177e4SLinus Torvalds| Register usage:
5651da177e4SLinus Torvalds|	Input/Output
5661da177e4SLinus Torvalds|	d0: FPSR with AINEX cleared/FPCR with size set to ext
5671da177e4SLinus Torvalds|	d2: x/x/scratch
5681da177e4SLinus Torvalds|	d3: x/x
5691da177e4SLinus Torvalds|	d4: LEN/Unchanged
5701da177e4SLinus Torvalds|	d5: ICTR:LAMBDA/Unchanged
5711da177e4SLinus Torvalds|	d6: ILOG/Unchanged
5721da177e4SLinus Torvalds|	d7: k-factor/Unchanged
5731da177e4SLinus Torvalds|	a0: ptr for original operand/src ptr for sintdo
5741da177e4SLinus Torvalds|	a1: ptr to PTENxx array/Unchanged
5751da177e4SLinus Torvalds|	a2: ptr to FP_SCR2(a6)/Unchanged
5761da177e4SLinus Torvalds|	a6: temp pointer to FP_SCR2(a6) - orig value saved and restored
5771da177e4SLinus Torvalds|	fp0: Y/YINT
5781da177e4SLinus Torvalds|	fp1: 10^ISCALE/Unchanged
5791da177e4SLinus Torvalds|	fp2: x/x
5801da177e4SLinus Torvalds|	F_SCR1:x/x
5811da177e4SLinus Torvalds|	F_SCR2:Y adjusted for inex/Y with original exponent
5821da177e4SLinus Torvalds|	L_SCR1:x/original USER_FPCR
5831da177e4SLinus Torvalds|	L_SCR2:first word of X packed/Unchanged
5841da177e4SLinus Torvalds
5851da177e4SLinus TorvaldsA12_st:
5861da177e4SLinus Torvalds	moveml	%d0-%d1/%a0-%a1,-(%a7)	|save regs used by sintd0
5871da177e4SLinus Torvalds	movel	L_SCR1(%a6),-(%a7)
5881da177e4SLinus Torvalds	movel	L_SCR2(%a6),-(%a7)
5891da177e4SLinus Torvalds	leal	FP_SCR2(%a6),%a0		|a0 is ptr to F_SCR2(a6)
5901da177e4SLinus Torvalds	fmovex	%fp0,(%a0)		|move Y to memory at FP_SCR2(a6)
5911da177e4SLinus Torvalds	tstl	L_SCR2(%a6)		|test sign of original operand
5921da177e4SLinus Torvalds	bges	do_fint			|if pos, use Y
5931da177e4SLinus Torvalds	orl	#0x80000000,(%a0)		|if neg, use -Y
5941da177e4SLinus Torvaldsdo_fint:
5951da177e4SLinus Torvalds	movel	USER_FPSR(%a6),-(%a7)
5961da177e4SLinus Torvalds	bsr	sintdo			|sint routine returns int in fp0
5971da177e4SLinus Torvalds	moveb	(%a7),USER_FPSR(%a6)
5981da177e4SLinus Torvalds	addl	#4,%a7
5991da177e4SLinus Torvalds	movel	(%a7)+,L_SCR2(%a6)
6001da177e4SLinus Torvalds	movel	(%a7)+,L_SCR1(%a6)
6011da177e4SLinus Torvalds	moveml	(%a7)+,%d0-%d1/%a0-%a1	|restore regs used by sint
6021da177e4SLinus Torvalds	movel	L_SCR2(%a6),FP_SCR2(%a6)	|restore original exponent
6031da177e4SLinus Torvalds	movel	L_SCR1(%a6),USER_FPCR(%a6) |restore user's FPCR
6041da177e4SLinus Torvalds
6051da177e4SLinus Torvalds
6061da177e4SLinus Torvalds| A13. Check for LEN digits.
6071da177e4SLinus Torvalds|      If the int operation results in more than LEN digits,
6081da177e4SLinus Torvalds|      or less than LEN -1 digits, adjust ILOG and repeat from
6091da177e4SLinus Torvalds|      A6.  This test occurs only on the first pass.  If the
6101da177e4SLinus Torvalds|      result is exactly 10^LEN, decrement ILOG and divide
6111da177e4SLinus Torvalds|      the mantissa by 10.  The calculation of 10^LEN cannot
6121da177e4SLinus Torvalds|      be inexact, since all powers of ten up to 10^27 are exact
6131da177e4SLinus Torvalds|      in extended precision, so the use of a previous power-of-ten
6141da177e4SLinus Torvalds|      table will introduce no error.
6151da177e4SLinus Torvalds|
6161da177e4SLinus Torvalds|
6171da177e4SLinus Torvalds| Register usage:
6181da177e4SLinus Torvalds|	Input/Output
6191da177e4SLinus Torvalds|	d0: FPCR with size set to ext/scratch final = 0
6201da177e4SLinus Torvalds|	d2: x/x
6211da177e4SLinus Torvalds|	d3: x/scratch final = x
6221da177e4SLinus Torvalds|	d4: LEN/LEN adjusted
6231da177e4SLinus Torvalds|	d5: ICTR:LAMBDA/LAMBDA:ICTR
6241da177e4SLinus Torvalds|	d6: ILOG/ILOG adjusted
6251da177e4SLinus Torvalds|	d7: k-factor/Unchanged
6261da177e4SLinus Torvalds|	a0: pointer into memory for packed bcd string formation
6271da177e4SLinus Torvalds|	a1: ptr to PTENxx array/Unchanged
6281da177e4SLinus Torvalds|	a2: ptr to FP_SCR2(a6)/Unchanged
6291da177e4SLinus Torvalds|	fp0: int portion of Y/abs(YINT) adjusted
6301da177e4SLinus Torvalds|	fp1: 10^ISCALE/Unchanged
6311da177e4SLinus Torvalds|	fp2: x/10^LEN
6321da177e4SLinus Torvalds|	F_SCR1:x/x
6331da177e4SLinus Torvalds|	F_SCR2:Y with original exponent/Unchanged
6341da177e4SLinus Torvalds|	L_SCR1:original USER_FPCR/Unchanged
6351da177e4SLinus Torvalds|	L_SCR2:first word of X packed/Unchanged
6361da177e4SLinus Torvalds
6371da177e4SLinus TorvaldsA13_st:
6381da177e4SLinus Torvalds	swap	%d5		|put ICTR in lower word of d5
6391da177e4SLinus Torvalds	tstw	%d5		|check if ICTR = 0
6401da177e4SLinus Torvalds	bne	not_zr		|if non-zero, go to second test
6411da177e4SLinus Torvalds|
6421da177e4SLinus Torvalds| Compute 10^(LEN-1)
6431da177e4SLinus Torvalds|
6441da177e4SLinus Torvalds	fmoves	FONE,%fp2	|init fp2 to 1.0
6451da177e4SLinus Torvalds	movel	%d4,%d0		|put LEN in d0
6461da177e4SLinus Torvalds	subql	#1,%d0		|d0 = LEN -1
6471da177e4SLinus Torvalds	clrl	%d3		|clr table index
6481da177e4SLinus Torvaldsl_loop:
6491da177e4SLinus Torvalds	lsrl	#1,%d0		|shift next bit into carry
6501da177e4SLinus Torvalds	bccs	l_next		|if zero, skip the mul
6511da177e4SLinus Torvalds	fmulx	(%a1,%d3),%fp2	|mul by 10**(d3_bit_no)
6521da177e4SLinus Torvaldsl_next:
6531da177e4SLinus Torvalds	addl	#12,%d3		|inc d3 to next pwrten table entry
6541da177e4SLinus Torvalds	tstl	%d0		|test if LEN is zero
6551da177e4SLinus Torvalds	bnes	l_loop		|if not, loop
6561da177e4SLinus Torvalds|
6571da177e4SLinus Torvalds| 10^LEN-1 is computed for this test and A14.  If the input was
6581da177e4SLinus Torvalds| denormalized, check only the case in which YINT > 10^LEN.
6591da177e4SLinus Torvalds|
6601da177e4SLinus Torvalds	tstb	BINDEC_FLG(%a6)	|check if input was norm
6611da177e4SLinus Torvalds	beqs	A13_con		|if norm, continue with checking
6621da177e4SLinus Torvalds	fabsx	%fp0		|take abs of YINT
6631da177e4SLinus Torvalds	bra	test_2
6641da177e4SLinus Torvalds|
6651da177e4SLinus Torvalds| Compare abs(YINT) to 10^(LEN-1) and 10^LEN
6661da177e4SLinus Torvalds|
6671da177e4SLinus TorvaldsA13_con:
6681da177e4SLinus Torvalds	fabsx	%fp0		|take abs of YINT
6691da177e4SLinus Torvalds	fcmpx	%fp2,%fp0		|compare abs(YINT) with 10^(LEN-1)
6701da177e4SLinus Torvalds	fbge	test_2		|if greater, do next test
6711da177e4SLinus Torvalds	subql	#1,%d6		|subtract 1 from ILOG
6721da177e4SLinus Torvalds	movew	#1,%d5		|set ICTR
6731da177e4SLinus Torvalds	fmovel	#rm_mode,%FPCR	|set rmode to RM
6741da177e4SLinus Torvalds	fmuls	FTEN,%fp2	|compute 10^LEN
6751da177e4SLinus Torvalds	bra	A6_str		|return to A6 and recompute YINT
6761da177e4SLinus Torvaldstest_2:
6771da177e4SLinus Torvalds	fmuls	FTEN,%fp2	|compute 10^LEN
6781da177e4SLinus Torvalds	fcmpx	%fp2,%fp0		|compare abs(YINT) with 10^LEN
6791da177e4SLinus Torvalds	fblt	A14_st		|if less, all is ok, go to A14
6801da177e4SLinus Torvalds	fbgt	fix_ex		|if greater, fix and redo
6811da177e4SLinus Torvalds	fdivs	FTEN,%fp0	|if equal, divide by 10
6821da177e4SLinus Torvalds	addql	#1,%d6		| and inc ILOG
6831da177e4SLinus Torvalds	bras	A14_st		| and continue elsewhere
6841da177e4SLinus Torvaldsfix_ex:
6851da177e4SLinus Torvalds	addql	#1,%d6		|increment ILOG by 1
6861da177e4SLinus Torvalds	movew	#1,%d5		|set ICTR
6871da177e4SLinus Torvalds	fmovel	#rm_mode,%FPCR	|set rmode to RM
6881da177e4SLinus Torvalds	bra	A6_str		|return to A6 and recompute YINT
6891da177e4SLinus Torvalds|
6901da177e4SLinus Torvalds| Since ICTR <> 0, we have already been through one adjustment,
6911da177e4SLinus Torvalds| and shouldn't have another; this is to check if abs(YINT) = 10^LEN
6921da177e4SLinus Torvalds| 10^LEN is again computed using whatever table is in a1 since the
6931da177e4SLinus Torvalds| value calculated cannot be inexact.
6941da177e4SLinus Torvalds|
6951da177e4SLinus Torvaldsnot_zr:
6961da177e4SLinus Torvalds	fmoves	FONE,%fp2	|init fp2 to 1.0
6971da177e4SLinus Torvalds	movel	%d4,%d0		|put LEN in d0
6981da177e4SLinus Torvalds	clrl	%d3		|clr table index
6991da177e4SLinus Torvaldsz_loop:
7001da177e4SLinus Torvalds	lsrl	#1,%d0		|shift next bit into carry
7011da177e4SLinus Torvalds	bccs	z_next		|if zero, skip the mul
7021da177e4SLinus Torvalds	fmulx	(%a1,%d3),%fp2	|mul by 10**(d3_bit_no)
7031da177e4SLinus Torvaldsz_next:
7041da177e4SLinus Torvalds	addl	#12,%d3		|inc d3 to next pwrten table entry
7051da177e4SLinus Torvalds	tstl	%d0		|test if LEN is zero
7061da177e4SLinus Torvalds	bnes	z_loop		|if not, loop
7071da177e4SLinus Torvalds	fabsx	%fp0		|get abs(YINT)
7081da177e4SLinus Torvalds	fcmpx	%fp2,%fp0		|check if abs(YINT) = 10^LEN
7091da177e4SLinus Torvalds	fbne	A14_st		|if not, skip this
7101da177e4SLinus Torvalds	fdivs	FTEN,%fp0	|divide abs(YINT) by 10
7111da177e4SLinus Torvalds	addql	#1,%d6		|and inc ILOG by 1
7121da177e4SLinus Torvalds	addql	#1,%d4		| and inc LEN
7131da177e4SLinus Torvalds	fmuls	FTEN,%fp2	| if LEN++, the get 10^^LEN
7141da177e4SLinus Torvalds
7151da177e4SLinus Torvalds
7161da177e4SLinus Torvalds| A14. Convert the mantissa to bcd.
7171da177e4SLinus Torvalds|      The binstr routine is used to convert the LEN digit
7181da177e4SLinus Torvalds|      mantissa to bcd in memory.  The input to binstr is
7191da177e4SLinus Torvalds|      to be a fraction; i.e. (mantissa)/10^LEN and adjusted
7201da177e4SLinus Torvalds|      such that the decimal point is to the left of bit 63.
7211da177e4SLinus Torvalds|      The bcd digits are stored in the correct position in
7221da177e4SLinus Torvalds|      the final string area in memory.
7231da177e4SLinus Torvalds|
7241da177e4SLinus Torvalds|
7251da177e4SLinus Torvalds| Register usage:
7261da177e4SLinus Torvalds|	Input/Output
7271da177e4SLinus Torvalds|	d0: x/LEN call to binstr - final is 0
7281da177e4SLinus Torvalds|	d1: x/0
7291da177e4SLinus Torvalds|	d2: x/ms 32-bits of mant of abs(YINT)
7301da177e4SLinus Torvalds|	d3: x/ls 32-bits of mant of abs(YINT)
7311da177e4SLinus Torvalds|	d4: LEN/Unchanged
7321da177e4SLinus Torvalds|	d5: ICTR:LAMBDA/LAMBDA:ICTR
7331da177e4SLinus Torvalds|	d6: ILOG
7341da177e4SLinus Torvalds|	d7: k-factor/Unchanged
7351da177e4SLinus Torvalds|	a0: pointer into memory for packed bcd string formation
7361da177e4SLinus Torvalds|	    /ptr to first mantissa byte in result string
7371da177e4SLinus Torvalds|	a1: ptr to PTENxx array/Unchanged
7381da177e4SLinus Torvalds|	a2: ptr to FP_SCR2(a6)/Unchanged
7391da177e4SLinus Torvalds|	fp0: int portion of Y/abs(YINT) adjusted
7401da177e4SLinus Torvalds|	fp1: 10^ISCALE/Unchanged
7411da177e4SLinus Torvalds|	fp2: 10^LEN/Unchanged
7421da177e4SLinus Torvalds|	F_SCR1:x/Work area for final result
7431da177e4SLinus Torvalds|	F_SCR2:Y with original exponent/Unchanged
7441da177e4SLinus Torvalds|	L_SCR1:original USER_FPCR/Unchanged
7451da177e4SLinus Torvalds|	L_SCR2:first word of X packed/Unchanged
7461da177e4SLinus Torvalds
7471da177e4SLinus TorvaldsA14_st:
7481da177e4SLinus Torvalds	fmovel	#rz_mode,%FPCR	|force rz for conversion
7491da177e4SLinus Torvalds	fdivx	%fp2,%fp0		|divide abs(YINT) by 10^LEN
7501da177e4SLinus Torvalds	leal	FP_SCR1(%a6),%a0
7511da177e4SLinus Torvalds	fmovex	%fp0,(%a0)	|move abs(YINT)/10^LEN to memory
7521da177e4SLinus Torvalds	movel	4(%a0),%d2	|move 2nd word of FP_RES to d2
7531da177e4SLinus Torvalds	movel	8(%a0),%d3	|move 3rd word of FP_RES to d3
7541da177e4SLinus Torvalds	clrl	4(%a0)		|zero word 2 of FP_RES
7551da177e4SLinus Torvalds	clrl	8(%a0)		|zero word 3 of FP_RES
7561da177e4SLinus Torvalds	movel	(%a0),%d0		|move exponent to d0
7571da177e4SLinus Torvalds	swap	%d0		|put exponent in lower word
7581da177e4SLinus Torvalds	beqs	no_sft		|if zero, don't shift
7591da177e4SLinus Torvalds	subil	#0x3ffd,%d0	|sub bias less 2 to make fract
7601da177e4SLinus Torvalds	tstl	%d0		|check if > 1
7611da177e4SLinus Torvalds	bgts	no_sft		|if so, don't shift
7621da177e4SLinus Torvalds	negl	%d0		|make exp positive
7631da177e4SLinus Torvaldsm_loop:
7641da177e4SLinus Torvalds	lsrl	#1,%d2		|shift d2:d3 right, add 0s
7651da177e4SLinus Torvalds	roxrl	#1,%d3		|the number of places
7661da177e4SLinus Torvalds	dbf	%d0,m_loop	|given in d0
7671da177e4SLinus Torvaldsno_sft:
7681da177e4SLinus Torvalds	tstl	%d2		|check for mantissa of zero
7691da177e4SLinus Torvalds	bnes	no_zr		|if not, go on
7701da177e4SLinus Torvalds	tstl	%d3		|continue zero check
7711da177e4SLinus Torvalds	beqs	zer_m		|if zero, go directly to binstr
7721da177e4SLinus Torvaldsno_zr:
7731da177e4SLinus Torvalds	clrl	%d1		|put zero in d1 for addx
7741da177e4SLinus Torvalds	addil	#0x00000080,%d3	|inc at bit 7
7751da177e4SLinus Torvalds	addxl	%d1,%d2		|continue inc
7761da177e4SLinus Torvalds	andil	#0xffffff80,%d3	|strip off lsb not used by 882
7771da177e4SLinus Torvaldszer_m:
7781da177e4SLinus Torvalds	movel	%d4,%d0		|put LEN in d0 for binstr call
7791da177e4SLinus Torvalds	addql	#3,%a0		|a0 points to M16 byte in result
7801da177e4SLinus Torvalds	bsr	binstr		|call binstr to convert mant
7811da177e4SLinus Torvalds
7821da177e4SLinus Torvalds
7831da177e4SLinus Torvalds| A15. Convert the exponent to bcd.
7841da177e4SLinus Torvalds|      As in A14 above, the exp is converted to bcd and the
7851da177e4SLinus Torvalds|      digits are stored in the final string.
7861da177e4SLinus Torvalds|
7871da177e4SLinus Torvalds|      Digits are stored in L_SCR1(a6) on return from BINDEC as:
7881da177e4SLinus Torvalds|
7891da177e4SLinus Torvalds|	 32               16 15                0
7901da177e4SLinus Torvalds|	-----------------------------------------
7911da177e4SLinus Torvalds|	|  0 | e3 | e2 | e1 | e4 |  X |  X |  X |
7921da177e4SLinus Torvalds|	-----------------------------------------
7931da177e4SLinus Torvalds|
7941da177e4SLinus Torvalds| And are moved into their proper places in FP_SCR1.  If digit e4
7951da177e4SLinus Torvalds| is non-zero, OPERR is signaled.  In all cases, all 4 digits are
7961da177e4SLinus Torvalds| written as specified in the 881/882 manual for packed decimal.
7971da177e4SLinus Torvalds|
7981da177e4SLinus Torvalds| Register usage:
7991da177e4SLinus Torvalds|	Input/Output
8001da177e4SLinus Torvalds|	d0: x/LEN call to binstr - final is 0
8011da177e4SLinus Torvalds|	d1: x/scratch (0);shift count for final exponent packing
8021da177e4SLinus Torvalds|	d2: x/ms 32-bits of exp fraction/scratch
8031da177e4SLinus Torvalds|	d3: x/ls 32-bits of exp fraction
8041da177e4SLinus Torvalds|	d4: LEN/Unchanged
8051da177e4SLinus Torvalds|	d5: ICTR:LAMBDA/LAMBDA:ICTR
8061da177e4SLinus Torvalds|	d6: ILOG
8071da177e4SLinus Torvalds|	d7: k-factor/Unchanged
8081da177e4SLinus Torvalds|	a0: ptr to result string/ptr to L_SCR1(a6)
8091da177e4SLinus Torvalds|	a1: ptr to PTENxx array/Unchanged
8101da177e4SLinus Torvalds|	a2: ptr to FP_SCR2(a6)/Unchanged
8111da177e4SLinus Torvalds|	fp0: abs(YINT) adjusted/float(ILOG)
8121da177e4SLinus Torvalds|	fp1: 10^ISCALE/Unchanged
8131da177e4SLinus Torvalds|	fp2: 10^LEN/Unchanged
8141da177e4SLinus Torvalds|	F_SCR1:Work area for final result/BCD result
8151da177e4SLinus Torvalds|	F_SCR2:Y with original exponent/ILOG/10^4
8161da177e4SLinus Torvalds|	L_SCR1:original USER_FPCR/Exponent digits on return from binstr
8171da177e4SLinus Torvalds|	L_SCR2:first word of X packed/Unchanged
8181da177e4SLinus Torvalds
8191da177e4SLinus TorvaldsA15_st:
8201da177e4SLinus Torvalds	tstb	BINDEC_FLG(%a6)	|check for denorm
8211da177e4SLinus Torvalds	beqs	not_denorm
8221da177e4SLinus Torvalds	ftstx	%fp0		|test for zero
8231da177e4SLinus Torvalds	fbeq	den_zero	|if zero, use k-factor or 4933
8241da177e4SLinus Torvalds	fmovel	%d6,%fp0		|float ILOG
8251da177e4SLinus Torvalds	fabsx	%fp0		|get abs of ILOG
8261da177e4SLinus Torvalds	bras	convrt
8271da177e4SLinus Torvaldsden_zero:
8281da177e4SLinus Torvalds	tstl	%d7		|check sign of the k-factor
8291da177e4SLinus Torvalds	blts	use_ilog	|if negative, use ILOG
8301da177e4SLinus Torvalds	fmoves	F4933,%fp0	|force exponent to 4933
8311da177e4SLinus Torvalds	bras	convrt		|do it
8321da177e4SLinus Torvaldsuse_ilog:
8331da177e4SLinus Torvalds	fmovel	%d6,%fp0		|float ILOG
8341da177e4SLinus Torvalds	fabsx	%fp0		|get abs of ILOG
8351da177e4SLinus Torvalds	bras	convrt
8361da177e4SLinus Torvaldsnot_denorm:
8371da177e4SLinus Torvalds	ftstx	%fp0		|test for zero
8381da177e4SLinus Torvalds	fbne	not_zero	|if zero, force exponent
8391da177e4SLinus Torvalds	fmoves	FONE,%fp0	|force exponent to 1
8401da177e4SLinus Torvalds	bras	convrt		|do it
8411da177e4SLinus Torvaldsnot_zero:
8421da177e4SLinus Torvalds	fmovel	%d6,%fp0		|float ILOG
8431da177e4SLinus Torvalds	fabsx	%fp0		|get abs of ILOG
8441da177e4SLinus Torvaldsconvrt:
8451da177e4SLinus Torvalds	fdivx	24(%a1),%fp0	|compute ILOG/10^4
8461da177e4SLinus Torvalds	fmovex	%fp0,FP_SCR2(%a6)	|store fp0 in memory
8471da177e4SLinus Torvalds	movel	4(%a2),%d2	|move word 2 to d2
8481da177e4SLinus Torvalds	movel	8(%a2),%d3	|move word 3 to d3
8491da177e4SLinus Torvalds	movew	(%a2),%d0		|move exp to d0
8501da177e4SLinus Torvalds	beqs	x_loop_fin	|if zero, skip the shift
8511da177e4SLinus Torvalds	subiw	#0x3ffd,%d0	|subtract off bias
8521da177e4SLinus Torvalds	negw	%d0		|make exp positive
8531da177e4SLinus Torvaldsx_loop:
8541da177e4SLinus Torvalds	lsrl	#1,%d2		|shift d2:d3 right
8551da177e4SLinus Torvalds	roxrl	#1,%d3		|the number of places
8561da177e4SLinus Torvalds	dbf	%d0,x_loop	|given in d0
8571da177e4SLinus Torvaldsx_loop_fin:
8581da177e4SLinus Torvalds	clrl	%d1		|put zero in d1 for addx
8591da177e4SLinus Torvalds	addil	#0x00000080,%d3	|inc at bit 6
8601da177e4SLinus Torvalds	addxl	%d1,%d2		|continue inc
8611da177e4SLinus Torvalds	andil	#0xffffff80,%d3	|strip off lsb not used by 882
8621da177e4SLinus Torvalds	movel	#4,%d0		|put 4 in d0 for binstr call
8631da177e4SLinus Torvalds	leal	L_SCR1(%a6),%a0	|a0 is ptr to L_SCR1 for exp digits
8641da177e4SLinus Torvalds	bsr	binstr		|call binstr to convert exp
8651da177e4SLinus Torvalds	movel	L_SCR1(%a6),%d0	|load L_SCR1 lword to d0
8661da177e4SLinus Torvalds	movel	#12,%d1		|use d1 for shift count
8671da177e4SLinus Torvalds	lsrl	%d1,%d0		|shift d0 right by 12
8681da177e4SLinus Torvalds	bfins	%d0,FP_SCR1(%a6){#4:#12} |put e3:e2:e1 in FP_SCR1
8691da177e4SLinus Torvalds	lsrl	%d1,%d0		|shift d0 right by 12
8701da177e4SLinus Torvalds	bfins	%d0,FP_SCR1(%a6){#16:#4} |put e4 in FP_SCR1
8711da177e4SLinus Torvalds	tstb	%d0		|check if e4 is zero
8721da177e4SLinus Torvalds	beqs	A16_st		|if zero, skip rest
8731da177e4SLinus Torvalds	orl	#opaop_mask,USER_FPSR(%a6) |set OPERR & AIOP in USER_FPSR
8741da177e4SLinus Torvalds
8751da177e4SLinus Torvalds
8761da177e4SLinus Torvalds| A16. Write sign bits to final string.
8771da177e4SLinus Torvalds|	   Sigma is bit 31 of initial value; RHO is bit 31 of d6 (ILOG).
8781da177e4SLinus Torvalds|
8791da177e4SLinus Torvalds| Register usage:
8801da177e4SLinus Torvalds|	Input/Output
8811da177e4SLinus Torvalds|	d0: x/scratch - final is x
8821da177e4SLinus Torvalds|	d2: x/x
8831da177e4SLinus Torvalds|	d3: x/x
8841da177e4SLinus Torvalds|	d4: LEN/Unchanged
8851da177e4SLinus Torvalds|	d5: ICTR:LAMBDA/LAMBDA:ICTR
8861da177e4SLinus Torvalds|	d6: ILOG/ILOG adjusted
8871da177e4SLinus Torvalds|	d7: k-factor/Unchanged
8881da177e4SLinus Torvalds|	a0: ptr to L_SCR1(a6)/Unchanged
8891da177e4SLinus Torvalds|	a1: ptr to PTENxx array/Unchanged
8901da177e4SLinus Torvalds|	a2: ptr to FP_SCR2(a6)/Unchanged
8911da177e4SLinus Torvalds|	fp0: float(ILOG)/Unchanged
8921da177e4SLinus Torvalds|	fp1: 10^ISCALE/Unchanged
8931da177e4SLinus Torvalds|	fp2: 10^LEN/Unchanged
8941da177e4SLinus Torvalds|	F_SCR1:BCD result with correct signs
8951da177e4SLinus Torvalds|	F_SCR2:ILOG/10^4
8961da177e4SLinus Torvalds|	L_SCR1:Exponent digits on return from binstr
8971da177e4SLinus Torvalds|	L_SCR2:first word of X packed/Unchanged
8981da177e4SLinus Torvalds
8991da177e4SLinus TorvaldsA16_st:
9001da177e4SLinus Torvalds	clrl	%d0		|clr d0 for collection of signs
9011da177e4SLinus Torvalds	andib	#0x0f,FP_SCR1(%a6) |clear first nibble of FP_SCR1
9021da177e4SLinus Torvalds	tstl	L_SCR2(%a6)	|check sign of original mantissa
9031da177e4SLinus Torvalds	bges	mant_p		|if pos, don't set SM
9041da177e4SLinus Torvalds	moveql	#2,%d0		|move 2 in to d0 for SM
9051da177e4SLinus Torvaldsmant_p:
9061da177e4SLinus Torvalds	tstl	%d6		|check sign of ILOG
9071da177e4SLinus Torvalds	bges	wr_sgn		|if pos, don't set SE
9081da177e4SLinus Torvalds	addql	#1,%d0		|set bit 0 in d0 for SE
9091da177e4SLinus Torvaldswr_sgn:
9101da177e4SLinus Torvalds	bfins	%d0,FP_SCR1(%a6){#0:#2} |insert SM and SE into FP_SCR1
9111da177e4SLinus Torvalds
9121da177e4SLinus Torvalds| Clean up and restore all registers used.
9131da177e4SLinus Torvalds
9141da177e4SLinus Torvalds	fmovel	#0,%FPSR		|clear possible inex2/ainex bits
9151da177e4SLinus Torvalds	fmovemx (%a7)+,%fp0-%fp2
9161da177e4SLinus Torvalds	moveml	(%a7)+,%d2-%d7/%a2
9171da177e4SLinus Torvalds	rts
9181da177e4SLinus Torvalds
9191da177e4SLinus Torvalds	|end
920