1 /*        $NetBSD: e_acosl.c,v 1.5 2024/06/09 13:35:38 riastradh Exp $          */
2 
3 /* FreeBSD: head/lib/msun/src/e_acos.c 176451 2008-02-22 02:30:36Z das */
4 /*
5  * ====================================================
6  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
7  *
8  * Developed at SunSoft, a Sun Microsystems, Inc. business.
9  * Permission to use, copy, modify, and distribute this
10  * software is freely granted, provided that this notice
11  * is preserved.
12  * ====================================================
13  */
14 
15 #include <sys/cdefs.h>
16 __RCSID("$NetBSD: e_acosl.c,v 1.5 2024/06/09 13:35:38 riastradh Exp $");
17 
18 /*
19  * See comments in e_acos.c.
20  * Converted to long double by David Schultz <das@FreeBSD.ORG>.
21  */
22 
23 #include "namespace.h"
24 
25 #include <float.h>
26 #include <machine/ieee.h>
27 
28 #include "math.h"
29 #include "math_private.h"
30 
31 #ifdef __HAVE_LONG_DOUBLE
32 
33 __weak_alias(acosl, _acosl)
34 
35 #if LDBL_MANT_DIG == 64
36 #include "../ld80/invtrig.h"
37 #elif LDBL_MANT_DIG == 113
38 #include "../ld128/invtrig.h"
39 #else
40 #error "Unsupported long double format"
41 #endif
42 
43 #ifdef LDBL_IMPLICIT_NBIT
44 #define   LDBL_NBIT 0
45 #endif
46 
47 static const long double
48 one=  1.00000000000000000000e+00;
49 
50 #ifdef __i386__
51 /* XXX Work around the fact that gcc truncates long double constants on i386 */
52 static volatile double
53 pi1 =  3.14159265358979311600e+00,      /*  0x1.921fb54442d18p+1  */
54 pi2 =  1.22514845490862001043e-16;      /*  0x1.1a80000000000p-53 */
55 #define   pi        ((long double)pi1 + pi2)
56 #else
57 static const long double
58 pi =  3.14159265358979323846264338327950280e+00L;
59 #endif
60 
61 long double
acosl(long double x)62 acosl(long double x)
63 {
64           union ieee_ext_u u;
65           long double z,p,q,r,w,s,c,df;
66           int16_t expsign, expt;
67           u.extu_ld = x;
68           expsign = GET_EXPSIGN(&u);
69           expt = expsign & 0x7fff;
70           if(expt >= BIAS) {  /* |x| >= 1 */
71               if(expt==BIAS && ((u.extu_frach&~LDBL_NBIT)|u.extu_fracl)==0) {
72                     if (expsign>0) return 0.0;    /* acos(1) = 0  */
73                     else return pi+2.0*pio2_lo;   /* acos(-1)= pi */
74               }
75               return (x-x)/(x-x);                 /* acos(|x|>1) is NaN */
76           }
77           if(expt<BIAS-1) {   /* |x| < 0.5 */
78               if(expt<ACOS_CONST) return pio2_hi+pio2_lo;/*x tiny: acosl=pi/2*/
79               z = x*x;
80               p = P(z);
81               q = Q(z);
82               r = p/q;
83               return pio2_hi - (x - (pio2_lo-x*r));
84           } else  if (expsign<0) {      /* x < -0.5 */
85               z = (one+x)*0.5;
86               p = P(z);
87               q = Q(z);
88               s = sqrtl(z);
89               r = p/q;
90               w = r*s-pio2_lo;
91               return pi - 2.0*(s+w);
92           } else {                      /* x > 0.5 */
93               z = (one-x)*0.5;
94               s = sqrtl(z);
95               u.extu_ld = s;
96               u.extu_fracl = 0;
97               df = u.extu_ld;
98               c  = (z-df*df)/(s+df);
99               p = P(z);
100               q = Q(z);
101               r = p/q;
102               w = r*s+c;
103               return 2.0*(df+w);
104           }
105 }
106 #endif
107