xref: /freebsd/lib/msun/src/k_sincosl.h (revision 02e9120893770924227138ba49df1edb3896112a)
1 /*-
2  * ====================================================
3  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
4  * Copyright (c) 2008 Steven G. Kargl, David Schultz, Bruce D. Evans.
5  *
6  * Developed at SunSoft, a Sun Microsystems, Inc. business.
7  * Permission to use, copy, modify, and distribute this
8  * software is freely granted, provided that this notice
9  * is preserved.
10  * ====================================================
11  *
12  * k_sinl.c and k_cosl.c merged by Steven G. Kargl
13  */
14 
15 #include <sys/cdefs.h>
16 #if LDBL_MANT_DIG == 64		/* ld80 version of k_sincosl.c. */
17 
18 #if defined(__amd64__) || defined(__i386__)
19 /* Long double constants are slow on these arches, and broken on i386. */
20 static const volatile double
21 C1hi = 0.041666666666666664,		/*  0x15555555555555.0p-57 */
22 C1lo = 2.2598839032744733e-18,		/*  0x14d80000000000.0p-111 */
23 S1hi = -0.16666666666666666,		/* -0x15555555555555.0p-55 */
24 S1lo = -9.2563760475949941e-18;		/* -0x15580000000000.0p-109 */
25 #define	S1	((long double)S1hi + S1lo)
26 #define	C1	((long double)C1hi + C1lo)
27 #else
28 static const long double
29 C1 =  0.0416666666666666666136L,	/*  0xaaaaaaaaaaaaaa9b.0p-68 */
30 S1 = -0.166666666666666666671L;		/* -0xaaaaaaaaaaaaaaab.0p-66 */
31 #endif
32 
33 static const double
34 C2 = -0.0013888888888888874,		/* -0x16c16c16c16c10.0p-62 */
35 C3 =  0.000024801587301571716,		/*  0x1a01a01a018e22.0p-68 */
36 C4 = -0.00000027557319215507120,	/* -0x127e4fb7602f22.0p-74 */
37 C5 =  0.0000000020876754400407278,	/*  0x11eed8caaeccf1.0p-81 */
38 C6 = -1.1470297442401303e-11,		/* -0x19393412bd1529.0p-89 */
39 C7 =  4.7383039476436467e-14,		/*  0x1aac9d9af5c43e.0p-97 */
40 S2 =  0.0083333333333333332,		/*  0x11111111111111.0p-59 */
41 S3 = -0.00019841269841269427,		/* -0x1a01a01a019f81.0p-65 */
42 S4 =  0.0000027557319223597490,		/*  0x171de3a55560f7.0p-71 */
43 S5 = -0.000000025052108218074604,	/* -0x1ae64564f16cad.0p-78 */
44 S6 =  1.6059006598854211e-10,		/*  0x161242b90243b5.0p-85 */
45 S7 = -7.6429779983024564e-13,		/* -0x1ae42ebd1b2e00.0p-93 */
46 S8 =  2.6174587166648325e-15;		/*  0x179372ea0b3f64.0p-101 */
47 
48 static inline void
49 __kernel_sincosl(long double x, long double y, int iy, long double *sn,
50     long double *cs)
51 {
52 	long double hz, r, v, w, z;
53 
54 	z = x * x;
55 	v = z * x;
56 	/*
57 	 * XXX Replace Horner scheme with an algorithm suitable for CPUs
58 	 * with more complex pipelines.
59 	 */
60 	r = S2 + z * (S3 + z * (S4 + z * (S5 + z * (S6 + z * (S7 + z * S8)))));
61 
62 	if (iy == 0)
63 		*sn = x + v * (S1 + z * r);
64 	else
65 		*sn = x - ((z * (y / 2 - v * r) - y) - v * S1);
66 
67 	hz = z / 2;
68 	w = 1 - hz;
69 	r = z * (C1 + z * (C2 + z * (C3 + z * (C4 + z * (C5 + z * (C6 +
70 	    z * C7))))));
71 	*cs = w + (((1 - w) - hz) + (z * r - x * y));
72 }
73 
74 #elif LDBL_MANT_DIG == 113	/* ld128 version of k_sincosl.c. */
75 
76 static const long double
77 S1 = -0.16666666666666666666666666666666666606732416116558L,
78 S2 =  0.0083333333333333333333333333333331135404851288270047L,
79 S3 = -0.00019841269841269841269841269839935785325638310428717L,
80 S4 =  0.27557319223985890652557316053039946268333231205686e-5L,
81 S5 = -0.25052108385441718775048214826384312253862930064745e-7L,
82 S6 =  0.16059043836821614596571832194524392581082444805729e-9L,
83 S7 = -0.76471637318198151807063387954939213287488216303768e-12L,
84 S8 =  0.28114572543451292625024967174638477283187397621303e-14L;
85 
86 static const double
87 S9  = -0.82206352458348947812512122163446202498005154296863e-17,
88 S10 =  0.19572940011906109418080609928334380560135358385256e-19,
89 S11 = -0.38680813379701966970673724299207480965452616911420e-22,
90 S12 =  0.64038150078671872796678569586315881020659912139412e-25;
91 
92 static const long double
93 C1 =  4.16666666666666666666666666666666667e-02L,
94 C2 = -1.38888888888888888888888888888888834e-03L,
95 C3 =  2.48015873015873015873015873015446795e-05L,
96 C4 = -2.75573192239858906525573190949988493e-07L,
97 C5 =  2.08767569878680989792098886701451072e-09L,
98 C6 = -1.14707455977297247136657111139971865e-11L,
99 C7 =  4.77947733238738518870113294139830239e-14L,
100 C8 = -1.56192069685858079920640872925306403e-16L,
101 C9 =  4.11031762320473354032038893429515732e-19L,
102 C10= -8.89679121027589608738005163931958096e-22L,
103 C11=  1.61171797801314301767074036661901531e-24L,
104 C12= -2.46748624357670948912574279501044295e-27L;
105 
106 static inline void
107 __kernel_sincosl(long double x, long double y, int iy, long double *sn,
108     long double *cs)
109 {
110 	long double hz, r, v, w, z;
111 
112 	z = x * x;
113 	v = z * x;
114 	/*
115 	 * XXX Replace Horner scheme with an algorithm suitable for CPUs
116 	 * with more complex pipelines.
117 	 */
118 	r = S2 + z * (S3 + z * (S4 + z * (S5 + z * (S6 + z * (S7 + z * (S8 +
119 	    z * (S9 + z * (S10 + z * (S11 + z * S12)))))))));
120 
121 	if (iy == 0)
122 		*sn = x + v * (S1 + z * r);
123 	else
124 		*sn = x - ((z * (y / 2 - v * r) - y) - v * S1);
125 
126 	hz = z / 2;
127 	w = 1 - hz;
128 	r = z * (C1 + z * (C2 + z * (C3 + z * (C4 + z * (C5 + z * (C6 +
129 	    z * (C7 + z * (C8 + z * (C9 + z * (C10 + z * (C11+z*C12)))))))))));
130 
131 	*cs =  w + (((1 - w) - hz) + (z * r - x * y));
132 }
133 #else
134 #error "Unsupported long double format"
135 #endif
136