Path: blob/master/src/java.base/share/native/libfdlibm/k_sin.c
41149 views
/*1* Copyright (c) 1998, 2001, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/* __kernel_sin( x, y, iy)26* kernel sin function on [-pi/4, pi/4], pi/4 ~ 0.785427* Input x is assumed to be bounded by ~pi/4 in magnitude.28* Input y is the tail of x.29* Input iy indicates whether y is 0. (if iy=0, y assume to be 0).30*31* Algorithm32* 1. Since sin(-x) = -sin(x), we need only to consider positive x.33* 2. if x < 2^-27 (hx<0x3e400000 0), return x with inexact if x!=0.34* 3. sin(x) is approximated by a polynomial of degree 13 on35* [0,pi/4]36* 3 1337* sin(x) ~ x + S1*x + ... + S6*x38* where39*40* |sin(x) 2 4 6 8 10 12 | -5841* |----- - (1+S1*x +S2*x +S3*x +S4*x +S5*x +S6*x )| <= 242* | x |43*44* 4. sin(x+y) = sin(x) + sin'(x')*y45* ~ sin(x) + (1-x*x/2)*y46* For better accuracy, let47* 3 2 2 2 248* r = x *(S2+x *(S3+x *(S4+x *(S5+x *S6))))49* then 3 250* sin(x) = x + (S1*x + (x *(r-y/2)+y))51*/5253#include "fdlibm.h"5455#ifdef __STDC__56static const double57#else58static double59#endif60half = 5.00000000000000000000e-01, /* 0x3FE00000, 0x00000000 */61S1 = -1.66666666666666324348e-01, /* 0xBFC55555, 0x55555549 */62S2 = 8.33333333332248946124e-03, /* 0x3F811111, 0x1110F8A6 */63S3 = -1.98412698298579493134e-04, /* 0xBF2A01A0, 0x19C161D5 */64S4 = 2.75573137070700676789e-06, /* 0x3EC71DE3, 0x57B1FE7D */65S5 = -2.50507602534068634195e-08, /* 0xBE5AE5E6, 0x8A2B9CEB */66S6 = 1.58969099521155010221e-10; /* 0x3DE5D93A, 0x5ACFD57C */6768#ifdef __STDC__69double __kernel_sin(double x, double y, int iy)70#else71double __kernel_sin(x, y, iy)72double x,y; int iy; /* iy=0 if y is zero */73#endif74{75double z,r,v;76int ix;77ix = __HI(x)&0x7fffffff; /* high word of x */78if(ix<0x3e400000) /* |x| < 2**-27 */79{if((int)x==0) return x;} /* generate inexact */80z = x*x;81v = z*x;82r = S2+z*(S3+z*(S4+z*(S5+z*S6)));83if(iy==0) return x+v*(S1+z*r);84else return x-((z*(half*y-v*r)-y)-v*S1);85}868788