1/*	$NetBSD: s_cosl.c,v 1.3 2024/05/08 01:42:23 riastradh Exp $	*/
2
3/*-
4 * SPDX-License-Identifier: BSD-2-Clause
5 *
6 * Copyright (c) 2007 Steven G. Kargl
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice unmodified, this list of conditions, and the following
14 *    disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include <sys/cdefs.h>
32__RCSID("$NetBSD: s_cosl.c,v 1.3 2024/05/08 01:42:23 riastradh Exp $");
33
34/*
35 * Limited testing on pseudorandom numbers drawn within [-2e8:4e8] shows
36 * an accuracy of <= 0.7412 ULP.
37 */
38
39#include "namespace.h"
40#include <float.h>
41
42#ifdef __weak_alias
43__weak_alias(cosl, _cosl)
44#endif
45
46
47#ifdef __FreeBSD__
48#include "fpmath.h"
49#endif
50
51#include "math.h"
52#include "math_private.h"
53
54#ifdef __HAVE_LONG_DOUBLE
55
56#if LDBL_MANT_DIG == 64
57#include "../ld80/e_rem_pio2l.h"
58#include "../ld80/k_cosl.c"
59static const union ieee_ext_u
60pio4u = LD80C(0xc90fdaa22168c235, -00001,  7.85398163397448309628e-01L);
61#define	pio4	(pio4u.extu_ld)
62#elif LDBL_MANT_DIG == 113
63#include "../ld128/e_rem_pio2l.h"
64#include "../ld128/k_cosl.c"
65static long double pio4 =  7.85398163397448309615660845819875721e-1L;
66#else
67#error "Unsupported long double format"
68#endif
69
70long double
71cosl(long double x)
72{
73	union ieee_ext_u z;
74	int e0;
75	long double y[2];
76	long double hi, lo;
77
78	z.extu_ld = x;
79	z.extu_sign = 0;
80
81	/* If x = +-0 or x is a subnormal number, then cos(x) = 1 */
82	if (z.extu_exp == 0)
83		return (1.0);
84
85	/* If x = NaN or Inf, then cos(x) = NaN. */
86	if (z.extu_exp == 32767)
87		return ((x - x) / (x - x));
88
89	ENTERI();
90
91	/* Optimize the case where x is already within range. */
92	if (z.extu_ld < pio4)
93		RETURNI(__kernel_cosl(z.extu_ld, 0));
94
95	e0 = __ieee754_rem_pio2l(x, y);
96	hi = y[0];
97	lo = y[1];
98
99	switch (e0 & 3) {
100	case 0:
101	    hi = __kernel_cosl(hi, lo);
102	    break;
103	case 1:
104	    hi = - __kernel_sinl(hi, lo, 1);
105	    break;
106	case 2:
107	    hi = - __kernel_cosl(hi, lo);
108	    break;
109	case 3:
110	    hi = __kernel_sinl(hi, lo, 1);
111	    break;
112	}
113
114	RETURNI(hi);
115}
116#else
117
118long double
119cosl(long double x)
120{
121	return cos(x);
122}
123
124#endif
125