amath  1.8.5
Simple command line calculator
ksin.c
Go to the documentation of this file.
1 /*-
2  * Copyright (c) 2014-2018 Carsten Sonne Larsen <cs@innolan.net>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * Project homepage:
26  * https://amath.innolan.net
27  *
28  * The original source code can be obtained from:
29  * http://www.netlib.org/fdlibm/k_sin.c
30  *
31  * =================================================================
32  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
33  *
34  * Developed at SunSoft, a Sun Microsystems, Inc. business.
35  * Permission to use, copy, modify, and distribute this
36  * software is freely granted, provided that this notice
37  * is preserved.
38  * =================================================================
39  */
40 
41 /**
42  * @file ksin.c
43  * @brief Kernel sin function
44  */
45 
46 #include "prim.h"
47 
48 static const double
49  half = 5.00000000000000000000e-01, /* 0x3FE00000, 0x00000000 */
50  S1 = -1.66666666666666324348e-01, /* 0xBFC55555, 0x55555549 */
51  S2 = 8.33333333332248946124e-03, /* 0x3F811111, 0x1110F8A6 */
52  S3 = -1.98412698298579493134e-04, /* 0xBF2A01A0, 0x19C161D5 */
53  S4 = 2.75573137070700676789e-06, /* 0x3EC71DE3, 0x57B1FE7D */
54  S5 = -2.50507602534068634195e-08, /* 0xBE5AE5E6, 0x8A2B9CEB */
55  S6 = 1.58969099521155010221e-10; /* 0x3DE5D93A, 0x5ACFD57C */
56 
57 /**
58  * @brief Kernel sin function
59  * @details
60  * <pre>
61  * Kernel sin function on [-pi/4, pi/4], pi/4 ~ 0.7854
62  * Input x is assumed to be bounded by ~pi/4 in magnitude.
63  * Input y is the tail of x.
64  * Input iy indicates whether y is 0. (if iy=0, y assume to be 0).
65  *
66  * Algorithm
67  * 1. Since sin(-x) = -sin(x), we need only to consider positive x.
68  * 2. if x < 2^-27 (hx<0X3E400000 0), return x with inexact if x!=0.
69  * 3. sin(x) is approximated by a polynomial of degree 13 on [0,pi/4]
70  * 3 13
71  * sin(x) ~ x + S1*x + ... + S6*x
72  *
73  * where
74  *
75  * |sin(x) 2 4 6 8 10 12 | -58
76  * |----- - (1+S1*x +S2*x +S3*x +S4*x +S5*x +S6*x )| <= 2
77  * | x |
78  *
79  * 4. sin(x+y) = sin(x) + sin'(x')*y
80  * ~ sin(x) + (1-x*x/2)*y
81  *
82  * For better accuracy, let
83  * 3 2 2 2 2
84  * r = x *(S2+x *(S3+x *(S4+x *(S5+x *S6))))
85  * then 3 2
86  * sin(x) = x + (S1*x + (x *(r-y/2)+y))
87  * </pre>
88  */
89 double __kernel_sin(double x, double y, int iy)
90 {
91  double z, r, v;
92  int32_t ix;
93 
94  GET_HIGH_WORD(ix, x);
95  ix &= 0x7FFFFFFF;
96 
97  // |x| < 2**-27
98  if (ix < 0x3E400000)
99  {
100  // generate inexact
101  if ((int)x == 0)
102  {
103  return x;
104  }
105  }
106 
107  z = x * x;
108  v = z * x;
109  r = S2 + z * (S3 + z * (S4 + z * (S5 + z * S6)));
110 
111  if (iy == 0)
112  {
113  return x + v * (S1 + z * r);
114  }
115 
116  return x - ((z * (half * y - v * r) - y) - v * S1);
117 }
#define GET_HIGH_WORD(i, d)
Get the more significant 32 bit int from a double.
Definition: prim.h:167
static const double S1
Definition: ksin.c:50
static const double half
Definition: ksin.c:49
static const double S3
Definition: ksin.c:52
double __kernel_sin(double x, double y, int iy)
Kernel sin function.
Definition: ksin.c:89
static const double S4
Definition: ksin.c:53
static const double S6
Definition: ksin.c:55
static const double S5
Definition: ksin.c:54
static const double S2
Definition: ksin.c:51