18e77cc64SDavid Schultz /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
35e53a4f9SPedro F. Giffuni *
48e77cc64SDavid Schultz * Copyright (c) 2007 Steven G. Kargl
58e77cc64SDavid Schultz * All rights reserved.
68e77cc64SDavid Schultz *
78e77cc64SDavid Schultz * Redistribution and use in source and binary forms, with or without
88e77cc64SDavid Schultz * modification, are permitted provided that the following conditions
98e77cc64SDavid Schultz * are met:
108e77cc64SDavid Schultz * 1. Redistributions of source code must retain the above copyright
118e77cc64SDavid Schultz * notice unmodified, this list of conditions, and the following
128e77cc64SDavid Schultz * disclaimer.
138e77cc64SDavid Schultz * 2. Redistributions in binary form must reproduce the above copyright
148e77cc64SDavid Schultz * notice, this list of conditions and the following disclaimer in the
158e77cc64SDavid Schultz * documentation and/or other materials provided with the distribution.
168e77cc64SDavid Schultz *
178e77cc64SDavid Schultz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
188e77cc64SDavid Schultz * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
198e77cc64SDavid Schultz * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
208e77cc64SDavid Schultz * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
218e77cc64SDavid Schultz * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
228e77cc64SDavid Schultz * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
238e77cc64SDavid Schultz * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
248e77cc64SDavid Schultz * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
258e77cc64SDavid Schultz * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
268e77cc64SDavid Schultz * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
278e77cc64SDavid Schultz */
288e77cc64SDavid Schultz
298e77cc64SDavid Schultz /*
308e77cc64SDavid Schultz * Limited testing on pseudorandom numbers drawn within [-2e8:4e8] shows
318e77cc64SDavid Schultz * an accuracy of <= 0.7412 ULP.
328e77cc64SDavid Schultz */
338e77cc64SDavid Schultz
348e77cc64SDavid Schultz #include <float.h>
35340076f0SSteve Kargl #ifdef __i386__
36340076f0SSteve Kargl #include <ieeefp.h>
37340076f0SSteve Kargl #endif
388e77cc64SDavid Schultz
39046e2d5dSSteve Kargl #include "fpmath.h"
408e77cc64SDavid Schultz #include "math.h"
418e77cc64SDavid Schultz #include "math_private.h"
428e77cc64SDavid Schultz #if LDBL_MANT_DIG == 64
43c273267eSSteve Kargl #include "../ld80/e_rem_pio2l.h"
44046e2d5dSSteve Kargl static const union IEEEl2bits
45046e2d5dSSteve Kargl pio4u = LD80C(0xc90fdaa22168c235, -00001, 7.85398163397448309628e-01L);
46046e2d5dSSteve Kargl #define pio4 (pio4u.e)
478e77cc64SDavid Schultz #elif LDBL_MANT_DIG == 113
48c273267eSSteve Kargl #include "../ld128/e_rem_pio2l.h"
49046e2d5dSSteve Kargl long double pio4 = 7.85398163397448309615660845819875721e-1L;
508e77cc64SDavid Schultz #else
518e77cc64SDavid Schultz #error "Unsupported long double format"
528e77cc64SDavid Schultz #endif
538e77cc64SDavid Schultz
548e77cc64SDavid Schultz long double
cosl(long double x)558e77cc64SDavid Schultz cosl(long double x)
568e77cc64SDavid Schultz {
578e77cc64SDavid Schultz union IEEEl2bits z;
58c273267eSSteve Kargl int e0;
59c273267eSSteve Kargl long double y[2];
608e77cc64SDavid Schultz long double hi, lo;
618e77cc64SDavid Schultz
628e77cc64SDavid Schultz z.e = x;
638e77cc64SDavid Schultz z.bits.sign = 0;
648e77cc64SDavid Schultz
658e77cc64SDavid Schultz /* If x = +-0 or x is a subnormal number, then cos(x) = 1 */
668e77cc64SDavid Schultz if (z.bits.exp == 0)
678e77cc64SDavid Schultz return (1.0);
688e77cc64SDavid Schultz
698e77cc64SDavid Schultz /* If x = NaN or Inf, then cos(x) = NaN. */
708e77cc64SDavid Schultz if (z.bits.exp == 32767)
718e77cc64SDavid Schultz return ((x - x) / (x - x));
728e77cc64SDavid Schultz
73340076f0SSteve Kargl ENTERI();
74340076f0SSteve Kargl
758e77cc64SDavid Schultz /* Optimize the case where x is already within range. */
76046e2d5dSSteve Kargl if (z.e < pio4)
77340076f0SSteve Kargl RETURNI(__kernel_cosl(z.e, 0));
788e77cc64SDavid Schultz
79c273267eSSteve Kargl e0 = __ieee754_rem_pio2l(x, y);
80c273267eSSteve Kargl hi = y[0];
81c273267eSSteve Kargl lo = y[1];
828e77cc64SDavid Schultz
838e77cc64SDavid Schultz switch (e0 & 3) {
848e77cc64SDavid Schultz case 0:
858e77cc64SDavid Schultz hi = __kernel_cosl(hi, lo);
868e77cc64SDavid Schultz break;
878e77cc64SDavid Schultz case 1:
888e77cc64SDavid Schultz hi = - __kernel_sinl(hi, lo, 1);
898e77cc64SDavid Schultz break;
908e77cc64SDavid Schultz case 2:
918e77cc64SDavid Schultz hi = - __kernel_cosl(hi, lo);
928e77cc64SDavid Schultz break;
938e77cc64SDavid Schultz case 3:
948e77cc64SDavid Schultz hi = __kernel_sinl(hi, lo, 1);
958e77cc64SDavid Schultz break;
968e77cc64SDavid Schultz }
978e77cc64SDavid Schultz
98340076f0SSteve Kargl RETURNI(hi);
998e77cc64SDavid Schultz }
100