1176360Sdas/*-
2176360Sdas * Copyright (c) 2007 Steven G. Kargl
3176360Sdas * All rights reserved.
4176360Sdas *
5176360Sdas * Redistribution and use in source and binary forms, with or without
6176360Sdas * modification, are permitted provided that the following conditions
7176360Sdas * are met:
8176360Sdas * 1. Redistributions of source code must retain the above copyright
9176360Sdas *    notice unmodified, this list of conditions, and the following
10176360Sdas *    disclaimer.
11176360Sdas * 2. Redistributions in binary form must reproduce the above copyright
12176360Sdas *    notice, this list of conditions and the following disclaimer in the
13176360Sdas *    documentation and/or other materials provided with the distribution.
14176360Sdas *
15176360Sdas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16176360Sdas * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17176360Sdas * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18176360Sdas * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19176360Sdas * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20176360Sdas * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21176360Sdas * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22176360Sdas * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23176360Sdas * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24176360Sdas * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25176360Sdas */
26176360Sdas
27176360Sdas#include <sys/cdefs.h>
28176360Sdas__FBSDID("$FreeBSD$");
29176360Sdas
30176360Sdas/*
31176360Sdas * Limited testing on pseudorandom numbers drawn within [-2e8:4e8] shows
32176360Sdas * an accuracy of <= 0.7412 ULP.
33176360Sdas */
34176360Sdas
35176360Sdas#include <float.h>
36240828Skargl#ifdef __i386__
37240828Skargl#include <ieeefp.h>
38240828Skargl#endif
39176360Sdas
40176360Sdas#include "math.h"
41176360Sdas#include "math_private.h"
42176360Sdas#if LDBL_MANT_DIG == 64
43221234Skargl#include "../ld80/e_rem_pio2l.h"
44176360Sdas#elif LDBL_MANT_DIG == 113
45221234Skargl#include "../ld128/e_rem_pio2l.h"
46176360Sdas#else
47176360Sdas#error "Unsupported long double format"
48176360Sdas#endif
49176360Sdas
50176360Sdaslong double
51176360Sdascosl(long double x)
52176360Sdas{
53176360Sdas	union IEEEl2bits z;
54221234Skargl	int e0;
55221234Skargl	long double y[2];
56176360Sdas	long double hi, lo;
57176360Sdas
58176360Sdas	z.e = x;
59176360Sdas	z.bits.sign = 0;
60176360Sdas
61176360Sdas	/* If x = +-0 or x is a subnormal number, then cos(x) = 1 */
62176360Sdas	if (z.bits.exp == 0)
63176360Sdas		return (1.0);
64176360Sdas
65176360Sdas	/* If x = NaN or Inf, then cos(x) = NaN. */
66176360Sdas	if (z.bits.exp == 32767)
67176360Sdas		return ((x - x) / (x - x));
68176360Sdas
69240828Skargl	ENTERI();
70240828Skargl
71176360Sdas	/* Optimize the case where x is already within range. */
72176360Sdas	if (z.e < M_PI_4)
73240828Skargl		RETURNI(__kernel_cosl(z.e, 0));
74176360Sdas
75221234Skargl	e0 = __ieee754_rem_pio2l(x, y);
76221234Skargl	hi = y[0];
77221234Skargl	lo = y[1];
78176360Sdas
79176360Sdas	switch (e0 & 3) {
80176360Sdas	case 0:
81176360Sdas	    hi = __kernel_cosl(hi, lo);
82176360Sdas	    break;
83176360Sdas	case 1:
84176360Sdas	    hi = - __kernel_sinl(hi, lo, 1);
85176360Sdas	    break;
86176360Sdas	case 2:
87176360Sdas	    hi = - __kernel_cosl(hi, lo);
88176360Sdas	    break;
89176360Sdas	case 3:
90176360Sdas	    hi = __kernel_sinl(hi, lo, 1);
91176360Sdas	    break;
92176360Sdas	}
93176360Sdas
94240828Skargl	RETURNI(hi);
95176360Sdas}
96