11dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*-
2a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * Copyright (c) 2007 Steven G. Kargl
31dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * All rights reserved.
41dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
51dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Redistribution and use in source and binary forms, with or without
61dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * modification, are permitted provided that the following conditions
71dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * are met:
81dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 1. Redistributions of source code must retain the above copyright
91dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    notice unmodified, this list of conditions, and the following
101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    disclaimer.
111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 2. Redistributions in binary form must reproduce the above copyright
121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    notice, this list of conditions and the following disclaimer in the
131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    documentation and/or other materials provided with the distribution.
141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <sys/cdefs.h>
28a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes__FBSDID("$FreeBSD$");
291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
30a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes/*
31a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * Limited testing on pseudorandom numbers drawn within [-2e8:4e8] shows
32a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * an accuracy of <= 0.7412 ULP.
33a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes */
34a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes
35a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes#include <float.h>
36a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes#ifdef __i386__
37a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes#include <ieeefp.h>
38a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes#endif
39a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes
40a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes#include "math.h"
41a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes#include "math_private.h"
42a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes#if LDBL_MANT_DIG == 64
43a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes#include "../ld80/e_rem_pio2l.h"
44a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes#elif LDBL_MANT_DIG == 113
45a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes#include "../ld128/e_rem_pio2l.h"
46a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes#else
47a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes#error "Unsupported long double format"
48a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes#endif
491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectlong double
51a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughescosl(long double x)
521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
53a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	union IEEEl2bits z;
54a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	int e0;
55a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	long double y[2];
56a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	long double hi, lo;
57a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes
58a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	z.e = x;
59a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	z.bits.sign = 0;
60a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes
61a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	/* If x = +-0 or x is a subnormal number, then cos(x) = 1 */
62a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	if (z.bits.exp == 0)
63a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes		return (1.0);
64a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes
65a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	/* If x = NaN or Inf, then cos(x) = NaN. */
66a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	if (z.bits.exp == 32767)
67a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes		return ((x - x) / (x - x));
68a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes
69a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	ENTERI();
70a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes
71a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	/* Optimize the case where x is already within range. */
72a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	if (z.e < M_PI_4)
73a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes		RETURNI(__kernel_cosl(z.e, 0));
74a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes
75a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	e0 = __ieee754_rem_pio2l(x, y);
76a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	hi = y[0];
77a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	lo = y[1];
78a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes
79a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	switch (e0 & 3) {
80a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	case 0:
81a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	    hi = __kernel_cosl(hi, lo);
82a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	    break;
83a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	case 1:
84a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	    hi = - __kernel_sinl(hi, lo, 1);
85a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	    break;
86a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	case 2:
87a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	    hi = - __kernel_cosl(hi, lo);
88a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	    break;
89a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	case 3:
90a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	    hi = __kernel_sinl(hi, lo, 1);
91a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	    break;
921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	}
93a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes
94a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	RETURNI(hi);
951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
96