@ohos-ports/math-base-special-kernel-tan 0.0.0-stage → 0.2.4-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/addon.c ADDED
@@ -0,0 +1,43 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2024 The Stdlib Authors.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ #include "stdlib/math/base/special/kernel_tan.h"
20
+ #include "stdlib/napi/argv.h"
21
+ #include "stdlib/napi/argv_double.h"
22
+ #include "stdlib/napi/argv_int32.h"
23
+ #include "stdlib/napi/create_double.h"
24
+ #include "stdlib/napi/export.h"
25
+ #include <node_api.h>
26
+
27
+ /**
28
+ * Receives JavaScript callback invocation data.
29
+ *
30
+ * @param env environment under which the function is invoked
31
+ * @param info callback data
32
+ * @return Node-API value
33
+ */
34
+ static napi_value addon( napi_env env, napi_callback_info info ) {
35
+ STDLIB_NAPI_ARGV( env, info, argv, argc, 3 );
36
+ STDLIB_NAPI_ARGV_DOUBLE( env, x, argv, 0 );
37
+ STDLIB_NAPI_ARGV_DOUBLE( env, y, argv, 1 );
38
+ STDLIB_NAPI_ARGV_INT32( env, k, argv, 2 );
39
+ STDLIB_NAPI_CREATE_DOUBLE( env, stdlib_base_kernel_tan( x, y, k ), out );
40
+ return out;
41
+ }
42
+
43
+ STDLIB_NAPI_MODULE_EXPORT_FCN( addon )
package/src/main.c ADDED
@@ -0,0 +1,198 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2022 The Stdlib Authors.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ *
18
+ *
19
+ * ## Notice
20
+ *
21
+ * The following copyright, license, and long comment were part of the original implementation available as part of [FreeBSD]{@link https://svnweb.freebsd.org/base/release/12.2.0/lib/msun/src/k_tan.c}. The implementation follows the original, but has been modified according to project conventions.
22
+ *
23
+ * ```text
24
+ * Copyright (C) 2004 by Sun Microsystems, Inc. All rights reserved.
25
+ *
26
+ * Developed at SunPro, a Sun Microsystems, Inc. business.
27
+ * Permission to use, copy, modify, and distribute this
28
+ * software is freely granted, provided that this notice
29
+ * is preserved.
30
+ * ```
31
+ */
32
+
33
+ #include "stdlib/math/base/special/kernel_tan.h"
34
+ #include "stdlib/number/float64/base/get_high_word.h"
35
+ #include "stdlib/number/float64/base/set_low_word.h"
36
+ #include <stdint.h>
37
+
38
+ static const double PI04 = 7.85398163397448278999e-01;
39
+ static const double PI04LO = 3.06161699786838301793e-17;
40
+ static const double T0 = 3.33333333333334091986e-01; // 3FD55555, 55555563
41
+
42
+ // Absolute value mask: 2147483647 => 0x7fffffff => 01111111111111111111111111111111
43
+ static const int32_t HIGH_WORD_ABS_MASK = 0x7fffffff;
44
+
45
+ /* Begin auto-generated functions. The following functions are auto-generated. Do not edit directly. */
46
+
47
+ // BEGIN: polyval_t_odd
48
+
49
+ /**
50
+ * Evaluates a polynomial.
51
+ *
52
+ * ## Notes
53
+ *
54
+ * - The implementation uses [Horner's rule][horners-method] for efficient computation.
55
+ *
56
+ * [horners-method]: https://en.wikipedia.org/wiki/Horner%27s_method
57
+ *
58
+ * @param x value at which to evaluate the polynomial
59
+ * @return evaluated polynomial
60
+ */
61
+ static double polyval_t_odd( const double x ) {
62
+ return 0.13333333333320124 + (x * (0.021869488294859542 + (x * (0.0035920791075913124 + (x * (0.0005880412408202641 + (x * (0.00007817944429395571 + (x * -0.000018558637485527546)))))))));
63
+ }
64
+
65
+ // END: polyval_t_odd
66
+
67
+ // BEGIN: polyval_t_even
68
+
69
+ /**
70
+ * Evaluates a polynomial.
71
+ *
72
+ * ## Notes
73
+ *
74
+ * - The implementation uses [Horner's rule][horners-method] for efficient computation.
75
+ *
76
+ * [horners-method]: https://en.wikipedia.org/wiki/Horner%27s_method
77
+ *
78
+ * @param x value at which to evaluate the polynomial
79
+ * @return evaluated polynomial
80
+ */
81
+ static double polyval_t_even( const double x ) {
82
+ return 0.05396825397622605 + (x * (0.0088632398235993 + (x * (0.0014562094543252903 + (x * (0.0002464631348184699 + (x * (0.00007140724913826082 + (x * 0.00002590730518636337)))))))));
83
+ }
84
+
85
+ // END: polyval_t_even
86
+
87
+ /* End auto-generated functions. */
88
+
89
+ /**
90
+ * Computes the tangent on \\( \approx\[-\pi/4, \pi/4] \\) (except on -0), \\( \pi/4 \approx 0.7854 \\).
91
+ *
92
+ * ## Method
93
+ *
94
+ * - Since \\( \tan(-x) = -\tan(x) \\), we need only to consider positive \\( x \\).
95
+ *
96
+ * - Callers must return \\( \tan(-0) = -0 \\) without calling here since our odd polynomial is not evaluated in a way that preserves \\( -0 \\). Callers may do the optimization \\( \tan(x) \approx x \\) for tiny \\( x \\).
97
+ *
98
+ * - \\( \tan(x) \\) is approximated by a odd polynomial of degree 27 on \\( \[0, 0.67434] \\)
99
+ *
100
+ * ```tex
101
+ * \tan(x) \approx x + T_1 x^3 + \ldots + T_{13} x^{27}
102
+ * ```
103
+ * where
104
+ *
105
+ * ```tex
106
+ * \left| \frac{\tan(x)}{x} - \left( 1 + T_1 x^2 + T_2 x^4 + \ldots + T_{13} x^{26} \right) \right| \le 2^{-59.2}
107
+ * ```
108
+ *
109
+ * - Note: \\( \tan(x+y) = \tan(x) + \tan'(x) \cdot y \approx \tan(x) + ( 1 + x \cdot x ) \cdot y \\). Therefore, for better accuracy in computing \\( \tan(x+y) \\), let
110
+ *
111
+ * ```tex
112
+ * r = x^3 \cdot \left( T_2+x^2 \cdot (T_3+x^2 \cdot (\ldots+x^2 \cdot (T_{12}+x^2 \cdot T_{13}))) \right)
113
+ * ```
114
+ *
115
+ * then
116
+ *
117
+ * ```tex
118
+ * \tan(x+y) = x^3 + \left( T_1 \cdot x^2 + (x \cdot (r+y)+y) \right)
119
+ * ```
120
+ *
121
+ * - For \\( x \\) in \\( \[0.67434, \pi/4] \\), let \\( y = \pi/4 - x \\), then
122
+ *
123
+ * ```tex
124
+ * \tan(x) = \tan\left(\tfrac{\pi}{4}-y\right) = \frac{1-\tan(y)}{1+\tan(y)} \\
125
+ * = 1 - 2 \cdot \left( \tan(y) - \tfrac{\tan(y)^2}{1+\tan(y)} \right)
126
+ * ```
127
+ *
128
+ * @param x input value (in radians, assumed to be bounded by `~pi/4` in magnitude)
129
+ * @param y tail of `x`
130
+ * @param k indicates whether tan (if k = 1) or -1/tan (if k = -1) is returned
131
+ * @return tangent
132
+ *
133
+ * @example
134
+ * double out = stdlib_base_kernel_tan( 3.141592653589793/4.0, 0.0, 1 );
135
+ * // returns ~1.0
136
+ */
137
+ double stdlib_base_kernel_tan( const double x, const double y, const int32_t k ) {
138
+ uint32_t hx;
139
+ int32_t ix;
140
+ double xc;
141
+ double yc;
142
+ double z;
143
+ double r;
144
+ double t;
145
+ double v;
146
+ double w;
147
+ double s;
148
+ double a;
149
+
150
+ stdlib_base_float64_get_high_word( x, &hx );
151
+
152
+ // High word of |x|:
153
+ ix = ( hx & HIGH_WORD_ABS_MASK );
154
+
155
+ // Case: |x| >= 0.6744
156
+ if ( ix >= 0x3FE59428 ) {
157
+ if ( x < 0 ) {
158
+ xc = -x;
159
+ yc = -y;
160
+ } else {
161
+ xc = x;
162
+ yc = y;
163
+ }
164
+ z = PI04 - xc;
165
+ w = PI04LO - yc;
166
+ xc = z + w;
167
+ yc = 0.0;
168
+ } else {
169
+ xc = x;
170
+ yc = y;
171
+ }
172
+ z = xc * xc;
173
+ w = z * z;
174
+
175
+ // Break x^5*(T[1]+x^2*T[2]+...) into x^5(T[1]+x^4*T[3]+...+x^20*T[11]) + x^5(x^2*(T[2]+x^4*T[4]+...+x^22*T[12]))...
176
+ r = polyval_t_odd( w );
177
+ v = z * polyval_t_even( w );
178
+ s = z * xc;
179
+ r = yc + (z * ((s * (r + v)) + yc));
180
+ r += T0 * s;
181
+ w = xc + r;
182
+ if ( ix >= 0x3FE59428 ) {
183
+ v = (double)k;
184
+ return ( 1.0 - (double)( (hx >> 30) & 2 ) ) * ( v - (2.0 * (xc - ((w * w / (w + v)) - r)) ));
185
+ }
186
+ if ( k == 1 ) {
187
+ return w;
188
+ }
189
+ // Compute -1/(x+r) accurately...
190
+ z = w;
191
+ stdlib_base_float64_set_low_word( 0, &z );
192
+ v = r - (z - xc); // z + v = r + xc
193
+ a = -1.0 / w;
194
+ t = a;
195
+ stdlib_base_float64_set_low_word( 0, &t );
196
+ s = 1.0 + (t * z);
197
+ return t + (a * (s + (t * v)));
198
+ }