11.3Swiz*	$NetBSD: sint.sa,v 1.3 2010/02/09 23:07:14 wiz Exp $
21.2Scgd
31.1Smycroft*	MOTOROLA MICROPROCESSOR & MEMORY TECHNOLOGY GROUP
41.1Smycroft*	M68000 Hi-Performance Microprocessor Division
51.1Smycroft*	M68040 Software Package 
61.1Smycroft*
71.1Smycroft*	M68040 Software Package Copyright (c) 1993, 1994 Motorola Inc.
81.1Smycroft*	All rights reserved.
91.1Smycroft*
101.1Smycroft*	THE SOFTWARE is provided on an "AS IS" basis and without warranty.
111.1Smycroft*	To the maximum extent permitted by applicable law,
121.1Smycroft*	MOTOROLA DISCLAIMS ALL WARRANTIES WHETHER EXPRESS OR IMPLIED,
131.1Smycroft*	INCLUDING IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A
141.1Smycroft*	PARTICULAR PURPOSE and any warranty against infringement with
151.1Smycroft*	regard to the SOFTWARE (INCLUDING ANY MODIFIED VERSIONS THEREOF)
161.1Smycroft*	and any accompanying written materials. 
171.1Smycroft*
181.1Smycroft*	To the maximum extent permitted by applicable law,
191.1Smycroft*	IN NO EVENT SHALL MOTOROLA BE LIABLE FOR ANY DAMAGES WHATSOEVER
201.1Smycroft*	(INCLUDING WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS
211.1Smycroft*	PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR
221.1Smycroft*	OTHER PECUNIARY LOSS) ARISING OF THE USE OR INABILITY TO USE THE
231.1Smycroft*	SOFTWARE.  Motorola assumes no responsibility for the maintenance
241.1Smycroft*	and support of the SOFTWARE.  
251.1Smycroft*
261.1Smycroft*	You are hereby granted a copyright license to use, modify, and
271.1Smycroft*	distribute the SOFTWARE so long as this entire notice is retained
281.1Smycroft*	without alteration in any modified and/or redistributed versions,
291.1Smycroft*	and that such modified versions are clearly identified as such.
301.1Smycroft*	No licenses are granted by implication, estoppel or otherwise
311.1Smycroft*	under any patents or trademarks of Motorola, Inc.
321.1Smycroft
331.1Smycroft*
341.1Smycroft*	sint.sa 3.1 12/10/90
351.1Smycroft*
361.1Smycroft*	The entry point sINT computes the rounded integer 
371.1Smycroft*	equivalent of the input argument, sINTRZ computes 
381.1Smycroft*	the integer rounded to zero of the input argument.
391.1Smycroft*
401.1Smycroft*	Entry points sint and sintrz are called from do_func
411.1Smycroft*	to emulate the fint and fintrz unimplemented instructions,
421.1Smycroft*	respectively.  Entry point sintdo is used by bindec.
431.1Smycroft*
441.1Smycroft*	Input: (Entry points sint and sintrz) Double-extended
451.1Smycroft*		number X in the ETEMP space in the floating-point
461.1Smycroft*		save stack.
471.1Smycroft*	       (Entry point sintdo) Double-extended number X in
481.1Smycroft*		location pointed to by the address register a0.
491.1Smycroft*	       (Entry point sintd) Double-extended denormalized
501.1Smycroft*		number X in the ETEMP space in the floating-point
511.1Smycroft*		save stack.
521.1Smycroft*
531.1Smycroft*	Output: The function returns int(X) or intrz(X) in fp0.
541.1Smycroft*
551.1Smycroft*	Modifies: fp0.
561.1Smycroft*
571.1Smycroft*	Algorithm: (sint and sintrz)
581.1Smycroft*
591.1Smycroft*	1. If exp(X) >= 63, return X. 
601.1Smycroft*	   If exp(X) < 0, return +/- 0 or +/- 1, according to
611.1Smycroft*	   the rounding mode.
621.1Smycroft*	
631.1Smycroft*	2. (X is in range) set rsc = 63 - exp(X). Unnormalize the
641.1Smycroft*	   result to the exponent $403e.
651.1Smycroft*
661.1Smycroft*	3. Round the result in the mode given in USER_FPCR. For
671.1Smycroft*	   sintrz, force round-to-zero mode.
681.1Smycroft*
691.1Smycroft*	4. Normalize the rounded result; store in fp0.
701.1Smycroft*
711.1Smycroft*	For the denormalized cases, force the correct result
721.1Smycroft*	for the given sign and rounding mode.
731.1Smycroft*
741.1Smycroft*		        Sign(X)
751.1Smycroft*		RMODE   +    -
761.1Smycroft*		-----  --------
771.1Smycroft*		 RN    +0   -0
781.1Smycroft*		 RZ    +0   -0
791.1Smycroft*		 RM    +0   -1
801.1Smycroft*		 RP    +1   -0
811.1Smycroft*
821.1Smycroft
831.1SmycroftSINT    IDNT    2,1 Motorola 040 Floating Point Software Package
841.1Smycroft
851.1Smycroft	section	8
861.1Smycroft
871.1Smycroft	include	fpsp.h
881.1Smycroft
891.1Smycroft	xref	dnrm_lp
901.1Smycroft	xref	nrm_set
911.1Smycroft	xref	round
921.1Smycroft	xref	t_inx2
931.1Smycroft	xref	ld_pone
941.1Smycroft	xref	ld_mone
951.1Smycroft	xref	ld_pzero
961.1Smycroft	xref	ld_mzero
971.1Smycroft	xref	snzrinx
981.1Smycroft
991.1Smycroft*
1001.1Smycroft*	FINT
1011.1Smycroft*
1021.1Smycroft	xdef	sint
1031.1Smycroftsint:
1041.1Smycroft	bfextu	FPCR_MODE(a6){2:2},d1	;use user's mode for rounding
1051.1Smycroft*					;implicity has extend precision
1061.1Smycroft*					;in upper word. 
1071.1Smycroft	move.l	d1,L_SCR1(a6)		;save mode bits
1081.1Smycroft	bra.b	sintexc			
1091.1Smycroft
1101.1Smycroft*
1111.1Smycroft*	FINT with extended denorm inputs.
1121.1Smycroft*
1131.1Smycroft	xdef	sintd
1141.1Smycroftsintd:
1151.1Smycroft	btst.b	#5,FPCR_MODE(a6)
1161.1Smycroft	beq	snzrinx		;if round nearest or round zero, +/- 0
1171.1Smycroft	btst.b	#4,FPCR_MODE(a6)
1181.1Smycroft	beq.b	rnd_mns
1191.1Smycroftrnd_pls:
1201.1Smycroft	btst.b	#sign_bit,LOCAL_EX(a0)
1211.1Smycroft	bne.b	sintmz
1221.1Smycroft	bsr	ld_pone		;if round plus inf and pos, answer is +1
1231.1Smycroft	bra	t_inx2
1241.1Smycroftrnd_mns:
1251.1Smycroft	btst.b	#sign_bit,LOCAL_EX(a0)
1261.1Smycroft	beq.b	sintpz
1271.1Smycroft	bsr	ld_mone		;if round mns inf and neg, answer is -1
1281.1Smycroft	bra	t_inx2
1291.1Smycroftsintpz:
1301.1Smycroft	bsr	ld_pzero
1311.1Smycroft	bra	t_inx2
1321.1Smycroftsintmz:
1331.1Smycroft	bsr	ld_mzero
1341.1Smycroft	bra	t_inx2
1351.1Smycroft
1361.1Smycroft*
1371.1Smycroft*	FINTRZ
1381.1Smycroft*
1391.1Smycroft	xdef	sintrz
1401.1Smycroftsintrz:
1411.1Smycroft	move.l	#1,L_SCR1(a6)		;use rz mode for rounding
1421.1Smycroft*					;implicity has extend precision
1431.1Smycroft*					;in upper word. 
1441.1Smycroft	bra.b	sintexc			
1451.1Smycroft*
1461.1Smycroft*	SINTDO
1471.1Smycroft*
1481.1Smycroft*	Input:	a0 points to an IEEE extended format operand
1491.1Smycroft* 	Output:	fp0 has the result 
1501.1Smycroft*
1511.3Swiz* Exceptions:
1521.1Smycroft*
1531.1Smycroft* If the subroutine results in an inexact operation, the inx2 and
1541.1Smycroft* ainx bits in the USER_FPSR are set.
1551.1Smycroft*
1561.1Smycroft*
1571.1Smycroft	xdef	sintdo
1581.1Smycroftsintdo:
1591.1Smycroft	bfextu	FPCR_MODE(a6){2:2},d1	;use user's mode for rounding
1601.1Smycroft*					;implicitly has ext precision
1611.1Smycroft*					;in upper word. 
1621.1Smycroft	move.l	d1,L_SCR1(a6)		;save mode bits
1631.1Smycroft*
1641.1Smycroft* Real work of sint is in sintexc
1651.1Smycroft*
1661.1Smycroftsintexc:
1671.1Smycroft	bclr.b	#sign_bit,LOCAL_EX(a0)	;convert to internal extended
1681.1Smycroft*					;format
1691.1Smycroft	sne	LOCAL_SGN(a0)		
1701.1Smycroft	cmp.w	#$403e,LOCAL_EX(a0)	;check if (unbiased) exp > 63
1711.1Smycroft	bgt.b	out_rnge			;branch if exp < 63
1721.1Smycroft	cmp.w	#$3ffd,LOCAL_EX(a0)	;check if (unbiased) exp < 0
1731.1Smycroft	bgt.w	in_rnge			;if 63 >= exp > 0, do calc
1741.1Smycroft*
1751.1Smycroft* Input is less than zero.  Restore sign, and check for directed
1761.1Smycroft* rounding modes.  L_SCR1 contains the rmode in the lower byte.
1771.1Smycroft*
1781.1Smycroftun_rnge:
1791.1Smycroft	btst.b	#1,L_SCR1+3(a6)		;check for rn and rz
1801.1Smycroft	beq.b	un_rnrz
1811.1Smycroft	tst.b	LOCAL_SGN(a0)		;check for sign
1821.1Smycroft	bne.b	un_rmrp_neg
1831.1Smycroft*
1841.1Smycroft* Sign is +.  If rp, load +1.0, if rm, load +0.0
1851.1Smycroft*
1861.1Smycroft	cmpi.b	#3,L_SCR1+3(a6)		;check for rp
1871.1Smycroft	beq.b	un_ldpone		;if rp, load +1.0
1881.1Smycroft	bsr	ld_pzero		;if rm, load +0.0
1891.1Smycroft	bra	t_inx2
1901.1Smycroftun_ldpone:
1911.1Smycroft	bsr	ld_pone
1921.1Smycroft	bra	t_inx2
1931.1Smycroft*
1941.1Smycroft* Sign is -.  If rm, load -1.0, if rp, load -0.0
1951.1Smycroft*
1961.1Smycroftun_rmrp_neg:
1971.1Smycroft	cmpi.b	#2,L_SCR1+3(a6)		;check for rm
1981.1Smycroft	beq.b	un_ldmone		;if rm, load -1.0
1991.1Smycroft	bsr	ld_mzero		;if rp, load -0.0
2001.1Smycroft	bra	t_inx2
2011.1Smycroftun_ldmone:
2021.1Smycroft	bsr	ld_mone
2031.1Smycroft	bra	t_inx2
2041.1Smycroft*
2051.1Smycroft* Rmode is rn or rz; return signed zero
2061.1Smycroft*
2071.1Smycroftun_rnrz:
2081.1Smycroft	tst.b	LOCAL_SGN(a0)		;check for sign
2091.1Smycroft	bne.b	un_rnrz_neg
2101.1Smycroft	bsr	ld_pzero
2111.1Smycroft	bra	t_inx2
2121.1Smycroftun_rnrz_neg:
2131.1Smycroft	bsr	ld_mzero
2141.1Smycroft	bra	t_inx2
2151.1Smycroft	
2161.1Smycroft*
2171.1Smycroft* Input is greater than 2^63.  All bits are significant.  Return
2181.1Smycroft* the input.
2191.1Smycroft*
2201.1Smycroftout_rnge:
2211.1Smycroft	bfclr	LOCAL_SGN(a0){0:8}	;change back to IEEE ext format
2221.1Smycroft	beq.b	intps
2231.1Smycroft	bset.b	#sign_bit,LOCAL_EX(a0)
2241.1Smycroftintps:
2251.1Smycroft	fmove.l	fpcr,-(sp)
2261.1Smycroft	fmove.l	#0,fpcr
2271.1Smycroft	fmove.x LOCAL_EX(a0),fp0	;if exp > 63
2281.1Smycroft*					;then return X to the user
2291.1Smycroft*					;there are no fraction bits
2301.1Smycroft	fmove.l	(sp)+,fpcr
2311.1Smycroft	rts
2321.1Smycroft
2331.1Smycroftin_rnge:
2341.1Smycroft* 					;shift off fraction bits
2351.1Smycroft	clr.l	d0			;clear d0 - initial g,r,s for
2361.1Smycroft*					;dnrm_lp
2371.1Smycroft	move.l	#$403e,d1		;set threshold for dnrm_lp
2381.1Smycroft*					;assumes a0 points to operand
2391.1Smycroft	bsr	dnrm_lp
2401.1Smycroft*					;returns unnormalized number
2411.1Smycroft*					;pointed by a0
2421.1Smycroft*					;output d0 supplies g,r,s
2431.1Smycroft*					;used by round
2441.1Smycroft	move.l	L_SCR1(a6),d1		;use selected rounding mode
2451.1Smycroft*
2461.1Smycroft*
2471.1Smycroft	bsr	round			;round the unnorm based on users
2481.1Smycroft*					;input	a0 ptr to ext X
2491.1Smycroft*					;	d0 g,r,s bits
2501.1Smycroft*					;	d1 PREC/MODE info
2511.1Smycroft*					;output a0 ptr to rounded result
2521.1Smycroft*					;inexact flag set in USER_FPSR
2531.1Smycroft*					;if initial grs set
2541.1Smycroft*
2551.1Smycroft* normalize the rounded result and store value in fp0
2561.1Smycroft*
2571.1Smycroft	bsr	nrm_set			;normalize the unnorm
2581.1Smycroft*					;Input: a0 points to operand to
2591.1Smycroft*					;be normalized
2601.1Smycroft*					;Output: a0 points to normalized
2611.1Smycroft*					;result
2621.1Smycroft	bfclr	LOCAL_SGN(a0){0:8}
2631.1Smycroft	beq.b	nrmrndp
2641.1Smycroft	bset.b	#sign_bit,LOCAL_EX(a0)	;return to IEEE extended format
2651.1Smycroftnrmrndp:
2661.1Smycroft	fmove.l	fpcr,-(sp)
2671.1Smycroft	fmove.l	#0,fpcr
2681.1Smycroft	fmove.x LOCAL_EX(a0),fp0	;move result to fp0
2691.1Smycroft	fmove.l	(sp)+,fpcr
2701.1Smycroft	rts
2711.1Smycroft
2721.1Smycroft	end
273