162306a36Sopenharmony_ci|
262306a36Sopenharmony_ci|	decbin.sa 3.3 12/19/90
362306a36Sopenharmony_ci|
462306a36Sopenharmony_ci|	Description: Converts normalized packed bcd value pointed to by
562306a36Sopenharmony_ci|	register A6 to extended-precision value in FP0.
662306a36Sopenharmony_ci|
762306a36Sopenharmony_ci|	Input: Normalized packed bcd value in ETEMP(a6).
862306a36Sopenharmony_ci|
962306a36Sopenharmony_ci|	Output:	Exact floating-point representation of the packed bcd value.
1062306a36Sopenharmony_ci|
1162306a36Sopenharmony_ci|	Saves and Modifies: D2-D5
1262306a36Sopenharmony_ci|
1362306a36Sopenharmony_ci|	Speed: The program decbin takes ??? cycles to execute.
1462306a36Sopenharmony_ci|
1562306a36Sopenharmony_ci|	Object Size:
1662306a36Sopenharmony_ci|
1762306a36Sopenharmony_ci|	External Reference(s): None.
1862306a36Sopenharmony_ci|
1962306a36Sopenharmony_ci|	Algorithm:
2062306a36Sopenharmony_ci|	Expected is a normal bcd (i.e. non-exceptional; all inf, zero,
2162306a36Sopenharmony_ci|	and NaN operands are dispatched without entering this routine)
2262306a36Sopenharmony_ci|	value in 68881/882 format at location ETEMP(A6).
2362306a36Sopenharmony_ci|
2462306a36Sopenharmony_ci|	A1.	Convert the bcd exponent to binary by successive adds and muls.
2562306a36Sopenharmony_ci|	Set the sign according to SE. Subtract 16 to compensate
2662306a36Sopenharmony_ci|	for the mantissa which is to be interpreted as 17 integer
2762306a36Sopenharmony_ci|	digits, rather than 1 integer and 16 fraction digits.
2862306a36Sopenharmony_ci|	Note: this operation can never overflow.
2962306a36Sopenharmony_ci|
3062306a36Sopenharmony_ci|	A2. Convert the bcd mantissa to binary by successive
3162306a36Sopenharmony_ci|	adds and muls in FP0. Set the sign according to SM.
3262306a36Sopenharmony_ci|	The mantissa digits will be converted with the decimal point
3362306a36Sopenharmony_ci|	assumed following the least-significant digit.
3462306a36Sopenharmony_ci|	Note: this operation can never overflow.
3562306a36Sopenharmony_ci|
3662306a36Sopenharmony_ci|	A3. Count the number of leading/trailing zeros in the
3762306a36Sopenharmony_ci|	bcd string.  If SE is positive, count the leading zeros;
3862306a36Sopenharmony_ci|	if negative, count the trailing zeros.  Set the adjusted
3962306a36Sopenharmony_ci|	exponent equal to the exponent from A1 and the zero count
4062306a36Sopenharmony_ci|	added if SM = 1 and subtracted if SM = 0.  Scale the
4162306a36Sopenharmony_ci|	mantissa the equivalent of forcing in the bcd value:
4262306a36Sopenharmony_ci|
4362306a36Sopenharmony_ci|	SM = 0	a non-zero digit in the integer position
4462306a36Sopenharmony_ci|	SM = 1	a non-zero digit in Mant0, lsd of the fraction
4562306a36Sopenharmony_ci|
4662306a36Sopenharmony_ci|	this will insure that any value, regardless of its
4762306a36Sopenharmony_ci|	representation (ex. 0.1E2, 1E1, 10E0, 100E-1), is converted
4862306a36Sopenharmony_ci|	consistently.
4962306a36Sopenharmony_ci|
5062306a36Sopenharmony_ci|	A4. Calculate the factor 10^exp in FP1 using a table of
5162306a36Sopenharmony_ci|	10^(2^n) values.  To reduce the error in forming factors
5262306a36Sopenharmony_ci|	greater than 10^27, a directed rounding scheme is used with
5362306a36Sopenharmony_ci|	tables rounded to RN, RM, and RP, according to the table
5462306a36Sopenharmony_ci|	in the comments of the pwrten section.
5562306a36Sopenharmony_ci|
5662306a36Sopenharmony_ci|	A5. Form the final binary number by scaling the mantissa by
5762306a36Sopenharmony_ci|	the exponent factor.  This is done by multiplying the
5862306a36Sopenharmony_ci|	mantissa in FP0 by the factor in FP1 if the adjusted
5962306a36Sopenharmony_ci|	exponent sign is positive, and dividing FP0 by FP1 if
6062306a36Sopenharmony_ci|	it is negative.
6162306a36Sopenharmony_ci|
6262306a36Sopenharmony_ci|	Clean up and return.  Check if the final mul or div resulted
6362306a36Sopenharmony_ci|	in an inex2 exception.  If so, set inex1 in the fpsr and
6462306a36Sopenharmony_ci|	check if the inex1 exception is enabled.  If so, set d7 upper
6562306a36Sopenharmony_ci|	word to $0100.  This will signal unimp.sa that an enabled inex1
6662306a36Sopenharmony_ci|	exception occurred.  Unimp will fix the stack.
6762306a36Sopenharmony_ci|
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci|		Copyright (C) Motorola, Inc. 1990
7062306a36Sopenharmony_ci|			All Rights Reserved
7162306a36Sopenharmony_ci|
7262306a36Sopenharmony_ci|       For details on the license for this file, please see the
7362306a36Sopenharmony_ci|       file, README, in this same directory.
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci|DECBIN    idnt    2,1 | Motorola 040 Floating Point Software Package
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci	|section	8
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci#include "fpsp.h"
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci|
8262306a36Sopenharmony_ci|	PTENRN, PTENRM, and PTENRP are arrays of powers of 10 rounded
8362306a36Sopenharmony_ci|	to nearest, minus, and plus, respectively.  The tables include
8462306a36Sopenharmony_ci|	10**{1,2,4,8,16,32,64,128,256,512,1024,2048,4096}.  No rounding
8562306a36Sopenharmony_ci|	is required until the power is greater than 27, however, all
8662306a36Sopenharmony_ci|	tables include the first 5 for ease of indexing.
8762306a36Sopenharmony_ci|
8862306a36Sopenharmony_ci	|xref	PTENRN
8962306a36Sopenharmony_ci	|xref	PTENRM
9062306a36Sopenharmony_ci	|xref	PTENRP
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ciRTABLE:	.byte	0,0,0,0
9362306a36Sopenharmony_ci	.byte	2,3,2,3
9462306a36Sopenharmony_ci	.byte	2,3,3,2
9562306a36Sopenharmony_ci	.byte	3,2,2,3
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci	.global	decbin
9862306a36Sopenharmony_ci	.global	calc_e
9962306a36Sopenharmony_ci	.global	pwrten
10062306a36Sopenharmony_ci	.global	calc_m
10162306a36Sopenharmony_ci	.global	norm
10262306a36Sopenharmony_ci	.global	ap_st_z
10362306a36Sopenharmony_ci	.global	ap_st_n
10462306a36Sopenharmony_ci|
10562306a36Sopenharmony_ci	.set	FNIBS,7
10662306a36Sopenharmony_ci	.set	FSTRT,0
10762306a36Sopenharmony_ci|
10862306a36Sopenharmony_ci	.set	ESTRT,4
10962306a36Sopenharmony_ci	.set	EDIGITS,2	|
11062306a36Sopenharmony_ci|
11162306a36Sopenharmony_ci| Constants in single precision
11262306a36Sopenharmony_ciFZERO:	.long	0x00000000
11362306a36Sopenharmony_ciFONE:	.long	0x3F800000
11462306a36Sopenharmony_ciFTEN:	.long	0x41200000
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci	.set	TEN,10
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ci|
11962306a36Sopenharmony_cidecbin:
12062306a36Sopenharmony_ci	| fmovel	#0,FPCR		;clr real fpcr
12162306a36Sopenharmony_ci	moveml	%d2-%d5,-(%a7)
12262306a36Sopenharmony_ci|
12362306a36Sopenharmony_ci| Calculate exponent:
12462306a36Sopenharmony_ci|  1. Copy bcd value in memory for use as a working copy.
12562306a36Sopenharmony_ci|  2. Calculate absolute value of exponent in d1 by mul and add.
12662306a36Sopenharmony_ci|  3. Correct for exponent sign.
12762306a36Sopenharmony_ci|  4. Subtract 16 to compensate for interpreting the mant as all integer digits.
12862306a36Sopenharmony_ci|     (i.e., all digits assumed left of the decimal point.)
12962306a36Sopenharmony_ci|
13062306a36Sopenharmony_ci| Register usage:
13162306a36Sopenharmony_ci|
13262306a36Sopenharmony_ci|  calc_e:
13362306a36Sopenharmony_ci|	(*)  d0: temp digit storage
13462306a36Sopenharmony_ci|	(*)  d1: accumulator for binary exponent
13562306a36Sopenharmony_ci|	(*)  d2: digit count
13662306a36Sopenharmony_ci|	(*)  d3: offset pointer
13762306a36Sopenharmony_ci|	( )  d4: first word of bcd
13862306a36Sopenharmony_ci|	( )  a0: pointer to working bcd value
13962306a36Sopenharmony_ci|	( )  a6: pointer to original bcd value
14062306a36Sopenharmony_ci|	(*)  FP_SCR1: working copy of original bcd value
14162306a36Sopenharmony_ci|	(*)  L_SCR1: copy of original exponent word
14262306a36Sopenharmony_ci|
14362306a36Sopenharmony_cicalc_e:
14462306a36Sopenharmony_ci	movel	#EDIGITS,%d2	|# of nibbles (digits) in fraction part
14562306a36Sopenharmony_ci	moveql	#ESTRT,%d3	|counter to pick up digits
14662306a36Sopenharmony_ci	leal	FP_SCR1(%a6),%a0	|load tmp bcd storage address
14762306a36Sopenharmony_ci	movel	ETEMP(%a6),(%a0)	|save input bcd value
14862306a36Sopenharmony_ci	movel	ETEMP_HI(%a6),4(%a0) |save words 2 and 3
14962306a36Sopenharmony_ci	movel	ETEMP_LO(%a6),8(%a0) |and work with these
15062306a36Sopenharmony_ci	movel	(%a0),%d4	|get first word of bcd
15162306a36Sopenharmony_ci	clrl	%d1		|zero d1 for accumulator
15262306a36Sopenharmony_cie_gd:
15362306a36Sopenharmony_ci	mulul	#TEN,%d1	|mul partial product by one digit place
15462306a36Sopenharmony_ci	bfextu	%d4{%d3:#4},%d0	|get the digit and zero extend into d0
15562306a36Sopenharmony_ci	addl	%d0,%d1		|d1 = d1 + d0
15662306a36Sopenharmony_ci	addqb	#4,%d3		|advance d3 to the next digit
15762306a36Sopenharmony_ci	dbf	%d2,e_gd	|if we have used all 3 digits, exit loop
15862306a36Sopenharmony_ci	btst	#30,%d4		|get SE
15962306a36Sopenharmony_ci	beqs	e_pos		|don't negate if pos
16062306a36Sopenharmony_ci	negl	%d1		|negate before subtracting
16162306a36Sopenharmony_cie_pos:
16262306a36Sopenharmony_ci	subl	#16,%d1		|sub to compensate for shift of mant
16362306a36Sopenharmony_ci	bges	e_save		|if still pos, do not neg
16462306a36Sopenharmony_ci	negl	%d1		|now negative, make pos and set SE
16562306a36Sopenharmony_ci	orl	#0x40000000,%d4	|set SE in d4,
16662306a36Sopenharmony_ci	orl	#0x40000000,(%a0)	|and in working bcd
16762306a36Sopenharmony_cie_save:
16862306a36Sopenharmony_ci	movel	%d1,L_SCR1(%a6)	|save exp in memory
16962306a36Sopenharmony_ci|
17062306a36Sopenharmony_ci|
17162306a36Sopenharmony_ci| Calculate mantissa:
17262306a36Sopenharmony_ci|  1. Calculate absolute value of mantissa in fp0 by mul and add.
17362306a36Sopenharmony_ci|  2. Correct for mantissa sign.
17462306a36Sopenharmony_ci|     (i.e., all digits assumed left of the decimal point.)
17562306a36Sopenharmony_ci|
17662306a36Sopenharmony_ci| Register usage:
17762306a36Sopenharmony_ci|
17862306a36Sopenharmony_ci|  calc_m:
17962306a36Sopenharmony_ci|	(*)  d0: temp digit storage
18062306a36Sopenharmony_ci|	(*)  d1: lword counter
18162306a36Sopenharmony_ci|	(*)  d2: digit count
18262306a36Sopenharmony_ci|	(*)  d3: offset pointer
18362306a36Sopenharmony_ci|	( )  d4: words 2 and 3 of bcd
18462306a36Sopenharmony_ci|	( )  a0: pointer to working bcd value
18562306a36Sopenharmony_ci|	( )  a6: pointer to original bcd value
18662306a36Sopenharmony_ci|	(*) fp0: mantissa accumulator
18762306a36Sopenharmony_ci|	( )  FP_SCR1: working copy of original bcd value
18862306a36Sopenharmony_ci|	( )  L_SCR1: copy of original exponent word
18962306a36Sopenharmony_ci|
19062306a36Sopenharmony_cicalc_m:
19162306a36Sopenharmony_ci	moveql	#1,%d1		|word counter, init to 1
19262306a36Sopenharmony_ci	fmoves	FZERO,%fp0	|accumulator
19362306a36Sopenharmony_ci|
19462306a36Sopenharmony_ci|
19562306a36Sopenharmony_ci|  Since the packed number has a long word between the first & second parts,
19662306a36Sopenharmony_ci|  get the integer digit then skip down & get the rest of the
19762306a36Sopenharmony_ci|  mantissa.  We will unroll the loop once.
19862306a36Sopenharmony_ci|
19962306a36Sopenharmony_ci	bfextu	(%a0){#28:#4},%d0	|integer part is ls digit in long word
20062306a36Sopenharmony_ci	faddb	%d0,%fp0		|add digit to sum in fp0
20162306a36Sopenharmony_ci|
20262306a36Sopenharmony_ci|
20362306a36Sopenharmony_ci|  Get the rest of the mantissa.
20462306a36Sopenharmony_ci|
20562306a36Sopenharmony_ciloadlw:
20662306a36Sopenharmony_ci	movel	(%a0,%d1.L*4),%d4	|load mantissa longword into d4
20762306a36Sopenharmony_ci	moveql	#FSTRT,%d3	|counter to pick up digits
20862306a36Sopenharmony_ci	moveql	#FNIBS,%d2	|reset number of digits per a0 ptr
20962306a36Sopenharmony_cimd2b:
21062306a36Sopenharmony_ci	fmuls	FTEN,%fp0	|fp0 = fp0 * 10
21162306a36Sopenharmony_ci	bfextu	%d4{%d3:#4},%d0	|get the digit and zero extend
21262306a36Sopenharmony_ci	faddb	%d0,%fp0	|fp0 = fp0 + digit
21362306a36Sopenharmony_ci|
21462306a36Sopenharmony_ci|
21562306a36Sopenharmony_ci|  If all the digits (8) in that long word have been converted (d2=0),
21662306a36Sopenharmony_ci|  then inc d1 (=2) to point to the next long word and reset d3 to 0
21762306a36Sopenharmony_ci|  to initialize the digit offset, and set d2 to 7 for the digit count;
21862306a36Sopenharmony_ci|  else continue with this long word.
21962306a36Sopenharmony_ci|
22062306a36Sopenharmony_ci	addqb	#4,%d3		|advance d3 to the next digit
22162306a36Sopenharmony_ci	dbf	%d2,md2b		|check for last digit in this lw
22262306a36Sopenharmony_cinextlw:
22362306a36Sopenharmony_ci	addql	#1,%d1		|inc lw pointer in mantissa
22462306a36Sopenharmony_ci	cmpl	#2,%d1		|test for last lw
22562306a36Sopenharmony_ci	ble	loadlw		|if not, get last one
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_ci|
22862306a36Sopenharmony_ci|  Check the sign of the mant and make the value in fp0 the same sign.
22962306a36Sopenharmony_ci|
23062306a36Sopenharmony_cim_sign:
23162306a36Sopenharmony_ci	btst	#31,(%a0)	|test sign of the mantissa
23262306a36Sopenharmony_ci	beq	ap_st_z		|if clear, go to append/strip zeros
23362306a36Sopenharmony_ci	fnegx	%fp0		|if set, negate fp0
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_ci|
23662306a36Sopenharmony_ci| Append/strip zeros:
23762306a36Sopenharmony_ci|
23862306a36Sopenharmony_ci|  For adjusted exponents which have an absolute value greater than 27*,
23962306a36Sopenharmony_ci|  this routine calculates the amount needed to normalize the mantissa
24062306a36Sopenharmony_ci|  for the adjusted exponent.  That number is subtracted from the exp
24162306a36Sopenharmony_ci|  if the exp was positive, and added if it was negative.  The purpose
24262306a36Sopenharmony_ci|  of this is to reduce the value of the exponent and the possibility
24362306a36Sopenharmony_ci|  of error in calculation of pwrten.
24462306a36Sopenharmony_ci|
24562306a36Sopenharmony_ci|  1. Branch on the sign of the adjusted exponent.
24662306a36Sopenharmony_ci|  2p.(positive exp)
24762306a36Sopenharmony_ci|   2. Check M16 and the digits in lwords 2 and 3 in descending order.
24862306a36Sopenharmony_ci|   3. Add one for each zero encountered until a non-zero digit.
24962306a36Sopenharmony_ci|   4. Subtract the count from the exp.
25062306a36Sopenharmony_ci|   5. Check if the exp has crossed zero in #3 above; make the exp abs
25162306a36Sopenharmony_ci|	   and set SE.
25262306a36Sopenharmony_ci|	6. Multiply the mantissa by 10**count.
25362306a36Sopenharmony_ci|  2n.(negative exp)
25462306a36Sopenharmony_ci|   2. Check the digits in lwords 3 and 2 in descending order.
25562306a36Sopenharmony_ci|   3. Add one for each zero encountered until a non-zero digit.
25662306a36Sopenharmony_ci|   4. Add the count to the exp.
25762306a36Sopenharmony_ci|   5. Check if the exp has crossed zero in #3 above; clear SE.
25862306a36Sopenharmony_ci|   6. Divide the mantissa by 10**count.
25962306a36Sopenharmony_ci|
26062306a36Sopenharmony_ci|  *Why 27?  If the adjusted exponent is within -28 < expA < 28, than
26162306a36Sopenharmony_ci|   any adjustment due to append/strip zeros will drive the resultant
26262306a36Sopenharmony_ci|   exponent towards zero.  Since all pwrten constants with a power
26362306a36Sopenharmony_ci|   of 27 or less are exact, there is no need to use this routine to
26462306a36Sopenharmony_ci|   attempt to lessen the resultant exponent.
26562306a36Sopenharmony_ci|
26662306a36Sopenharmony_ci| Register usage:
26762306a36Sopenharmony_ci|
26862306a36Sopenharmony_ci|  ap_st_z:
26962306a36Sopenharmony_ci|	(*)  d0: temp digit storage
27062306a36Sopenharmony_ci|	(*)  d1: zero count
27162306a36Sopenharmony_ci|	(*)  d2: digit count
27262306a36Sopenharmony_ci|	(*)  d3: offset pointer
27362306a36Sopenharmony_ci|	( )  d4: first word of bcd
27462306a36Sopenharmony_ci|	(*)  d5: lword counter
27562306a36Sopenharmony_ci|	( )  a0: pointer to working bcd value
27662306a36Sopenharmony_ci|	( )  FP_SCR1: working copy of original bcd value
27762306a36Sopenharmony_ci|	( )  L_SCR1: copy of original exponent word
27862306a36Sopenharmony_ci|
27962306a36Sopenharmony_ci|
28062306a36Sopenharmony_ci| First check the absolute value of the exponent to see if this
28162306a36Sopenharmony_ci| routine is necessary.  If so, then check the sign of the exponent
28262306a36Sopenharmony_ci| and do append (+) or strip (-) zeros accordingly.
28362306a36Sopenharmony_ci| This section handles a positive adjusted exponent.
28462306a36Sopenharmony_ci|
28562306a36Sopenharmony_ciap_st_z:
28662306a36Sopenharmony_ci	movel	L_SCR1(%a6),%d1	|load expA for range test
28762306a36Sopenharmony_ci	cmpl	#27,%d1		|test is with 27
28862306a36Sopenharmony_ci	ble	pwrten		|if abs(expA) <28, skip ap/st zeros
28962306a36Sopenharmony_ci	btst	#30,(%a0)	|check sign of exp
29062306a36Sopenharmony_ci	bne	ap_st_n		|if neg, go to neg side
29162306a36Sopenharmony_ci	clrl	%d1		|zero count reg
29262306a36Sopenharmony_ci	movel	(%a0),%d4		|load lword 1 to d4
29362306a36Sopenharmony_ci	bfextu	%d4{#28:#4},%d0	|get M16 in d0
29462306a36Sopenharmony_ci	bnes	ap_p_fx		|if M16 is non-zero, go fix exp
29562306a36Sopenharmony_ci	addql	#1,%d1		|inc zero count
29662306a36Sopenharmony_ci	moveql	#1,%d5		|init lword counter
29762306a36Sopenharmony_ci	movel	(%a0,%d5.L*4),%d4	|get lword 2 to d4
29862306a36Sopenharmony_ci	bnes	ap_p_cl		|if lw 2 is zero, skip it
29962306a36Sopenharmony_ci	addql	#8,%d1		|and inc count by 8
30062306a36Sopenharmony_ci	addql	#1,%d5		|inc lword counter
30162306a36Sopenharmony_ci	movel	(%a0,%d5.L*4),%d4	|get lword 3 to d4
30262306a36Sopenharmony_ciap_p_cl:
30362306a36Sopenharmony_ci	clrl	%d3		|init offset reg
30462306a36Sopenharmony_ci	moveql	#7,%d2		|init digit counter
30562306a36Sopenharmony_ciap_p_gd:
30662306a36Sopenharmony_ci	bfextu	%d4{%d3:#4},%d0	|get digit
30762306a36Sopenharmony_ci	bnes	ap_p_fx		|if non-zero, go to fix exp
30862306a36Sopenharmony_ci	addql	#4,%d3		|point to next digit
30962306a36Sopenharmony_ci	addql	#1,%d1		|inc digit counter
31062306a36Sopenharmony_ci	dbf	%d2,ap_p_gd	|get next digit
31162306a36Sopenharmony_ciap_p_fx:
31262306a36Sopenharmony_ci	movel	%d1,%d0		|copy counter to d2
31362306a36Sopenharmony_ci	movel	L_SCR1(%a6),%d1	|get adjusted exp from memory
31462306a36Sopenharmony_ci	subl	%d0,%d1		|subtract count from exp
31562306a36Sopenharmony_ci	bges	ap_p_fm		|if still pos, go to pwrten
31662306a36Sopenharmony_ci	negl	%d1		|now its neg; get abs
31762306a36Sopenharmony_ci	movel	(%a0),%d4		|load lword 1 to d4
31862306a36Sopenharmony_ci	orl	#0x40000000,%d4	| and set SE in d4
31962306a36Sopenharmony_ci	orl	#0x40000000,(%a0)	| and in memory
32062306a36Sopenharmony_ci|
32162306a36Sopenharmony_ci| Calculate the mantissa multiplier to compensate for the striping of
32262306a36Sopenharmony_ci| zeros from the mantissa.
32362306a36Sopenharmony_ci|
32462306a36Sopenharmony_ciap_p_fm:
32562306a36Sopenharmony_ci	movel	#PTENRN,%a1	|get address of power-of-ten table
32662306a36Sopenharmony_ci	clrl	%d3		|init table index
32762306a36Sopenharmony_ci	fmoves	FONE,%fp1	|init fp1 to 1
32862306a36Sopenharmony_ci	moveql	#3,%d2		|init d2 to count bits in counter
32962306a36Sopenharmony_ciap_p_el:
33062306a36Sopenharmony_ci	asrl	#1,%d0		|shift lsb into carry
33162306a36Sopenharmony_ci	bccs	ap_p_en		|if 1, mul fp1 by pwrten factor
33262306a36Sopenharmony_ci	fmulx	(%a1,%d3),%fp1	|mul by 10**(d3_bit_no)
33362306a36Sopenharmony_ciap_p_en:
33462306a36Sopenharmony_ci	addl	#12,%d3		|inc d3 to next rtable entry
33562306a36Sopenharmony_ci	tstl	%d0		|check if d0 is zero
33662306a36Sopenharmony_ci	bnes	ap_p_el		|if not, get next bit
33762306a36Sopenharmony_ci	fmulx	%fp1,%fp0		|mul mantissa by 10**(no_bits_shifted)
33862306a36Sopenharmony_ci	bra	pwrten		|go calc pwrten
33962306a36Sopenharmony_ci|
34062306a36Sopenharmony_ci| This section handles a negative adjusted exponent.
34162306a36Sopenharmony_ci|
34262306a36Sopenharmony_ciap_st_n:
34362306a36Sopenharmony_ci	clrl	%d1		|clr counter
34462306a36Sopenharmony_ci	moveql	#2,%d5		|set up d5 to point to lword 3
34562306a36Sopenharmony_ci	movel	(%a0,%d5.L*4),%d4	|get lword 3
34662306a36Sopenharmony_ci	bnes	ap_n_cl		|if not zero, check digits
34762306a36Sopenharmony_ci	subl	#1,%d5		|dec d5 to point to lword 2
34862306a36Sopenharmony_ci	addql	#8,%d1		|inc counter by 8
34962306a36Sopenharmony_ci	movel	(%a0,%d5.L*4),%d4	|get lword 2
35062306a36Sopenharmony_ciap_n_cl:
35162306a36Sopenharmony_ci	movel	#28,%d3		|point to last digit
35262306a36Sopenharmony_ci	moveql	#7,%d2		|init digit counter
35362306a36Sopenharmony_ciap_n_gd:
35462306a36Sopenharmony_ci	bfextu	%d4{%d3:#4},%d0	|get digit
35562306a36Sopenharmony_ci	bnes	ap_n_fx		|if non-zero, go to exp fix
35662306a36Sopenharmony_ci	subql	#4,%d3		|point to previous digit
35762306a36Sopenharmony_ci	addql	#1,%d1		|inc digit counter
35862306a36Sopenharmony_ci	dbf	%d2,ap_n_gd	|get next digit
35962306a36Sopenharmony_ciap_n_fx:
36062306a36Sopenharmony_ci	movel	%d1,%d0		|copy counter to d0
36162306a36Sopenharmony_ci	movel	L_SCR1(%a6),%d1	|get adjusted exp from memory
36262306a36Sopenharmony_ci	subl	%d0,%d1		|subtract count from exp
36362306a36Sopenharmony_ci	bgts	ap_n_fm		|if still pos, go fix mantissa
36462306a36Sopenharmony_ci	negl	%d1		|take abs of exp and clr SE
36562306a36Sopenharmony_ci	movel	(%a0),%d4		|load lword 1 to d4
36662306a36Sopenharmony_ci	andl	#0xbfffffff,%d4	| and clr SE in d4
36762306a36Sopenharmony_ci	andl	#0xbfffffff,(%a0)	| and in memory
36862306a36Sopenharmony_ci|
36962306a36Sopenharmony_ci| Calculate the mantissa multiplier to compensate for the appending of
37062306a36Sopenharmony_ci| zeros to the mantissa.
37162306a36Sopenharmony_ci|
37262306a36Sopenharmony_ciap_n_fm:
37362306a36Sopenharmony_ci	movel	#PTENRN,%a1	|get address of power-of-ten table
37462306a36Sopenharmony_ci	clrl	%d3		|init table index
37562306a36Sopenharmony_ci	fmoves	FONE,%fp1	|init fp1 to 1
37662306a36Sopenharmony_ci	moveql	#3,%d2		|init d2 to count bits in counter
37762306a36Sopenharmony_ciap_n_el:
37862306a36Sopenharmony_ci	asrl	#1,%d0		|shift lsb into carry
37962306a36Sopenharmony_ci	bccs	ap_n_en		|if 1, mul fp1 by pwrten factor
38062306a36Sopenharmony_ci	fmulx	(%a1,%d3),%fp1	|mul by 10**(d3_bit_no)
38162306a36Sopenharmony_ciap_n_en:
38262306a36Sopenharmony_ci	addl	#12,%d3		|inc d3 to next rtable entry
38362306a36Sopenharmony_ci	tstl	%d0		|check if d0 is zero
38462306a36Sopenharmony_ci	bnes	ap_n_el		|if not, get next bit
38562306a36Sopenharmony_ci	fdivx	%fp1,%fp0		|div mantissa by 10**(no_bits_shifted)
38662306a36Sopenharmony_ci|
38762306a36Sopenharmony_ci|
38862306a36Sopenharmony_ci| Calculate power-of-ten factor from adjusted and shifted exponent.
38962306a36Sopenharmony_ci|
39062306a36Sopenharmony_ci| Register usage:
39162306a36Sopenharmony_ci|
39262306a36Sopenharmony_ci|  pwrten:
39362306a36Sopenharmony_ci|	(*)  d0: temp
39462306a36Sopenharmony_ci|	( )  d1: exponent
39562306a36Sopenharmony_ci|	(*)  d2: {FPCR[6:5],SM,SE} as index in RTABLE; temp
39662306a36Sopenharmony_ci|	(*)  d3: FPCR work copy
39762306a36Sopenharmony_ci|	( )  d4: first word of bcd
39862306a36Sopenharmony_ci|	(*)  a1: RTABLE pointer
39962306a36Sopenharmony_ci|  calc_p:
40062306a36Sopenharmony_ci|	(*)  d0: temp
40162306a36Sopenharmony_ci|	( )  d1: exponent
40262306a36Sopenharmony_ci|	(*)  d3: PWRTxx table index
40362306a36Sopenharmony_ci|	( )  a0: pointer to working copy of bcd
40462306a36Sopenharmony_ci|	(*)  a1: PWRTxx pointer
40562306a36Sopenharmony_ci|	(*) fp1: power-of-ten accumulator
40662306a36Sopenharmony_ci|
40762306a36Sopenharmony_ci| Pwrten calculates the exponent factor in the selected rounding mode
40862306a36Sopenharmony_ci| according to the following table:
40962306a36Sopenharmony_ci|
41062306a36Sopenharmony_ci|	Sign of Mant  Sign of Exp  Rounding Mode  PWRTEN Rounding Mode
41162306a36Sopenharmony_ci|
41262306a36Sopenharmony_ci|	ANY	  ANY	RN	RN
41362306a36Sopenharmony_ci|
41462306a36Sopenharmony_ci|	 +	   +	RP	RP
41562306a36Sopenharmony_ci|	 -	   +	RP	RM
41662306a36Sopenharmony_ci|	 +	   -	RP	RM
41762306a36Sopenharmony_ci|	 -	   -	RP	RP
41862306a36Sopenharmony_ci|
41962306a36Sopenharmony_ci|	 +	   +	RM	RM
42062306a36Sopenharmony_ci|	 -	   +	RM	RP
42162306a36Sopenharmony_ci|	 +	   -	RM	RP
42262306a36Sopenharmony_ci|	 -	   -	RM	RM
42362306a36Sopenharmony_ci|
42462306a36Sopenharmony_ci|	 +	   +	RZ	RM
42562306a36Sopenharmony_ci|	 -	   +	RZ	RM
42662306a36Sopenharmony_ci|	 +	   -	RZ	RP
42762306a36Sopenharmony_ci|	 -	   -	RZ	RP
42862306a36Sopenharmony_ci|
42962306a36Sopenharmony_ci|
43062306a36Sopenharmony_cipwrten:
43162306a36Sopenharmony_ci	movel	USER_FPCR(%a6),%d3 |get user's FPCR
43262306a36Sopenharmony_ci	bfextu	%d3{#26:#2},%d2	|isolate rounding mode bits
43362306a36Sopenharmony_ci	movel	(%a0),%d4		|reload 1st bcd word to d4
43462306a36Sopenharmony_ci	asll	#2,%d2		|format d2 to be
43562306a36Sopenharmony_ci	bfextu	%d4{#0:#2},%d0	| {FPCR[6],FPCR[5],SM,SE}
43662306a36Sopenharmony_ci	addl	%d0,%d2		|in d2 as index into RTABLE
43762306a36Sopenharmony_ci	leal	RTABLE,%a1	|load rtable base
43862306a36Sopenharmony_ci	moveb	(%a1,%d2),%d0	|load new rounding bits from table
43962306a36Sopenharmony_ci	clrl	%d3			|clear d3 to force no exc and extended
44062306a36Sopenharmony_ci	bfins	%d0,%d3{#26:#2}	|stuff new rounding bits in FPCR
44162306a36Sopenharmony_ci	fmovel	%d3,%FPCR		|write new FPCR
44262306a36Sopenharmony_ci	asrl	#1,%d0		|write correct PTENxx table
44362306a36Sopenharmony_ci	bccs	not_rp		|to a1
44462306a36Sopenharmony_ci	leal	PTENRP,%a1	|it is RP
44562306a36Sopenharmony_ci	bras	calc_p		|go to init section
44662306a36Sopenharmony_cinot_rp:
44762306a36Sopenharmony_ci	asrl	#1,%d0		|keep checking
44862306a36Sopenharmony_ci	bccs	not_rm
44962306a36Sopenharmony_ci	leal	PTENRM,%a1	|it is RM
45062306a36Sopenharmony_ci	bras	calc_p		|go to init section
45162306a36Sopenharmony_cinot_rm:
45262306a36Sopenharmony_ci	leal	PTENRN,%a1	|it is RN
45362306a36Sopenharmony_cicalc_p:
45462306a36Sopenharmony_ci	movel	%d1,%d0		|copy exp to d0;use d0
45562306a36Sopenharmony_ci	bpls	no_neg		|if exp is negative,
45662306a36Sopenharmony_ci	negl	%d0		|invert it
45762306a36Sopenharmony_ci	orl	#0x40000000,(%a0)	|and set SE bit
45862306a36Sopenharmony_cino_neg:
45962306a36Sopenharmony_ci	clrl	%d3		|table index
46062306a36Sopenharmony_ci	fmoves	FONE,%fp1	|init fp1 to 1
46162306a36Sopenharmony_cie_loop:
46262306a36Sopenharmony_ci	asrl	#1,%d0		|shift next bit into carry
46362306a36Sopenharmony_ci	bccs	e_next		|if zero, skip the mul
46462306a36Sopenharmony_ci	fmulx	(%a1,%d3),%fp1	|mul by 10**(d3_bit_no)
46562306a36Sopenharmony_cie_next:
46662306a36Sopenharmony_ci	addl	#12,%d3		|inc d3 to next rtable entry
46762306a36Sopenharmony_ci	tstl	%d0		|check if d0 is zero
46862306a36Sopenharmony_ci	bnes	e_loop		|not zero, continue shifting
46962306a36Sopenharmony_ci|
47062306a36Sopenharmony_ci|
47162306a36Sopenharmony_ci|  Check the sign of the adjusted exp and make the value in fp0 the
47262306a36Sopenharmony_ci|  same sign. If the exp was pos then multiply fp1*fp0;
47362306a36Sopenharmony_ci|  else divide fp0/fp1.
47462306a36Sopenharmony_ci|
47562306a36Sopenharmony_ci| Register Usage:
47662306a36Sopenharmony_ci|  norm:
47762306a36Sopenharmony_ci|	( )  a0: pointer to working bcd value
47862306a36Sopenharmony_ci|	(*) fp0: mantissa accumulator
47962306a36Sopenharmony_ci|	( ) fp1: scaling factor - 10**(abs(exp))
48062306a36Sopenharmony_ci|
48162306a36Sopenharmony_cinorm:
48262306a36Sopenharmony_ci	btst	#30,(%a0)	|test the sign of the exponent
48362306a36Sopenharmony_ci	beqs	mul		|if clear, go to multiply
48462306a36Sopenharmony_cidiv:
48562306a36Sopenharmony_ci	fdivx	%fp1,%fp0		|exp is negative, so divide mant by exp
48662306a36Sopenharmony_ci	bras	end_dec
48762306a36Sopenharmony_cimul:
48862306a36Sopenharmony_ci	fmulx	%fp1,%fp0		|exp is positive, so multiply by exp
48962306a36Sopenharmony_ci|
49062306a36Sopenharmony_ci|
49162306a36Sopenharmony_ci| Clean up and return with result in fp0.
49262306a36Sopenharmony_ci|
49362306a36Sopenharmony_ci| If the final mul/div in decbin incurred an inex exception,
49462306a36Sopenharmony_ci| it will be inex2, but will be reported as inex1 by get_op.
49562306a36Sopenharmony_ci|
49662306a36Sopenharmony_ciend_dec:
49762306a36Sopenharmony_ci	fmovel	%FPSR,%d0		|get status register
49862306a36Sopenharmony_ci	bclrl	#inex2_bit+8,%d0	|test for inex2 and clear it
49962306a36Sopenharmony_ci	fmovel	%d0,%FPSR		|return status reg w/o inex2
50062306a36Sopenharmony_ci	beqs	no_exc		|skip this if no exc
50162306a36Sopenharmony_ci	orl	#inx1a_mask,USER_FPSR(%a6) |set inex1/ainex
50262306a36Sopenharmony_cino_exc:
50362306a36Sopenharmony_ci	moveml	(%a7)+,%d2-%d5
50462306a36Sopenharmony_ci	rts
50562306a36Sopenharmony_ci	|end
506