xref: /OK3568_Linux_fs/kernel/arch/x86/math-emu/poly_tan.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*---------------------------------------------------------------------------+
3*4882a593Smuzhiyun  |  poly_tan.c                                                               |
4*4882a593Smuzhiyun  |                                                                           |
5*4882a593Smuzhiyun  | Compute the tan of a FPU_REG, using a polynomial approximation.           |
6*4882a593Smuzhiyun  |                                                                           |
7*4882a593Smuzhiyun  | Copyright (C) 1992,1993,1994,1997,1999                                    |
8*4882a593Smuzhiyun  |                       W. Metzenthen, 22 Parker St, Ormond, Vic 3163,      |
9*4882a593Smuzhiyun  |                       Australia.  E-mail   billm@melbpc.org.au            |
10*4882a593Smuzhiyun  |                                                                           |
11*4882a593Smuzhiyun  |                                                                           |
12*4882a593Smuzhiyun  +---------------------------------------------------------------------------*/
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include "exception.h"
15*4882a593Smuzhiyun #include "reg_constant.h"
16*4882a593Smuzhiyun #include "fpu_emu.h"
17*4882a593Smuzhiyun #include "fpu_system.h"
18*4882a593Smuzhiyun #include "control_w.h"
19*4882a593Smuzhiyun #include "poly.h"
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun #define	HiPOWERop	3	/* odd poly, positive terms */
22*4882a593Smuzhiyun static const unsigned long long oddplterm[HiPOWERop] = {
23*4882a593Smuzhiyun 	0x0000000000000000LL,
24*4882a593Smuzhiyun 	0x0051a1cf08fca228LL,
25*4882a593Smuzhiyun 	0x0000000071284ff7LL
26*4882a593Smuzhiyun };
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun #define	HiPOWERon	2	/* odd poly, negative terms */
29*4882a593Smuzhiyun static const unsigned long long oddnegterm[HiPOWERon] = {
30*4882a593Smuzhiyun 	0x1291a9a184244e80LL,
31*4882a593Smuzhiyun 	0x0000583245819c21LL
32*4882a593Smuzhiyun };
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun #define	HiPOWERep	2	/* even poly, positive terms */
35*4882a593Smuzhiyun static const unsigned long long evenplterm[HiPOWERep] = {
36*4882a593Smuzhiyun 	0x0e848884b539e888LL,
37*4882a593Smuzhiyun 	0x00003c7f18b887daLL
38*4882a593Smuzhiyun };
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun #define	HiPOWERen	2	/* even poly, negative terms */
41*4882a593Smuzhiyun static const unsigned long long evennegterm[HiPOWERen] = {
42*4882a593Smuzhiyun 	0xf1f0200fd51569ccLL,
43*4882a593Smuzhiyun 	0x003afb46105c4432LL
44*4882a593Smuzhiyun };
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun static const unsigned long long twothirds = 0xaaaaaaaaaaaaaaabLL;
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun /*--- poly_tan() ------------------------------------------------------------+
49*4882a593Smuzhiyun  |                                                                           |
50*4882a593Smuzhiyun  +---------------------------------------------------------------------------*/
poly_tan(FPU_REG * st0_ptr)51*4882a593Smuzhiyun void poly_tan(FPU_REG *st0_ptr)
52*4882a593Smuzhiyun {
53*4882a593Smuzhiyun 	long int exponent;
54*4882a593Smuzhiyun 	int invert;
55*4882a593Smuzhiyun 	Xsig argSq, argSqSq, accumulatoro, accumulatore, accum,
56*4882a593Smuzhiyun 	    argSignif, fix_up;
57*4882a593Smuzhiyun 	unsigned long adj;
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun 	exponent = exponent(st0_ptr);
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun #ifdef PARANOID
62*4882a593Smuzhiyun 	if (signnegative(st0_ptr)) {	/* Can't hack a number < 0.0 */
63*4882a593Smuzhiyun 		arith_invalid(0);
64*4882a593Smuzhiyun 		return;
65*4882a593Smuzhiyun 	}			/* Need a positive number */
66*4882a593Smuzhiyun #endif /* PARANOID */
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 	/* Split the problem into two domains, smaller and larger than pi/4 */
69*4882a593Smuzhiyun 	if ((exponent == 0)
70*4882a593Smuzhiyun 	    || ((exponent == -1) && (st0_ptr->sigh > 0xc90fdaa2))) {
71*4882a593Smuzhiyun 		/* The argument is greater than (approx) pi/4 */
72*4882a593Smuzhiyun 		invert = 1;
73*4882a593Smuzhiyun 		accum.lsw = 0;
74*4882a593Smuzhiyun 		XSIG_LL(accum) = significand(st0_ptr);
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 		if (exponent == 0) {
77*4882a593Smuzhiyun 			/* The argument is >= 1.0 */
78*4882a593Smuzhiyun 			/* Put the binary point at the left. */
79*4882a593Smuzhiyun 			XSIG_LL(accum) <<= 1;
80*4882a593Smuzhiyun 		}
81*4882a593Smuzhiyun 		/* pi/2 in hex is: 1.921fb54442d18469 898CC51701B839A2 52049C1 */
82*4882a593Smuzhiyun 		XSIG_LL(accum) = 0x921fb54442d18469LL - XSIG_LL(accum);
83*4882a593Smuzhiyun 		/* This is a special case which arises due to rounding. */
84*4882a593Smuzhiyun 		if (XSIG_LL(accum) == 0xffffffffffffffffLL) {
85*4882a593Smuzhiyun 			FPU_settag0(TAG_Valid);
86*4882a593Smuzhiyun 			significand(st0_ptr) = 0x8a51e04daabda360LL;
87*4882a593Smuzhiyun 			setexponent16(st0_ptr,
88*4882a593Smuzhiyun 				      (0x41 + EXTENDED_Ebias) | SIGN_Negative);
89*4882a593Smuzhiyun 			return;
90*4882a593Smuzhiyun 		}
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 		argSignif.lsw = accum.lsw;
93*4882a593Smuzhiyun 		XSIG_LL(argSignif) = XSIG_LL(accum);
94*4882a593Smuzhiyun 		exponent = -1 + norm_Xsig(&argSignif);
95*4882a593Smuzhiyun 	} else {
96*4882a593Smuzhiyun 		invert = 0;
97*4882a593Smuzhiyun 		argSignif.lsw = 0;
98*4882a593Smuzhiyun 		XSIG_LL(accum) = XSIG_LL(argSignif) = significand(st0_ptr);
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 		if (exponent < -1) {
101*4882a593Smuzhiyun 			/* shift the argument right by the required places */
102*4882a593Smuzhiyun 			if (FPU_shrx(&XSIG_LL(accum), -1 - exponent) >=
103*4882a593Smuzhiyun 			    0x80000000U)
104*4882a593Smuzhiyun 				XSIG_LL(accum)++;	/* round up */
105*4882a593Smuzhiyun 		}
106*4882a593Smuzhiyun 	}
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	XSIG_LL(argSq) = XSIG_LL(accum);
109*4882a593Smuzhiyun 	argSq.lsw = accum.lsw;
110*4882a593Smuzhiyun 	mul_Xsig_Xsig(&argSq, &argSq);
111*4882a593Smuzhiyun 	XSIG_LL(argSqSq) = XSIG_LL(argSq);
112*4882a593Smuzhiyun 	argSqSq.lsw = argSq.lsw;
113*4882a593Smuzhiyun 	mul_Xsig_Xsig(&argSqSq, &argSqSq);
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	/* Compute the negative terms for the numerator polynomial */
116*4882a593Smuzhiyun 	accumulatoro.msw = accumulatoro.midw = accumulatoro.lsw = 0;
117*4882a593Smuzhiyun 	polynomial_Xsig(&accumulatoro, &XSIG_LL(argSqSq), oddnegterm,
118*4882a593Smuzhiyun 			HiPOWERon - 1);
119*4882a593Smuzhiyun 	mul_Xsig_Xsig(&accumulatoro, &argSq);
120*4882a593Smuzhiyun 	negate_Xsig(&accumulatoro);
121*4882a593Smuzhiyun 	/* Add the positive terms */
122*4882a593Smuzhiyun 	polynomial_Xsig(&accumulatoro, &XSIG_LL(argSqSq), oddplterm,
123*4882a593Smuzhiyun 			HiPOWERop - 1);
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 	/* Compute the positive terms for the denominator polynomial */
126*4882a593Smuzhiyun 	accumulatore.msw = accumulatore.midw = accumulatore.lsw = 0;
127*4882a593Smuzhiyun 	polynomial_Xsig(&accumulatore, &XSIG_LL(argSqSq), evenplterm,
128*4882a593Smuzhiyun 			HiPOWERep - 1);
129*4882a593Smuzhiyun 	mul_Xsig_Xsig(&accumulatore, &argSq);
130*4882a593Smuzhiyun 	negate_Xsig(&accumulatore);
131*4882a593Smuzhiyun 	/* Add the negative terms */
132*4882a593Smuzhiyun 	polynomial_Xsig(&accumulatore, &XSIG_LL(argSqSq), evennegterm,
133*4882a593Smuzhiyun 			HiPOWERen - 1);
134*4882a593Smuzhiyun 	/* Multiply by arg^2 */
135*4882a593Smuzhiyun 	mul64_Xsig(&accumulatore, &XSIG_LL(argSignif));
136*4882a593Smuzhiyun 	mul64_Xsig(&accumulatore, &XSIG_LL(argSignif));
137*4882a593Smuzhiyun 	/* de-normalize and divide by 2 */
138*4882a593Smuzhiyun 	shr_Xsig(&accumulatore, -2 * (1 + exponent) + 1);
139*4882a593Smuzhiyun 	negate_Xsig(&accumulatore);	/* This does 1 - accumulator */
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 	/* Now find the ratio. */
142*4882a593Smuzhiyun 	if (accumulatore.msw == 0) {
143*4882a593Smuzhiyun 		/* accumulatoro must contain 1.0 here, (actually, 0) but it
144*4882a593Smuzhiyun 		   really doesn't matter what value we use because it will
145*4882a593Smuzhiyun 		   have negligible effect in later calculations
146*4882a593Smuzhiyun 		 */
147*4882a593Smuzhiyun 		XSIG_LL(accum) = 0x8000000000000000LL;
148*4882a593Smuzhiyun 		accum.lsw = 0;
149*4882a593Smuzhiyun 	} else {
150*4882a593Smuzhiyun 		div_Xsig(&accumulatoro, &accumulatore, &accum);
151*4882a593Smuzhiyun 	}
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 	/* Multiply by 1/3 * arg^3 */
154*4882a593Smuzhiyun 	mul64_Xsig(&accum, &XSIG_LL(argSignif));
155*4882a593Smuzhiyun 	mul64_Xsig(&accum, &XSIG_LL(argSignif));
156*4882a593Smuzhiyun 	mul64_Xsig(&accum, &XSIG_LL(argSignif));
157*4882a593Smuzhiyun 	mul64_Xsig(&accum, &twothirds);
158*4882a593Smuzhiyun 	shr_Xsig(&accum, -2 * (exponent + 1));
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	/* tan(arg) = arg + accum */
161*4882a593Smuzhiyun 	add_two_Xsig(&accum, &argSignif, &exponent);
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	if (invert) {
164*4882a593Smuzhiyun 		/* We now have the value of tan(pi_2 - arg) where pi_2 is an
165*4882a593Smuzhiyun 		   approximation for pi/2
166*4882a593Smuzhiyun 		 */
167*4882a593Smuzhiyun 		/* The next step is to fix the answer to compensate for the
168*4882a593Smuzhiyun 		   error due to the approximation used for pi/2
169*4882a593Smuzhiyun 		 */
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 		/* This is (approx) delta, the error in our approx for pi/2
172*4882a593Smuzhiyun 		   (see above). It has an exponent of -65
173*4882a593Smuzhiyun 		 */
174*4882a593Smuzhiyun 		XSIG_LL(fix_up) = 0x898cc51701b839a2LL;
175*4882a593Smuzhiyun 		fix_up.lsw = 0;
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 		if (exponent == 0)
178*4882a593Smuzhiyun 			adj = 0xffffffff;	/* We want approx 1.0 here, but
179*4882a593Smuzhiyun 						   this is close enough. */
180*4882a593Smuzhiyun 		else if (exponent > -30) {
181*4882a593Smuzhiyun 			adj = accum.msw >> -(exponent + 1);	/* tan */
182*4882a593Smuzhiyun 			adj = mul_32_32(adj, adj);	/* tan^2 */
183*4882a593Smuzhiyun 		} else
184*4882a593Smuzhiyun 			adj = 0;
185*4882a593Smuzhiyun 		adj = mul_32_32(0x898cc517, adj);	/* delta * tan^2 */
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun 		fix_up.msw += adj;
188*4882a593Smuzhiyun 		if (!(fix_up.msw & 0x80000000)) {	/* did fix_up overflow ? */
189*4882a593Smuzhiyun 			/* Yes, we need to add an msb */
190*4882a593Smuzhiyun 			shr_Xsig(&fix_up, 1);
191*4882a593Smuzhiyun 			fix_up.msw |= 0x80000000;
192*4882a593Smuzhiyun 			shr_Xsig(&fix_up, 64 + exponent);
193*4882a593Smuzhiyun 		} else
194*4882a593Smuzhiyun 			shr_Xsig(&fix_up, 65 + exponent);
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 		add_two_Xsig(&accum, &fix_up, &exponent);
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 		/* accum now contains tan(pi/2 - arg).
199*4882a593Smuzhiyun 		   Use tan(arg) = 1.0 / tan(pi/2 - arg)
200*4882a593Smuzhiyun 		 */
201*4882a593Smuzhiyun 		accumulatoro.lsw = accumulatoro.midw = 0;
202*4882a593Smuzhiyun 		accumulatoro.msw = 0x80000000;
203*4882a593Smuzhiyun 		div_Xsig(&accumulatoro, &accum, &accum);
204*4882a593Smuzhiyun 		exponent = -exponent - 1;
205*4882a593Smuzhiyun 	}
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	/* Transfer the result */
208*4882a593Smuzhiyun 	round_Xsig(&accum);
209*4882a593Smuzhiyun 	FPU_settag0(TAG_Valid);
210*4882a593Smuzhiyun 	significand(st0_ptr) = XSIG_LL(accum);
211*4882a593Smuzhiyun 	setexponent16(st0_ptr, exponent + EXTENDED_Ebias);	/* Result is positive. */
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun }
214