decbin.sa revision 1.1
11.1Smycroft*	MOTOROLA MICROPROCESSOR & MEMORY TECHNOLOGY GROUP
21.1Smycroft*	M68000 Hi-Performance Microprocessor Division
31.1Smycroft*	M68040 Software Package 
41.1Smycroft*
51.1Smycroft*	M68040 Software Package Copyright (c) 1993, 1994 Motorola Inc.
61.1Smycroft*	All rights reserved.
71.1Smycroft*
81.1Smycroft*	THE SOFTWARE is provided on an "AS IS" basis and without warranty.
91.1Smycroft*	To the maximum extent permitted by applicable law,
101.1Smycroft*	MOTOROLA DISCLAIMS ALL WARRANTIES WHETHER EXPRESS OR IMPLIED,
111.1Smycroft*	INCLUDING IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A
121.1Smycroft*	PARTICULAR PURPOSE and any warranty against infringement with
131.1Smycroft*	regard to the SOFTWARE (INCLUDING ANY MODIFIED VERSIONS THEREOF)
141.1Smycroft*	and any accompanying written materials. 
151.1Smycroft*
161.1Smycroft*	To the maximum extent permitted by applicable law,
171.1Smycroft*	IN NO EVENT SHALL MOTOROLA BE LIABLE FOR ANY DAMAGES WHATSOEVER
181.1Smycroft*	(INCLUDING WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS
191.1Smycroft*	PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR
201.1Smycroft*	OTHER PECUNIARY LOSS) ARISING OF THE USE OR INABILITY TO USE THE
211.1Smycroft*	SOFTWARE.  Motorola assumes no responsibility for the maintenance
221.1Smycroft*	and support of the SOFTWARE.  
231.1Smycroft*
241.1Smycroft*	You are hereby granted a copyright license to use, modify, and
251.1Smycroft*	distribute the SOFTWARE so long as this entire notice is retained
261.1Smycroft*	without alteration in any modified and/or redistributed versions,
271.1Smycroft*	and that such modified versions are clearly identified as such.
281.1Smycroft*	No licenses are granted by implication, estoppel or otherwise
291.1Smycroft*	under any patents or trademarks of Motorola, Inc.
301.1Smycroft
311.1Smycroft*
321.1Smycroft*	decbin.sa 3.3 12/19/90
331.1Smycroft*
341.1Smycroft*	Description: Converts normalized packed bcd value pointed to by
351.1Smycroft*	register A6 to extended-precision value in FP0.
361.1Smycroft*
371.1Smycroft*	Input: Normalized packed bcd value in ETEMP(a6).
381.1Smycroft*
391.1Smycroft*	Output:	Exact floating-point representation of the packed bcd value.
401.1Smycroft*
411.1Smycroft*	Saves and Modifies: D2-D5
421.1Smycroft*
431.1Smycroft*	Speed: The program decbin takes ??? cycles to execute.
441.1Smycroft*
451.1Smycroft*	Object Size:
461.1Smycroft*
471.1Smycroft*	External Reference(s): None.
481.1Smycroft*
491.1Smycroft*	Algorithm:
501.1Smycroft*	Expected is a normal bcd (i.e. non-exceptional; all inf, zero,
511.1Smycroft*	and NaN operands are dispatched without entering this routine)
521.1Smycroft*	value in 68881/882 format at location ETEMP(A6).
531.1Smycroft*
541.1Smycroft*	A1.	Convert the bcd exponent to binary by successive adds and muls.
551.1Smycroft*	Set the sign according to SE. Subtract 16 to compensate
561.1Smycroft*	for the mantissa which is to be interpreted as 17 integer
571.1Smycroft*	digits, rather than 1 integer and 16 fraction digits.
581.1Smycroft*	Note: this operation can never overflow.
591.1Smycroft*
601.1Smycroft*	A2. Convert the bcd mantissa to binary by successive
611.1Smycroft*	adds and muls in FP0. Set the sign according to SM.
621.1Smycroft*	The mantissa digits will be converted with the decimal point
631.1Smycroft*	assumed following the least-significant digit.
641.1Smycroft*	Note: this operation can never overflow.
651.1Smycroft*
661.1Smycroft*	A3. Count the number of leading/trailing zeros in the
671.1Smycroft*	bcd string.  If SE is positive, count the leading zeros;
681.1Smycroft*	if negative, count the trailing zeros.  Set the adjusted
691.1Smycroft*	exponent equal to the exponent from A1 and the zero count
701.1Smycroft*	added if SM = 1 and subtracted if SM = 0.  Scale the
711.1Smycroft*	mantissa the equivalent of forcing in the bcd value:
721.1Smycroft*
731.1Smycroft*	SM = 0	a non-zero digit in the integer position
741.1Smycroft*	SM = 1	a non-zero digit in Mant0, lsd of the fraction
751.1Smycroft*
761.1Smycroft*	this will insure that any value, regardless of its
771.1Smycroft*	representation (ex. 0.1E2, 1E1, 10E0, 100E-1), is converted
781.1Smycroft*	consistently.
791.1Smycroft*
801.1Smycroft*	A4. Calculate the factor 10^exp in FP1 using a table of
811.1Smycroft*	10^(2^n) values.  To reduce the error in forming factors
821.1Smycroft*	greater than 10^27, a directed rounding scheme is used with
831.1Smycroft*	tables rounded to RN, RM, and RP, according to the table
841.1Smycroft*	in the comments of the pwrten section.
851.1Smycroft*
861.1Smycroft*	A5. Form the final binary number by scaling the mantissa by
871.1Smycroft*	the exponent factor.  This is done by multiplying the
881.1Smycroft*	mantissa in FP0 by the factor in FP1 if the adjusted
891.1Smycroft*	exponent sign is positive, and dividing FP0 by FP1 if
901.1Smycroft*	it is negative.
911.1Smycroft*
921.1Smycroft*	Clean up and return.  Check if the final mul or div resulted
931.1Smycroft*	in an inex2 exception.  If so, set inex1 in the fpsr and 
941.1Smycroft*	check if the inex1 exception is enabled.  If so, set d7 upper
951.1Smycroft*	word to $0100.  This will signal unimp.sa that an enabled inex1
961.1Smycroft*	exception occured.  Unimp will fix the stack.
971.1Smycroft*	
981.1Smycroft
991.1SmycroftDECBIN    IDNT    2,1 Motorola 040 Floating Point Software Package
1001.1Smycroft
1011.1Smycroft	section	8
1021.1Smycroft
1031.1Smycroft	include	fpsp.h
1041.1Smycroft
1051.1Smycroft*
1061.1Smycroft*	PTENRN, PTENRM, and PTENRP are arrays of powers of 10 rounded
1071.1Smycroft*	to nearest, minus, and plus, respectively.  The tables include
1081.1Smycroft*	10**{1,2,4,8,16,32,64,128,256,512,1024,2048,4096}.  No rounding
1091.1Smycroft*	is required until the power is greater than 27, however, all
1101.1Smycroft*	tables include the first 5 for ease of indexing.
1111.1Smycroft*
1121.1Smycroft	xref	PTENRN
1131.1Smycroft	xref	PTENRM
1141.1Smycroft	xref	PTENRP
1151.1Smycroft
1161.1SmycroftRTABLE	dc.b	0,0,0,0
1171.1Smycroft	dc.b	2,3,2,3
1181.1Smycroft	dc.b	2,3,3,2
1191.1Smycroft	dc.b	3,2,2,3
1201.1Smycroft
1211.1Smycroft	xdef	decbin
1221.1Smycroft	xdef	calc_e
1231.1Smycroft	xdef	pwrten
1241.1Smycroft	xdef	calc_m
1251.1Smycroft	xdef	norm
1261.1Smycroft	xdef	ap_st_z
1271.1Smycroft	xdef	ap_st_n
1281.1Smycroft*
1291.1SmycroftFNIBS	equ	7
1301.1SmycroftFSTRT	equ	0
1311.1Smycroft*
1321.1SmycroftESTRT	equ	4
1331.1SmycroftEDIGITS equ	2	
1341.1Smycroft*
1351.1Smycroft* Constants in single precision
1361.1SmycroftFZERO 	dc.l	$00000000
1371.1SmycroftFONE 	dc.l	$3F800000
1381.1SmycroftFTEN 	dc.l	$41200000
1391.1Smycroft
1401.1SmycroftTEN	equ	10
1411.1Smycroft
1421.1Smycroft*
1431.1Smycroftdecbin:
1441.1Smycroft	fmove.l	#0,FPCR		;clr real fpcr
1451.1Smycroft	movem.l	d2-d5,-(a7)
1461.1Smycroft*
1471.1Smycroft* Calculate exponent:
1481.1Smycroft*  1. Copy bcd value in memory for use as a working copy.
1491.1Smycroft*  2. Calculate absolute value of exponent in d1 by mul and add.
1501.1Smycroft*  3. Correct for exponent sign.
1511.1Smycroft*  4. Subtract 16 to compensate for interpreting the mant as all integer digits.
1521.1Smycroft*     (i.e., all digits assumed left of the decimal point.)
1531.1Smycroft*
1541.1Smycroft* Register usage:
1551.1Smycroft*
1561.1Smycroft*  calc_e:
1571.1Smycroft*	(*)  d0: temp digit storage
1581.1Smycroft*	(*)  d1: accumulator for binary exponent
1591.1Smycroft*	(*)  d2: digit count
1601.1Smycroft*	(*)  d3: offset pointer
1611.1Smycroft*	( )  d4: first word of bcd
1621.1Smycroft*	( )  a0: pointer to working bcd value
1631.1Smycroft*	( )  a6: pointer to original bcd value
1641.1Smycroft*	(*)  FP_SCR1: working copy of original bcd value
1651.1Smycroft*	(*)  L_SCR1: copy of original exponent word
1661.1Smycroft*
1671.1Smycroftcalc_e:
1681.1Smycroft	move.l	#EDIGITS,d2	;# of nibbles (digits) in fraction part
1691.1Smycroft	moveq.l	#ESTRT,d3	;counter to pick up digits
1701.1Smycroft	lea.l	FP_SCR1(a6),a0	;load tmp bcd storage address
1711.1Smycroft	move.l	ETEMP(a6),(a0)	;save input bcd value
1721.1Smycroft	move.l	ETEMP_HI(a6),4(a0) ;save words 2 and 3
1731.1Smycroft	move.l	ETEMP_LO(a6),8(a0) ;and work with these
1741.1Smycroft	move.l	(a0),d4		;get first word of bcd
1751.1Smycroft	clr.l	d1		;zero d1 for accumulator
1761.1Smycrofte_gd:
1771.1Smycroft	mulu.l	#TEN,d1		;mul partial product by one digit place
1781.1Smycroft	bfextu	d4{d3:4},d0	;get the digit and zero extend into d0
1791.1Smycroft	add.l	d0,d1		;d1 = d1 + d0
1801.1Smycroft	addq.b	#4,d3		;advance d3 to the next digit
1811.1Smycroft	dbf.w	d2,e_gd		;if we have used all 3 digits, exit loop
1821.1Smycroft	btst	#30,d4		;get SE
1831.1Smycroft	beq.b	e_pos		;don't negate if pos
1841.1Smycroft	neg.l	d1		;negate before subtracting
1851.1Smycrofte_pos:
1861.1Smycroft	sub.l	#16,d1		;sub to compensate for shift of mant
1871.1Smycroft	bge.b	e_save		;if still pos, do not neg
1881.1Smycroft	neg.l	d1		;now negative, make pos and set SE
1891.1Smycroft	or.l	#$40000000,d4	;set SE in d4,
1901.1Smycroft	or.l	#$40000000,(a0)	;and in working bcd
1911.1Smycrofte_save:
1921.1Smycroft	move.l	d1,L_SCR1(a6)	;save exp in memory
1931.1Smycroft*
1941.1Smycroft*
1951.1Smycroft* Calculate mantissa:
1961.1Smycroft*  1. Calculate absolute value of mantissa in fp0 by mul and add.
1971.1Smycroft*  2. Correct for mantissa sign.
1981.1Smycroft*     (i.e., all digits assumed left of the decimal point.)
1991.1Smycroft*
2001.1Smycroft* Register usage:
2011.1Smycroft*
2021.1Smycroft*  calc_m:
2031.1Smycroft*	(*)  d0: temp digit storage
2041.1Smycroft*	(*)  d1: lword counter
2051.1Smycroft*	(*)  d2: digit count
2061.1Smycroft*	(*)  d3: offset pointer
2071.1Smycroft*	( )  d4: words 2 and 3 of bcd
2081.1Smycroft*	( )  a0: pointer to working bcd value
2091.1Smycroft*	( )  a6: pointer to original bcd value
2101.1Smycroft*	(*) fp0: mantissa accumulator
2111.1Smycroft*	( )  FP_SCR1: working copy of original bcd value
2121.1Smycroft*	( )  L_SCR1: copy of original exponent word
2131.1Smycroft*
2141.1Smycroftcalc_m:
2151.1Smycroft	moveq.l	#1,d1		;word counter, init to 1
2161.1Smycroft	fmove.s	FZERO,fp0	;accumulator
2171.1Smycroft*
2181.1Smycroft*
2191.1Smycroft*  Since the packed number has a long word between the first & second parts,
2201.1Smycroft*  get the integer digit then skip down & get the rest of the
2211.1Smycroft*  mantissa.  We will unroll the loop once.
2221.1Smycroft*
2231.1Smycroft	bfextu	(a0){28:4},d0	;integer part is ls digit in long word
2241.1Smycroft	fadd.b	d0,fp0		;add digit to sum in fp0
2251.1Smycroft*
2261.1Smycroft*
2271.1Smycroft*  Get the rest of the mantissa.
2281.1Smycroft*
2291.1Smycroftloadlw:
2301.1Smycroft	move.l	(a0,d1.L*4),d4	;load mantissa lonqword into d4
2311.1Smycroft	moveq.l	#FSTRT,d3	;counter to pick up digits
2321.1Smycroft	moveq.l	#FNIBS,d2	;reset number of digits per a0 ptr
2331.1Smycroftmd2b:
2341.1Smycroft	fmul.s	FTEN,fp0	;fp0 = fp0 * 10
2351.1Smycroft	bfextu	d4{d3:4},d0	;get the digit and zero extend
2361.1Smycroft	fadd.b	d0,fp0		;fp0 = fp0 + digit
2371.1Smycroft*
2381.1Smycroft*
2391.1Smycroft*  If all the digits (8) in that long word have been converted (d2=0),
2401.1Smycroft*  then inc d1 (=2) to point to the next long word and reset d3 to 0
2411.1Smycroft*  to initialize the digit offset, and set d2 to 7 for the digit count;
2421.1Smycroft*  else continue with this long word.
2431.1Smycroft*
2441.1Smycroft	addq.b	#4,d3		;advance d3 to the next digit
2451.1Smycroft	dbf.w	d2,md2b		;check for last digit in this lw
2461.1Smycroftnextlw:
2471.1Smycroft	addq.l	#1,d1		;inc lw pointer in mantissa
2481.1Smycroft	cmp.l	#2,d1		;test for last lw
2491.1Smycroft	ble	loadlw		;if not, get last one
2501.1Smycroft	
2511.1Smycroft*
2521.1Smycroft*  Check the sign of the mant and make the value in fp0 the same sign.
2531.1Smycroft*
2541.1Smycroftm_sign:
2551.1Smycroft	btst	#31,(a0)	;test sign of the mantissa
2561.1Smycroft	beq.b	ap_st_z		;if clear, go to append/strip zeros
2571.1Smycroft	fneg.x	fp0		;if set, negate fp0
2581.1Smycroft	
2591.1Smycroft*
2601.1Smycroft* Append/strip zeros:
2611.1Smycroft*
2621.1Smycroft*  For adjusted exponents which have an absolute value greater than 27*,
2631.1Smycroft*  this routine calculates the amount needed to normalize the mantissa
2641.1Smycroft*  for the adjusted exponent.  That number is subtracted from the exp
2651.1Smycroft*  if the exp was positive, and added if it was negative.  The purpose
2661.1Smycroft*  of this is to reduce the value of the exponent and the possibility
2671.1Smycroft*  of error in calculation of pwrten.
2681.1Smycroft*
2691.1Smycroft*  1. Branch on the sign of the adjusted exponent.
2701.1Smycroft*  2p.(positive exp)
2711.1Smycroft*   2. Check M16 and the digits in lwords 2 and 3 in decending order.
2721.1Smycroft*   3. Add one for each zero encountered until a non-zero digit.
2731.1Smycroft*   4. Subtract the count from the exp.
2741.1Smycroft*   5. Check if the exp has crossed zero in #3 above; make the exp abs
2751.1Smycroft*	   and set SE.
2761.1Smycroft*	6. Multiply the mantissa by 10**count.
2771.1Smycroft*  2n.(negative exp)
2781.1Smycroft*   2. Check the digits in lwords 3 and 2 in decending order.
2791.1Smycroft*   3. Add one for each zero encountered until a non-zero digit.
2801.1Smycroft*   4. Add the count to the exp.
2811.1Smycroft*   5. Check if the exp has crossed zero in #3 above; clear SE.
2821.1Smycroft*   6. Divide the mantissa by 10**count.
2831.1Smycroft*
2841.1Smycroft*  *Why 27?  If the adjusted exponent is within -28 < expA < 28, than
2851.1Smycroft*   any adjustment due to append/strip zeros will drive the resultane
2861.1Smycroft*   exponent towards zero.  Since all pwrten constants with a power
2871.1Smycroft*   of 27 or less are exact, there is no need to use this routine to
2881.1Smycroft*   attempt to lessen the resultant exponent.
2891.1Smycroft*
2901.1Smycroft* Register usage:
2911.1Smycroft*
2921.1Smycroft*  ap_st_z:
2931.1Smycroft*	(*)  d0: temp digit storage
2941.1Smycroft*	(*)  d1: zero count
2951.1Smycroft*	(*)  d2: digit count
2961.1Smycroft*	(*)  d3: offset pointer
2971.1Smycroft*	( )  d4: first word of bcd
2981.1Smycroft*	(*)  d5: lword counter
2991.1Smycroft*	( )  a0: pointer to working bcd value
3001.1Smycroft*	( )  FP_SCR1: working copy of original bcd value
3011.1Smycroft*	( )  L_SCR1: copy of original exponent word
3021.1Smycroft*
3031.1Smycroft*
3041.1Smycroft* First check the absolute value of the exponent to see if this
3051.1Smycroft* routine is necessary.  If so, then check the sign of the exponent
3061.1Smycroft* and do append (+) or strip (-) zeros accordingly.
3071.1Smycroft* This section handles a positive adjusted exponent.
3081.1Smycroft*
3091.1Smycroftap_st_z:
3101.1Smycroft	move.l	L_SCR1(a6),d1	;load expA for range test
3111.1Smycroft	cmp.l	#27,d1		;test is with 27
3121.1Smycroft	ble.w	pwrten		;if abs(expA) <28, skip ap/st zeros
3131.1Smycroft	btst	#30,(a0)	;check sign of exp
3141.1Smycroft	bne.b	ap_st_n		;if neg, go to neg side
3151.1Smycroft	clr.l	d1		;zero count reg
3161.1Smycroft	move.l	(a0),d4		;load lword 1 to d4
3171.1Smycroft	bfextu	d4{28:4},d0	;get M16 in d0
3181.1Smycroft	bne.b	ap_p_fx		;if M16 is non-zero, go fix exp
3191.1Smycroft	addq.l	#1,d1		;inc zero count
3201.1Smycroft	moveq.l	#1,d5		;init lword counter
3211.1Smycroft	move.l	(a0,d5.L*4),d4	;get lword 2 to d4
3221.1Smycroft	bne.b	ap_p_cl		;if lw 2 is zero, skip it
3231.1Smycroft	addq.l	#8,d1		;and inc count by 8
3241.1Smycroft	addq.l	#1,d5		;inc lword counter
3251.1Smycroft	move.l	(a0,d5.L*4),d4	;get lword 3 to d4
3261.1Smycroftap_p_cl:
3271.1Smycroft	clr.l	d3		;init offset reg
3281.1Smycroft	moveq.l	#7,d2		;init digit counter
3291.1Smycroftap_p_gd:
3301.1Smycroft	bfextu	d4{d3:4},d0	;get digit
3311.1Smycroft	bne.b	ap_p_fx		;if non-zero, go to fix exp
3321.1Smycroft	addq.l	#4,d3		;point to next digit
3331.1Smycroft	addq.l	#1,d1		;inc digit counter
3341.1Smycroft	dbf.w	d2,ap_p_gd	;get next digit
3351.1Smycroftap_p_fx:
3361.1Smycroft	move.l	d1,d0		;copy counter to d2
3371.1Smycroft	move.l	L_SCR1(a6),d1	;get adjusted exp from memory
3381.1Smycroft	sub.l	d0,d1		;subtract count from exp
3391.1Smycroft	bge.b	ap_p_fm		;if still pos, go to pwrten
3401.1Smycroft	neg.l	d1		;now its neg; get abs
3411.1Smycroft	move.l	(a0),d4		;load lword 1 to d4
3421.1Smycroft	or.l	#$40000000,d4	; and set SE in d4
3431.1Smycroft	or.l	#$40000000,(a0)	; and in memory
3441.1Smycroft*
3451.1Smycroft* Calculate the mantissa multiplier to compensate for the striping of
3461.1Smycroft* zeros from the mantissa.
3471.1Smycroft*
3481.1Smycroftap_p_fm:
3491.1Smycroft	move.l	#PTENRN,a1	;get address of power-of-ten table
3501.1Smycroft	clr.l	d3		;init table index
3511.1Smycroft	fmove.s	FONE,fp1	;init fp1 to 1
3521.1Smycroft	moveq.l	#3,d2		;init d2 to count bits in counter
3531.1Smycroftap_p_el:
3541.1Smycroft	asr.l	#1,d0		;shift lsb into carry
3551.1Smycroft	bcc.b	ap_p_en		;if 1, mul fp1 by pwrten factor
3561.1Smycroft	fmul.x	(a1,d3),fp1	;mul by 10**(d3_bit_no)
3571.1Smycroftap_p_en:
3581.1Smycroft	add.l	#12,d3		;inc d3 to next rtable entry
3591.1Smycroft	tst.l	d0		;check if d0 is zero
3601.1Smycroft	bne.b	ap_p_el		;if not, get next bit
3611.1Smycroft	fmul.x	fp1,fp0		;mul mantissa by 10**(no_bits_shifted)
3621.1Smycroft	bra.b	pwrten		;go calc pwrten
3631.1Smycroft*
3641.1Smycroft* This section handles a negative adjusted exponent.
3651.1Smycroft*
3661.1Smycroftap_st_n:
3671.1Smycroft	clr.l	d1		;clr counter
3681.1Smycroft	moveq.l	#2,d5		;set up d5 to point to lword 3
3691.1Smycroft	move.l	(a0,d5.L*4),d4	;get lword 3
3701.1Smycroft	bne.b	ap_n_cl		;if not zero, check digits
3711.1Smycroft	sub.l	#1,d5		;dec d5 to point to lword 2
3721.1Smycroft	addq.l	#8,d1		;inc counter by 8
3731.1Smycroft	move.l	(a0,d5.L*4),d4	;get lword 2
3741.1Smycroftap_n_cl:
3751.1Smycroft	move.l	#28,d3		;point to last digit
3761.1Smycroft	moveq.l	#7,d2		;init digit counter
3771.1Smycroftap_n_gd:
3781.1Smycroft	bfextu	d4{d3:4},d0	;get digit
3791.1Smycroft	bne.b	ap_n_fx		;if non-zero, go to exp fix
3801.1Smycroft	subq.l	#4,d3		;point to previous digit
3811.1Smycroft	addq.l	#1,d1		;inc digit counter
3821.1Smycroft	dbf.w	d2,ap_n_gd	;get next digit
3831.1Smycroftap_n_fx:
3841.1Smycroft	move.l	d1,d0		;copy counter to d0
3851.1Smycroft	move.l	L_SCR1(a6),d1	;get adjusted exp from memory
3861.1Smycroft	sub.l	d0,d1		;subtract count from exp
3871.1Smycroft	bgt.b	ap_n_fm		;if still pos, go fix mantissa
3881.1Smycroft	neg.l	d1		;take abs of exp and clr SE
3891.1Smycroft	move.l	(a0),d4		;load lword 1 to d4
3901.1Smycroft	and.l	#$bfffffff,d4	; and clr SE in d4
3911.1Smycroft	and.l	#$bfffffff,(a0)	; and in memory
3921.1Smycroft*
3931.1Smycroft* Calculate the mantissa multiplier to compensate for the appending of
3941.1Smycroft* zeros to the mantissa.
3951.1Smycroft*
3961.1Smycroftap_n_fm:
3971.1Smycroft	move.l	#PTENRN,a1	;get address of power-of-ten table
3981.1Smycroft	clr.l	d3		;init table index
3991.1Smycroft	fmove.s	FONE,fp1	;init fp1 to 1
4001.1Smycroft	moveq.l	#3,d2		;init d2 to count bits in counter
4011.1Smycroftap_n_el:
4021.1Smycroft	asr.l	#1,d0		;shift lsb into carry
4031.1Smycroft	bcc.b	ap_n_en		;if 1, mul fp1 by pwrten factor
4041.1Smycroft	fmul.x	(a1,d3),fp1	;mul by 10**(d3_bit_no)
4051.1Smycroftap_n_en:
4061.1Smycroft	add.l	#12,d3		;inc d3 to next rtable entry
4071.1Smycroft	tst.l	d0		;check if d0 is zero
4081.1Smycroft	bne.b	ap_n_el		;if not, get next bit
4091.1Smycroft	fdiv.x	fp1,fp0		;div mantissa by 10**(no_bits_shifted)
4101.1Smycroft*
4111.1Smycroft*
4121.1Smycroft* Calculate power-of-ten factor from adjusted and shifted exponent.
4131.1Smycroft*
4141.1Smycroft* Register usage:
4151.1Smycroft*
4161.1Smycroft*  pwrten:
4171.1Smycroft*	(*)  d0: temp
4181.1Smycroft*	( )  d1: exponent
4191.1Smycroft*	(*)  d2: {FPCR[6:5],SM,SE} as index in RTABLE; temp
4201.1Smycroft*	(*)  d3: FPCR work copy
4211.1Smycroft*	( )  d4: first word of bcd
4221.1Smycroft*	(*)  a1: RTABLE pointer
4231.1Smycroft*  calc_p:
4241.1Smycroft*	(*)  d0: temp
4251.1Smycroft*	( )  d1: exponent
4261.1Smycroft*	(*)  d3: PWRTxx table index
4271.1Smycroft*	( )  a0: pointer to working copy of bcd
4281.1Smycroft*	(*)  a1: PWRTxx pointer
4291.1Smycroft*	(*) fp1: power-of-ten accumulator
4301.1Smycroft*
4311.1Smycroft* Pwrten calculates the exponent factor in the selected rounding mode
4321.1Smycroft* according to the following table:
4331.1Smycroft*	
4341.1Smycroft*	Sign of Mant  Sign of Exp  Rounding Mode  PWRTEN Rounding Mode
4351.1Smycroft*
4361.1Smycroft*	ANY	  ANY	RN	RN
4371.1Smycroft*
4381.1Smycroft*	 +	   +	RP	RP
4391.1Smycroft*	 -	   +	RP	RM
4401.1Smycroft*	 +	   -	RP	RM
4411.1Smycroft*	 -	   -	RP	RP
4421.1Smycroft*
4431.1Smycroft*	 +	   +	RM	RM
4441.1Smycroft*	 -	   +	RM	RP
4451.1Smycroft*	 +	   -	RM	RP
4461.1Smycroft*	 -	   -	RM	RM
4471.1Smycroft*
4481.1Smycroft*	 +	   +	RZ	RM
4491.1Smycroft*	 -	   +	RZ	RM
4501.1Smycroft*	 +	   -	RZ	RP
4511.1Smycroft*	 -	   -	RZ	RP
4521.1Smycroft*
4531.1Smycroft*
4541.1Smycroftpwrten:
4551.1Smycroft	move.l	USER_FPCR(a6),d3 ;get user's FPCR
4561.1Smycroft	bfextu	d3{26:2},d2	;isolate rounding mode bits
4571.1Smycroft	move.l	(a0),d4		;reload 1st bcd word to d4
4581.1Smycroft	asl.l	#2,d2		;format d2 to be
4591.1Smycroft	bfextu	d4{0:2},d0	; {FPCR[6],FPCR[5],SM,SE}
4601.1Smycroft	add.l	d0,d2		;in d2 as index into RTABLE
4611.1Smycroft	lea.l	RTABLE,a1	;load rtable base
4621.1Smycroft	move.b	(a1,d2),d0	;load new rounding bits from table
4631.1Smycroft	clr.l	d3			;clear d3 to force no exc and extended
4641.1Smycroft	bfins	d0,d3{26:2}	;stuff new rounding bits in FPCR
4651.1Smycroft	fmove.l	d3,FPCR		;write new FPCR
4661.1Smycroft	asr.l	#1,d0		;write correct PTENxx table
4671.1Smycroft	bcc.b	not_rp		;to a1
4681.1Smycroft	lea.l	PTENRP,a1	;it is RP
4691.1Smycroft	bra.b	calc_p		;go to init section
4701.1Smycroftnot_rp:
4711.1Smycroft	asr.l	#1,d0		;keep checking
4721.1Smycroft	bcc.b	not_rm
4731.1Smycroft	lea.l	PTENRM,a1	;it is RM
4741.1Smycroft	bra.b	calc_p		;go to init section
4751.1Smycroftnot_rm:
4761.1Smycroft	lea.l	PTENRN,a1	;it is RN
4771.1Smycroftcalc_p:
4781.1Smycroft	move.l	d1,d0		;copy exp to d0;use d0
4791.1Smycroft	bpl.b	no_neg		;if exp is negative,
4801.1Smycroft	neg.l	d0		;invert it
4811.1Smycroft	or.l	#$40000000,(a0)	;and set SE bit
4821.1Smycroftno_neg:
4831.1Smycroft	clr.l	d3		;table index
4841.1Smycroft	fmove.s	FONE,fp1	;init fp1 to 1
4851.1Smycrofte_loop:
4861.1Smycroft	asr.l	#1,d0		;shift next bit into carry
4871.1Smycroft	bcc.b	e_next		;if zero, skip the mul
4881.1Smycroft	fmul.x	(a1,d3),fp1	;mul by 10**(d3_bit_no)
4891.1Smycrofte_next:
4901.1Smycroft	add.l	#12,d3		;inc d3 to next rtable entry
4911.1Smycroft	tst.l	d0		;check if d0 is zero
4921.1Smycroft	bne.b	e_loop		;not zero, continue shifting
4931.1Smycroft*
4941.1Smycroft*
4951.1Smycroft*  Check the sign of the adjusted exp and make the value in fp0 the
4961.1Smycroft*  same sign. If the exp was pos then multiply fp1*fp0;
4971.1Smycroft*  else divide fp0/fp1.
4981.1Smycroft*
4991.1Smycroft* Register Usage:
5001.1Smycroft*  norm:
5011.1Smycroft*	( )  a0: pointer to working bcd value
5021.1Smycroft*	(*) fp0: mantissa accumulator
5031.1Smycroft*	( ) fp1: scaling factor - 10**(abs(exp))
5041.1Smycroft*
5051.1Smycroftnorm:
5061.1Smycroft	btst	#30,(a0)	;test the sign of the exponent
5071.1Smycroft	beq.b	mul		;if clear, go to multiply
5081.1Smycroftdiv:
5091.1Smycroft	fdiv.x	fp1,fp0		;exp is negative, so divide mant by exp
5101.1Smycroft	bra.b	end_dec
5111.1Smycroftmul:
5121.1Smycroft	fmul.x	fp1,fp0		;exp is positive, so multiply by exp
5131.1Smycroft*
5141.1Smycroft*
5151.1Smycroft* Clean up and return with result in fp0.
5161.1Smycroft*
5171.1Smycroft* If the final mul/div in decbin incurred an inex exception,
5181.1Smycroft* it will be inex2, but will be reported as inex1 by get_op.
5191.1Smycroft*
5201.1Smycroftend_dec:
5211.1Smycroft	fmove.l	FPSR,d0		;get status register	
5221.1Smycroft	bclr.l	#inex2_bit+8,d0	;test for inex2 and clear it
5231.1Smycroft	fmove.l	d0,FPSR		;return status reg w/o inex2
5241.1Smycroft	beq.b	no_exc		;skip this if no exc
5251.1Smycroft	or.l	#inx1a_mask,USER_FPSR(a6) ;set inex1/ainex
5261.1Smycroftno_exc:
5271.1Smycroft	movem.l	(a7)+,d2-d5
5281.1Smycroft	rts
5291.1Smycroft	end
530