xref: /openbsd/lib/libm/src/s_cosl.c (revision 2f2c0062)
1*2f2c0062Sguenther /*	$OpenBSD: s_cosl.c,v 1.2 2016/09/12 19:47:02 guenther Exp $	*/
2390c8400Smartynas /*-
3390c8400Smartynas  * Copyright (c) 2007 Steven G. Kargl
4390c8400Smartynas  * All rights reserved.
5390c8400Smartynas  *
6390c8400Smartynas  * Redistribution and use in source and binary forms, with or without
7390c8400Smartynas  * modification, are permitted provided that the following conditions
8390c8400Smartynas  * are met:
9390c8400Smartynas  * 1. Redistributions of source code must retain the above copyright
10390c8400Smartynas  *    notice unmodified, this list of conditions, and the following
11390c8400Smartynas  *    disclaimer.
12390c8400Smartynas  * 2. Redistributions in binary form must reproduce the above copyright
13390c8400Smartynas  *    notice, this list of conditions and the following disclaimer in the
14390c8400Smartynas  *    documentation and/or other materials provided with the distribution.
15390c8400Smartynas  *
16390c8400Smartynas  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17390c8400Smartynas  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18390c8400Smartynas  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19390c8400Smartynas  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20390c8400Smartynas  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21390c8400Smartynas  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22390c8400Smartynas  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23390c8400Smartynas  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24390c8400Smartynas  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25390c8400Smartynas  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26390c8400Smartynas  */
27390c8400Smartynas 
28390c8400Smartynas /*
29390c8400Smartynas  * Compute cos(x) for x where x is reduced to y = x - k * pi / 2.
30390c8400Smartynas  * Limited testing on pseudorandom numbers drawn within [-2e8:4e8] shows
31390c8400Smartynas  * an accuracy of <= 0.7412 ULP.
32390c8400Smartynas  */
33390c8400Smartynas 
34390c8400Smartynas #include <sys/types.h>
35390c8400Smartynas #include <machine/ieee.h>
36390c8400Smartynas #include <float.h>
37390c8400Smartynas #include <math.h>
38390c8400Smartynas 
39390c8400Smartynas #include "math_private.h"
40390c8400Smartynas 
41390c8400Smartynas #if LDBL_MANT_DIG == 64
42390c8400Smartynas #define	NX	3
43390c8400Smartynas #define	PREC	2
44390c8400Smartynas #elif LDBL_MANT_DIG == 113
45390c8400Smartynas #define	NX	5
46390c8400Smartynas #define	PREC	3
47390c8400Smartynas #else
48390c8400Smartynas #error "Unsupported long double format"
49390c8400Smartynas #endif
50390c8400Smartynas 
51390c8400Smartynas static const long double two24 = 1.67772160000000000000e+07L;
52390c8400Smartynas 
53390c8400Smartynas long double
cosl(long double x)54390c8400Smartynas cosl(long double x)
55390c8400Smartynas {
56390c8400Smartynas 	union {
57390c8400Smartynas 		long double e;
58390c8400Smartynas 		struct ieee_ext bits;
59390c8400Smartynas 	} z;
60390c8400Smartynas 	int i, e0;
61390c8400Smartynas 	double xd[NX], yd[PREC];
62390c8400Smartynas 	long double hi, lo;
63390c8400Smartynas 
64390c8400Smartynas 	z.e = x;
65390c8400Smartynas 	z.bits.ext_sign = 0;
66390c8400Smartynas 
67390c8400Smartynas 	/* If x = +-0 or x is a subnormal number, then cos(x) = 1 */
68390c8400Smartynas 	if (z.bits.ext_exp == 0)
69390c8400Smartynas 		return (1.0);
70390c8400Smartynas 
71390c8400Smartynas 	/* If x = NaN or Inf, then cos(x) = NaN. */
72390c8400Smartynas 	if (z.bits.ext_exp == 32767)
73390c8400Smartynas 		return ((x - x) / (x - x));
74390c8400Smartynas 
75390c8400Smartynas 	/* Optimize the case where x is already within range. */
76390c8400Smartynas 	if (z.e < M_PI_4)
77390c8400Smartynas 		return (__kernel_cosl(z.e, 0));
78390c8400Smartynas 
79390c8400Smartynas 	/* Split z.e into a 24-bit representation. */
80390c8400Smartynas 	e0 = ilogbl(z.e) - 23;
81390c8400Smartynas 	z.e = scalbnl(z.e, -e0);
82390c8400Smartynas 	for (i = 0; i < NX; i++) {
83390c8400Smartynas 		xd[i] = (double)((int32_t)z.e);
84390c8400Smartynas 		z.e = (z.e - xd[i]) * two24;
85390c8400Smartynas 	}
86390c8400Smartynas 
87390c8400Smartynas 	/* yd contains the pieces of xd rem pi/2 such that |yd| < pi/4. */
88390c8400Smartynas 	e0 = __kernel_rem_pio2(xd, yd, e0, NX, PREC);
89390c8400Smartynas 
90390c8400Smartynas #if PREC == 2
91390c8400Smartynas 	hi = (long double)yd[0] + yd[1];
92390c8400Smartynas 	lo = yd[1] - (hi - yd[0]);
93390c8400Smartynas #else /* PREC == 3 */
94390c8400Smartynas 	long double t;
95390c8400Smartynas 	t = (long double)yd[2] + yd[1];
96390c8400Smartynas 	hi = t + yd[0];
97390c8400Smartynas 	lo = yd[0] - (hi - t);
98390c8400Smartynas #endif
99390c8400Smartynas 
100390c8400Smartynas 	switch (e0 & 3) {
101390c8400Smartynas 	case 0:
102390c8400Smartynas 	    hi = __kernel_cosl(hi, lo);
103390c8400Smartynas 	    break;
104390c8400Smartynas 	case 1:
105390c8400Smartynas 	    hi = - __kernel_sinl(hi, lo, 1);
106390c8400Smartynas 	    break;
107390c8400Smartynas 	case 2:
108390c8400Smartynas 	    hi = - __kernel_cosl(hi, lo);
109390c8400Smartynas 	    break;
110390c8400Smartynas 	case 3:
111390c8400Smartynas 	    hi = __kernel_sinl(hi, lo, 1);
112390c8400Smartynas 	    break;
113390c8400Smartynas 	}
114390c8400Smartynas 
115390c8400Smartynas 	return (hi);
116390c8400Smartynas }
117*2f2c0062Sguenther DEF_STD(cosl);
118