1141296Sdas 2141296Sdas/* @(#)k_cos.c 1.3 95/01/18 */ 32116Sjkh/* 42116Sjkh * ==================================================== 52116Sjkh * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 62116Sjkh * 7141296Sdas * Developed at SunSoft, a Sun Microsystems, Inc. business. 82116Sjkh * Permission to use, copy, modify, and distribute this 9141296Sdas * software is freely granted, provided that this notice 102116Sjkh * is preserved. 112116Sjkh * ==================================================== 122116Sjkh */ 132116Sjkh 14176408Sbde#include <sys/cdefs.h> 15176408Sbde__FBSDID("$FreeBSD$"); 162116Sjkh 172116Sjkh/* 182116Sjkh * __kernel_cos( x, y ) 192116Sjkh * kernel cos function on [-pi/4, pi/4], pi/4 ~ 0.785398164 202116Sjkh * Input x is assumed to be bounded by ~pi/4 in magnitude. 21141296Sdas * Input y is the tail of x. 222116Sjkh * 232116Sjkh * Algorithm 242116Sjkh * 1. Since cos(-x) = cos(x), we need only to consider positive x. 252116Sjkh * 2. if x < 2^-27 (hx<0x3e400000 0), return 1 with inexact if x!=0. 262116Sjkh * 3. cos(x) is approximated by a polynomial of degree 14 on 272116Sjkh * [0,pi/4] 282116Sjkh * 4 14 292116Sjkh * cos(x) ~ 1 - x*x/2 + C1*x + ... + C6*x 302116Sjkh * where the remez error is 31141296Sdas * 322116Sjkh * | 2 4 6 8 10 12 14 | -58 332116Sjkh * |cos(x)-(1-.5*x +C1*x +C2*x +C3*x +C4*x +C5*x +C6*x )| <= 2 34141296Sdas * | | 35141296Sdas * 36141296Sdas * 4 6 8 10 12 14 372116Sjkh * 4. let r = C1*x +C2*x +C3*x +C4*x +C5*x +C6*x , then 38151698Sbde * cos(x) ~ 1 - x*x/2 + r 39141296Sdas * since cos(x+y) ~ cos(x) - sin(x)*y 402116Sjkh * ~ cos(x) - x*y, 412116Sjkh * a correction term is necessary in cos(x) and hence 422116Sjkh * cos(x+y) = 1 - (x*x/2 - (r - x*y)) 43151698Sbde * For better accuracy, rearrange to 44151698Sbde * cos(x+y) ~ w + (tmp + (r-x*y)) 45151698Sbde * where w = 1 - x*x/2 and tmp is a tiny correction term 46151698Sbde * (1 - x*x/2 == w + tmp exactly in infinite precision). 47151698Sbde * The exactness of w + tmp in infinite precision depends on w 48151698Sbde * and tmp having the same precision as x. If they have extra 49151698Sbde * precision due to compiler bugs, then the extra precision is 50151698Sbde * only good provided it is retained in all terms of the final 51151698Sbde * expression for cos(). Retention happens in all cases tested 52151698Sbde * under FreeBSD, so don't pessimize things by forcibly clipping 53151698Sbde * any extra precision in w. 542116Sjkh */ 552116Sjkh 562116Sjkh#include "math.h" 572116Sjkh#include "math_private.h" 582116Sjkh 598870Srgrimesstatic const double 602116Sjkhone = 1.00000000000000000000e+00, /* 0x3FF00000, 0x00000000 */ 612116SjkhC1 = 4.16666666666666019037e-02, /* 0x3FA55555, 0x5555554C */ 622116SjkhC2 = -1.38888888888741095749e-03, /* 0xBF56C16C, 0x16C15177 */ 632116SjkhC3 = 2.48015872894767294178e-05, /* 0x3EFA01A0, 0x19CB1590 */ 642116SjkhC4 = -2.75573143513906633035e-07, /* 0xBE927E4F, 0x809C52AD */ 652116SjkhC5 = 2.08757232129817482790e-09, /* 0x3E21EE9E, 0xBDB4B1C4 */ 662116SjkhC6 = -1.13596475577881948265e-11; /* 0xBDA8FAE9, 0xBE8838D4 */ 672116Sjkh 6897413Salfreddouble 6997413Salfred__kernel_cos(double x, double y) 702116Sjkh{ 71151698Sbde double hz,z,r,w; 72151698Sbde 732116Sjkh z = x*x; 74176408Sbde w = z*z; 75176408Sbde r = z*(C1+z*(C2+z*C3)) + w*w*(C4+z*(C5+z*C6)); 76175665Sbde hz = 0.5*z; 77151698Sbde w = one-hz; 78151698Sbde return w + (((one-w)-hz) + (z*r-x*y)); 792116Sjkh} 80