s_cosl.c revision 1.1
1/*	$NetBSD: s_cosl.c,v 1.1 2024/01/21 18:53:18 christos 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.1 2024/01/21 18:53:18 christos 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#ifdef __i386__
42#include <ieeefp.h>
43#endif
44
45#ifdef __weak_alias
46__weak_alias(cosl, _cosl)
47#endif
48
49
50#ifdef __FreeBSD__
51#include "fpmath.h"
52#endif
53
54#include "math.h"
55#include "math_private.h"
56
57#ifdef __HAVE_LONG_DOUBLE
58
59#if LDBL_MANT_DIG == 64
60#include "../ld80/e_rem_pio2l.h"
61#include "../ld80/k_cosl.c"
62static const union ieee_ext_u
63pio4u = LD80C(0xc90fdaa22168c235, -00001,  7.85398163397448309628e-01L);
64#define	pio4	(pio4u.extu_ld)
65#elif LDBL_MANT_DIG == 113
66#include "../ld128/e_rem_pio2l.h"
67#include "../ld128/k_cosl.c"
68long double pio4 =  7.85398163397448309615660845819875721e-1L;
69#else
70#error "Unsupported long double format"
71#endif
72
73long double
74cosl(long double x)
75{
76	union ieee_ext_u z;
77	int e0;
78	long double y[2];
79	long double hi, lo;
80
81	z.extu_ld = x;
82	z.extu_sign = 0;
83
84	/* If x = +-0 or x is a subnormal number, then cos(x) = 1 */
85	if (z.extu_exp == 0)
86		return (1.0);
87
88	/* If x = NaN or Inf, then cos(x) = NaN. */
89	if (z.extu_exp == 32767)
90		return ((x - x) / (x - x));
91
92	ENTERI();
93
94	/* Optimize the case where x is already within range. */
95	if (z.extu_ld < pio4)
96		RETURNI(__kernel_cosl(z.extu_ld, 0));
97
98	e0 = __ieee754_rem_pio2l(x, y);
99	hi = y[0];
100	lo = y[1];
101
102	switch (e0 & 3) {
103	case 0:
104	    hi = __kernel_cosl(hi, lo);
105	    break;
106	case 1:
107	    hi = - __kernel_sinl(hi, lo, 1);
108	    break;
109	case 2:
110	    hi = - __kernel_cosl(hi, lo);
111	    break;
112	case 3:
113	    hi = __kernel_sinl(hi, lo, 1);
114	    break;
115	}
116
117	RETURNI(hi);
118}
119#else
120
121long double
122cosl(long double x)
123{
124	return cos(x);
125}
126
127#endif
128