1 1.1 christos /* From: @(#)k_cos.c 1.3 95/01/18 */ 2 1.1 christos /* 3 1.1 christos * ==================================================== 4 1.1 christos * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 5 1.1 christos * Copyright (c) 2008 Steven G. Kargl, David Schultz, Bruce D. Evans. 6 1.1 christos * 7 1.1 christos * Developed at SunSoft, a Sun Microsystems, Inc. business. 8 1.1 christos * Permission to use, copy, modify, and distribute this 9 1.1 christos * software is freely granted, provided that this notice 10 1.1 christos * is preserved. 11 1.1 christos * ==================================================== 12 1.1 christos */ 13 1.1 christos 14 1.1 christos #include <sys/cdefs.h> 15 1.1 christos /* 16 1.1 christos * ld80 version of k_cos.c. See ../src/k_cos.c for most comments. 17 1.1 christos */ 18 1.1 christos 19 1.1 christos #include "math_private.h" 20 1.1 christos 21 1.1 christos /* 22 1.1 christos * Domain [-0.7854, 0.7854], range ~[-2.43e-23, 2.425e-23]: 23 1.1 christos * |cos(x) - c(x)| < 2**-75.1 24 1.1 christos * 25 1.1 christos * The coefficients of c(x) were generated by a pari-gp script using 26 1.1 christos * a Remez algorithm that searches for the best higher coefficients 27 1.1 christos * after rounding leading coefficients to a specified precision. 28 1.1 christos * 29 1.1 christos * Simpler methods like Chebyshev or basic Remez barely suffice for 30 1.1 christos * cos() in 64-bit precision, because we want the coefficient of x^2 31 1.1 christos * to be precisely -0.5 so that multiplying by it is exact, and plain 32 1.1 christos * rounding of the coefficients of a good polynomial approximation only 33 1.1 christos * gives this up to about 64-bit precision. Plain rounding also gives 34 1.1 christos * a mediocre approximation for the coefficient of x^4, but a rounding 35 1.1 christos * error of 0.5 ulps for this coefficient would only contribute ~0.01 36 1.1 christos * ulps to the final error, so this is unimportant. Rounding errors in 37 1.1 christos * higher coefficients are even less important. 38 1.1 christos * 39 1.1 christos * In fact, coefficients above the x^4 one only need to have 53-bit 40 1.1 christos * precision, and this is more efficient. We get this optimization 41 1.1 christos * almost for free from the complications needed to search for the best 42 1.1 christos * higher coefficients. 43 1.1 christos */ 44 1.1 christos static const double 45 1.1 christos one = 1.0; 46 1.1 christos 47 1.1 christos #if defined(__amd64__) || defined(__i386__) 48 1.1 christos /* Long double constants are slow on these arches, and broken on i386. */ 49 1.1 christos static const volatile double 50 1.1 christos C1hi = 0.041666666666666664, /* 0x15555555555555.0p-57 */ 51 1.1 christos C1lo = 2.2598839032744733e-18; /* 0x14d80000000000.0p-111 */ 52 1.1 christos #define C1 ((long double)C1hi + C1lo) 53 1.1 christos #else 54 1.1 christos static const long double 55 1.1 christos C1 = 0.0416666666666666666136L; /* 0xaaaaaaaaaaaaaa9b.0p-68 */ 56 1.1 christos #endif 57 1.1 christos 58 1.1 christos static const double 59 1.1 christos C2 = -0.0013888888888888874, /* -0x16c16c16c16c10.0p-62 */ 60 1.1 christos C3 = 0.000024801587301571716, /* 0x1a01a01a018e22.0p-68 */ 61 1.1 christos C4 = -0.00000027557319215507120, /* -0x127e4fb7602f22.0p-74 */ 62 1.1 christos C5 = 0.0000000020876754400407278, /* 0x11eed8caaeccf1.0p-81 */ 63 1.1 christos C6 = -1.1470297442401303e-11, /* -0x19393412bd1529.0p-89 */ 64 1.1 christos C7 = 4.7383039476436467e-14; /* 0x1aac9d9af5c43e.0p-97 */ 65 1.1 christos 66 1.1 christos long double 67 1.1 christos __kernel_cosl(long double x, long double y) 68 1.1 christos { 69 1.1 christos long double hz,z,r,w; 70 1.1 christos 71 1.1 christos z = x*x; 72 1.1 christos r = z*(C1+z*(C2+z*(C3+z*(C4+z*(C5+z*(C6+z*C7)))))); 73 1.1 christos hz = 0.5*z; 74 1.1 christos w = one-hz; 75 1.1 christos return w + (((one-w)-hz) + (z*r-x*y)); 76 1.1 christos } 77