1*2f2c0062Sguenther /* $OpenBSD: s_atanl.c,v 1.2 2016/09/12 19:47:02 guenther Exp $ */
2390c8400Smartynas /* @(#)s_atan.c 5.1 93/09/24 */
3390c8400Smartynas /* FreeBSD: head/lib/msun/src/s_atan.c 176451 2008-02-22 02:30:36Z das */
4390c8400Smartynas /*
5390c8400Smartynas * ====================================================
6390c8400Smartynas * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
7390c8400Smartynas *
8390c8400Smartynas * Developed at SunPro, a Sun Microsystems, Inc. business.
9390c8400Smartynas * Permission to use, copy, modify, and distribute this
10390c8400Smartynas * software is freely granted, provided that this notice
11390c8400Smartynas * is preserved.
12390c8400Smartynas * ====================================================
13390c8400Smartynas */
14390c8400Smartynas
15390c8400Smartynas /*
16390c8400Smartynas * See comments in s_atan.c.
17390c8400Smartynas * Converted to long double by David Schultz <das@FreeBSD.ORG>.
18390c8400Smartynas * Adapted for OpenBSD by Martynas Venckus <martynas@openbsd.org>.
19390c8400Smartynas */
20390c8400Smartynas
21390c8400Smartynas #include <float.h>
22390c8400Smartynas #include <math.h>
23390c8400Smartynas
24390c8400Smartynas #include "invtrig.h"
25390c8400Smartynas #include "math_private.h"
26390c8400Smartynas
27390c8400Smartynas #ifdef EXT_IMPLICIT_NBIT
28390c8400Smartynas #define LDBL_NBIT 0
29390c8400Smartynas #else /* EXT_IMPLICIT_NBIT */
30390c8400Smartynas #define LDBL_NBIT 0x80000000
31390c8400Smartynas #endif /* EXT_IMPLICIT_NBIT */
32390c8400Smartynas
33390c8400Smartynas static const long double
34390c8400Smartynas one = 1.0,
35390c8400Smartynas huge = 1.0e300;
36390c8400Smartynas
37390c8400Smartynas long double
atanl(long double x)38390c8400Smartynas atanl(long double x)
39390c8400Smartynas {
40390c8400Smartynas union {
41390c8400Smartynas long double e;
42390c8400Smartynas struct ieee_ext bits;
43390c8400Smartynas } u;
44390c8400Smartynas long double w,s1,s2,z;
45390c8400Smartynas int id;
46390c8400Smartynas int16_t expsign, expt;
47390c8400Smartynas int32_t expman;
48390c8400Smartynas
49390c8400Smartynas u.e = x;
50390c8400Smartynas expsign = (u.bits.ext_sign << 15) | u.bits.ext_exp;
51390c8400Smartynas expt = expsign & 0x7fff;
52390c8400Smartynas if(expt >= ATAN_CONST) { /* if |x| is large, atan(x)~=pi/2 */
53390c8400Smartynas if(expt == BIAS + LDBL_MAX_EXP &&
54390c8400Smartynas ((u.bits.ext_frach&~LDBL_NBIT)
55390c8400Smartynas #ifdef EXT_FRACHMBITS
56390c8400Smartynas | u.bits.ext_frachm
57390c8400Smartynas #endif /* EXT_FRACHMBITS */
58390c8400Smartynas #ifdef EXT_FRACLMBITS
59390c8400Smartynas | u.bits.ext_fraclm
60390c8400Smartynas #endif /* EXT_FRACLMBITS */
61390c8400Smartynas | u.bits.ext_fracl)!=0)
62390c8400Smartynas return x+x; /* NaN */
63390c8400Smartynas if(expsign>0) return atanhi[3]+atanlo[3];
64390c8400Smartynas else return -atanhi[3]-atanlo[3];
65390c8400Smartynas }
66390c8400Smartynas /* Extract the exponent and the first few bits of the mantissa. */
67390c8400Smartynas /* XXX There should be a more convenient way to do this. */
68390c8400Smartynas expman = (expt << 8) |
69390c8400Smartynas ((u.bits.ext_frach >> (EXT_FRACHBITS - 9)) & 0xff);
70390c8400Smartynas if (expman < ((BIAS - 2) << 8) + 0xc0) { /* |x| < 0.4375 */
71390c8400Smartynas if (expt < ATAN_LINEAR) { /* if |x| is small, atanl(x)~=x */
72390c8400Smartynas if(huge+x>one) return x; /* raise inexact */
73390c8400Smartynas }
74390c8400Smartynas id = -1;
75390c8400Smartynas } else {
76390c8400Smartynas x = fabsl(x);
77390c8400Smartynas if (expman < (BIAS << 8) + 0x30) { /* |x| < 1.1875 */
78390c8400Smartynas if (expman < ((BIAS - 1) << 8) + 0x60) { /* 7/16 <=|x|<11/16 */
79390c8400Smartynas id = 0; x = (2.0*x-one)/(2.0+x);
80390c8400Smartynas } else { /* 11/16<=|x|< 19/16 */
81390c8400Smartynas id = 1; x = (x-one)/(x+one);
82390c8400Smartynas }
83390c8400Smartynas } else {
84390c8400Smartynas if (expman < ((BIAS + 1) << 8) + 0x38) { /* |x| < 2.4375 */
85390c8400Smartynas id = 2; x = (x-1.5)/(one+1.5*x);
86390c8400Smartynas } else { /* 2.4375 <= |x| < 2^ATAN_CONST */
87390c8400Smartynas id = 3; x = -1.0/x;
88390c8400Smartynas }
89390c8400Smartynas }}
90390c8400Smartynas /* end of argument reduction */
91390c8400Smartynas z = x*x;
92390c8400Smartynas w = z*z;
93390c8400Smartynas /* break sum aT[i]z**(i+1) into odd and even poly */
94390c8400Smartynas s1 = z*T_even(w);
95390c8400Smartynas s2 = w*T_odd(w);
96390c8400Smartynas if (id<0) return x - x*(s1+s2);
97390c8400Smartynas else {
98390c8400Smartynas z = atanhi[id] - ((x*(s1+s2) - atanlo[id]) - x);
99390c8400Smartynas return (expsign<0)? -z:z;
100390c8400Smartynas }
101390c8400Smartynas }
102*2f2c0062Sguenther DEF_STD(atanl);
103