xref: /netbsd-src/sys/arch/m68k/fpsp/stan.sa (revision 95054da1a1715c0b896c3642a426a0f5c9283364)
1*95054da1Ssoren*	$NetBSD: stan.sa,v 1.4 2000/03/13 23:52:32 soren Exp $
257fb77a1Scgd
322ef5fa9Smycroft*	MOTOROLA MICROPROCESSOR & MEMORY TECHNOLOGY GROUP
422ef5fa9Smycroft*	M68000 Hi-Performance Microprocessor Division
522ef5fa9Smycroft*	M68040 Software Package
622ef5fa9Smycroft*
722ef5fa9Smycroft*	M68040 Software Package Copyright (c) 1993, 1994 Motorola Inc.
822ef5fa9Smycroft*	All rights reserved.
922ef5fa9Smycroft*
1022ef5fa9Smycroft*	THE SOFTWARE is provided on an "AS IS" basis and without warranty.
1122ef5fa9Smycroft*	To the maximum extent permitted by applicable law,
1222ef5fa9Smycroft*	MOTOROLA DISCLAIMS ALL WARRANTIES WHETHER EXPRESS OR IMPLIED,
1322ef5fa9Smycroft*	INCLUDING IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A
1422ef5fa9Smycroft*	PARTICULAR PURPOSE and any warranty against infringement with
1522ef5fa9Smycroft*	regard to the SOFTWARE (INCLUDING ANY MODIFIED VERSIONS THEREOF)
1622ef5fa9Smycroft*	and any accompanying written materials.
1722ef5fa9Smycroft*
1822ef5fa9Smycroft*	To the maximum extent permitted by applicable law,
1922ef5fa9Smycroft*	IN NO EVENT SHALL MOTOROLA BE LIABLE FOR ANY DAMAGES WHATSOEVER
2022ef5fa9Smycroft*	(INCLUDING WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS
2122ef5fa9Smycroft*	PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR
2222ef5fa9Smycroft*	OTHER PECUNIARY LOSS) ARISING OF THE USE OR INABILITY TO USE THE
2322ef5fa9Smycroft*	SOFTWARE.  Motorola assumes no responsibility for the maintenance
2422ef5fa9Smycroft*	and support of the SOFTWARE.
2522ef5fa9Smycroft*
2622ef5fa9Smycroft*	You are hereby granted a copyright license to use, modify, and
2722ef5fa9Smycroft*	distribute the SOFTWARE so long as this entire notice is retained
2822ef5fa9Smycroft*	without alteration in any modified and/or redistributed versions,
2922ef5fa9Smycroft*	and that such modified versions are clearly identified as such.
3022ef5fa9Smycroft*	No licenses are granted by implication, estoppel or otherwise
3122ef5fa9Smycroft*	under any patents or trademarks of Motorola, Inc.
3222ef5fa9Smycroft
3322ef5fa9Smycroft*
3422ef5fa9Smycroft*	stan.sa 3.3 7/29/91
3522ef5fa9Smycroft*
3622ef5fa9Smycroft*	The entry point stan computes the tangent of
3722ef5fa9Smycroft*	an input argument;
3822ef5fa9Smycroft*	stand does the same except for denormalized input.
3922ef5fa9Smycroft*
4022ef5fa9Smycroft*	Input: Double-extended number X in location pointed to
4122ef5fa9Smycroft*		by address register a0.
4222ef5fa9Smycroft*
4322ef5fa9Smycroft*	Output: The value tan(X) returned in floating-point register Fp0.
4422ef5fa9Smycroft*
4522ef5fa9Smycroft*	Accuracy and Monotonicity: The returned result is within 3 ulp in
4622ef5fa9Smycroft*		64 significant bit, i.e. within 0.5001 ulp to 53 bits if the
4722ef5fa9Smycroft*		result is subsequently rounded to double precision. The
4822ef5fa9Smycroft*		result is provably monotonic in double precision.
4922ef5fa9Smycroft*
5022ef5fa9Smycroft*	Speed: The program sTAN takes approximately 170 cycles for
51*95054da1Ssoren*		input argument X such that |X| < 15Pi, which is the usual
5222ef5fa9Smycroft*		situation.
5322ef5fa9Smycroft*
5422ef5fa9Smycroft*	Algorithm:
5522ef5fa9Smycroft*
5622ef5fa9Smycroft*	1. If |X| >= 15Pi or |X| < 2**(-40), go to 6.
5722ef5fa9Smycroft*
5822ef5fa9Smycroft*	2. Decompose X as X = N(Pi/2) + r where |r| <= Pi/4. Let
5922ef5fa9Smycroft*		k = N mod 2, so in particular, k = 0 or 1.
6022ef5fa9Smycroft*
6122ef5fa9Smycroft*	3. If k is odd, go to 5.
6222ef5fa9Smycroft*
6322ef5fa9Smycroft*	4. (k is even) Tan(X) = tan(r) and tan(r) is approximated by a
6422ef5fa9Smycroft*		rational function U/V where
6522ef5fa9Smycroft*		U = r + r*s*(P1 + s*(P2 + s*P3)), and
6622ef5fa9Smycroft*		V = 1 + s*(Q1 + s*(Q2 + s*(Q3 + s*Q4))),  s = r*r.
6722ef5fa9Smycroft*		Exit.
6822ef5fa9Smycroft*
6922ef5fa9Smycroft*	4. (k is odd) Tan(X) = -cot(r). Since tan(r) is approximated by a
7022ef5fa9Smycroft*		rational function U/V where
7122ef5fa9Smycroft*		U = r + r*s*(P1 + s*(P2 + s*P3)), and
7222ef5fa9Smycroft*		V = 1 + s*(Q1 + s*(Q2 + s*(Q3 + s*Q4))), s = r*r,
7322ef5fa9Smycroft*		-Cot(r) = -V/U. Exit.
7422ef5fa9Smycroft*
7522ef5fa9Smycroft*	6. If |X| > 1, go to 8.
7622ef5fa9Smycroft*
7722ef5fa9Smycroft*	7. (|X|<2**(-40)) Tan(X) = X. Exit.
7822ef5fa9Smycroft*
7922ef5fa9Smycroft*	8. Overwrite X by X := X rem 2Pi. Now that |X| <= Pi, go back to 2.
8022ef5fa9Smycroft*
8122ef5fa9Smycroft
8222ef5fa9SmycroftSTAN	IDNT	2,1 Motorola 040 Floating Point Software Package
8322ef5fa9Smycroft
8422ef5fa9Smycroft	section	8
8522ef5fa9Smycroft
8622ef5fa9Smycroft	include fpsp.h
8722ef5fa9Smycroft
8822ef5fa9SmycroftBOUNDS1	DC.L $3FD78000,$4004BC7E
8922ef5fa9SmycroftTWOBYPI	DC.L $3FE45F30,$6DC9C883
9022ef5fa9Smycroft
9122ef5fa9SmycroftTANQ4	DC.L $3EA0B759,$F50F8688
9222ef5fa9SmycroftTANP3	DC.L $BEF2BAA5,$A8924F04
9322ef5fa9Smycroft
9422ef5fa9SmycroftTANQ3	DC.L $BF346F59,$B39BA65F,$00000000,$00000000
9522ef5fa9Smycroft
9622ef5fa9SmycroftTANP2	DC.L $3FF60000,$E073D3FC,$199C4A00,$00000000
9722ef5fa9Smycroft
9822ef5fa9SmycroftTANQ2	DC.L $3FF90000,$D23CD684,$15D95FA1,$00000000
9922ef5fa9Smycroft
10022ef5fa9SmycroftTANP1	DC.L $BFFC0000,$8895A6C5,$FB423BCA,$00000000
10122ef5fa9Smycroft
10222ef5fa9SmycroftTANQ1	DC.L $BFFD0000,$EEF57E0D,$A84BC8CE,$00000000
10322ef5fa9Smycroft
10422ef5fa9SmycroftINVTWOPI DC.L $3FFC0000,$A2F9836E,$4E44152A,$00000000
10522ef5fa9Smycroft
10622ef5fa9SmycroftTWOPI1	DC.L $40010000,$C90FDAA2,$00000000,$00000000
10722ef5fa9SmycroftTWOPI2	DC.L $3FDF0000,$85A308D4,$00000000,$00000000
10822ef5fa9Smycroft
10922ef5fa9Smycroft*--N*PI/2, -32 <= N <= 32, IN A LEADING TERM IN EXT. AND TRAILING
11022ef5fa9Smycroft*--TERM IN SGL. NOTE THAT PI IS 64-BIT LONG, THUS N*PI/2 IS AT
11122ef5fa9Smycroft*--MOST 69 BITS LONG.
11222ef5fa9Smycroft	xdef	PITBL
11322ef5fa9SmycroftPITBL:
11422ef5fa9Smycroft  DC.L  $C0040000,$C90FDAA2,$2168C235,$21800000
11522ef5fa9Smycroft  DC.L  $C0040000,$C2C75BCD,$105D7C23,$A0D00000
11622ef5fa9Smycroft  DC.L  $C0040000,$BC7EDCF7,$FF523611,$A1E80000
11722ef5fa9Smycroft  DC.L  $C0040000,$B6365E22,$EE46F000,$21480000
11822ef5fa9Smycroft  DC.L  $C0040000,$AFEDDF4D,$DD3BA9EE,$A1200000
11922ef5fa9Smycroft  DC.L  $C0040000,$A9A56078,$CC3063DD,$21FC0000
12022ef5fa9Smycroft  DC.L  $C0040000,$A35CE1A3,$BB251DCB,$21100000
12122ef5fa9Smycroft  DC.L  $C0040000,$9D1462CE,$AA19D7B9,$A1580000
12222ef5fa9Smycroft  DC.L  $C0040000,$96CBE3F9,$990E91A8,$21E00000
12322ef5fa9Smycroft  DC.L  $C0040000,$90836524,$88034B96,$20B00000
12422ef5fa9Smycroft  DC.L  $C0040000,$8A3AE64F,$76F80584,$A1880000
12522ef5fa9Smycroft  DC.L  $C0040000,$83F2677A,$65ECBF73,$21C40000
12622ef5fa9Smycroft  DC.L  $C0030000,$FB53D14A,$A9C2F2C2,$20000000
12722ef5fa9Smycroft  DC.L  $C0030000,$EEC2D3A0,$87AC669F,$21380000
12822ef5fa9Smycroft  DC.L  $C0030000,$E231D5F6,$6595DA7B,$A1300000
12922ef5fa9Smycroft  DC.L  $C0030000,$D5A0D84C,$437F4E58,$9FC00000
13022ef5fa9Smycroft  DC.L  $C0030000,$C90FDAA2,$2168C235,$21000000
13122ef5fa9Smycroft  DC.L  $C0030000,$BC7EDCF7,$FF523611,$A1680000
13222ef5fa9Smycroft  DC.L  $C0030000,$AFEDDF4D,$DD3BA9EE,$A0A00000
13322ef5fa9Smycroft  DC.L  $C0030000,$A35CE1A3,$BB251DCB,$20900000
13422ef5fa9Smycroft  DC.L  $C0030000,$96CBE3F9,$990E91A8,$21600000
13522ef5fa9Smycroft  DC.L  $C0030000,$8A3AE64F,$76F80584,$A1080000
13622ef5fa9Smycroft  DC.L  $C0020000,$FB53D14A,$A9C2F2C2,$1F800000
13722ef5fa9Smycroft  DC.L  $C0020000,$E231D5F6,$6595DA7B,$A0B00000
13822ef5fa9Smycroft  DC.L  $C0020000,$C90FDAA2,$2168C235,$20800000
13922ef5fa9Smycroft  DC.L  $C0020000,$AFEDDF4D,$DD3BA9EE,$A0200000
14022ef5fa9Smycroft  DC.L  $C0020000,$96CBE3F9,$990E91A8,$20E00000
14122ef5fa9Smycroft  DC.L  $C0010000,$FB53D14A,$A9C2F2C2,$1F000000
14222ef5fa9Smycroft  DC.L  $C0010000,$C90FDAA2,$2168C235,$20000000
14322ef5fa9Smycroft  DC.L  $C0010000,$96CBE3F9,$990E91A8,$20600000
14422ef5fa9Smycroft  DC.L  $C0000000,$C90FDAA2,$2168C235,$1F800000
14522ef5fa9Smycroft  DC.L  $BFFF0000,$C90FDAA2,$2168C235,$1F000000
14622ef5fa9Smycroft  DC.L  $00000000,$00000000,$00000000,$00000000
14722ef5fa9Smycroft  DC.L  $3FFF0000,$C90FDAA2,$2168C235,$9F000000
14822ef5fa9Smycroft  DC.L  $40000000,$C90FDAA2,$2168C235,$9F800000
14922ef5fa9Smycroft  DC.L  $40010000,$96CBE3F9,$990E91A8,$A0600000
15022ef5fa9Smycroft  DC.L  $40010000,$C90FDAA2,$2168C235,$A0000000
15122ef5fa9Smycroft  DC.L  $40010000,$FB53D14A,$A9C2F2C2,$9F000000
15222ef5fa9Smycroft  DC.L  $40020000,$96CBE3F9,$990E91A8,$A0E00000
15322ef5fa9Smycroft  DC.L  $40020000,$AFEDDF4D,$DD3BA9EE,$20200000
15422ef5fa9Smycroft  DC.L  $40020000,$C90FDAA2,$2168C235,$A0800000
15522ef5fa9Smycroft  DC.L  $40020000,$E231D5F6,$6595DA7B,$20B00000
15622ef5fa9Smycroft  DC.L  $40020000,$FB53D14A,$A9C2F2C2,$9F800000
15722ef5fa9Smycroft  DC.L  $40030000,$8A3AE64F,$76F80584,$21080000
15822ef5fa9Smycroft  DC.L  $40030000,$96CBE3F9,$990E91A8,$A1600000
15922ef5fa9Smycroft  DC.L  $40030000,$A35CE1A3,$BB251DCB,$A0900000
16022ef5fa9Smycroft  DC.L  $40030000,$AFEDDF4D,$DD3BA9EE,$20A00000
16122ef5fa9Smycroft  DC.L  $40030000,$BC7EDCF7,$FF523611,$21680000
16222ef5fa9Smycroft  DC.L  $40030000,$C90FDAA2,$2168C235,$A1000000
16322ef5fa9Smycroft  DC.L  $40030000,$D5A0D84C,$437F4E58,$1FC00000
16422ef5fa9Smycroft  DC.L  $40030000,$E231D5F6,$6595DA7B,$21300000
16522ef5fa9Smycroft  DC.L  $40030000,$EEC2D3A0,$87AC669F,$A1380000
16622ef5fa9Smycroft  DC.L  $40030000,$FB53D14A,$A9C2F2C2,$A0000000
16722ef5fa9Smycroft  DC.L  $40040000,$83F2677A,$65ECBF73,$A1C40000
16822ef5fa9Smycroft  DC.L  $40040000,$8A3AE64F,$76F80584,$21880000
16922ef5fa9Smycroft  DC.L  $40040000,$90836524,$88034B96,$A0B00000
17022ef5fa9Smycroft  DC.L  $40040000,$96CBE3F9,$990E91A8,$A1E00000
17122ef5fa9Smycroft  DC.L  $40040000,$9D1462CE,$AA19D7B9,$21580000
17222ef5fa9Smycroft  DC.L  $40040000,$A35CE1A3,$BB251DCB,$A1100000
17322ef5fa9Smycroft  DC.L  $40040000,$A9A56078,$CC3063DD,$A1FC0000
17422ef5fa9Smycroft  DC.L  $40040000,$AFEDDF4D,$DD3BA9EE,$21200000
17522ef5fa9Smycroft  DC.L  $40040000,$B6365E22,$EE46F000,$A1480000
17622ef5fa9Smycroft  DC.L  $40040000,$BC7EDCF7,$FF523611,$21E80000
17722ef5fa9Smycroft  DC.L  $40040000,$C2C75BCD,$105D7C23,$20D00000
17822ef5fa9Smycroft  DC.L  $40040000,$C90FDAA2,$2168C235,$A1800000
17922ef5fa9Smycroft
18022ef5fa9SmycroftINARG	equ	FP_SCR4
18122ef5fa9Smycroft
18222ef5fa9SmycroftTWOTO63 equ     L_SCR1
18322ef5fa9SmycroftENDFLAG	equ	L_SCR2
18422ef5fa9SmycroftN       equ     L_SCR3
18522ef5fa9Smycroft
18622ef5fa9Smycroft	xref	t_frcinx
18722ef5fa9Smycroft	xref	t_extdnrm
18822ef5fa9Smycroft
18922ef5fa9Smycroft	xdef	stand
19022ef5fa9Smycroftstand:
19122ef5fa9Smycroft*--TAN(X) = X FOR DENORMALIZED X
19222ef5fa9Smycroft
19322ef5fa9Smycroft	bra		t_extdnrm
19422ef5fa9Smycroft
19522ef5fa9Smycroft	xdef	stan
19622ef5fa9Smycroftstan:
19722ef5fa9Smycroft	FMOVE.X		(a0),FP0	...LOAD INPUT
19822ef5fa9Smycroft
19922ef5fa9Smycroft	MOVE.L		(A0),D0
20022ef5fa9Smycroft	MOVE.W		4(A0),D0
20122ef5fa9Smycroft	ANDI.L		#$7FFFFFFF,D0
20222ef5fa9Smycroft
20322ef5fa9Smycroft	CMPI.L		#$3FD78000,D0		...|X| >= 2**(-40)?
20422ef5fa9Smycroft	BGE.B		TANOK1
20522ef5fa9Smycroft	BRA.W		TANSM
20622ef5fa9SmycroftTANOK1:
20722ef5fa9Smycroft	CMPI.L		#$4004BC7E,D0		...|X| < 15 PI?
20822ef5fa9Smycroft	BLT.B		TANMAIN
20922ef5fa9Smycroft	BRA.W		REDUCEX
21022ef5fa9Smycroft
21122ef5fa9Smycroft
21222ef5fa9SmycroftTANMAIN:
21322ef5fa9Smycroft*--THIS IS THE USUAL CASE, |X| <= 15 PI.
21422ef5fa9Smycroft*--THE ARGUMENT REDUCTION IS DONE BY TABLE LOOK UP.
21522ef5fa9Smycroft	FMOVE.X		FP0,FP1
21622ef5fa9Smycroft	FMUL.D		TWOBYPI,FP1	...X*2/PI
21722ef5fa9Smycroft
21822ef5fa9Smycroft*--HIDE THE NEXT TWO INSTRUCTIONS
21922ef5fa9Smycroft	lea.l		PITBL+$200,a1 ...TABLE OF N*PI/2, N = -32,...,32
22022ef5fa9Smycroft
22122ef5fa9Smycroft*--FP1 IS NOW READY
22222ef5fa9Smycroft	FMOVE.L		FP1,D0		...CONVERT TO INTEGER
22322ef5fa9Smycroft
22422ef5fa9Smycroft	ASL.L		#4,D0
22522ef5fa9Smycroft	ADDA.L		D0,a1		...ADDRESS N*PIBY2 IN Y1, Y2
22622ef5fa9Smycroft
22722ef5fa9Smycroft	FSUB.X		(a1)+,FP0	...X-Y1
22822ef5fa9Smycroft*--HIDE THE NEXT ONE
22922ef5fa9Smycroft
23022ef5fa9Smycroft	FSUB.S		(a1),FP0	...FP0 IS R = (X-Y1)-Y2
23122ef5fa9Smycroft
23222ef5fa9Smycroft	ROR.L		#5,D0
23322ef5fa9Smycroft	ANDI.L		#$80000000,D0	...D0 WAS ODD IFF D0 < 0
23422ef5fa9Smycroft
23522ef5fa9SmycroftTANCONT:
23622ef5fa9Smycroft
237eddb30abSmycroft	TST.L		D0
23822ef5fa9Smycroft	BLT.W		NODD
23922ef5fa9Smycroft
24022ef5fa9Smycroft	FMOVE.X		FP0,FP1
24122ef5fa9Smycroft	FMUL.X		FP1,FP1	 	...S = R*R
24222ef5fa9Smycroft
24322ef5fa9Smycroft	FMOVE.D		TANQ4,FP3
24422ef5fa9Smycroft	FMOVE.D		TANP3,FP2
24522ef5fa9Smycroft
24622ef5fa9Smycroft	FMUL.X		FP1,FP3	 	...SQ4
24722ef5fa9Smycroft	FMUL.X		FP1,FP2	 	...SP3
24822ef5fa9Smycroft
24922ef5fa9Smycroft	FADD.D		TANQ3,FP3	...Q3+SQ4
25022ef5fa9Smycroft	FADD.X		TANP2,FP2	...P2+SP3
25122ef5fa9Smycroft
25222ef5fa9Smycroft	FMUL.X		FP1,FP3	 	...S(Q3+SQ4)
25322ef5fa9Smycroft	FMUL.X		FP1,FP2	 	...S(P2+SP3)
25422ef5fa9Smycroft
25522ef5fa9Smycroft	FADD.X		TANQ2,FP3	...Q2+S(Q3+SQ4)
25622ef5fa9Smycroft	FADD.X		TANP1,FP2	...P1+S(P2+SP3)
25722ef5fa9Smycroft
25822ef5fa9Smycroft	FMUL.X		FP1,FP3	 	...S(Q2+S(Q3+SQ4))
25922ef5fa9Smycroft	FMUL.X		FP1,FP2	 	...S(P1+S(P2+SP3))
26022ef5fa9Smycroft
26122ef5fa9Smycroft	FADD.X		TANQ1,FP3	...Q1+S(Q2+S(Q3+SQ4))
26222ef5fa9Smycroft	FMUL.X		FP0,FP2	 	...RS(P1+S(P2+SP3))
26322ef5fa9Smycroft
26422ef5fa9Smycroft	FMUL.X		FP3,FP1	 	...S(Q1+S(Q2+S(Q3+SQ4)))
26522ef5fa9Smycroft
26622ef5fa9Smycroft
26722ef5fa9Smycroft	FADD.X		FP2,FP0	 	...R+RS(P1+S(P2+SP3))
26822ef5fa9Smycroft
26922ef5fa9Smycroft
27022ef5fa9Smycroft	FADD.S		#:3F800000,FP1	...1+S(Q1+...)
27122ef5fa9Smycroft
27222ef5fa9Smycroft	FMOVE.L		d1,fpcr		;restore users exceptions
27322ef5fa9Smycroft	FDIV.X		FP1,FP0		;last inst - possible exception set
27422ef5fa9Smycroft
27522ef5fa9Smycroft	bra		t_frcinx
27622ef5fa9Smycroft
27722ef5fa9SmycroftNODD:
27822ef5fa9Smycroft	FMOVE.X		FP0,FP1
27922ef5fa9Smycroft	FMUL.X		FP0,FP0	 	...S = R*R
28022ef5fa9Smycroft
28122ef5fa9Smycroft	FMOVE.D		TANQ4,FP3
28222ef5fa9Smycroft	FMOVE.D		TANP3,FP2
28322ef5fa9Smycroft
28422ef5fa9Smycroft	FMUL.X		FP0,FP3	 	...SQ4
28522ef5fa9Smycroft	FMUL.X		FP0,FP2	 	...SP3
28622ef5fa9Smycroft
28722ef5fa9Smycroft	FADD.D		TANQ3,FP3	...Q3+SQ4
28822ef5fa9Smycroft	FADD.X		TANP2,FP2	...P2+SP3
28922ef5fa9Smycroft
29022ef5fa9Smycroft	FMUL.X		FP0,FP3	 	...S(Q3+SQ4)
29122ef5fa9Smycroft	FMUL.X		FP0,FP2	 	...S(P2+SP3)
29222ef5fa9Smycroft
29322ef5fa9Smycroft	FADD.X		TANQ2,FP3	...Q2+S(Q3+SQ4)
29422ef5fa9Smycroft	FADD.X		TANP1,FP2	...P1+S(P2+SP3)
29522ef5fa9Smycroft
29622ef5fa9Smycroft	FMUL.X		FP0,FP3	 	...S(Q2+S(Q3+SQ4))
29722ef5fa9Smycroft	FMUL.X		FP0,FP2	 	...S(P1+S(P2+SP3))
29822ef5fa9Smycroft
29922ef5fa9Smycroft	FADD.X		TANQ1,FP3	...Q1+S(Q2+S(Q3+SQ4))
30022ef5fa9Smycroft	FMUL.X		FP1,FP2	 	...RS(P1+S(P2+SP3))
30122ef5fa9Smycroft
30222ef5fa9Smycroft	FMUL.X		FP3,FP0	 	...S(Q1+S(Q2+S(Q3+SQ4)))
30322ef5fa9Smycroft
30422ef5fa9Smycroft
30522ef5fa9Smycroft	FADD.X		FP2,FP1	 	...R+RS(P1+S(P2+SP3))
30622ef5fa9Smycroft	FADD.S		#:3F800000,FP0	...1+S(Q1+...)
30722ef5fa9Smycroft
30822ef5fa9Smycroft
30922ef5fa9Smycroft	FMOVE.X		FP1,-(sp)
31022ef5fa9Smycroft	EORI.L		#$80000000,(sp)
31122ef5fa9Smycroft
31222ef5fa9Smycroft	FMOVE.L		d1,fpcr	 	;restore users exceptions
31322ef5fa9Smycroft	FDIV.X		(sp)+,FP0	;last inst - possible exception set
31422ef5fa9Smycroft
31522ef5fa9Smycroft	bra		t_frcinx
31622ef5fa9Smycroft
31722ef5fa9SmycroftTANBORS:
31822ef5fa9Smycroft*--IF |X| > 15PI, WE USE THE GENERAL ARGUMENT REDUCTION.
31922ef5fa9Smycroft*--IF |X| < 2**(-40), RETURN X OR 1.
32022ef5fa9Smycroft	CMPI.L		#$3FFF8000,D0
32122ef5fa9Smycroft	BGT.B		REDUCEX
32222ef5fa9Smycroft
32322ef5fa9SmycroftTANSM:
32422ef5fa9Smycroft
32522ef5fa9Smycroft	FMOVE.X		FP0,-(sp)
32622ef5fa9Smycroft	FMOVE.L		d1,fpcr		 ;restore users exceptions
32722ef5fa9Smycroft	FMOVE.X		(sp)+,FP0	;last inst - posibble exception set
32822ef5fa9Smycroft
32922ef5fa9Smycroft	bra		t_frcinx
33022ef5fa9Smycroft
33122ef5fa9Smycroft
33222ef5fa9SmycroftREDUCEX:
33322ef5fa9Smycroft*--WHEN REDUCEX IS USED, THE CODE WILL INEVITABLY BE SLOW.
33422ef5fa9Smycroft*--THIS REDUCTION METHOD, HOWEVER, IS MUCH FASTER THAN USING
33522ef5fa9Smycroft*--THE REMAINDER INSTRUCTION WHICH IS NOW IN SOFTWARE.
33622ef5fa9Smycroft
33722ef5fa9Smycroft	FMOVEM.X	FP2-FP5,-(A7)	...save FP2 through FP5
33822ef5fa9Smycroft	MOVE.L		D2,-(A7)
33922ef5fa9Smycroft        FMOVE.S         #:00000000,FP1
34022ef5fa9Smycroft
34122ef5fa9Smycroft*--If compact form of abs(arg) in d0=$7ffeffff, argument is so large that
34222ef5fa9Smycroft*--there is a danger of unwanted overflow in first LOOP iteration.  In this
34322ef5fa9Smycroft*--case, reduce argument by one remainder step to make subsequent reduction
34422ef5fa9Smycroft*--safe.
34522ef5fa9Smycroft	cmpi.l	#$7ffeffff,d0		;is argument dangerously large?
34622ef5fa9Smycroft	bne.b	LOOP
34722ef5fa9Smycroft	move.l	#$7ffe0000,FP_SCR2(a6)	;yes
34822ef5fa9Smycroft*					;create 2**16383*PI/2
34922ef5fa9Smycroft	move.l	#$c90fdaa2,FP_SCR2+4(a6)
35022ef5fa9Smycroft	clr.l	FP_SCR2+8(a6)
35122ef5fa9Smycroft	ftst.x	fp0			;test sign of argument
35222ef5fa9Smycroft	move.l	#$7fdc0000,FP_SCR3(a6)	;create low half of 2**16383*
35322ef5fa9Smycroft*					;PI/2 at FP_SCR3
35422ef5fa9Smycroft	move.l	#$85a308d3,FP_SCR3+4(a6)
35522ef5fa9Smycroft	clr.l   FP_SCR3+8(a6)
35622ef5fa9Smycroft	fblt.w	red_neg
35722ef5fa9Smycroft	or.w	#$8000,FP_SCR2(a6)	;positive arg
35822ef5fa9Smycroft	or.w	#$8000,FP_SCR3(a6)
35922ef5fa9Smycroftred_neg:
36022ef5fa9Smycroft	fadd.x  FP_SCR2(a6),fp0		;high part of reduction is exact
36122ef5fa9Smycroft	fmove.x  fp0,fp1		;save high result in fp1
36222ef5fa9Smycroft	fadd.x  FP_SCR3(a6),fp0		;low part of reduction
36322ef5fa9Smycroft	fsub.x  fp0,fp1			;determine low component of result
36422ef5fa9Smycroft	fadd.x  FP_SCR3(a6),fp1		;fp0/fp1 are reduced argument.
36522ef5fa9Smycroft
36622ef5fa9Smycroft*--ON ENTRY, FP0 IS X, ON RETURN, FP0 IS X REM PI/2, |X| <= PI/4.
36722ef5fa9Smycroft*--integer quotient will be stored in N
36822ef5fa9Smycroft*--Intermeditate remainder is 66-bit long; (R,r) in (FP0,FP1)
36922ef5fa9Smycroft
37022ef5fa9SmycroftLOOP:
37122ef5fa9Smycroft	FMOVE.X		FP0,INARG(a6)	...+-2**K * F, 1 <= F < 2
37222ef5fa9Smycroft	MOVE.W		INARG(a6),D0
37322ef5fa9Smycroft        MOVE.L          D0,A1		...save a copy of D0
37422ef5fa9Smycroft	ANDI.L		#$00007FFF,D0
37522ef5fa9Smycroft	SUBI.L		#$00003FFF,D0	...D0 IS K
37622ef5fa9Smycroft	CMPI.L		#28,D0
37722ef5fa9Smycroft	BLE.B		LASTLOOP
37822ef5fa9SmycroftCONTLOOP:
37922ef5fa9Smycroft	SUBI.L		#27,D0	 ...D0 IS L := K-27
380eddb30abSmycroft	CLR.L		ENDFLAG(a6)
38122ef5fa9Smycroft	BRA.B		WORK
38222ef5fa9SmycroftLASTLOOP:
38322ef5fa9Smycroft	CLR.L		D0		...D0 IS L := 0
38422ef5fa9Smycroft	MOVE.L		#1,ENDFLAG(a6)
38522ef5fa9Smycroft
38622ef5fa9SmycroftWORK:
38722ef5fa9Smycroft*--FIND THE REMAINDER OF (R,r) W.R.T.	2**L * (PI/2). L IS SO CHOSEN
38822ef5fa9Smycroft*--THAT	INT( X * (2/PI) / 2**(L) ) < 2**29.
38922ef5fa9Smycroft
39022ef5fa9Smycroft*--CREATE 2**(-L) * (2/PI), SIGN(INARG)*2**(63),
39122ef5fa9Smycroft*--2**L * (PIby2_1), 2**L * (PIby2_2)
39222ef5fa9Smycroft
39322ef5fa9Smycroft	MOVE.L		#$00003FFE,D2	...BIASED EXPO OF 2/PI
39422ef5fa9Smycroft	SUB.L		D0,D2		...BIASED EXPO OF 2**(-L)*(2/PI)
39522ef5fa9Smycroft
39622ef5fa9Smycroft	MOVE.L		#$A2F9836E,FP_SCR1+4(a6)
39722ef5fa9Smycroft	MOVE.L		#$4E44152A,FP_SCR1+8(a6)
39822ef5fa9Smycroft	MOVE.W		D2,FP_SCR1(a6)	...FP_SCR1 is 2**(-L)*(2/PI)
39922ef5fa9Smycroft
40022ef5fa9Smycroft	FMOVE.X		FP0,FP2
40122ef5fa9Smycroft	FMUL.X		FP_SCR1(a6),FP2
40222ef5fa9Smycroft*--WE MUST NOW FIND INT(FP2). SINCE WE NEED THIS VALUE IN
40322ef5fa9Smycroft*--FLOATING POINT FORMAT, THE TWO FMOVE'S	FMOVE.L FP <--> N
40422ef5fa9Smycroft*--WILL BE TOO INEFFICIENT. THE WAY AROUND IT IS THAT
40522ef5fa9Smycroft*--(SIGN(INARG)*2**63	+	FP2) - SIGN(INARG)*2**63 WILL GIVE
40622ef5fa9Smycroft*--US THE DESIRED VALUE IN FLOATING POINT.
40722ef5fa9Smycroft
40822ef5fa9Smycroft*--HIDE SIX CYCLES OF INSTRUCTION
40922ef5fa9Smycroft        MOVE.L		A1,D2
41022ef5fa9Smycroft        SWAP		D2
41122ef5fa9Smycroft	ANDI.L		#$80000000,D2
41222ef5fa9Smycroft	ORI.L		#$5F000000,D2	...D2 IS SIGN(INARG)*2**63 IN SGL
41322ef5fa9Smycroft	MOVE.L		D2,TWOTO63(a6)
41422ef5fa9Smycroft
41522ef5fa9Smycroft	MOVE.L		D0,D2
41622ef5fa9Smycroft	ADDI.L		#$00003FFF,D2	...BIASED EXPO OF 2**L * (PI/2)
41722ef5fa9Smycroft
41822ef5fa9Smycroft*--FP2 IS READY
41922ef5fa9Smycroft	FADD.S		TWOTO63(a6),FP2	...THE FRACTIONAL PART OF FP1 IS ROUNDED
42022ef5fa9Smycroft
42122ef5fa9Smycroft*--HIDE 4 CYCLES OF INSTRUCTION; creating 2**(L)*Piby2_1  and  2**(L)*Piby2_2
42222ef5fa9Smycroft        MOVE.W		D2,FP_SCR2(a6)
42322ef5fa9Smycroft	CLR.W           FP_SCR2+2(a6)
42422ef5fa9Smycroft	MOVE.L		#$C90FDAA2,FP_SCR2+4(a6)
42522ef5fa9Smycroft	CLR.L		FP_SCR2+8(a6)		...FP_SCR2 is  2**(L) * Piby2_1
42622ef5fa9Smycroft
42722ef5fa9Smycroft*--FP2 IS READY
42822ef5fa9Smycroft	FSUB.S		TWOTO63(a6),FP2		...FP2 is N
42922ef5fa9Smycroft
43022ef5fa9Smycroft	ADDI.L		#$00003FDD,D0
43122ef5fa9Smycroft        MOVE.W		D0,FP_SCR3(a6)
43222ef5fa9Smycroft	CLR.W           FP_SCR3+2(a6)
43322ef5fa9Smycroft	MOVE.L		#$85A308D3,FP_SCR3+4(a6)
43422ef5fa9Smycroft	CLR.L		FP_SCR3+8(a6)		...FP_SCR3 is 2**(L) * Piby2_2
43522ef5fa9Smycroft
43622ef5fa9Smycroft	MOVE.L		ENDFLAG(a6),D0
43722ef5fa9Smycroft
43822ef5fa9Smycroft*--We are now ready to perform (R+r) - N*P1 - N*P2, P1 = 2**(L) * Piby2_1 and
43922ef5fa9Smycroft*--P2 = 2**(L) * Piby2_2
44022ef5fa9Smycroft	FMOVE.X		FP2,FP4
44122ef5fa9Smycroft	FMul.X		FP_SCR2(a6),FP4		...W = N*P1
44222ef5fa9Smycroft	FMove.X		FP2,FP5
44322ef5fa9Smycroft	FMul.X		FP_SCR3(a6),FP5		...w = N*P2
44422ef5fa9Smycroft	FMove.X		FP4,FP3
44522ef5fa9Smycroft*--we want P+p = W+w  but  |p| <= half ulp of P
44622ef5fa9Smycroft*--Then, we need to compute  A := R-P   and  a := r-p
44722ef5fa9Smycroft	FAdd.X		FP5,FP3			...FP3 is P
44822ef5fa9Smycroft	FSub.X		FP3,FP4			...W-P
44922ef5fa9Smycroft
45022ef5fa9Smycroft	FSub.X		FP3,FP0			...FP0 is A := R - P
45122ef5fa9Smycroft        FAdd.X		FP5,FP4			...FP4 is p = (W-P)+w
45222ef5fa9Smycroft
45322ef5fa9Smycroft	FMove.X		FP0,FP3			...FP3 A
45422ef5fa9Smycroft	FSub.X		FP4,FP1			...FP1 is a := r - p
45522ef5fa9Smycroft
45622ef5fa9Smycroft*--Now we need to normalize (A,a) to  "new (R,r)" where R+r = A+a but
45722ef5fa9Smycroft*--|r| <= half ulp of R.
45822ef5fa9Smycroft	FAdd.X		FP1,FP0			...FP0 is R := A+a
45922ef5fa9Smycroft*--No need to calculate r if this is the last loop
460eddb30abSmycroft	TST.L		D0
46122ef5fa9Smycroft	BGT.W		RESTORE
46222ef5fa9Smycroft
46322ef5fa9Smycroft*--Need to calculate r
46422ef5fa9Smycroft	FSub.X		FP0,FP3			...A-R
46522ef5fa9Smycroft	FAdd.X		FP3,FP1			...FP1 is r := (A-R)+a
46622ef5fa9Smycroft	BRA.W		LOOP
46722ef5fa9Smycroft
46822ef5fa9SmycroftRESTORE:
46922ef5fa9Smycroft        FMOVE.L		FP2,N(a6)
47022ef5fa9Smycroft	MOVE.L		(A7)+,D2
47122ef5fa9Smycroft	FMOVEM.X	(A7)+,FP2-FP5
47222ef5fa9Smycroft
47322ef5fa9Smycroft
47422ef5fa9Smycroft	MOVE.L		N(a6),D0
47522ef5fa9Smycroft        ROR.L		#1,D0
47622ef5fa9Smycroft
47722ef5fa9Smycroft
47822ef5fa9Smycroft	BRA.W		TANCONT
47922ef5fa9Smycroft
48022ef5fa9Smycroft	end
481