Skip to content

Commit 1a73ee8

Browse files
committed
fix: correct C implementation and tests
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent d4f4761 commit 1a73ee8

File tree

7 files changed

+93
-131
lines changed

7 files changed

+93
-131
lines changed

lib/node_modules/@stdlib/math/base/special/expf/LICENSE

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -190,33 +190,3 @@ Developed at SunPro, a Sun Microsystems, Inc. business.
190190
Permission to use, copy, modify, and distribute this
191191
software is freely granted, provided that this notice
192192
is preserved.
193-
194-
* Go <https://raw.githubusercontent.com/golang/go/master/LICENSE>
195-
196-
Copyright (c) 2009 The Go Authors. All rights reserved.
197-
198-
Redistribution and use in source and binary forms, with or without
199-
modification, are permitted provided that the following conditions are
200-
met:
201-
202-
* Redistributions of source code must retain the above copyright
203-
notice, this list of conditions and the following disclaimer.
204-
* Redistributions in binary form must reproduce the above
205-
copyright notice, this list of conditions and the following disclaimer
206-
in the documentation and/or other materials provided with the
207-
distribution.
208-
* Neither the name of Google Inc. nor the names of its
209-
contributors may be used to endorse or promote products derived from
210-
this software without specific prior written permission.
211-
212-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
213-
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
214-
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
215-
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
216-
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
217-
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
218-
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
219-
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
220-
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
221-
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
222-
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

lib/node_modules/@stdlib/math/base/special/expf/include/stdlib/math/base/special/expf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ extern "C" {
2626
#endif
2727

2828
/**
29-
* Evaluates the natural exponential function (single-precision).
29+
* Evaluates the natural exponential function for a single-precision floating-point number.
3030
*/
3131
float stdlib_base_expf( const float x );
3232

lib/node_modules/@stdlib/math/base/special/expf/lib/main.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,21 +86,21 @@ var ONE = f32( 1.0 );
8686
* \end{align*}
8787
* ```
8888
*
89-
* We use a special Remes algorithm on \\(\[0,0.34658]\\) to generate a polynomial of degree \\(5\\) to approximate \\(R\\). The maximum error of this polynomial approximation is bounded by \\(2^{-59}\\). In other words,
89+
* We use a special Remes algorithm on \\(\[0,0.34658]\\) to generate a polynomial of degree \\(2\\) to approximate \\(R\\). The maximum error of this polynomial approximation is bounded by \\(2^{-27.74}\\). In other words,
9090
*
9191
* ```tex
92-
* R(z) \sim 2 + P_1 z + P_2 z^2 + P_3 z^3 + P_4 z^4 + P_5 z^5
92+
* R(z) \sim 2 + P_1 z + P_2 z^2
9393
* ```
9494
*
9595
* where \\( z = r^2 \\) and
9696
*
9797
* ```tex
98-
* \left| 2 + P_1 z + \ldots + P_5 z^5 - R(z) \right| \leq 2^{-59}
98+
* \left| 2 + P_1 z + P_2 z^2 - R(z) \right| \leq 2^{-27.74}
9999
* ```
100100
*
101101
* <!-- <note> -->
102102
*
103-
* The values of \\( P_1 \\) to \\( P_5 \\) are listed in the source code.
103+
* The values of \\( P_1 \\) to \\( P_2 \\) are listed in the source code.
104104
*
105105
* <!-- </note> -->
106106
*
@@ -116,7 +116,7 @@ var ONE = f32( 1.0 );
116116
* where
117117
*
118118
* ```tex
119-
* R_1(r) = r - P_1\ r^2 + P_2\ r^4 + \ldots + P_5\ r^{10}
119+
* R_1(r) = r - P_1\ r^2 + P_2\ r^4
120120
* ```
121121
*
122122
* 3. We scale back to obtain \\( e^{x} \\). From step 1, we have

lib/node_modules/@stdlib/math/base/special/expf/manifest.json

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,10 @@
3939
"libpath": [],
4040
"dependencies": [
4141
"@stdlib/math/base/napi/unary",
42-
"@stdlib/math/base/assert/is-nan",
42+
"@stdlib/math/base/assert/is-nanf",
43+
"@stdlib/math/base/special/ldexpf",
4344
"@stdlib/constants/float32/ninf",
44-
"@stdlib/constants/float32/pinf",
45-
"@stdlib/math/base/special/truncf",
46-
"@stdlib/math/base/special/ldexpf"
45+
"@stdlib/constants/float32/pinf"
4746
]
4847
},
4948
{
@@ -58,11 +57,10 @@
5857
"libraries": [],
5958
"libpath": [],
6059
"dependencies": [
61-
"@stdlib/math/base/assert/is-nan",
60+
"@stdlib/math/base/assert/is-nanf",
61+
"@stdlib/math/base/special/ldexpf",
6262
"@stdlib/constants/float32/ninf",
63-
"@stdlib/constants/float32/pinf",
64-
"@stdlib/math/base/special/truncf",
65-
"@stdlib/math/base/special/ldexpf"
63+
"@stdlib/constants/float32/pinf"
6664
]
6765
},
6866
{
@@ -77,11 +75,10 @@
7775
"libraries": [],
7876
"libpath": [],
7977
"dependencies": [
80-
"@stdlib/math/base/assert/is-nan",
78+
"@stdlib/math/base/assert/is-nanf",
79+
"@stdlib/math/base/special/ldexpf",
8180
"@stdlib/constants/float32/ninf",
82-
"@stdlib/constants/float32/pinf",
83-
"@stdlib/math/base/special/truncf",
84-
"@stdlib/math/base/special/ldexpf"
81+
"@stdlib/constants/float32/pinf"
8582
]
8683
},
8784
{
@@ -96,11 +93,10 @@
9693
"libraries": [],
9794
"libpath": [],
9895
"dependencies": [
99-
"@stdlib/math/base/assert/is-nan",
96+
"@stdlib/math/base/assert/is-nanf",
97+
"@stdlib/math/base/special/ldexpf",
10098
"@stdlib/constants/float32/ninf",
101-
"@stdlib/constants/float32/pinf",
102-
"@stdlib/math/base/special/truncf",
103-
"@stdlib/math/base/special/ldexpf"
99+
"@stdlib/constants/float32/pinf"
104100
]
105101
}
106102
]

lib/node_modules/@stdlib/math/base/special/expf/src/addon.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,4 @@
2020
#include "stdlib/math/base/napi/unary.h"
2121
#include "stdlib/math/base/special/expf.h"
2222

23-
// cppcheck-suppress shadowFunction
2423
STDLIB_MATH_BASE_NAPI_MODULE_F_F( stdlib_base_expf )

lib/node_modules/@stdlib/math/base/special/expf/src/main.c

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,20 @@
3030
* ```
3131
*/
3232

33-
#include "stdlib/constants/float32/ninf.h"
34-
#include "stdlib/constants/float32/pinf.h"
35-
#include "stdlib/math/base/assert/is_nan.h"
3633
#include "stdlib/math/base/special/expf.h"
34+
#include "stdlib/math/base/assert/is_nanf.h"
3735
#include "stdlib/math/base/special/ldexpf.h"
38-
#include "stdlib/math/base/special/truncf.h"
36+
#include "stdlib/constants/float32/ninf.h"
37+
#include "stdlib/constants/float32/pinf.h"
3938
#include <stdint.h>
4039

41-
static const float LN2_HI = 6.9314575195e-01;
42-
static const float LN2_LO = 1.4286067653e-06;
43-
static const float LOG2_E = 1.4426950216e+00;
44-
static const float half[ 2 ] = { 0.5, -0.5 };
45-
static const float EXP_OVERFLOW = 8.8721679688e+01;
46-
static const float EXP_UNDERFLOW = -1.0397208405e+02;
47-
static const float NEARZERO = 1.0 / ( 1 << 14 );
48-
static const float NEG_NEARZERO = -1.0 / ( 1 << 14 );
40+
static const float LN2_HI = 6.9314575195e-01; // 0x3F317200
41+
static const float LN2_LO = 1.4286067653e-06; // 0x35BFBE8E
42+
static const float INVLN2 = 1.4426950216e+00; // 0x3FB8AA3B
43+
static const float EXP_OVERFLOW = 8.8721679688e+01; // 0x42B17180
44+
static const float EXP_UNDERFLOW = -1.0397208405e+02; // 0xC2CFF1B5
45+
static const float NEARZERO = 1.0f / (1 << 14); // 0x39000000
46+
static const float NEG_NEARZERO = -NEARZERO; // 0xB9000000
4947

5048
/* Begin auto-generated functions. The following functions are auto-generated. Do not edit directly. */
5149

@@ -72,30 +70,25 @@ static float polyval_p( const float x ) {
7270
/* End auto-generated functions. */
7371

7472
/**
75-
* Computes \\(e^{r} 2^k\\) where \\(r = \mathrm{hi} - \mathrm{lo}\\) and \\(|r| \leq \ln(2)/2\\).
73+
* Computes \\(e^{r} 2^k\\) for a single-precision floating-point number, where \\(r = \mathrm{hi} - \mathrm{lo}\\) and \\(|r| \leq \ln(2)/2\\).
7674
*
7775
* @param hi upper bound
7876
* @param lo lower bound
7977
* @param k power of 2
8078
* @return function value
8179
*/
82-
static float expmulti( const float hi, const float lo, const int32_t k ) {
80+
static float expmultif( const float hi, const float lo, const int32_t k ) {
8381
float r;
8482
float t;
8583
float c;
8684
float y;
87-
float twom100;
8885

89-
twom100 = 7.8886090522e-31;
9086
r = hi - lo;
9187
t = r * r;
9288
c = r - ( t * polyval_p( t ) );
93-
y = 1.0 - ( lo - ( ( r * c ) / ( 2.0 - c ) ) - hi );
94-
if ( k >= -125 ) {
95-
return stdlib_base_ldexpf( y, k );
96-
} else {
97-
return stdlib_base_ldexpf( y, k + 100 ) * twom100 * twom100;
98-
}
89+
y = 1.0f - ( lo - ( ( r*c )/( 2.0f-c ) ) - hi );
90+
91+
return stdlib_base_ldexpf( y, k );
9992
}
10093

10194
/**
@@ -127,21 +120,21 @@ static float expmulti( const float hi, const float lo, const int32_t k ) {
127120
* \end{align*}
128121
* ```
129122
*
130-
* We use a special Remes algorithm on \\(\[0,0.34658]\\) to generate a polynomial of degree \\(5\\) to approximate \\(R\\). The maximum error of this polynomial approximation is bounded by \\(2^{-59}\\). In other words,
123+
* We use a special Remes algorithm on \\(\[0,0.34658]\\) to generate a polynomial of degree \\(2\\) to approximate \\(R\\). The maximum error of this polynomial approximation is bounded by \\(2^{-27.74}\\). In other words,
131124
*
132125
* ```tex
133-
* R(z) \sim 2 + P_1 z + P_2 z^2 + P_3 z^3 + P_4 z^4 + P_5 z^5
126+
* R(z) \sim 2 + P_1 z + P_2 z^2
134127
* ```
135128
*
136129
* where \\( z = r^2 \\) and
137130
*
138131
* ```tex
139-
* \left| 2 + P_1 z + \ldots + P_5 z^5 - R(z) \right| \leq 2^{-59}
132+
* \left| 2 + P_1 z + P_2 z^2 - R(z) \right| \leq 2^{-27.74}
140133
* ```
141134
*
142135
* <!-- <note> -->
143136
*
144-
* The values of \\( P_1 \\) to \\( P_5 \\) are listed in the source code.
137+
* The values of \\( P_1 \\) to \\( P_2 \\) are listed in the source code.
145138
*
146139
* <!-- </note> -->
147140
*
@@ -157,7 +150,7 @@ static float expmulti( const float hi, const float lo, const int32_t k ) {
157150
* where
158151
*
159152
* ```tex
160-
* R_1(r) = r - P_1\ r^2 + P_2\ r^4 + \ldots + P_5\ r^{10}
153+
* R_1(r) = r - P_1\ r^2 + P_2\ r^4
161154
* ```
162155
*
163156
* 3. We scale back to obtain \\( e^{x} \\). From step 1, we have
@@ -166,7 +159,6 @@ static float expmulti( const float hi, const float lo, const int32_t k ) {
166159
* e^{x} = 2^k e^{r}
167160
* ```
168161
*
169-
*
170162
* ## Special Cases
171163
*
172164
* ```tex
@@ -186,35 +178,37 @@ static float expmulti( const float hi, const float lo, const int32_t k ) {
186178
* @return output value
187179
*
188180
* @example
189-
* double out = stdlib_base_expf( 0.0f );
181+
* float out = stdlib_base_expf( 0.0f );
190182
* // returns 1.0f
191183
*/
192184
float stdlib_base_expf( const float x ) {
185+
int32_t k;
193186
float hi;
194187
float lo;
195-
int32_t k;
196-
int xsb;
197188

198-
xsb = ( x < 0.0 ) ? 1 : 0;
199-
if ( stdlib_base_is_nan( x ) || x == STDLIB_CONSTANT_FLOAT32_PINF ) {
189+
if ( stdlib_base_is_nanf( x ) || x == STDLIB_CONSTANT_FLOAT32_PINF ) {
200190
return x;
201191
}
202192
if ( x == STDLIB_CONSTANT_FLOAT32_NINF ) {
203-
return 0.0;
193+
return 0.0f;
204194
}
205195
if ( x > EXP_OVERFLOW ) {
206196
return STDLIB_CONSTANT_FLOAT32_PINF;
207197
}
208198
if ( x < EXP_UNDERFLOW ) {
209-
return 0.0;
199+
return 0.0f;
210200
}
211201
if ( x > NEG_NEARZERO && x < NEARZERO ) {
212-
return 1.0 + x;
202+
return 1.0f + x;
213203
}
214204
// Reduce and compute `r = hi - lo` for extra precision...
215-
k = stdlib_base_truncf( ( LOG2_E * x ) + half[ xsb ] );
205+
if ( x < 0.0f ) {
206+
k = (int32_t)( ( INVLN2 * x ) - 0.5f );
207+
} else {
208+
k = (int32_t)( ( INVLN2 * x ) + 0.5f );
209+
}
216210
hi = x - ( k * LN2_HI );
217211
lo = k * LN2_LO;
218212

219-
return expmulti( hi, lo, k );
213+
return expmultif( hi, lo, k );
220214
}

0 commit comments

Comments
 (0)