1 /*---------------------------------------------------------------------------+
4 | Compute the arctan of a FPU_REG, using a polynomial approximation. |
6 | Copyright (C) 1992,1993,1994,1997 |
7 | W. Metzenthen, 22 Parker St, Ormond, Vic 3163, Australia |
8 | E-mail billm@suburbia.net |
11 +---------------------------------------------------------------------------*/
13 #include "exception.h"
14 #include "reg_constant.h"
16 #include "fpu_system.h"
18 #include "control_w.h"
22 #define HIPOWERon 6 /* odd poly, negative terms */
23 static const unsigned long long oddnegterms
[HIPOWERon
] =
25 0x0000000000000000LL
, /* Dummy (not for - 1.0) */
33 #define HIPOWERop 6 /* odd poly, positive terms */
34 static const unsigned long long oddplterms
[HIPOWERop
] =
36 /* 0xaaaaaaaaaaaaaaabLL, transferred to fixedpterm[] */
45 static const unsigned long long denomterm
= 0xebd9b842c5c53a0eLL
;
47 static const Xsig fixedpterm
= MK_XSIG(0xaaaaaaaa, 0xaaaaaaaa, 0xaaaaaaaa);
49 static const Xsig pi_signif
= MK_XSIG(0xc90fdaa2, 0x2168c234, 0xc4c6628b);
52 /*--- poly_atan() -----------------------------------------------------------+
54 +---------------------------------------------------------------------------*/
55 void poly_atan(FPU_REG
*st0_ptr
, u_char st0_tag
,
56 FPU_REG
*st1_ptr
, u_char st1_tag
)
58 u_char transformed
, inverted
,
62 Xsig accumulator
, Numer
, Denom
, accumulatore
, argSignif
,
66 sign1
= getsign(st0_ptr
);
67 sign2
= getsign(st1_ptr
);
68 if ( st0_tag
== TAG_Valid
)
70 exponent
= exponent(st0_ptr
);
74 /* This gives non-compatible stack contents... */
75 FPU_to_exp16(st0_ptr
, st0_ptr
);
76 exponent
= exponent16(st0_ptr
);
78 if ( st1_tag
== TAG_Valid
)
80 exponent
-= exponent(st1_ptr
);
84 /* This gives non-compatible stack contents... */
85 FPU_to_exp16(st1_ptr
, st1_ptr
);
86 exponent
-= exponent16(st1_ptr
);
89 if ( (exponent
< 0) || ((exponent
== 0) &&
90 ((st0_ptr
->sigh
< st1_ptr
->sigh
) ||
91 ((st0_ptr
->sigh
== st1_ptr
->sigh
) &&
92 (st0_ptr
->sigl
< st1_ptr
->sigl
))) ) )
95 Numer
.lsw
= Denom
.lsw
= 0;
96 XSIG_LL(Numer
) = significand(st0_ptr
);
97 XSIG_LL(Denom
) = significand(st1_ptr
);
102 exponent
= -exponent
;
103 Numer
.lsw
= Denom
.lsw
= 0;
104 XSIG_LL(Numer
) = significand(st1_ptr
);
105 XSIG_LL(Denom
) = significand(st0_ptr
);
107 div_Xsig(&Numer
, &Denom
, &argSignif
);
108 exponent
+= norm_Xsig(&argSignif
);
110 if ( (exponent
>= -1)
111 || ((exponent
== -2) && (argSignif
.msw
> 0xd413ccd0)) )
113 /* The argument is greater than sqrt(2)-1 (=0.414213562...) */
114 /* Convert the argument by an identity for atan */
120 if ( !( (exponent
== 0) &&
121 (argSignif
.lsw
== 0) && (argSignif
.midw
== 0) &&
122 (argSignif
.msw
== 0x80000000) ) )
124 EXCEPTION(EX_INTERNAL
|0x104); /* There must be a logic error */
127 #endif /* PARANOID */
128 argSignif
.msw
= 0; /* Make the transformed arg -> 0.0 */
132 Numer
.lsw
= Denom
.lsw
= argSignif
.lsw
;
133 XSIG_LL(Numer
) = XSIG_LL(Denom
) = XSIG_LL(argSignif
);
136 shr_Xsig(&Numer
, -1-exponent
);
139 shr_Xsig(&Denom
, -exponent
);
140 Denom
.msw
|= 0x80000000;
142 div_Xsig(&Numer
, &Denom
, &argSignif
);
144 exponent
= -1 + norm_Xsig(&argSignif
);
152 argSq
.lsw
= argSignif
.lsw
; argSq
.midw
= argSignif
.midw
;
153 argSq
.msw
= argSignif
.msw
;
154 mul_Xsig_Xsig(&argSq
, &argSq
);
156 argSqSq
.lsw
= argSq
.lsw
; argSqSq
.midw
= argSq
.midw
; argSqSq
.msw
= argSq
.msw
;
157 mul_Xsig_Xsig(&argSqSq
, &argSqSq
);
159 accumulatore
.lsw
= argSq
.lsw
;
160 XSIG_LL(accumulatore
) = XSIG_LL(argSq
);
162 shr_Xsig(&argSq
, 2*(-1-exponent
-1));
163 shr_Xsig(&argSqSq
, 4*(-1-exponent
-1));
165 /* Now have argSq etc with binary point at the left
168 /* Do the basic fixed point polynomial evaluation */
169 accumulator
.msw
= accumulator
.midw
= accumulator
.lsw
= 0;
170 polynomial_Xsig(&accumulator
, &XSIG_LL(argSqSq
),
171 oddplterms
, HIPOWERop
-1);
172 mul64_Xsig(&accumulator
, &XSIG_LL(argSq
));
173 negate_Xsig(&accumulator
);
174 polynomial_Xsig(&accumulator
, &XSIG_LL(argSqSq
), oddnegterms
, HIPOWERon
-1);
175 negate_Xsig(&accumulator
);
176 add_two_Xsig(&accumulator
, &fixedpterm
, &dummy_exp
);
178 mul64_Xsig(&accumulatore
, &denomterm
);
179 shr_Xsig(&accumulatore
, 1 + 2*(-1-exponent
));
180 accumulatore
.msw
|= 0x80000000;
182 div_Xsig(&accumulator
, &accumulatore
, &accumulator
);
184 mul_Xsig_Xsig(&accumulator
, &argSignif
);
185 mul_Xsig_Xsig(&accumulator
, &argSq
);
187 shr_Xsig(&accumulator
, 3);
188 negate_Xsig(&accumulator
);
189 add_Xsig_Xsig(&accumulator
, &argSignif
);
193 /* compute pi/4 - accumulator */
194 shr_Xsig(&accumulator
, -1-exponent
);
195 negate_Xsig(&accumulator
);
196 add_Xsig_Xsig(&accumulator
, &pi_signif
);
202 /* compute pi/2 - accumulator */
203 shr_Xsig(&accumulator
, -exponent
);
204 negate_Xsig(&accumulator
);
205 add_Xsig_Xsig(&accumulator
, &pi_signif
);
211 /* compute pi - accumulator */
212 shr_Xsig(&accumulator
, 1 - exponent
);
213 negate_Xsig(&accumulator
);
214 add_Xsig_Xsig(&accumulator
, &pi_signif
);
218 exponent
+= round_Xsig(&accumulator
);
220 significand(st1_ptr
) = XSIG_LL(accumulator
);
221 setexponent16(st1_ptr
, exponent
);
223 tag
= FPU_round(st1_ptr
, 1, 0, FULL_PRECISION
, sign2
);
226 set_precision_flag_up(); /* We do not really know if up or down,
227 use this as the default. */