xref: /OK3568_Linux_fs/kernel/arch/m68k/fpsp040/bindec.S (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun|
2*4882a593Smuzhiyun|	bindec.sa 3.4 1/3/91
3*4882a593Smuzhiyun|
4*4882a593Smuzhiyun|	bindec
5*4882a593Smuzhiyun|
6*4882a593Smuzhiyun|	Description:
7*4882a593Smuzhiyun|		Converts an input in extended precision format
8*4882a593Smuzhiyun|		to bcd format.
9*4882a593Smuzhiyun|
10*4882a593Smuzhiyun|	Input:
11*4882a593Smuzhiyun|		a0 points to the input extended precision value
12*4882a593Smuzhiyun|		value in memory; d0 contains the k-factor sign-extended
13*4882a593Smuzhiyun|		to 32-bits.  The input may be either normalized,
14*4882a593Smuzhiyun|		unnormalized, or denormalized.
15*4882a593Smuzhiyun|
16*4882a593Smuzhiyun|	Output:	result in the FP_SCR1 space on the stack.
17*4882a593Smuzhiyun|
18*4882a593Smuzhiyun|	Saves and Modifies: D2-D7,A2,FP2
19*4882a593Smuzhiyun|
20*4882a593Smuzhiyun|	Algorithm:
21*4882a593Smuzhiyun|
22*4882a593Smuzhiyun|	A1.	Set RM and size ext;  Set SIGMA = sign of input.
23*4882a593Smuzhiyun|		The k-factor is saved for use in d7. Clear the
24*4882a593Smuzhiyun|		BINDEC_FLG for separating normalized/denormalized
25*4882a593Smuzhiyun|		input.  If input is unnormalized or denormalized,
26*4882a593Smuzhiyun|		normalize it.
27*4882a593Smuzhiyun|
28*4882a593Smuzhiyun|	A2.	Set X = abs(input).
29*4882a593Smuzhiyun|
30*4882a593Smuzhiyun|	A3.	Compute ILOG.
31*4882a593Smuzhiyun|		ILOG is the log base 10 of the input value.  It is
32*4882a593Smuzhiyun|		approximated by adding e + 0.f when the original
33*4882a593Smuzhiyun|		value is viewed as 2^^e * 1.f in extended precision.
34*4882a593Smuzhiyun|		This value is stored in d6.
35*4882a593Smuzhiyun|
36*4882a593Smuzhiyun|	A4.	Clr INEX bit.
37*4882a593Smuzhiyun|		The operation in A3 above may have set INEX2.
38*4882a593Smuzhiyun|
39*4882a593Smuzhiyun|	A5.	Set ICTR = 0;
40*4882a593Smuzhiyun|		ICTR is a flag used in A13.  It must be set before the
41*4882a593Smuzhiyun|		loop entry A6.
42*4882a593Smuzhiyun|
43*4882a593Smuzhiyun|	A6.	Calculate LEN.
44*4882a593Smuzhiyun|		LEN is the number of digits to be displayed.  The
45*4882a593Smuzhiyun|		k-factor can dictate either the total number of digits,
46*4882a593Smuzhiyun|		if it is a positive number, or the number of digits
47*4882a593Smuzhiyun|		after the decimal point which are to be included as
48*4882a593Smuzhiyun|		significant.  See the 68882 manual for examples.
49*4882a593Smuzhiyun|		If LEN is computed to be greater than 17, set OPERR in
50*4882a593Smuzhiyun|		USER_FPSR.  LEN is stored in d4.
51*4882a593Smuzhiyun|
52*4882a593Smuzhiyun|	A7.	Calculate SCALE.
53*4882a593Smuzhiyun|		SCALE is equal to 10^ISCALE, where ISCALE is the number
54*4882a593Smuzhiyun|		of decimal places needed to insure LEN integer digits
55*4882a593Smuzhiyun|		in the output before conversion to bcd. LAMBDA is the
56*4882a593Smuzhiyun|		sign of ISCALE, used in A9. Fp1 contains
57*4882a593Smuzhiyun|		10^^(abs(ISCALE)) using a rounding mode which is a
58*4882a593Smuzhiyun|		function of the original rounding mode and the signs
59*4882a593Smuzhiyun|		of ISCALE and X.  A table is given in the code.
60*4882a593Smuzhiyun|
61*4882a593Smuzhiyun|	A8.	Clr INEX; Force RZ.
62*4882a593Smuzhiyun|		The operation in A3 above may have set INEX2.
63*4882a593Smuzhiyun|		RZ mode is forced for the scaling operation to insure
64*4882a593Smuzhiyun|		only one rounding error.  The grs bits are collected in
65*4882a593Smuzhiyun|		the INEX flag for use in A10.
66*4882a593Smuzhiyun|
67*4882a593Smuzhiyun|	A9.	Scale X -> Y.
68*4882a593Smuzhiyun|		The mantissa is scaled to the desired number of
69*4882a593Smuzhiyun|		significant digits.  The excess digits are collected
70*4882a593Smuzhiyun|		in INEX2.
71*4882a593Smuzhiyun|
72*4882a593Smuzhiyun|	A10.	Or in INEX.
73*4882a593Smuzhiyun|		If INEX is set, round error occurred.  This is
74*4882a593Smuzhiyun|		compensated for by 'or-ing' in the INEX2 flag to
75*4882a593Smuzhiyun|		the lsb of Y.
76*4882a593Smuzhiyun|
77*4882a593Smuzhiyun|	A11.	Restore original FPCR; set size ext.
78*4882a593Smuzhiyun|		Perform FINT operation in the user's rounding mode.
79*4882a593Smuzhiyun|		Keep the size to extended.
80*4882a593Smuzhiyun|
81*4882a593Smuzhiyun|	A12.	Calculate YINT = FINT(Y) according to user's rounding
82*4882a593Smuzhiyun|		mode.  The FPSP routine sintd0 is used.  The output
83*4882a593Smuzhiyun|		is in fp0.
84*4882a593Smuzhiyun|
85*4882a593Smuzhiyun|	A13.	Check for LEN digits.
86*4882a593Smuzhiyun|		If the int operation results in more than LEN digits,
87*4882a593Smuzhiyun|		or less than LEN -1 digits, adjust ILOG and repeat from
88*4882a593Smuzhiyun|		A6.  This test occurs only on the first pass.  If the
89*4882a593Smuzhiyun|		result is exactly 10^LEN, decrement ILOG and divide
90*4882a593Smuzhiyun|		the mantissa by 10.
91*4882a593Smuzhiyun|
92*4882a593Smuzhiyun|	A14.	Convert the mantissa to bcd.
93*4882a593Smuzhiyun|		The binstr routine is used to convert the LEN digit
94*4882a593Smuzhiyun|		mantissa to bcd in memory.  The input to binstr is
95*4882a593Smuzhiyun|		to be a fraction; i.e. (mantissa)/10^LEN and adjusted
96*4882a593Smuzhiyun|		such that the decimal point is to the left of bit 63.
97*4882a593Smuzhiyun|		The bcd digits are stored in the correct position in
98*4882a593Smuzhiyun|		the final string area in memory.
99*4882a593Smuzhiyun|
100*4882a593Smuzhiyun|	A15.	Convert the exponent to bcd.
101*4882a593Smuzhiyun|		As in A14 above, the exp is converted to bcd and the
102*4882a593Smuzhiyun|		digits are stored in the final string.
103*4882a593Smuzhiyun|		Test the length of the final exponent string.  If the
104*4882a593Smuzhiyun|		length is 4, set operr.
105*4882a593Smuzhiyun|
106*4882a593Smuzhiyun|	A16.	Write sign bits to final string.
107*4882a593Smuzhiyun|
108*4882a593Smuzhiyun|	Implementation Notes:
109*4882a593Smuzhiyun|
110*4882a593Smuzhiyun|	The registers are used as follows:
111*4882a593Smuzhiyun|
112*4882a593Smuzhiyun|		d0: scratch; LEN input to binstr
113*4882a593Smuzhiyun|		d1: scratch
114*4882a593Smuzhiyun|		d2: upper 32-bits of mantissa for binstr
115*4882a593Smuzhiyun|		d3: scratch;lower 32-bits of mantissa for binstr
116*4882a593Smuzhiyun|		d4: LEN
117*4882a593Smuzhiyun|		d5: LAMBDA/ICTR
118*4882a593Smuzhiyun|		d6: ILOG
119*4882a593Smuzhiyun|		d7: k-factor
120*4882a593Smuzhiyun|		a0: ptr for original operand/final result
121*4882a593Smuzhiyun|		a1: scratch pointer
122*4882a593Smuzhiyun|		a2: pointer to FP_X; abs(original value) in ext
123*4882a593Smuzhiyun|		fp0: scratch
124*4882a593Smuzhiyun|		fp1: scratch
125*4882a593Smuzhiyun|		fp2: scratch
126*4882a593Smuzhiyun|		F_SCR1:
127*4882a593Smuzhiyun|		F_SCR2:
128*4882a593Smuzhiyun|		L_SCR1:
129*4882a593Smuzhiyun|		L_SCR2:
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun|		Copyright (C) Motorola, Inc. 1990
132*4882a593Smuzhiyun|			All Rights Reserved
133*4882a593Smuzhiyun|
134*4882a593Smuzhiyun|       For details on the license for this file, please see the
135*4882a593Smuzhiyun|       file, README, in this same directory.
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun|BINDEC    idnt    2,1 | Motorola 040 Floating Point Software Package
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun#include "fpsp.h"
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun	|section	8
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun| Constants in extended precision
144*4882a593SmuzhiyunLOG2:	.long	0x3FFD0000,0x9A209A84,0xFBCFF798,0x00000000
145*4882a593SmuzhiyunLOG2UP1:	.long	0x3FFD0000,0x9A209A84,0xFBCFF799,0x00000000
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun| Constants in single precision
148*4882a593SmuzhiyunFONE:	.long	0x3F800000,0x00000000,0x00000000,0x00000000
149*4882a593SmuzhiyunFTWO:	.long	0x40000000,0x00000000,0x00000000,0x00000000
150*4882a593SmuzhiyunFTEN:	.long	0x41200000,0x00000000,0x00000000,0x00000000
151*4882a593SmuzhiyunF4933:	.long	0x459A2800,0x00000000,0x00000000,0x00000000
152*4882a593Smuzhiyun
153*4882a593SmuzhiyunRBDTBL:	.byte	0,0,0,0
154*4882a593Smuzhiyun	.byte	3,3,2,2
155*4882a593Smuzhiyun	.byte	3,2,2,3
156*4882a593Smuzhiyun	.byte	2,3,3,2
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun	|xref	binstr
159*4882a593Smuzhiyun	|xref	sintdo
160*4882a593Smuzhiyun	|xref	ptenrn,ptenrm,ptenrp
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun	.global	bindec
163*4882a593Smuzhiyun	.global	sc_mul
164*4882a593Smuzhiyunbindec:
165*4882a593Smuzhiyun	moveml	%d2-%d7/%a2,-(%a7)
166*4882a593Smuzhiyun	fmovemx %fp0-%fp2,-(%a7)
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun| A1. Set RM and size ext. Set SIGMA = sign input;
169*4882a593Smuzhiyun|     The k-factor is saved for use in d7.  Clear BINDEC_FLG for
170*4882a593Smuzhiyun|     separating  normalized/denormalized input.  If the input
171*4882a593Smuzhiyun|     is a denormalized number, set the BINDEC_FLG memory word
172*4882a593Smuzhiyun|     to signal denorm.  If the input is unnormalized, normalize
173*4882a593Smuzhiyun|     the input and test for denormalized result.
174*4882a593Smuzhiyun|
175*4882a593Smuzhiyun	fmovel	#rm_mode,%FPCR	|set RM and ext
176*4882a593Smuzhiyun	movel	(%a0),L_SCR2(%a6)	|save exponent for sign check
177*4882a593Smuzhiyun	movel	%d0,%d7		|move k-factor to d7
178*4882a593Smuzhiyun	clrb	BINDEC_FLG(%a6)	|clr norm/denorm flag
179*4882a593Smuzhiyun	movew	STAG(%a6),%d0	|get stag
180*4882a593Smuzhiyun	andiw	#0xe000,%d0	|isolate stag bits
181*4882a593Smuzhiyun	beq	A2_str		|if zero, input is norm
182*4882a593Smuzhiyun|
183*4882a593Smuzhiyun| Normalize the denorm
184*4882a593Smuzhiyun|
185*4882a593Smuzhiyunun_de_norm:
186*4882a593Smuzhiyun	movew	(%a0),%d0
187*4882a593Smuzhiyun	andiw	#0x7fff,%d0	|strip sign of normalized exp
188*4882a593Smuzhiyun	movel	4(%a0),%d1
189*4882a593Smuzhiyun	movel	8(%a0),%d2
190*4882a593Smuzhiyunnorm_loop:
191*4882a593Smuzhiyun	subw	#1,%d0
192*4882a593Smuzhiyun	lsll	#1,%d2
193*4882a593Smuzhiyun	roxll	#1,%d1
194*4882a593Smuzhiyun	tstl	%d1
195*4882a593Smuzhiyun	bges	norm_loop
196*4882a593Smuzhiyun|
197*4882a593Smuzhiyun| Test if the normalized input is denormalized
198*4882a593Smuzhiyun|
199*4882a593Smuzhiyun	tstw	%d0
200*4882a593Smuzhiyun	bgts	pos_exp		|if greater than zero, it is a norm
201*4882a593Smuzhiyun	st	BINDEC_FLG(%a6)	|set flag for denorm
202*4882a593Smuzhiyunpos_exp:
203*4882a593Smuzhiyun	andiw	#0x7fff,%d0	|strip sign of normalized exp
204*4882a593Smuzhiyun	movew	%d0,(%a0)
205*4882a593Smuzhiyun	movel	%d1,4(%a0)
206*4882a593Smuzhiyun	movel	%d2,8(%a0)
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun| A2. Set X = abs(input).
209*4882a593Smuzhiyun|
210*4882a593SmuzhiyunA2_str:
211*4882a593Smuzhiyun	movel	(%a0),FP_SCR2(%a6) | move input to work space
212*4882a593Smuzhiyun	movel	4(%a0),FP_SCR2+4(%a6) | move input to work space
213*4882a593Smuzhiyun	movel	8(%a0),FP_SCR2+8(%a6) | move input to work space
214*4882a593Smuzhiyun	andil	#0x7fffffff,FP_SCR2(%a6) |create abs(X)
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun| A3. Compute ILOG.
217*4882a593Smuzhiyun|     ILOG is the log base 10 of the input value.  It is approx-
218*4882a593Smuzhiyun|     imated by adding e + 0.f when the original value is viewed
219*4882a593Smuzhiyun|     as 2^^e * 1.f in extended precision.  This value is stored
220*4882a593Smuzhiyun|     in d6.
221*4882a593Smuzhiyun|
222*4882a593Smuzhiyun| Register usage:
223*4882a593Smuzhiyun|	Input/Output
224*4882a593Smuzhiyun|	d0: k-factor/exponent
225*4882a593Smuzhiyun|	d2: x/x
226*4882a593Smuzhiyun|	d3: x/x
227*4882a593Smuzhiyun|	d4: x/x
228*4882a593Smuzhiyun|	d5: x/x
229*4882a593Smuzhiyun|	d6: x/ILOG
230*4882a593Smuzhiyun|	d7: k-factor/Unchanged
231*4882a593Smuzhiyun|	a0: ptr for original operand/final result
232*4882a593Smuzhiyun|	a1: x/x
233*4882a593Smuzhiyun|	a2: x/x
234*4882a593Smuzhiyun|	fp0: x/float(ILOG)
235*4882a593Smuzhiyun|	fp1: x/x
236*4882a593Smuzhiyun|	fp2: x/x
237*4882a593Smuzhiyun|	F_SCR1:x/x
238*4882a593Smuzhiyun|	F_SCR2:Abs(X)/Abs(X) with $3fff exponent
239*4882a593Smuzhiyun|	L_SCR1:x/x
240*4882a593Smuzhiyun|	L_SCR2:first word of X packed/Unchanged
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun	tstb	BINDEC_FLG(%a6)	|check for denorm
243*4882a593Smuzhiyun	beqs	A3_cont		|if clr, continue with norm
244*4882a593Smuzhiyun	movel	#-4933,%d6	|force ILOG = -4933
245*4882a593Smuzhiyun	bras	A4_str
246*4882a593SmuzhiyunA3_cont:
247*4882a593Smuzhiyun	movew	FP_SCR2(%a6),%d0	|move exp to d0
248*4882a593Smuzhiyun	movew	#0x3fff,FP_SCR2(%a6) |replace exponent with 0x3fff
249*4882a593Smuzhiyun	fmovex	FP_SCR2(%a6),%fp0	|now fp0 has 1.f
250*4882a593Smuzhiyun	subw	#0x3fff,%d0	|strip off bias
251*4882a593Smuzhiyun	faddw	%d0,%fp0		|add in exp
252*4882a593Smuzhiyun	fsubs	FONE,%fp0	|subtract off 1.0
253*4882a593Smuzhiyun	fbge	pos_res		|if pos, branch
254*4882a593Smuzhiyun	fmulx	LOG2UP1,%fp0	|if neg, mul by LOG2UP1
255*4882a593Smuzhiyun	fmovel	%fp0,%d6		|put ILOG in d6 as a lword
256*4882a593Smuzhiyun	bras	A4_str		|go move out ILOG
257*4882a593Smuzhiyunpos_res:
258*4882a593Smuzhiyun	fmulx	LOG2,%fp0	|if pos, mul by LOG2
259*4882a593Smuzhiyun	fmovel	%fp0,%d6		|put ILOG in d6 as a lword
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun
262*4882a593Smuzhiyun| A4. Clr INEX bit.
263*4882a593Smuzhiyun|     The operation in A3 above may have set INEX2.
264*4882a593Smuzhiyun
265*4882a593SmuzhiyunA4_str:
266*4882a593Smuzhiyun	fmovel	#0,%FPSR		|zero all of fpsr - nothing needed
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun| A5. Set ICTR = 0;
270*4882a593Smuzhiyun|     ICTR is a flag used in A13.  It must be set before the
271*4882a593Smuzhiyun|     loop entry A6. The lower word of d5 is used for ICTR.
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun	clrw	%d5		|clear ICTR
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun| A6. Calculate LEN.
277*4882a593Smuzhiyun|     LEN is the number of digits to be displayed.  The k-factor
278*4882a593Smuzhiyun|     can dictate either the total number of digits, if it is
279*4882a593Smuzhiyun|     a positive number, or the number of digits after the
280*4882a593Smuzhiyun|     original decimal point which are to be included as
281*4882a593Smuzhiyun|     significant.  See the 68882 manual for examples.
282*4882a593Smuzhiyun|     If LEN is computed to be greater than 17, set OPERR in
283*4882a593Smuzhiyun|     USER_FPSR.  LEN is stored in d4.
284*4882a593Smuzhiyun|
285*4882a593Smuzhiyun| Register usage:
286*4882a593Smuzhiyun|	Input/Output
287*4882a593Smuzhiyun|	d0: exponent/Unchanged
288*4882a593Smuzhiyun|	d2: x/x/scratch
289*4882a593Smuzhiyun|	d3: x/x
290*4882a593Smuzhiyun|	d4: exc picture/LEN
291*4882a593Smuzhiyun|	d5: ICTR/Unchanged
292*4882a593Smuzhiyun|	d6: ILOG/Unchanged
293*4882a593Smuzhiyun|	d7: k-factor/Unchanged
294*4882a593Smuzhiyun|	a0: ptr for original operand/final result
295*4882a593Smuzhiyun|	a1: x/x
296*4882a593Smuzhiyun|	a2: x/x
297*4882a593Smuzhiyun|	fp0: float(ILOG)/Unchanged
298*4882a593Smuzhiyun|	fp1: x/x
299*4882a593Smuzhiyun|	fp2: x/x
300*4882a593Smuzhiyun|	F_SCR1:x/x
301*4882a593Smuzhiyun|	F_SCR2:Abs(X) with $3fff exponent/Unchanged
302*4882a593Smuzhiyun|	L_SCR1:x/x
303*4882a593Smuzhiyun|	L_SCR2:first word of X packed/Unchanged
304*4882a593Smuzhiyun
305*4882a593SmuzhiyunA6_str:
306*4882a593Smuzhiyun	tstl	%d7		|branch on sign of k
307*4882a593Smuzhiyun	bles	k_neg		|if k <= 0, LEN = ILOG + 1 - k
308*4882a593Smuzhiyun	movel	%d7,%d4		|if k > 0, LEN = k
309*4882a593Smuzhiyun	bras	len_ck		|skip to LEN check
310*4882a593Smuzhiyunk_neg:
311*4882a593Smuzhiyun	movel	%d6,%d4		|first load ILOG to d4
312*4882a593Smuzhiyun	subl	%d7,%d4		|subtract off k
313*4882a593Smuzhiyun	addql	#1,%d4		|add in the 1
314*4882a593Smuzhiyunlen_ck:
315*4882a593Smuzhiyun	tstl	%d4		|LEN check: branch on sign of LEN
316*4882a593Smuzhiyun	bles	LEN_ng		|if neg, set LEN = 1
317*4882a593Smuzhiyun	cmpl	#17,%d4		|test if LEN > 17
318*4882a593Smuzhiyun	bles	A7_str		|if not, forget it
319*4882a593Smuzhiyun	movel	#17,%d4		|set max LEN = 17
320*4882a593Smuzhiyun	tstl	%d7		|if negative, never set OPERR
321*4882a593Smuzhiyun	bles	A7_str		|if positive, continue
322*4882a593Smuzhiyun	orl	#opaop_mask,USER_FPSR(%a6) |set OPERR & AIOP in USER_FPSR
323*4882a593Smuzhiyun	bras	A7_str		|finished here
324*4882a593SmuzhiyunLEN_ng:
325*4882a593Smuzhiyun	moveql	#1,%d4		|min LEN is 1
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun
328*4882a593Smuzhiyun| A7. Calculate SCALE.
329*4882a593Smuzhiyun|     SCALE is equal to 10^ISCALE, where ISCALE is the number
330*4882a593Smuzhiyun|     of decimal places needed to insure LEN integer digits
331*4882a593Smuzhiyun|     in the output before conversion to bcd. LAMBDA is the sign
332*4882a593Smuzhiyun|     of ISCALE, used in A9.  Fp1 contains 10^^(abs(ISCALE)) using
333*4882a593Smuzhiyun|     the rounding mode as given in the following table (see
334*4882a593Smuzhiyun|     Coonen, p. 7.23 as ref.; however, the SCALE variable is
335*4882a593Smuzhiyun|     of opposite sign in bindec.sa from Coonen).
336*4882a593Smuzhiyun|
337*4882a593Smuzhiyun|	Initial					USE
338*4882a593Smuzhiyun|	FPCR[6:5]	LAMBDA	SIGN(X)		FPCR[6:5]
339*4882a593Smuzhiyun|	----------------------------------------------
340*4882a593Smuzhiyun|	 RN	00	   0	   0		00/0	RN
341*4882a593Smuzhiyun|	 RN	00	   0	   1		00/0	RN
342*4882a593Smuzhiyun|	 RN	00	   1	   0		00/0	RN
343*4882a593Smuzhiyun|	 RN	00	   1	   1		00/0	RN
344*4882a593Smuzhiyun|	 RZ	01	   0	   0		11/3	RP
345*4882a593Smuzhiyun|	 RZ	01	   0	   1		11/3	RP
346*4882a593Smuzhiyun|	 RZ	01	   1	   0		10/2	RM
347*4882a593Smuzhiyun|	 RZ	01	   1	   1		10/2	RM
348*4882a593Smuzhiyun|	 RM	10	   0	   0		11/3	RP
349*4882a593Smuzhiyun|	 RM	10	   0	   1		10/2	RM
350*4882a593Smuzhiyun|	 RM	10	   1	   0		10/2	RM
351*4882a593Smuzhiyun|	 RM	10	   1	   1		11/3	RP
352*4882a593Smuzhiyun|	 RP	11	   0	   0		10/2	RM
353*4882a593Smuzhiyun|	 RP	11	   0	   1		11/3	RP
354*4882a593Smuzhiyun|	 RP	11	   1	   0		11/3	RP
355*4882a593Smuzhiyun|	 RP	11	   1	   1		10/2	RM
356*4882a593Smuzhiyun|
357*4882a593Smuzhiyun| Register usage:
358*4882a593Smuzhiyun|	Input/Output
359*4882a593Smuzhiyun|	d0: exponent/scratch - final is 0
360*4882a593Smuzhiyun|	d2: x/0 or 24 for A9
361*4882a593Smuzhiyun|	d3: x/scratch - offset ptr into PTENRM array
362*4882a593Smuzhiyun|	d4: LEN/Unchanged
363*4882a593Smuzhiyun|	d5: 0/ICTR:LAMBDA
364*4882a593Smuzhiyun|	d6: ILOG/ILOG or k if ((k<=0)&(ILOG<k))
365*4882a593Smuzhiyun|	d7: k-factor/Unchanged
366*4882a593Smuzhiyun|	a0: ptr for original operand/final result
367*4882a593Smuzhiyun|	a1: x/ptr to PTENRM array
368*4882a593Smuzhiyun|	a2: x/x
369*4882a593Smuzhiyun|	fp0: float(ILOG)/Unchanged
370*4882a593Smuzhiyun|	fp1: x/10^ISCALE
371*4882a593Smuzhiyun|	fp2: x/x
372*4882a593Smuzhiyun|	F_SCR1:x/x
373*4882a593Smuzhiyun|	F_SCR2:Abs(X) with $3fff exponent/Unchanged
374*4882a593Smuzhiyun|	L_SCR1:x/x
375*4882a593Smuzhiyun|	L_SCR2:first word of X packed/Unchanged
376*4882a593Smuzhiyun
377*4882a593SmuzhiyunA7_str:
378*4882a593Smuzhiyun	tstl	%d7		|test sign of k
379*4882a593Smuzhiyun	bgts	k_pos		|if pos and > 0, skip this
380*4882a593Smuzhiyun	cmpl	%d6,%d7		|test k - ILOG
381*4882a593Smuzhiyun	blts	k_pos		|if ILOG >= k, skip this
382*4882a593Smuzhiyun	movel	%d7,%d6		|if ((k<0) & (ILOG < k)) ILOG = k
383*4882a593Smuzhiyunk_pos:
384*4882a593Smuzhiyun	movel	%d6,%d0		|calc ILOG + 1 - LEN in d0
385*4882a593Smuzhiyun	addql	#1,%d0		|add the 1
386*4882a593Smuzhiyun	subl	%d4,%d0		|sub off LEN
387*4882a593Smuzhiyun	swap	%d5		|use upper word of d5 for LAMBDA
388*4882a593Smuzhiyun	clrw	%d5		|set it zero initially
389*4882a593Smuzhiyun	clrw	%d2		|set up d2 for very small case
390*4882a593Smuzhiyun	tstl	%d0		|test sign of ISCALE
391*4882a593Smuzhiyun	bges	iscale		|if pos, skip next inst
392*4882a593Smuzhiyun	addqw	#1,%d5		|if neg, set LAMBDA true
393*4882a593Smuzhiyun	cmpl	#0xffffecd4,%d0	|test iscale <= -4908
394*4882a593Smuzhiyun	bgts	no_inf		|if false, skip rest
395*4882a593Smuzhiyun	addil	#24,%d0		|add in 24 to iscale
396*4882a593Smuzhiyun	movel	#24,%d2		|put 24 in d2 for A9
397*4882a593Smuzhiyunno_inf:
398*4882a593Smuzhiyun	negl	%d0		|and take abs of ISCALE
399*4882a593Smuzhiyuniscale:
400*4882a593Smuzhiyun	fmoves	FONE,%fp1	|init fp1 to 1
401*4882a593Smuzhiyun	bfextu	USER_FPCR(%a6){#26:#2},%d1 |get initial rmode bits
402*4882a593Smuzhiyun	lslw	#1,%d1		|put them in bits 2:1
403*4882a593Smuzhiyun	addw	%d5,%d1		|add in LAMBDA
404*4882a593Smuzhiyun	lslw	#1,%d1		|put them in bits 3:1
405*4882a593Smuzhiyun	tstl	L_SCR2(%a6)	|test sign of original x
406*4882a593Smuzhiyun	bges	x_pos		|if pos, don't set bit 0
407*4882a593Smuzhiyun	addql	#1,%d1		|if neg, set bit 0
408*4882a593Smuzhiyunx_pos:
409*4882a593Smuzhiyun	leal	RBDTBL,%a2	|load rbdtbl base
410*4882a593Smuzhiyun	moveb	(%a2,%d1),%d3	|load d3 with new rmode
411*4882a593Smuzhiyun	lsll	#4,%d3		|put bits in proper position
412*4882a593Smuzhiyun	fmovel	%d3,%fpcr		|load bits into fpu
413*4882a593Smuzhiyun	lsrl	#4,%d3		|put bits in proper position
414*4882a593Smuzhiyun	tstb	%d3		|decode new rmode for pten table
415*4882a593Smuzhiyun	bnes	not_rn		|if zero, it is RN
416*4882a593Smuzhiyun	leal	PTENRN,%a1	|load a1 with RN table base
417*4882a593Smuzhiyun	bras	rmode		|exit decode
418*4882a593Smuzhiyunnot_rn:
419*4882a593Smuzhiyun	lsrb	#1,%d3		|get lsb in carry
420*4882a593Smuzhiyun	bccs	not_rp		|if carry clear, it is RM
421*4882a593Smuzhiyun	leal	PTENRP,%a1	|load a1 with RP table base
422*4882a593Smuzhiyun	bras	rmode		|exit decode
423*4882a593Smuzhiyunnot_rp:
424*4882a593Smuzhiyun	leal	PTENRM,%a1	|load a1 with RM table base
425*4882a593Smuzhiyunrmode:
426*4882a593Smuzhiyun	clrl	%d3		|clr table index
427*4882a593Smuzhiyune_loop:
428*4882a593Smuzhiyun	lsrl	#1,%d0		|shift next bit into carry
429*4882a593Smuzhiyun	bccs	e_next		|if zero, skip the mul
430*4882a593Smuzhiyun	fmulx	(%a1,%d3),%fp1	|mul by 10**(d3_bit_no)
431*4882a593Smuzhiyune_next:
432*4882a593Smuzhiyun	addl	#12,%d3		|inc d3 to next pwrten table entry
433*4882a593Smuzhiyun	tstl	%d0		|test if ISCALE is zero
434*4882a593Smuzhiyun	bnes	e_loop		|if not, loop
435*4882a593Smuzhiyun
436*4882a593Smuzhiyun
437*4882a593Smuzhiyun| A8. Clr INEX; Force RZ.
438*4882a593Smuzhiyun|     The operation in A3 above may have set INEX2.
439*4882a593Smuzhiyun|     RZ mode is forced for the scaling operation to insure
440*4882a593Smuzhiyun|     only one rounding error.  The grs bits are collected in
441*4882a593Smuzhiyun|     the INEX flag for use in A10.
442*4882a593Smuzhiyun|
443*4882a593Smuzhiyun| Register usage:
444*4882a593Smuzhiyun|	Input/Output
445*4882a593Smuzhiyun
446*4882a593Smuzhiyun	fmovel	#0,%FPSR		|clr INEX
447*4882a593Smuzhiyun	fmovel	#rz_mode,%FPCR	|set RZ rounding mode
448*4882a593Smuzhiyun
449*4882a593Smuzhiyun
450*4882a593Smuzhiyun| A9. Scale X -> Y.
451*4882a593Smuzhiyun|     The mantissa is scaled to the desired number of significant
452*4882a593Smuzhiyun|     digits.  The excess digits are collected in INEX2. If mul,
453*4882a593Smuzhiyun|     Check d2 for excess 10 exponential value.  If not zero,
454*4882a593Smuzhiyun|     the iscale value would have caused the pwrten calculation
455*4882a593Smuzhiyun|     to overflow.  Only a negative iscale can cause this, so
456*4882a593Smuzhiyun|     multiply by 10^(d2), which is now only allowed to be 24,
457*4882a593Smuzhiyun|     with a multiply by 10^8 and 10^16, which is exact since
458*4882a593Smuzhiyun|     10^24 is exact.  If the input was denormalized, we must
459*4882a593Smuzhiyun|     create a busy stack frame with the mul command and the
460*4882a593Smuzhiyun|     two operands, and allow the fpu to complete the multiply.
461*4882a593Smuzhiyun|
462*4882a593Smuzhiyun| Register usage:
463*4882a593Smuzhiyun|	Input/Output
464*4882a593Smuzhiyun|	d0: FPCR with RZ mode/Unchanged
465*4882a593Smuzhiyun|	d2: 0 or 24/unchanged
466*4882a593Smuzhiyun|	d3: x/x
467*4882a593Smuzhiyun|	d4: LEN/Unchanged
468*4882a593Smuzhiyun|	d5: ICTR:LAMBDA
469*4882a593Smuzhiyun|	d6: ILOG/Unchanged
470*4882a593Smuzhiyun|	d7: k-factor/Unchanged
471*4882a593Smuzhiyun|	a0: ptr for original operand/final result
472*4882a593Smuzhiyun|	a1: ptr to PTENRM array/Unchanged
473*4882a593Smuzhiyun|	a2: x/x
474*4882a593Smuzhiyun|	fp0: float(ILOG)/X adjusted for SCALE (Y)
475*4882a593Smuzhiyun|	fp1: 10^ISCALE/Unchanged
476*4882a593Smuzhiyun|	fp2: x/x
477*4882a593Smuzhiyun|	F_SCR1:x/x
478*4882a593Smuzhiyun|	F_SCR2:Abs(X) with $3fff exponent/Unchanged
479*4882a593Smuzhiyun|	L_SCR1:x/x
480*4882a593Smuzhiyun|	L_SCR2:first word of X packed/Unchanged
481*4882a593Smuzhiyun
482*4882a593SmuzhiyunA9_str:
483*4882a593Smuzhiyun	fmovex	(%a0),%fp0	|load X from memory
484*4882a593Smuzhiyun	fabsx	%fp0		|use abs(X)
485*4882a593Smuzhiyun	tstw	%d5		|LAMBDA is in lower word of d5
486*4882a593Smuzhiyun	bne	sc_mul		|if neg (LAMBDA = 1), scale by mul
487*4882a593Smuzhiyun	fdivx	%fp1,%fp0		|calculate X / SCALE -> Y to fp0
488*4882a593Smuzhiyun	bras	A10_st		|branch to A10
489*4882a593Smuzhiyun
490*4882a593Smuzhiyunsc_mul:
491*4882a593Smuzhiyun	tstb	BINDEC_FLG(%a6)	|check for denorm
492*4882a593Smuzhiyun	beqs	A9_norm		|if norm, continue with mul
493*4882a593Smuzhiyun	fmovemx %fp1-%fp1,-(%a7)	|load ETEMP with 10^ISCALE
494*4882a593Smuzhiyun	movel	8(%a0),-(%a7)	|load FPTEMP with input arg
495*4882a593Smuzhiyun	movel	4(%a0),-(%a7)
496*4882a593Smuzhiyun	movel	(%a0),-(%a7)
497*4882a593Smuzhiyun	movel	#18,%d3		|load count for busy stack
498*4882a593SmuzhiyunA9_loop:
499*4882a593Smuzhiyun	clrl	-(%a7)		|clear lword on stack
500*4882a593Smuzhiyun	dbf	%d3,A9_loop
501*4882a593Smuzhiyun	moveb	VER_TMP(%a6),(%a7) |write current version number
502*4882a593Smuzhiyun	moveb	#BUSY_SIZE-4,1(%a7) |write current busy size
503*4882a593Smuzhiyun	moveb	#0x10,0x44(%a7)	|set fcefpte[15] bit
504*4882a593Smuzhiyun	movew	#0x0023,0x40(%a7)	|load cmdreg1b with mul command
505*4882a593Smuzhiyun	moveb	#0xfe,0x8(%a7)	|load all 1s to cu savepc
506*4882a593Smuzhiyun	frestore (%a7)+		|restore frame to fpu for completion
507*4882a593Smuzhiyun	fmulx	36(%a1),%fp0	|multiply fp0 by 10^8
508*4882a593Smuzhiyun	fmulx	48(%a1),%fp0	|multiply fp0 by 10^16
509*4882a593Smuzhiyun	bras	A10_st
510*4882a593SmuzhiyunA9_norm:
511*4882a593Smuzhiyun	tstw	%d2		|test for small exp case
512*4882a593Smuzhiyun	beqs	A9_con		|if zero, continue as normal
513*4882a593Smuzhiyun	fmulx	36(%a1),%fp0	|multiply fp0 by 10^8
514*4882a593Smuzhiyun	fmulx	48(%a1),%fp0	|multiply fp0 by 10^16
515*4882a593SmuzhiyunA9_con:
516*4882a593Smuzhiyun	fmulx	%fp1,%fp0		|calculate X * SCALE -> Y to fp0
517*4882a593Smuzhiyun
518*4882a593Smuzhiyun
519*4882a593Smuzhiyun| A10. Or in INEX.
520*4882a593Smuzhiyun|      If INEX is set, round error occurred.  This is compensated
521*4882a593Smuzhiyun|      for by 'or-ing' in the INEX2 flag to the lsb of Y.
522*4882a593Smuzhiyun|
523*4882a593Smuzhiyun| Register usage:
524*4882a593Smuzhiyun|	Input/Output
525*4882a593Smuzhiyun|	d0: FPCR with RZ mode/FPSR with INEX2 isolated
526*4882a593Smuzhiyun|	d2: x/x
527*4882a593Smuzhiyun|	d3: x/x
528*4882a593Smuzhiyun|	d4: LEN/Unchanged
529*4882a593Smuzhiyun|	d5: ICTR:LAMBDA
530*4882a593Smuzhiyun|	d6: ILOG/Unchanged
531*4882a593Smuzhiyun|	d7: k-factor/Unchanged
532*4882a593Smuzhiyun|	a0: ptr for original operand/final result
533*4882a593Smuzhiyun|	a1: ptr to PTENxx array/Unchanged
534*4882a593Smuzhiyun|	a2: x/ptr to FP_SCR2(a6)
535*4882a593Smuzhiyun|	fp0: Y/Y with lsb adjusted
536*4882a593Smuzhiyun|	fp1: 10^ISCALE/Unchanged
537*4882a593Smuzhiyun|	fp2: x/x
538*4882a593Smuzhiyun
539*4882a593SmuzhiyunA10_st:
540*4882a593Smuzhiyun	fmovel	%FPSR,%d0		|get FPSR
541*4882a593Smuzhiyun	fmovex	%fp0,FP_SCR2(%a6)	|move Y to memory
542*4882a593Smuzhiyun	leal	FP_SCR2(%a6),%a2	|load a2 with ptr to FP_SCR2
543*4882a593Smuzhiyun	btstl	#9,%d0		|check if INEX2 set
544*4882a593Smuzhiyun	beqs	A11_st		|if clear, skip rest
545*4882a593Smuzhiyun	oril	#1,8(%a2)	|or in 1 to lsb of mantissa
546*4882a593Smuzhiyun	fmovex	FP_SCR2(%a6),%fp0	|write adjusted Y back to fpu
547*4882a593Smuzhiyun
548*4882a593Smuzhiyun
549*4882a593Smuzhiyun| A11. Restore original FPCR; set size ext.
550*4882a593Smuzhiyun|      Perform FINT operation in the user's rounding mode.  Keep
551*4882a593Smuzhiyun|      the size to extended.  The sintdo entry point in the sint
552*4882a593Smuzhiyun|      routine expects the FPCR value to be in USER_FPCR for
553*4882a593Smuzhiyun|      mode and precision.  The original FPCR is saved in L_SCR1.
554*4882a593Smuzhiyun
555*4882a593SmuzhiyunA11_st:
556*4882a593Smuzhiyun	movel	USER_FPCR(%a6),L_SCR1(%a6) |save it for later
557*4882a593Smuzhiyun	andil	#0x00000030,USER_FPCR(%a6) |set size to ext,
558*4882a593Smuzhiyun|					;block exceptions
559*4882a593Smuzhiyun
560*4882a593Smuzhiyun
561*4882a593Smuzhiyun| A12. Calculate YINT = FINT(Y) according to user's rounding mode.
562*4882a593Smuzhiyun|      The FPSP routine sintd0 is used.  The output is in fp0.
563*4882a593Smuzhiyun|
564*4882a593Smuzhiyun| Register usage:
565*4882a593Smuzhiyun|	Input/Output
566*4882a593Smuzhiyun|	d0: FPSR with AINEX cleared/FPCR with size set to ext
567*4882a593Smuzhiyun|	d2: x/x/scratch
568*4882a593Smuzhiyun|	d3: x/x
569*4882a593Smuzhiyun|	d4: LEN/Unchanged
570*4882a593Smuzhiyun|	d5: ICTR:LAMBDA/Unchanged
571*4882a593Smuzhiyun|	d6: ILOG/Unchanged
572*4882a593Smuzhiyun|	d7: k-factor/Unchanged
573*4882a593Smuzhiyun|	a0: ptr for original operand/src ptr for sintdo
574*4882a593Smuzhiyun|	a1: ptr to PTENxx array/Unchanged
575*4882a593Smuzhiyun|	a2: ptr to FP_SCR2(a6)/Unchanged
576*4882a593Smuzhiyun|	a6: temp pointer to FP_SCR2(a6) - orig value saved and restored
577*4882a593Smuzhiyun|	fp0: Y/YINT
578*4882a593Smuzhiyun|	fp1: 10^ISCALE/Unchanged
579*4882a593Smuzhiyun|	fp2: x/x
580*4882a593Smuzhiyun|	F_SCR1:x/x
581*4882a593Smuzhiyun|	F_SCR2:Y adjusted for inex/Y with original exponent
582*4882a593Smuzhiyun|	L_SCR1:x/original USER_FPCR
583*4882a593Smuzhiyun|	L_SCR2:first word of X packed/Unchanged
584*4882a593Smuzhiyun
585*4882a593SmuzhiyunA12_st:
586*4882a593Smuzhiyun	moveml	%d0-%d1/%a0-%a1,-(%a7)	|save regs used by sintd0
587*4882a593Smuzhiyun	movel	L_SCR1(%a6),-(%a7)
588*4882a593Smuzhiyun	movel	L_SCR2(%a6),-(%a7)
589*4882a593Smuzhiyun	leal	FP_SCR2(%a6),%a0		|a0 is ptr to F_SCR2(a6)
590*4882a593Smuzhiyun	fmovex	%fp0,(%a0)		|move Y to memory at FP_SCR2(a6)
591*4882a593Smuzhiyun	tstl	L_SCR2(%a6)		|test sign of original operand
592*4882a593Smuzhiyun	bges	do_fint			|if pos, use Y
593*4882a593Smuzhiyun	orl	#0x80000000,(%a0)		|if neg, use -Y
594*4882a593Smuzhiyundo_fint:
595*4882a593Smuzhiyun	movel	USER_FPSR(%a6),-(%a7)
596*4882a593Smuzhiyun	bsr	sintdo			|sint routine returns int in fp0
597*4882a593Smuzhiyun	moveb	(%a7),USER_FPSR(%a6)
598*4882a593Smuzhiyun	addl	#4,%a7
599*4882a593Smuzhiyun	movel	(%a7)+,L_SCR2(%a6)
600*4882a593Smuzhiyun	movel	(%a7)+,L_SCR1(%a6)
601*4882a593Smuzhiyun	moveml	(%a7)+,%d0-%d1/%a0-%a1	|restore regs used by sint
602*4882a593Smuzhiyun	movel	L_SCR2(%a6),FP_SCR2(%a6)	|restore original exponent
603*4882a593Smuzhiyun	movel	L_SCR1(%a6),USER_FPCR(%a6) |restore user's FPCR
604*4882a593Smuzhiyun
605*4882a593Smuzhiyun
606*4882a593Smuzhiyun| A13. Check for LEN digits.
607*4882a593Smuzhiyun|      If the int operation results in more than LEN digits,
608*4882a593Smuzhiyun|      or less than LEN -1 digits, adjust ILOG and repeat from
609*4882a593Smuzhiyun|      A6.  This test occurs only on the first pass.  If the
610*4882a593Smuzhiyun|      result is exactly 10^LEN, decrement ILOG and divide
611*4882a593Smuzhiyun|      the mantissa by 10.  The calculation of 10^LEN cannot
612*4882a593Smuzhiyun|      be inexact, since all powers of ten up to 10^27 are exact
613*4882a593Smuzhiyun|      in extended precision, so the use of a previous power-of-ten
614*4882a593Smuzhiyun|      table will introduce no error.
615*4882a593Smuzhiyun|
616*4882a593Smuzhiyun|
617*4882a593Smuzhiyun| Register usage:
618*4882a593Smuzhiyun|	Input/Output
619*4882a593Smuzhiyun|	d0: FPCR with size set to ext/scratch final = 0
620*4882a593Smuzhiyun|	d2: x/x
621*4882a593Smuzhiyun|	d3: x/scratch final = x
622*4882a593Smuzhiyun|	d4: LEN/LEN adjusted
623*4882a593Smuzhiyun|	d5: ICTR:LAMBDA/LAMBDA:ICTR
624*4882a593Smuzhiyun|	d6: ILOG/ILOG adjusted
625*4882a593Smuzhiyun|	d7: k-factor/Unchanged
626*4882a593Smuzhiyun|	a0: pointer into memory for packed bcd string formation
627*4882a593Smuzhiyun|	a1: ptr to PTENxx array/Unchanged
628*4882a593Smuzhiyun|	a2: ptr to FP_SCR2(a6)/Unchanged
629*4882a593Smuzhiyun|	fp0: int portion of Y/abs(YINT) adjusted
630*4882a593Smuzhiyun|	fp1: 10^ISCALE/Unchanged
631*4882a593Smuzhiyun|	fp2: x/10^LEN
632*4882a593Smuzhiyun|	F_SCR1:x/x
633*4882a593Smuzhiyun|	F_SCR2:Y with original exponent/Unchanged
634*4882a593Smuzhiyun|	L_SCR1:original USER_FPCR/Unchanged
635*4882a593Smuzhiyun|	L_SCR2:first word of X packed/Unchanged
636*4882a593Smuzhiyun
637*4882a593SmuzhiyunA13_st:
638*4882a593Smuzhiyun	swap	%d5		|put ICTR in lower word of d5
639*4882a593Smuzhiyun	tstw	%d5		|check if ICTR = 0
640*4882a593Smuzhiyun	bne	not_zr		|if non-zero, go to second test
641*4882a593Smuzhiyun|
642*4882a593Smuzhiyun| Compute 10^(LEN-1)
643*4882a593Smuzhiyun|
644*4882a593Smuzhiyun	fmoves	FONE,%fp2	|init fp2 to 1.0
645*4882a593Smuzhiyun	movel	%d4,%d0		|put LEN in d0
646*4882a593Smuzhiyun	subql	#1,%d0		|d0 = LEN -1
647*4882a593Smuzhiyun	clrl	%d3		|clr table index
648*4882a593Smuzhiyunl_loop:
649*4882a593Smuzhiyun	lsrl	#1,%d0		|shift next bit into carry
650*4882a593Smuzhiyun	bccs	l_next		|if zero, skip the mul
651*4882a593Smuzhiyun	fmulx	(%a1,%d3),%fp2	|mul by 10**(d3_bit_no)
652*4882a593Smuzhiyunl_next:
653*4882a593Smuzhiyun	addl	#12,%d3		|inc d3 to next pwrten table entry
654*4882a593Smuzhiyun	tstl	%d0		|test if LEN is zero
655*4882a593Smuzhiyun	bnes	l_loop		|if not, loop
656*4882a593Smuzhiyun|
657*4882a593Smuzhiyun| 10^LEN-1 is computed for this test and A14.  If the input was
658*4882a593Smuzhiyun| denormalized, check only the case in which YINT > 10^LEN.
659*4882a593Smuzhiyun|
660*4882a593Smuzhiyun	tstb	BINDEC_FLG(%a6)	|check if input was norm
661*4882a593Smuzhiyun	beqs	A13_con		|if norm, continue with checking
662*4882a593Smuzhiyun	fabsx	%fp0		|take abs of YINT
663*4882a593Smuzhiyun	bra	test_2
664*4882a593Smuzhiyun|
665*4882a593Smuzhiyun| Compare abs(YINT) to 10^(LEN-1) and 10^LEN
666*4882a593Smuzhiyun|
667*4882a593SmuzhiyunA13_con:
668*4882a593Smuzhiyun	fabsx	%fp0		|take abs of YINT
669*4882a593Smuzhiyun	fcmpx	%fp2,%fp0		|compare abs(YINT) with 10^(LEN-1)
670*4882a593Smuzhiyun	fbge	test_2		|if greater, do next test
671*4882a593Smuzhiyun	subql	#1,%d6		|subtract 1 from ILOG
672*4882a593Smuzhiyun	movew	#1,%d5		|set ICTR
673*4882a593Smuzhiyun	fmovel	#rm_mode,%FPCR	|set rmode to RM
674*4882a593Smuzhiyun	fmuls	FTEN,%fp2	|compute 10^LEN
675*4882a593Smuzhiyun	bra	A6_str		|return to A6 and recompute YINT
676*4882a593Smuzhiyuntest_2:
677*4882a593Smuzhiyun	fmuls	FTEN,%fp2	|compute 10^LEN
678*4882a593Smuzhiyun	fcmpx	%fp2,%fp0		|compare abs(YINT) with 10^LEN
679*4882a593Smuzhiyun	fblt	A14_st		|if less, all is ok, go to A14
680*4882a593Smuzhiyun	fbgt	fix_ex		|if greater, fix and redo
681*4882a593Smuzhiyun	fdivs	FTEN,%fp0	|if equal, divide by 10
682*4882a593Smuzhiyun	addql	#1,%d6		| and inc ILOG
683*4882a593Smuzhiyun	bras	A14_st		| and continue elsewhere
684*4882a593Smuzhiyunfix_ex:
685*4882a593Smuzhiyun	addql	#1,%d6		|increment ILOG by 1
686*4882a593Smuzhiyun	movew	#1,%d5		|set ICTR
687*4882a593Smuzhiyun	fmovel	#rm_mode,%FPCR	|set rmode to RM
688*4882a593Smuzhiyun	bra	A6_str		|return to A6 and recompute YINT
689*4882a593Smuzhiyun|
690*4882a593Smuzhiyun| Since ICTR <> 0, we have already been through one adjustment,
691*4882a593Smuzhiyun| and shouldn't have another; this is to check if abs(YINT) = 10^LEN
692*4882a593Smuzhiyun| 10^LEN is again computed using whatever table is in a1 since the
693*4882a593Smuzhiyun| value calculated cannot be inexact.
694*4882a593Smuzhiyun|
695*4882a593Smuzhiyunnot_zr:
696*4882a593Smuzhiyun	fmoves	FONE,%fp2	|init fp2 to 1.0
697*4882a593Smuzhiyun	movel	%d4,%d0		|put LEN in d0
698*4882a593Smuzhiyun	clrl	%d3		|clr table index
699*4882a593Smuzhiyunz_loop:
700*4882a593Smuzhiyun	lsrl	#1,%d0		|shift next bit into carry
701*4882a593Smuzhiyun	bccs	z_next		|if zero, skip the mul
702*4882a593Smuzhiyun	fmulx	(%a1,%d3),%fp2	|mul by 10**(d3_bit_no)
703*4882a593Smuzhiyunz_next:
704*4882a593Smuzhiyun	addl	#12,%d3		|inc d3 to next pwrten table entry
705*4882a593Smuzhiyun	tstl	%d0		|test if LEN is zero
706*4882a593Smuzhiyun	bnes	z_loop		|if not, loop
707*4882a593Smuzhiyun	fabsx	%fp0		|get abs(YINT)
708*4882a593Smuzhiyun	fcmpx	%fp2,%fp0		|check if abs(YINT) = 10^LEN
709*4882a593Smuzhiyun	fbne	A14_st		|if not, skip this
710*4882a593Smuzhiyun	fdivs	FTEN,%fp0	|divide abs(YINT) by 10
711*4882a593Smuzhiyun	addql	#1,%d6		|and inc ILOG by 1
712*4882a593Smuzhiyun	addql	#1,%d4		| and inc LEN
713*4882a593Smuzhiyun	fmuls	FTEN,%fp2	| if LEN++, the get 10^^LEN
714*4882a593Smuzhiyun
715*4882a593Smuzhiyun
716*4882a593Smuzhiyun| A14. Convert the mantissa to bcd.
717*4882a593Smuzhiyun|      The binstr routine is used to convert the LEN digit
718*4882a593Smuzhiyun|      mantissa to bcd in memory.  The input to binstr is
719*4882a593Smuzhiyun|      to be a fraction; i.e. (mantissa)/10^LEN and adjusted
720*4882a593Smuzhiyun|      such that the decimal point is to the left of bit 63.
721*4882a593Smuzhiyun|      The bcd digits are stored in the correct position in
722*4882a593Smuzhiyun|      the final string area in memory.
723*4882a593Smuzhiyun|
724*4882a593Smuzhiyun|
725*4882a593Smuzhiyun| Register usage:
726*4882a593Smuzhiyun|	Input/Output
727*4882a593Smuzhiyun|	d0: x/LEN call to binstr - final is 0
728*4882a593Smuzhiyun|	d1: x/0
729*4882a593Smuzhiyun|	d2: x/ms 32-bits of mant of abs(YINT)
730*4882a593Smuzhiyun|	d3: x/ls 32-bits of mant of abs(YINT)
731*4882a593Smuzhiyun|	d4: LEN/Unchanged
732*4882a593Smuzhiyun|	d5: ICTR:LAMBDA/LAMBDA:ICTR
733*4882a593Smuzhiyun|	d6: ILOG
734*4882a593Smuzhiyun|	d7: k-factor/Unchanged
735*4882a593Smuzhiyun|	a0: pointer into memory for packed bcd string formation
736*4882a593Smuzhiyun|	    /ptr to first mantissa byte in result string
737*4882a593Smuzhiyun|	a1: ptr to PTENxx array/Unchanged
738*4882a593Smuzhiyun|	a2: ptr to FP_SCR2(a6)/Unchanged
739*4882a593Smuzhiyun|	fp0: int portion of Y/abs(YINT) adjusted
740*4882a593Smuzhiyun|	fp1: 10^ISCALE/Unchanged
741*4882a593Smuzhiyun|	fp2: 10^LEN/Unchanged
742*4882a593Smuzhiyun|	F_SCR1:x/Work area for final result
743*4882a593Smuzhiyun|	F_SCR2:Y with original exponent/Unchanged
744*4882a593Smuzhiyun|	L_SCR1:original USER_FPCR/Unchanged
745*4882a593Smuzhiyun|	L_SCR2:first word of X packed/Unchanged
746*4882a593Smuzhiyun
747*4882a593SmuzhiyunA14_st:
748*4882a593Smuzhiyun	fmovel	#rz_mode,%FPCR	|force rz for conversion
749*4882a593Smuzhiyun	fdivx	%fp2,%fp0		|divide abs(YINT) by 10^LEN
750*4882a593Smuzhiyun	leal	FP_SCR1(%a6),%a0
751*4882a593Smuzhiyun	fmovex	%fp0,(%a0)	|move abs(YINT)/10^LEN to memory
752*4882a593Smuzhiyun	movel	4(%a0),%d2	|move 2nd word of FP_RES to d2
753*4882a593Smuzhiyun	movel	8(%a0),%d3	|move 3rd word of FP_RES to d3
754*4882a593Smuzhiyun	clrl	4(%a0)		|zero word 2 of FP_RES
755*4882a593Smuzhiyun	clrl	8(%a0)		|zero word 3 of FP_RES
756*4882a593Smuzhiyun	movel	(%a0),%d0		|move exponent to d0
757*4882a593Smuzhiyun	swap	%d0		|put exponent in lower word
758*4882a593Smuzhiyun	beqs	no_sft		|if zero, don't shift
759*4882a593Smuzhiyun	subil	#0x3ffd,%d0	|sub bias less 2 to make fract
760*4882a593Smuzhiyun	tstl	%d0		|check if > 1
761*4882a593Smuzhiyun	bgts	no_sft		|if so, don't shift
762*4882a593Smuzhiyun	negl	%d0		|make exp positive
763*4882a593Smuzhiyunm_loop:
764*4882a593Smuzhiyun	lsrl	#1,%d2		|shift d2:d3 right, add 0s
765*4882a593Smuzhiyun	roxrl	#1,%d3		|the number of places
766*4882a593Smuzhiyun	dbf	%d0,m_loop	|given in d0
767*4882a593Smuzhiyunno_sft:
768*4882a593Smuzhiyun	tstl	%d2		|check for mantissa of zero
769*4882a593Smuzhiyun	bnes	no_zr		|if not, go on
770*4882a593Smuzhiyun	tstl	%d3		|continue zero check
771*4882a593Smuzhiyun	beqs	zer_m		|if zero, go directly to binstr
772*4882a593Smuzhiyunno_zr:
773*4882a593Smuzhiyun	clrl	%d1		|put zero in d1 for addx
774*4882a593Smuzhiyun	addil	#0x00000080,%d3	|inc at bit 7
775*4882a593Smuzhiyun	addxl	%d1,%d2		|continue inc
776*4882a593Smuzhiyun	andil	#0xffffff80,%d3	|strip off lsb not used by 882
777*4882a593Smuzhiyunzer_m:
778*4882a593Smuzhiyun	movel	%d4,%d0		|put LEN in d0 for binstr call
779*4882a593Smuzhiyun	addql	#3,%a0		|a0 points to M16 byte in result
780*4882a593Smuzhiyun	bsr	binstr		|call binstr to convert mant
781*4882a593Smuzhiyun
782*4882a593Smuzhiyun
783*4882a593Smuzhiyun| A15. Convert the exponent to bcd.
784*4882a593Smuzhiyun|      As in A14 above, the exp is converted to bcd and the
785*4882a593Smuzhiyun|      digits are stored in the final string.
786*4882a593Smuzhiyun|
787*4882a593Smuzhiyun|      Digits are stored in L_SCR1(a6) on return from BINDEC as:
788*4882a593Smuzhiyun|
789*4882a593Smuzhiyun|	 32               16 15                0
790*4882a593Smuzhiyun|	-----------------------------------------
791*4882a593Smuzhiyun|	|  0 | e3 | e2 | e1 | e4 |  X |  X |  X |
792*4882a593Smuzhiyun|	-----------------------------------------
793*4882a593Smuzhiyun|
794*4882a593Smuzhiyun| And are moved into their proper places in FP_SCR1.  If digit e4
795*4882a593Smuzhiyun| is non-zero, OPERR is signaled.  In all cases, all 4 digits are
796*4882a593Smuzhiyun| written as specified in the 881/882 manual for packed decimal.
797*4882a593Smuzhiyun|
798*4882a593Smuzhiyun| Register usage:
799*4882a593Smuzhiyun|	Input/Output
800*4882a593Smuzhiyun|	d0: x/LEN call to binstr - final is 0
801*4882a593Smuzhiyun|	d1: x/scratch (0);shift count for final exponent packing
802*4882a593Smuzhiyun|	d2: x/ms 32-bits of exp fraction/scratch
803*4882a593Smuzhiyun|	d3: x/ls 32-bits of exp fraction
804*4882a593Smuzhiyun|	d4: LEN/Unchanged
805*4882a593Smuzhiyun|	d5: ICTR:LAMBDA/LAMBDA:ICTR
806*4882a593Smuzhiyun|	d6: ILOG
807*4882a593Smuzhiyun|	d7: k-factor/Unchanged
808*4882a593Smuzhiyun|	a0: ptr to result string/ptr to L_SCR1(a6)
809*4882a593Smuzhiyun|	a1: ptr to PTENxx array/Unchanged
810*4882a593Smuzhiyun|	a2: ptr to FP_SCR2(a6)/Unchanged
811*4882a593Smuzhiyun|	fp0: abs(YINT) adjusted/float(ILOG)
812*4882a593Smuzhiyun|	fp1: 10^ISCALE/Unchanged
813*4882a593Smuzhiyun|	fp2: 10^LEN/Unchanged
814*4882a593Smuzhiyun|	F_SCR1:Work area for final result/BCD result
815*4882a593Smuzhiyun|	F_SCR2:Y with original exponent/ILOG/10^4
816*4882a593Smuzhiyun|	L_SCR1:original USER_FPCR/Exponent digits on return from binstr
817*4882a593Smuzhiyun|	L_SCR2:first word of X packed/Unchanged
818*4882a593Smuzhiyun
819*4882a593SmuzhiyunA15_st:
820*4882a593Smuzhiyun	tstb	BINDEC_FLG(%a6)	|check for denorm
821*4882a593Smuzhiyun	beqs	not_denorm
822*4882a593Smuzhiyun	ftstx	%fp0		|test for zero
823*4882a593Smuzhiyun	fbeq	den_zero	|if zero, use k-factor or 4933
824*4882a593Smuzhiyun	fmovel	%d6,%fp0		|float ILOG
825*4882a593Smuzhiyun	fabsx	%fp0		|get abs of ILOG
826*4882a593Smuzhiyun	bras	convrt
827*4882a593Smuzhiyunden_zero:
828*4882a593Smuzhiyun	tstl	%d7		|check sign of the k-factor
829*4882a593Smuzhiyun	blts	use_ilog	|if negative, use ILOG
830*4882a593Smuzhiyun	fmoves	F4933,%fp0	|force exponent to 4933
831*4882a593Smuzhiyun	bras	convrt		|do it
832*4882a593Smuzhiyunuse_ilog:
833*4882a593Smuzhiyun	fmovel	%d6,%fp0		|float ILOG
834*4882a593Smuzhiyun	fabsx	%fp0		|get abs of ILOG
835*4882a593Smuzhiyun	bras	convrt
836*4882a593Smuzhiyunnot_denorm:
837*4882a593Smuzhiyun	ftstx	%fp0		|test for zero
838*4882a593Smuzhiyun	fbne	not_zero	|if zero, force exponent
839*4882a593Smuzhiyun	fmoves	FONE,%fp0	|force exponent to 1
840*4882a593Smuzhiyun	bras	convrt		|do it
841*4882a593Smuzhiyunnot_zero:
842*4882a593Smuzhiyun	fmovel	%d6,%fp0		|float ILOG
843*4882a593Smuzhiyun	fabsx	%fp0		|get abs of ILOG
844*4882a593Smuzhiyunconvrt:
845*4882a593Smuzhiyun	fdivx	24(%a1),%fp0	|compute ILOG/10^4
846*4882a593Smuzhiyun	fmovex	%fp0,FP_SCR2(%a6)	|store fp0 in memory
847*4882a593Smuzhiyun	movel	4(%a2),%d2	|move word 2 to d2
848*4882a593Smuzhiyun	movel	8(%a2),%d3	|move word 3 to d3
849*4882a593Smuzhiyun	movew	(%a2),%d0		|move exp to d0
850*4882a593Smuzhiyun	beqs	x_loop_fin	|if zero, skip the shift
851*4882a593Smuzhiyun	subiw	#0x3ffd,%d0	|subtract off bias
852*4882a593Smuzhiyun	negw	%d0		|make exp positive
853*4882a593Smuzhiyunx_loop:
854*4882a593Smuzhiyun	lsrl	#1,%d2		|shift d2:d3 right
855*4882a593Smuzhiyun	roxrl	#1,%d3		|the number of places
856*4882a593Smuzhiyun	dbf	%d0,x_loop	|given in d0
857*4882a593Smuzhiyunx_loop_fin:
858*4882a593Smuzhiyun	clrl	%d1		|put zero in d1 for addx
859*4882a593Smuzhiyun	addil	#0x00000080,%d3	|inc at bit 6
860*4882a593Smuzhiyun	addxl	%d1,%d2		|continue inc
861*4882a593Smuzhiyun	andil	#0xffffff80,%d3	|strip off lsb not used by 882
862*4882a593Smuzhiyun	movel	#4,%d0		|put 4 in d0 for binstr call
863*4882a593Smuzhiyun	leal	L_SCR1(%a6),%a0	|a0 is ptr to L_SCR1 for exp digits
864*4882a593Smuzhiyun	bsr	binstr		|call binstr to convert exp
865*4882a593Smuzhiyun	movel	L_SCR1(%a6),%d0	|load L_SCR1 lword to d0
866*4882a593Smuzhiyun	movel	#12,%d1		|use d1 for shift count
867*4882a593Smuzhiyun	lsrl	%d1,%d0		|shift d0 right by 12
868*4882a593Smuzhiyun	bfins	%d0,FP_SCR1(%a6){#4:#12} |put e3:e2:e1 in FP_SCR1
869*4882a593Smuzhiyun	lsrl	%d1,%d0		|shift d0 right by 12
870*4882a593Smuzhiyun	bfins	%d0,FP_SCR1(%a6){#16:#4} |put e4 in FP_SCR1
871*4882a593Smuzhiyun	tstb	%d0		|check if e4 is zero
872*4882a593Smuzhiyun	beqs	A16_st		|if zero, skip rest
873*4882a593Smuzhiyun	orl	#opaop_mask,USER_FPSR(%a6) |set OPERR & AIOP in USER_FPSR
874*4882a593Smuzhiyun
875*4882a593Smuzhiyun
876*4882a593Smuzhiyun| A16. Write sign bits to final string.
877*4882a593Smuzhiyun|	   Sigma is bit 31 of initial value; RHO is bit 31 of d6 (ILOG).
878*4882a593Smuzhiyun|
879*4882a593Smuzhiyun| Register usage:
880*4882a593Smuzhiyun|	Input/Output
881*4882a593Smuzhiyun|	d0: x/scratch - final is x
882*4882a593Smuzhiyun|	d2: x/x
883*4882a593Smuzhiyun|	d3: x/x
884*4882a593Smuzhiyun|	d4: LEN/Unchanged
885*4882a593Smuzhiyun|	d5: ICTR:LAMBDA/LAMBDA:ICTR
886*4882a593Smuzhiyun|	d6: ILOG/ILOG adjusted
887*4882a593Smuzhiyun|	d7: k-factor/Unchanged
888*4882a593Smuzhiyun|	a0: ptr to L_SCR1(a6)/Unchanged
889*4882a593Smuzhiyun|	a1: ptr to PTENxx array/Unchanged
890*4882a593Smuzhiyun|	a2: ptr to FP_SCR2(a6)/Unchanged
891*4882a593Smuzhiyun|	fp0: float(ILOG)/Unchanged
892*4882a593Smuzhiyun|	fp1: 10^ISCALE/Unchanged
893*4882a593Smuzhiyun|	fp2: 10^LEN/Unchanged
894*4882a593Smuzhiyun|	F_SCR1:BCD result with correct signs
895*4882a593Smuzhiyun|	F_SCR2:ILOG/10^4
896*4882a593Smuzhiyun|	L_SCR1:Exponent digits on return from binstr
897*4882a593Smuzhiyun|	L_SCR2:first word of X packed/Unchanged
898*4882a593Smuzhiyun
899*4882a593SmuzhiyunA16_st:
900*4882a593Smuzhiyun	clrl	%d0		|clr d0 for collection of signs
901*4882a593Smuzhiyun	andib	#0x0f,FP_SCR1(%a6) |clear first nibble of FP_SCR1
902*4882a593Smuzhiyun	tstl	L_SCR2(%a6)	|check sign of original mantissa
903*4882a593Smuzhiyun	bges	mant_p		|if pos, don't set SM
904*4882a593Smuzhiyun	moveql	#2,%d0		|move 2 in to d0 for SM
905*4882a593Smuzhiyunmant_p:
906*4882a593Smuzhiyun	tstl	%d6		|check sign of ILOG
907*4882a593Smuzhiyun	bges	wr_sgn		|if pos, don't set SE
908*4882a593Smuzhiyun	addql	#1,%d0		|set bit 0 in d0 for SE
909*4882a593Smuzhiyunwr_sgn:
910*4882a593Smuzhiyun	bfins	%d0,FP_SCR1(%a6){#0:#2} |insert SM and SE into FP_SCR1
911*4882a593Smuzhiyun
912*4882a593Smuzhiyun| Clean up and restore all registers used.
913*4882a593Smuzhiyun
914*4882a593Smuzhiyun	fmovel	#0,%FPSR		|clear possible inex2/ainex bits
915*4882a593Smuzhiyun	fmovemx (%a7)+,%fp0-%fp2
916*4882a593Smuzhiyun	moveml	(%a7)+,%d2-%d7/%a2
917*4882a593Smuzhiyun	rts
918*4882a593Smuzhiyun
919*4882a593Smuzhiyun	|end
920