1176357Sdas/* From: @(#)k_cos.c 1.3 95/01/18 */ 2176357Sdas/* 3176357Sdas * ==================================================== 4176357Sdas * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 5176357Sdas * Copyright (c) 2008 Steven G. Kargl, David Schultz, Bruce D. Evans. 6176357Sdas * 7176357Sdas * Developed at SunSoft, a Sun Microsystems, Inc. business. 8176357Sdas * Permission to use, copy, modify, and distribute this 9176357Sdas * software is freely granted, provided that this notice 10176357Sdas * is preserved. 11176357Sdas * ==================================================== 12176357Sdas */ 13176357Sdas 14176357Sdas#include <sys/cdefs.h> 15176357Sdas__FBSDID("$FreeBSD$"); 16176357Sdas 17176357Sdas/* 18176357Sdas * ld80 version of k_cos.c. See ../src/k_cos.c for most comments. 19176357Sdas */ 20176357Sdas 21176357Sdas#include "math_private.h" 22176357Sdas 23176357Sdas/* 24176357Sdas * Domain [-0.7854, 0.7854], range ~[-2.43e-23, 2.425e-23]: 25176357Sdas * |cos(x) - c(x)| < 2**-75.1 26176357Sdas * 27176357Sdas * The coefficients of c(x) were generated by a pari-gp script using 28176357Sdas * a Remez algorithm that searches for the best higher coefficients 29176357Sdas * after rounding leading coefficients to a specified precision. 30176357Sdas * 31176357Sdas * Simpler methods like Chebyshev or basic Remez barely suffice for 32176357Sdas * cos() in 64-bit precision, because we want the coefficient of x^2 33176357Sdas * to be precisely -0.5 so that multiplying by it is exact, and plain 34176357Sdas * rounding of the coefficients of a good polynomial approximation only 35176357Sdas * gives this up to about 64-bit precision. Plain rounding also gives 36176357Sdas * a mediocre approximation for the coefficient of x^4, but a rounding 37176357Sdas * error of 0.5 ulps for this coefficient would only contribute ~0.01 38176357Sdas * ulps to the final error, so this is unimportant. Rounding errors in 39176357Sdas * higher coefficients are even less important. 40176357Sdas * 41176357Sdas * In fact, coefficients above the x^4 one only need to have 53-bit 42176357Sdas * precision, and this is more efficient. We get this optimization 43176357Sdas * almost for free from the complications needed to search for the best 44176357Sdas * higher coefficients. 45176357Sdas */ 46176357Sdasstatic const double 47176357Sdasone = 1.0; 48176357Sdas 49176357Sdas#if defined(__amd64__) || defined(__i386__) 50176357Sdas/* Long double constants are slow on these arches, and broken on i386. */ 51176357Sdasstatic const volatile double 52176357SdasC1hi = 0.041666666666666664, /* 0x15555555555555.0p-57 */ 53176357SdasC1lo = 2.2598839032744733e-18; /* 0x14d80000000000.0p-111 */ 54176357Sdas#define C1 ((long double)C1hi + C1lo) 55176357Sdas#else 56176357Sdasstatic const long double 57176357SdasC1 = 0.0416666666666666666136L; /* 0xaaaaaaaaaaaaaa9b.0p-68 */ 58176357Sdas#endif 59176357Sdas 60176357Sdasstatic const double 61176357SdasC2 = -0.0013888888888888874, /* -0x16c16c16c16c10.0p-62 */ 62176357SdasC3 = 0.000024801587301571716, /* 0x1a01a01a018e22.0p-68 */ 63176357SdasC4 = -0.00000027557319215507120, /* -0x127e4fb7602f22.0p-74 */ 64176357SdasC5 = 0.0000000020876754400407278, /* 0x11eed8caaeccf1.0p-81 */ 65176357SdasC6 = -1.1470297442401303e-11, /* -0x19393412bd1529.0p-89 */ 66176357SdasC7 = 4.7383039476436467e-14; /* 0x1aac9d9af5c43e.0p-97 */ 67176357Sdas 68176357Sdaslong double 69176357Sdas__kernel_cosl(long double x, long double y) 70176357Sdas{ 71176357Sdas long double hz,z,r,w; 72176357Sdas 73176357Sdas z = x*x; 74176357Sdas r = z*(C1+z*(C2+z*(C3+z*(C4+z*(C5+z*(C6+z*C7)))))); 75176357Sdas hz = 0.5*z; 76176357Sdas w = one-hz; 77176357Sdas return w + (((one-w)-hz) + (z*r-x*y)); 78176357Sdas} 79