From 9d124921a3432b3401e43f80ec3121f828fea771 Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Tue, 14 Jan 2025 09:26:42 +0000 Subject: [PATCH 1/3] feat: add C ndarray API and refactor --- 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: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: missing_dependencies - task: lint_c_examples status: missing_dependencies - task: lint_c_benchmarks status: missing_dependencies - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: na - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: na - task: run_c_benchmarks status: na - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: na --- --- .../@stdlib/blas/ext/base/sapxsumpw/README.md | 150 ++++++++++++++++-- .../ext/base/sapxsumpw/benchmark/benchmark.js | 15 +- .../sapxsumpw/benchmark/benchmark.native.js | 11 +- .../sapxsumpw/benchmark/benchmark.ndarray.js | 15 +- .../benchmark/benchmark.ndarray.native.js | 11 +- .../sapxsumpw/benchmark/c/benchmark.length.c | 50 +++++- .../blas/ext/base/sapxsumpw/docs/repl.txt | 40 +++-- .../ext/base/sapxsumpw/docs/types/index.d.ts | 24 +-- .../ext/base/sapxsumpw/examples/c/example.c | 15 +- .../blas/ext/base/sapxsumpw/examples/index.js | 10 +- .../include/stdlib/blas/ext/base/sapxsumpw.h | 9 +- .../blas/ext/base/sapxsumpw/lib/index.js | 5 +- .../blas/ext/base/sapxsumpw/lib/ndarray.js | 54 +++---- .../ext/base/sapxsumpw/lib/ndarray.native.js | 20 +-- .../blas/ext/base/sapxsumpw/lib/sapxsumpw.js | 41 +---- .../base/sapxsumpw/lib/sapxsumpw.native.js | 13 +- .../blas/ext/base/sapxsumpw/manifest.json | 35 ++-- .../blas/ext/base/sapxsumpw/package.json | 2 +- .../blas/ext/base/sapxsumpw/src/addon.c | 30 +++- .../blas/ext/base/sapxsumpw/src/main.c | 125 +++++++++++++++ .../blas/ext/base/sapxsumpw/src/sapxsumpw.c | 122 -------------- .../ext/base/sapxsumpw/test/test.ndarray.js | 4 +- .../sapxsumpw/test/test.ndarray.native.js | 4 +- .../ext/base/sapxsumpw/test/test.sapxsumpw.js | 4 +- .../sapxsumpw/test/test.sapxsumpw.native.js | 4 +- 25 files changed, 484 insertions(+), 329 deletions(-) create mode 100644 lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/src/main.c delete mode 100644 lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/src/sapxsumpw.c diff --git a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/README.md b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/README.md index b4ec0a254771..bc0884a453e5 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/README.md +++ b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/README.md @@ -20,7 +20,7 @@ limitations under the License. # sapxsumpw -> Add a constant to each single-precision floating-point strided array element and compute the sum using pairwise summation. +> Add a scalar constant to each single-precision floating-point strided array element and compute the sum using pairwise summation.
@@ -36,27 +36,27 @@ limitations under the License. var sapxsumpw = require( '@stdlib/blas/ext/base/sapxsumpw' ); ``` -#### sapxsumpw( N, alpha, x, stride ) +#### sapxsumpw( N, alpha, x, strideX ) -Adds a constant to each single-precision floating-point strided array element and computes the sum using pairwise summation. +Adds a scalar constant to each single-precision floating-point strided array element and computes the sum using pairwise summation. ```javascript var Float32Array = require( '@stdlib/array/float32' ); var x = new Float32Array( [ 1.0, -2.0, 2.0 ] ); -var N = x.length; -var v = sapxsumpw( N, 5.0, x, 1 ); +var v = sapxsumpw( x.length, 5.0, x, 1 ); // returns 16.0 ``` The function has the following parameters: - **N**: number of indexed elements. +- **alpha**: scalar constant. - **x**: input [`Float32Array`][@stdlib/array/float32]. -- **stride**: index increment for `x`. +- **stride**: stride length. -The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to access every other element in `x`, +The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to access every other element: ```javascript var Float32Array = require( '@stdlib/array/float32' ); @@ -81,25 +81,24 @@ var v = sapxsumpw( 4, 5.0, x1, 2 ); // returns 25.0 ``` -#### sapxsumpw.ndarray( N, alpha, x, stride, offset ) +#### sapxsumpw.ndarray( N, alpha, x, strideX, offsetX ) -Adds a constant to each single-precision floating-point strided array element and computes the sum using pairwise summation and alternative indexing semantics. +Adds a scalar constant to each single-precision floating-point strided array element and computes the sum using pairwise summation and alternative indexing semantics. ```javascript var Float32Array = require( '@stdlib/array/float32' ); var x = new Float32Array( [ 1.0, -2.0, 2.0 ] ); -var N = x.length; -var v = sapxsumpw.ndarray( N, 5.0, x, 1, 0 ); +var v = sapxsumpw.ndarray( x.length, 5.0, x, 1, 0 ); // returns 16.0 ``` The function has the following additional parameters: -- **offset**: starting index for `x`. +- **offsetX**: starting index. -While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying `buffer`, the `offset` parameter supports indexing semantics based on a starting index. For example, to access every other value in `x` starting from the second value +While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to access every other element starting from the second element: ```javascript var Float32Array = require( '@stdlib/array/float32' ); @@ -131,12 +130,12 @@ var v = sapxsumpw.ndarray( 4, 5.0, x, 2, 1 ); ```javascript -var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory; -var filledarrayBy = require( '@stdlib/array/filled-by' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); var sapxsumpw = require( '@stdlib/blas/ext/base/sapxsumpw' ); -var x = filledarrayBy( 10, 'float32', discreteUniform( 0, 100 ) ); - +var x = discreteUniform( 10, -100, 100, { + 'dtype': 'float32' +}); console.log( x ); var v = sapxsumpw( x.length, 5.0, x, 1 ); @@ -147,8 +146,125 @@ console.log( v ); + + * * * +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### Usage + +```c +#include "stdlib/blas/ext/base/sapxsumpw.h" +``` + +#### stdlib_strided_sapxsumpw( N, alpha, \*X, strideX ) + +Adds a scalar constant to each single-precision floating-point strided array element and computes the sum using pairwise summation. + +```c +const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f }; + +float v = stdlib_strided_sapxsumpw( 4, 5.0f, x, 1 ); +// returns 30.0 +``` + +The function accepts the following arguments: + +- **N**: `[in] CBLAS_INT` number of indexed elements. +- **alpha**: `[in] float` scalar constant. +- **X**: `[in] float*` input array. +- **strideX**: `[in] CBLAS_INT` stride length. + +```c +float stdlib_strided_sapxsumpw( const CBLAS_INT N, const float alpha, const float *X, const CBLAS_INT strideX ); +``` + +#### stdlib_strided_sapxsumpw_ndarray( N, alpha, \*X, strideX, offsetX ) + +Adds a scalar constant to each single-precision floating-point strided array element and computes the sum using pairwise summation and alternative indexing semantics. + +```c +const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f }; + +float v = stdlib_strided_sapxsumpw_ndarray( 4, 5.0f, x, 1, 0 ); +// returns 30.0 +``` + +The function accepts the following arguments: + +- **N**: `[in] CBLAS_INT` number of indexed elements. +- **alpha**: `[in] float` scalar constant. +- **X**: `[in] float*` input array. +- **strideX**: `[in] CBLAS_INT` stride length. +- **offsetX**: `[in] CBLAS_INT` starting index. + +```c +float stdlib_strided_sapxsumpw_ndarray( const CBLAS_INT N, const float alpha, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ); +``` + +
+ + + + + +
+ +
+ + + + + +
+ +### Examples + +```c +#include "stdlib/blas/ext/base/sapxsumpw.h" +#include + +int main( void ) { + // Create a strided array: + const float x[] = { 1.0f, -2.0f, 3.0f, -4.0f, 5.0f, -6.0f, 7.0f, -8.0f }; + + // Specify the number of indexed elements: + const int N = 8; + + // Specify a stride: + const int strideX = 1; + + // Compute the sum: + float v = stdlib_strided_sapxsumpw( N, 5.0f, x, strideX ); + + // Print the result: + printf( "Sum: %f\n", v ); +} +``` + +
+ + + +
+ + +
## References diff --git a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/benchmark/benchmark.js index 1e03ab275c86..411cceebc610 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/benchmark/benchmark.js @@ -21,14 +21,20 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var uniform = require( '@stdlib/random/base/uniform' ).factory; -var filledarrayBy = require( '@stdlib/array/filled-by' ); +var uniform = require( '@stdlib/random/array/uniform' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pow = require( '@stdlib/math/base/special/pow' ); var pkg = require( './../package.json' ).name; var sapxsumpw = require( './../lib/sapxsumpw.js' ); +// VARIABLES // + +var options = { + 'dtype': 'float32' +}; + + // FUNCTIONS // /** @@ -39,10 +45,7 @@ var sapxsumpw = require( './../lib/sapxsumpw.js' ); * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x; - - x = filledarrayBy( len, 'float32', uniform( -10.0, 10.0 ) ); - + var x = uniform( len, -100, 100, options ); return benchmark; function benchmark( b ) { diff --git a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/benchmark/benchmark.native.js b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/benchmark/benchmark.native.js index 756d5a078148..520514c97340 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/benchmark/benchmark.native.js +++ b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/benchmark/benchmark.native.js @@ -22,8 +22,7 @@ var resolve = require( 'path' ).resolve; var bench = require( '@stdlib/bench' ); -var uniform = require( '@stdlib/random/base/uniform' ).factory; -var filledarrayBy = require( '@stdlib/array/filled-by' ); +var uniform = require( '@stdlib/random/array/uniform' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pow = require( '@stdlib/math/base/special/pow' ); var tryRequire = require( '@stdlib/utils/try-require' ); @@ -36,6 +35,9 @@ var sapxsumpw = tryRequire( resolve( __dirname, './../lib/sapxsumpw.native.js' ) var opts = { 'skip': ( sapxsumpw instanceof Error ) }; +var options = { + 'dtype': 'float32' +}; // FUNCTIONS // @@ -48,10 +50,7 @@ var opts = { * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x; - - x = filledarrayBy( len, 'float32', uniform( -10.0, 10.0 ) ); - + var x = uniform( len, -100, 100, options ); return benchmark; function benchmark( b ) { diff --git a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/benchmark/benchmark.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/benchmark/benchmark.ndarray.js index e680e6575f61..5558078e88e8 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/benchmark/benchmark.ndarray.js +++ b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/benchmark/benchmark.ndarray.js @@ -21,14 +21,20 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var uniform = require( '@stdlib/random/base/uniform' ).factory; -var filledarrayBy = require( '@stdlib/array/filled-by' ); +var uniform = require( '@stdlib/random/array/uniform' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pow = require( '@stdlib/math/base/special/pow' ); var pkg = require( './../package.json' ).name; var sapxsumpw = require( './../lib/ndarray.js' ); +// VARIABLES // + +var options = { + 'dtype': 'float32' +}; + + // FUNCTIONS // /** @@ -39,10 +45,7 @@ var sapxsumpw = require( './../lib/ndarray.js' ); * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x; - - x = filledarrayBy( len, 'float32', uniform( -10.0, 10.0 ) ); - + var x = uniform( len, -100, 100, options ); return benchmark; function benchmark( b ) { diff --git a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/benchmark/benchmark.ndarray.native.js b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/benchmark/benchmark.ndarray.native.js index 00d203a97740..f62b256ca9fd 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/benchmark/benchmark.ndarray.native.js +++ b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/benchmark/benchmark.ndarray.native.js @@ -22,8 +22,7 @@ var resolve = require( 'path' ).resolve; var bench = require( '@stdlib/bench' ); -var uniform = require( '@stdlib/random/base/uniform' ).factory; -var filledarrayBy = require( '@stdlib/array/filled-by' ); +var uniform = require( '@stdlib/random/array/uniform' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pow = require( '@stdlib/math/base/special/pow' ); var tryRequire = require( '@stdlib/utils/try-require' ); @@ -36,6 +35,9 @@ var sapxsumpw = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' ) ) var opts = { 'skip': ( sapxsumpw instanceof Error ) }; +var options = { + 'dtype': 'float32' +}; // FUNCTIONS // @@ -48,10 +50,7 @@ var opts = { * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x; - - x = filledarrayBy( len, 'float32', uniform( -10.0, 10.0 ) ); - + var x = uniform( len, -100, 100, options ); return benchmark; function benchmark( b ) { diff --git a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/benchmark/c/benchmark.length.c b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/benchmark/c/benchmark.length.c index d347bace99ea..178f14510774 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/benchmark/c/benchmark.length.c +++ b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/benchmark/c/benchmark.length.c @@ -94,7 +94,7 @@ static float rand_float( void ) { * @param len array length * @return elapsed time in seconds */ -static double benchmark( int iterations, int len ) { +static double benchmark1( int iterations, int len ) { double elapsed; float x[ len ]; float v; @@ -107,6 +107,7 @@ static double benchmark( int iterations, int len ) { v = 0.0f; t = tic(); for ( i = 0; i < iterations; i++ ) { + // cppcheck-suppress uninitvar v = stdlib_strided_sapxsumpw( len, 5.0f, x, 1 ); if ( v != v ) { printf( "should not return NaN\n" ); @@ -120,6 +121,40 @@ static double benchmark( int iterations, int len ) { return elapsed; } +/** +* Runs a benchmark. +* +* @param iterations number of iterations +* @param len array length +* @return elapsed time in seconds +*/ +static double benchmark2( int iterations, int len ) { + double elapsed; + float x[ len ]; + float v; + double t; + int i; + + for ( i = 0; i < len; i++ ) { + x[ i ] = ( rand_float() * 20000.0f ) - 10000.0f; + } + v = 0.0f; + t = tic(); + for ( i = 0; i < iterations; i++ ) { + // cppcheck-suppress uninitvar + v = stdlib_strided_sapxsumpw_ndarray( len, 5.0f, x, 1, 0 ); + if ( v != v ) { + printf( "should not return NaN\n" ); + break; + } + } + elapsed = tic() - t; + if ( v != v ) { + printf( "should not return NaN\n" ); + } + return elapsed; +} + /** * Main execution sequence. */ @@ -142,7 +177,18 @@ int main( void ) { for ( j = 0; j < REPEATS; j++ ) { count += 1; printf( "# c::%s:len=%d\n", NAME, len ); - elapsed = benchmark( iter, len ); + elapsed = benchmark1( iter, len ); + print_results( iter, elapsed ); + printf( "ok %d benchmark finished\n", count ); + } + } + for ( i = MIN; i <= MAX; i++ ) { + len = pow( 10, i ); + iter = ITERATIONS / pow( 10, i-1 ); + for ( j = 0; j < REPEATS; j++ ) { + count += 1; + printf( "# c::%s:ndarray:len=%d\n", NAME, len ); + elapsed = benchmark2( iter, len ); print_results( iter, elapsed ); printf( "ok %d benchmark finished\n", count ); } diff --git a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/docs/repl.txt index 469b9b207f4f..fb126ecf3d70 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/docs/repl.txt +++ b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/docs/repl.txt @@ -1,10 +1,10 @@ -{{alias}}( N, alpha, x, stride ) - Adds a constant to each single-precision floating-point strided array - element and computes the sum using pairwise summation. +{{alias}}( N, alpha, x, strideX ) + Adds a scalar constant to each single-precision floating-point strided + array element and computes the sum using pairwise summation. - The `N` and stride parameters determine which elements in `x` are accessed - at runtime. + The `N` and stride parameters determine which elements in the strided array + are accessed at runtime. Indexing is relative to the first index. To introduce an offset, use a typed array view. @@ -17,13 +17,13 @@ Number of indexed elements. alpha: number - Constant. + Scalar constant. x: Float32Array Input array. - stride: integer - Index increment. + strideX: integer + Stride length. Returns ------- @@ -37,27 +37,25 @@ > {{alias}}( x.length, 5.0, x, 1 ) 16.0 - // Using `N` and `stride` parameters: + // Using `N` and stride parameters: > x = new {{alias:@stdlib/array/float32}}( [ -2.0, 1.0, 1.0, -5.0, 2.0, -1.0 ] ); - > var stride = 2; - > {{alias}}( 3, 5.0, x, stride ) + > {{alias}}( 3, 5.0, x, 2 ) 16.0 // Using view offsets: > var x0 = new {{alias:@stdlib/array/float32}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ] ); > var x1 = new {{alias:@stdlib/array/float32}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); - > stride = 2; - > {{alias}}( 3, 5.0, x1, stride ) + > {{alias}}( 3, 5.0, x1, 2 ) 14.0 -{{alias}}.ndarray( N, alpha, x, stride, offset ) - Adds a constant to each single-precision floating-point strided array - element and computes the sum using pairwise summation and alternative +{{alias}}.ndarray( N, alpha, x, strideX, offsetX ) + Adds a scalar constant to each single-precision floating-point strided + array element and computes the sum using pairwise summation and alternative indexing semantics. While typed array views mandate a view offset based on the underlying - buffer, the `offset` parameter supports indexing semantics based on a + buffer, the offset parameter supports indexing semantics based on a starting index. Parameters @@ -66,15 +64,15 @@ Number of indexed elements. alpha: number - Constant. + Scalar constant. x: Float32Array Input array. - stride: integer - Index increment. + strideX: integer + Stride length. - offset: integer + offsetX: integer Starting index. Returns diff --git a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/docs/types/index.d.ts index 6d3d5190de71..30a0802846df 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/docs/types/index.d.ts @@ -23,12 +23,12 @@ */ interface Routine { /** - * Adds a constant to each single-precision floating-point strided array element and computes the sum using pairwise summation. + * Adds a scalar constant to each single-precision floating-point strided array element and computes the sum using pairwise summation. * * @param N - number of indexed elements - * @param alpha - constant + * @param alpha - scalar constant * @param x - input array - * @param stride - stride length + * @param strideX - stride length * @returns sum * * @example @@ -39,16 +39,16 @@ interface Routine { * var v = sapxsumpw( x.length, 5.0, x, 1 ); * // returns 16.0 */ - ( N: number, alpha: number, x: Float32Array, stride: number ): number; + ( N: number, alpha: number, x: Float32Array, strideX: number ): number; /** - * Adds a constant to each single-precision floating-point strided array element and computes the sum using pairwise summation and alternative indexing semantics. + * Adds a scalar constant to each single-precision floating-point strided array element and computes the sum using pairwise summation and alternative indexing semantics. * * @param N - number of indexed elements - * @param alpha - constant + * @param alpha - scalar constant * @param x - input array - * @param stride - stride length - * @param offset - starting index + * @param strideX - stride length + * @param offsetX - starting index * @returns sum * * @example @@ -59,16 +59,16 @@ interface Routine { * var v = sapxsumpw.ndarray( x.length, 5.0, x, 1, 0 ); * // returns 16.0 */ - ndarray( N: number, alpha: number, x: Float32Array, stride: number, offset: number ): number; + ndarray( N: number, alpha: number, x: Float32Array, strideX: number, offsetX: number ): number; } /** -* Adds a constant to each single-precision floating-point strided array element and computes the sum using pairwise summation. +* Adds a scalar constant to each single-precision floating-point strided array element and computes the sum using pairwise summation. * * @param N - number of indexed elements -* @param alpha - constant +* @param alpha - scalar constant * @param x - input array -* @param stride - stride length +* @param strideX - stride length * @returns sum * * @example diff --git a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/examples/c/example.c b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/examples/c/example.c index ed7c4a77a0e6..a93d075fc6bb 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/examples/c/example.c +++ b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/examples/c/example.c @@ -17,22 +17,21 @@ */ #include "stdlib/blas/ext/base/sapxsumpw.h" -#include #include int main( void ) { // Create a strided array: - const float x[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 }; + const float x[] = { 1.0f, -2.0f, 3.0f, -4.0f, 5.0f, -6.0f, 7.0f, -8.0f }; - // Specify the number of elements: - const int64_t N = 4; + // Specify the number of indexed elements: + const int N = 8; - // Specify the stride length: - const int64_t stride = 2; + // Specify a stride: + const int strideX = 1; // Compute the sum: - float v = stdlib_strided_sapxsumpw( N, 5.0f, x, stride ); + float v = stdlib_strided_sapxsumpw( N, 5.0f, x, strideX ); // Print the result: - printf( "sum: %f\n", v ); + printf( "Sum: %f\n", v ); } diff --git a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/examples/index.js b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/examples/index.js index ce7accccd889..aa7262357c1b 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/examples/index.js +++ b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/examples/index.js @@ -18,14 +18,12 @@ 'use strict'; -var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory; -var filledarrayBy = require( '@stdlib/array/filled-by' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); var sapxsumpw = require( './../lib' ); -var x; - -x = filledarrayBy( 10, 'float32', discreteUniform( 0, 100 ) ); - +var x = discreteUniform( 10, -100, 100, { + 'dtype': 'float32' +}); console.log( x ); var v = sapxsumpw( x.length, 5.0, x, 1 ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/include/stdlib/blas/ext/base/sapxsumpw.h b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/include/stdlib/blas/ext/base/sapxsumpw.h index 46125e9980ef..dcb9d1e328e7 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/include/stdlib/blas/ext/base/sapxsumpw.h +++ b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/include/stdlib/blas/ext/base/sapxsumpw.h @@ -19,7 +19,7 @@ #ifndef STDLIB_BLAS_EXT_BASE_SAPXSUMPW_H #define STDLIB_BLAS_EXT_BASE_SAPXSUMPW_H -#include +#include "stdlib/blas/base/shared.h" /* * If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. @@ -31,7 +31,12 @@ extern "C" { /** * Adds a scalar constant to each single-precision floating-point strided array element and computes the sum using pairwise summation. */ -float stdlib_strided_sapxsumpw( const int64_t N, const float alpha, const float *X, const int64_t stride ); +float API_SUFFIX(stdlib_strided_sapxsumpw)( const CBLAS_INT N, const float alpha, const float *X, const CBLAS_INT strideX ); + +/** +* Adds a scalar constant to each single-precision floating-point strided array element and computes the sum using pairwise summation and alternative indexing semantics. +*/ +float API_SUFFIX(stdlib_strided_sapxsumpw_ndarray)( const CBLAS_INT N, const float alpha, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ); #ifdef __cplusplus } diff --git a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/lib/index.js b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/lib/index.js index 9c01c7d56d7f..0ef59d61a28f 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/lib/index.js +++ b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/lib/index.js @@ -19,7 +19,7 @@ 'use strict'; /** -* Add a constant to each single-precision floating-point strided array element and compute the sum using pairwise summation. +* Add a scalar constant to each single-precision floating-point strided array element and compute the sum using pairwise summation. * * @module @stdlib/blas/ext/base/sapxsumpw * @@ -28,9 +28,8 @@ * var sapxsumpw = require( '@stdlib/blas/ext/base/sapxsumpw' ); * * var x = new Float32Array( [ 1.0, -2.0, 2.0 ] ); -* var N = x.length; * -* var v = sapxsumpw( N, 5.0, x, 1 ); +* var v = sapxsumpw( x.length, 5.0, x, 1 ); * // returns 16.0 * * @example diff --git a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/lib/ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/lib/ndarray.js index 2a036cc5a1f4..49493d469969 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/lib/ndarray.js +++ b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/lib/ndarray.js @@ -35,7 +35,7 @@ var BLOCKSIZE = 128; // MAIN // /** -* Adds a constant to each single-precision floating-point strided array element and computes the sum using pairwise summation. +* Adds a scalar constant to each single-precision floating-point strided array element and computes the sum using pairwise summation. * * ## Method * @@ -46,10 +46,10 @@ var BLOCKSIZE = 128; * - Higham, Nicholas J. 1993. "The Accuracy of Floating Point Summation." _SIAM Journal on Scientific Computing_ 14 (4): 783–99. doi:[10.1137/0914050](https://doi.org/10.1137/0914050). * * @param {PositiveInteger} N - number of indexed elements -* @param {number} alpha - constant +* @param {number} alpha - scalar constant * @param {Float32Array} x - input array -* @param {integer} stride - stride length -* @param {NonNegativeInteger} offset - starting index +* @param {integer} strideX - stride length +* @param {NonNegativeInteger} offsetX - starting index * @returns {number} sum * * @example @@ -60,7 +60,7 @@ var BLOCKSIZE = 128; * var v = sapxsumpw( 4, 5.0, x, 2, 1 ); * // returns 25.0 */ -function sapxsumpw( N, alpha, x, stride, offset ) { +function sapxsumpw( N, alpha, x, strideX, offsetX ) { var ix; var s0; var s1; @@ -78,42 +78,42 @@ function sapxsumpw( N, alpha, x, stride, offset ) { if ( N <= 0 ) { return 0.0; } - if ( N === 1 || stride === 0 ) { - return float64ToFloat32( alpha + x[ offset ] ); + ix = offsetX; + if ( strideX === 0 ) { + return float64ToFloat32( N * ( alpha + x[ ix ] ) ); } - ix = offset; if ( N < 8 ) { // Use simple summation... s = 0.0; for ( i = 0; i < N; i++ ) { s = float64ToFloat32( s + float64ToFloat32( alpha + x[ ix ] ) ); - ix += stride; + ix += strideX; } return s; } if ( N <= BLOCKSIZE ) { // Sum a block with 8 accumulators (by loop unrolling, we lower the effective blocksize to 16)... s0 = float64ToFloat32( alpha + x[ ix ] ); - s1 = float64ToFloat32( alpha + x[ ix+stride ] ); - s2 = float64ToFloat32( alpha + x[ ix+(2*stride) ] ); - s3 = float64ToFloat32( alpha + x[ ix+(3*stride) ] ); - s4 = float64ToFloat32( alpha + x[ ix+(4*stride) ] ); - s5 = float64ToFloat32( alpha + x[ ix+(5*stride) ] ); - s6 = float64ToFloat32( alpha + x[ ix+(6*stride) ] ); - s7 = float64ToFloat32( alpha + x[ ix+(7*stride) ] ); - ix += 8 * stride; + s1 = float64ToFloat32( alpha + x[ ix+strideX ] ); + s2 = float64ToFloat32( alpha + x[ ix+(2*strideX) ] ); + s3 = float64ToFloat32( alpha + x[ ix+(3*strideX) ] ); + s4 = float64ToFloat32( alpha + x[ ix+(4*strideX) ] ); + s5 = float64ToFloat32( alpha + x[ ix+(5*strideX) ] ); + s6 = float64ToFloat32( alpha + x[ ix+(6*strideX) ] ); + s7 = float64ToFloat32( alpha + x[ ix+(7*strideX) ] ); + ix += 8 * strideX; M = N % 8; for ( i = 8; i < N-M; i += 8 ) { s0 = float64ToFloat32( s0 + float64ToFloat32( alpha + x[ ix ] ) ); - s1 = float64ToFloat32( s1 + float64ToFloat32( alpha + x[ ix+stride ] ) ); - s2 = float64ToFloat32( s2 + float64ToFloat32( alpha + x[ ix+(2*stride) ] ) ); - s3 = float64ToFloat32( s3 + float64ToFloat32( alpha + x[ ix+(3*stride) ] ) ); - s4 = float64ToFloat32( s4 + float64ToFloat32( alpha + x[ ix+(4*stride) ] ) ); - s5 = float64ToFloat32( s5 + float64ToFloat32( alpha + x[ ix+(5*stride) ] ) ); - s6 = float64ToFloat32( s6 + float64ToFloat32( alpha + x[ ix+(6*stride) ] ) ); - s7 = float64ToFloat32( s7 + float64ToFloat32( alpha + x[ ix+(7*stride) ] ) ); - ix += 8 * stride; + s1 = float64ToFloat32( s1 + float64ToFloat32( alpha + x[ ix+strideX ] ) ); + s2 = float64ToFloat32( s2 + float64ToFloat32( alpha + x[ ix+(2*strideX) ] ) ); + s3 = float64ToFloat32( s3 + float64ToFloat32( alpha + x[ ix+(3*strideX) ] ) ); + s4 = float64ToFloat32( s4 + float64ToFloat32( alpha + x[ ix+(4*strideX) ] ) ); + s5 = float64ToFloat32( s5 + float64ToFloat32( alpha + x[ ix+(5*strideX) ] ) ); + s6 = float64ToFloat32( s6 + float64ToFloat32( alpha + x[ ix+(6*strideX) ] ) ); + s7 = float64ToFloat32( s7 + float64ToFloat32( alpha + x[ ix+(7*strideX) ] ) ); + ix += 8 * strideX; } // Pairwise sum the accumulators: s = float64ToFloat32( float64ToFloat32( float64ToFloat32(s0+s1) + float64ToFloat32(s2+s3) ) + float64ToFloat32( float64ToFloat32(s4+s5) + float64ToFloat32(s6+s7) ) ); @@ -121,14 +121,14 @@ function sapxsumpw( N, alpha, x, stride, offset ) { // Clean-up loop... for ( i; i < N; i++ ) { s = float64ToFloat32( s + float64ToFloat32( alpha + x[ ix ] ) ); - ix += stride; + ix += strideX; } return s; } // Recurse by dividing by two, but avoiding non-multiples of unroll factor... n = floor( N/2 ); n -= n % 8; - return float64ToFloat32( sapxsumpw( n, alpha, x, stride, ix ) + sapxsumpw( N-n, alpha, x, stride, ix+(n*stride) ) ); + return float64ToFloat32( sapxsumpw( n, alpha, x, strideX, ix ) + sapxsumpw( N-n, alpha, x, strideX, ix+(n*strideX) ) ); } diff --git a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/lib/ndarray.native.js b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/lib/ndarray.native.js index a84faa05e0e2..305809947ddd 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/lib/ndarray.native.js +++ b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/lib/ndarray.native.js @@ -20,20 +20,19 @@ // MODULES // -var Float32Array = require( '@stdlib/array/float32' ); -var addon = require( './sapxsumpw.native.js' ); +var addon = require( './../src/addon.node' ); // MAIN // /** -* Adds a constant to each single-precision floating-point strided array element and computes the sum using pairwise summation. +* Adds a scalar constant to each single-precision floating-point strided array element and computes the sum using pairwise summation. * * @param {PositiveInteger} N - number of indexed elements -* @param {number} alpha - constant +* @param {number} alpha - scalar constant * @param {Float32Array} x - input array -* @param {integer} stride - stride length -* @param {NonNegativeInteger} offset - starting index +* @param {integer} strideX - stride length +* @param {NonNegativeInteger} offsetX - starting index * @returns {number} sum * * @example @@ -44,13 +43,8 @@ var addon = require( './sapxsumpw.native.js' ); * var v = sapxsumpw( 4, 5.0, x, 2, 1 ); * // returns 25.0 */ -function sapxsumpw( N, alpha, x, stride, offset ) { - var view; - if ( stride < 0 ) { - offset += (N-1) * stride; - } - view = new Float32Array( x.buffer, x.byteOffset+(x.BYTES_PER_ELEMENT*offset), x.length-offset ); // eslint-disable-line max-len - return addon( N, alpha, view, stride ); +function sapxsumpw( N, alpha, x, strideX, offsetX ) { + return addon.ndarray( N, alpha, x, strideX, offsetX ); } diff --git a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/lib/sapxsumpw.js b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/lib/sapxsumpw.js index 02e9d4ccbd5a..62bdd59f3c7a 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/lib/sapxsumpw.js +++ b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/lib/sapxsumpw.js @@ -20,14 +20,14 @@ // MODULES // -var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' ); -var sum = require( './ndarray.js' ); +var stride2offset = require( '@stdlib/strided/base/stride2offset' ); +var ndarray = require( './ndarray.js' ); // MAIN // /** -* Adds a constant to each single-precision floating-point strided array element and computes the sum using pairwise summation. +* Adds a scalar constant to each single-precision floating-point strided array element and computes the sum using pairwise summation. * * ## Method * @@ -38,46 +38,21 @@ var sum = require( './ndarray.js' ); * - Higham, Nicholas J. 1993. "The Accuracy of Floating Point Summation." _SIAM Journal on Scientific Computing_ 14 (4): 783–99. doi:[10.1137/0914050](https://doi.org/10.1137/0914050). * * @param {PositiveInteger} N - number of indexed elements -* @param {number} alpha - constant +* @param {number} alpha - scalar constant * @param {Float32Array} x - input array -* @param {integer} stride - stride length +* @param {integer} strideX - stride length * @returns {number} sum * * @example * var Float32Array = require( '@stdlib/array/float32' ); * * var x = new Float32Array( [ 1.0, -2.0, 2.0 ] ); -* var N = x.length; * -* var v = sapxsumpw( N, 5.0, x, 1 ); +* var v = sapxsumpw( x.length, 5.0, x, 1 ); * // returns 16.0 */ -function sapxsumpw( N, alpha, x, stride ) { - var ix; - var s; - var i; - - if ( N <= 0 ) { - return 0.0; - } - if ( N === 1 || stride === 0 ) { - return float64ToFloat32( alpha + x[ 0 ] ); - } - if ( stride < 0 ) { - ix = (1-N) * stride; - } else { - ix = 0; - } - if ( N < 8 ) { - // Use simple summation... - s = 0.0; - for ( i = 0; i < N; i++ ) { - s = float64ToFloat32( s + float64ToFloat32( alpha + x[ ix ] ) ); - ix += stride; - } - return s; - } - return sum( N, alpha, x, stride, ix ); +function sapxsumpw( N, alpha, x, strideX ) { + return ndarray( N, alpha, x, strideX, stride2offset( N, strideX ) ); } diff --git a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/lib/sapxsumpw.native.js b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/lib/sapxsumpw.native.js index 939d4c29ccb4..549ff195a4ec 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/lib/sapxsumpw.native.js +++ b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/lib/sapxsumpw.native.js @@ -26,25 +26,24 @@ var addon = require( './../src/addon.node' ); // MAIN // /** -* Adds a constant to each single-precision floating-point strided array element and computes the sum using pairwise summation. +* Adds a scalar constant to each single-precision floating-point strided array element and computes the sum using pairwise summation. * * @param {PositiveInteger} N - number of indexed elements -* @param {number} alpha - constant +* @param {number} alpha - scalar constant * @param {Float32Array} x - input array -* @param {integer} stride - stride length +* @param {integer} strideX - stride length * @returns {number} sum * * @example * var Float32Array = require( '@stdlib/array/float32' ); * * var x = new Float32Array( [ 1.0, -2.0, 2.0 ] ); -* var N = x.length; * -* var v = sapxsumpw( N, 5.0, x, 1 ); +* var v = sapxsumpw( x.length, 5.0, x, 1 ); * // returns 16.0 */ -function sapxsumpw( N, alpha, x, stride ) { - return addon( N, alpha, x, stride ); +function sapxsumpw( N, alpha, x, strideX ) { + return addon( N, alpha, x, strideX ); } diff --git a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/manifest.json b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/manifest.json index cdc880d4dbbb..bdecb363835f 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/manifest.json +++ b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/manifest.json @@ -28,50 +28,53 @@ { "task": "build", "src": [ - "./src/sapxsumpw.c" + "./src/main.c" ], "include": [ "./include" ], - "libraries": [ - "-lm" - ], + "libraries": [], "libpath": [], "dependencies": [ - "@stdlib/napi/export", + "@stdlib/napi/export", "@stdlib/napi/argv", "@stdlib/napi/argv-float", "@stdlib/napi/argv-int64", - "@stdlib/napi/argv-strided-float32array" + "@stdlib/napi/argv-strided-float32array", + "@stdlib/napi/create-double", + "@stdlib/strided/base/stride2offset", + "@stdlib/blas/base/shared" ] }, { "task": "benchmark", "src": [ - "./src/sapxsumpw.c" + "./src/main.c" ], "include": [ "./include" ], - "libraries": [ - "-lm" - ], + "libraries": [], "libpath": [], - "dependencies": [] + "dependencies": [ + "@stdlib/strided/base/stride2offset", + "@stdlib/blas/base/shared" + ] }, { "task": "examples", "src": [ - "./src/sapxsumpw.c" + "./src/main.c" ], "include": [ "./include" ], - "libraries": [ - "-lm" - ], + "libraries": [], "libpath": [], - "dependencies": [] + "dependencies": [ + "@stdlib/strided/base/stride2offset", + "@stdlib/blas/base/shared" + ] } ] } diff --git a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/package.json b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/package.json index 1144e51039c5..07c8d4cde7f1 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/package.json +++ b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/package.json @@ -1,7 +1,7 @@ { "name": "@stdlib/blas/ext/base/sapxsumpw", "version": "0.0.0", - "description": "Add a constant to each single-precision floating-point strided array element and compute the sum using pairwise summation.", + "description": "Add a scalar constant to each single-precision floating-point strided array element and compute the sum using pairwise summation.", "license": "Apache-2.0", "author": { "name": "The Stdlib Authors", diff --git a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/src/addon.c b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/src/addon.c index 79f34333bd90..c21adf10d16d 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/src/addon.c +++ b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/src/addon.c @@ -17,11 +17,13 @@ */ #include "stdlib/blas/ext/base/sapxsumpw.h" +#include "stdlib/blas/base/shared.h" #include "stdlib/napi/export.h" #include "stdlib/napi/argv.h" #include "stdlib/napi/argv_float.h" #include "stdlib/napi/argv_int64.h" #include "stdlib/napi/argv_strided_float32array.h" +#include "stdlib/napi/create_double.h" #include /** @@ -35,14 +37,28 @@ static napi_value addon( napi_env env, napi_callback_info info ) { STDLIB_NAPI_ARGV( env, info, argv, argc, 4 ); STDLIB_NAPI_ARGV_INT64( env, N, argv, 0 ); STDLIB_NAPI_ARGV_FLOAT( env, alpha, argv, 1 ); - STDLIB_NAPI_ARGV_INT64( env, stride, argv, 3 ); - STDLIB_NAPI_ARGV_STRIDED_FLOAT32ARRAY( env, X, N, stride, argv, 2 ); - - napi_value v; - napi_status status = napi_create_double( env, (double)stdlib_strided_sapxsumpw( N, alpha, X, stride ), &v ); - assert( status == napi_ok ); + STDLIB_NAPI_ARGV_INT64( env, strideX, argv, 3 ); + STDLIB_NAPI_ARGV_STRIDED_FLOAT32ARRAY( env, X, N, strideX, argv, 2 ); + STDLIB_NAPI_CREATE_DOUBLE( env, (double)API_SUFFIX(stdlib_strided_sapxsumpw)( N, alpha, X, strideX ), v ); + return v; +} +/** +* Receives JavaScript callback invocation data. +* +* @param env environment under which the function is invoked +* @param info callback data +* @return Node-API value +*/ +static napi_value addon_method( napi_env env, napi_callback_info info ) { + STDLIB_NAPI_ARGV( env, info, argv, argc, 5 ); + STDLIB_NAPI_ARGV_INT64( env, N, argv, 0 ); + STDLIB_NAPI_ARGV_FLOAT( env, alpha, argv, 1 ); + STDLIB_NAPI_ARGV_INT64( env, strideX, argv, 3 ); + STDLIB_NAPI_ARGV_INT64( env, offsetX, argv, 4 ); + STDLIB_NAPI_ARGV_STRIDED_FLOAT32ARRAY( env, X, N, strideX, argv, 2 ); + STDLIB_NAPI_CREATE_DOUBLE( env, (double)API_SUFFIX(stdlib_strided_sapxsumpw_ndarray)( N, alpha, X, strideX, offsetX ), v ); return v; } -STDLIB_NAPI_MODULE_EXPORT_FCN( addon ) +STDLIB_NAPI_MODULE_EXPORT_FCN_WITH_METHOD( addon, "ndarray", addon_method ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/src/main.c b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/src/main.c new file mode 100644 index 000000000000..5111888632fb --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/src/main.c @@ -0,0 +1,125 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "stdlib/blas/ext/base/sapxsumpw.h" +#include "stdlib/strided/base/stride2offset.h" +#include "stdlib/blas/base/shared.h" + +/** +* Adds a scalar constant to each single-precision floating-point strided array element and computes the sum using pairwise summation. +* +* @param N number of indexed elements +* @param alpha scalar constant +* @param X input array +* @param strideX stride length +* @return output value +*/ +float API_SUFFIX(stdlib_strided_sapxsumpw)( const CBLAS_INT N, const float alpha, const float *X, const CBLAS_INT strideX ) { + CBLAS_INT ox = stdlib_strided_stride2offset( N, strideX ); + return API_SUFFIX(stdlib_strided_sapxsumpw_ndarray)( N, alpha, X, strideX, ox ); +} + +/** +* Adds a scalar constant to each single-precision floating-point strided array element and computes the sum using pairwise summation and alternative indexing semantics. +* +* ## Method +* +* - This implementation uses pairwise summation, which accrues rounding error `O(log2 N)` instead of `O(N)`. The recursion depth is also `O(log2 N)`. +* +* ## References +* +* - Higham, Nicholas J. 1993. "The Accuracy of Floating Point Summation." _SIAM Journal on Scientific Computing_ 14 (4): 783–99. doi:[10.1137/0914050](https://doi.org/10.1137/0914050). +* +* @param N number of indexed elements +* @param alpha scalar constant +* @param X input array +* @param strideX stride length +* @param offsetX starting index +* @return output value +*/ +float API_SUFFIX(stdlib_strided_sapxsumpw_ndarray)( const CBLAS_INT N, const float alpha, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ) { + CBLAS_INT ix; + CBLAS_INT M; + CBLAS_INT n; + CBLAS_INT i; + float sum; + float s0; + float s1; + float s2; + float s3; + float s4; + float s5; + float s6; + float s7; + + if ( N <= 0 ) { + return 0.0f; + } + ix = offsetX; + if ( strideX == 0 ) { + return N * ( alpha + X[ ix ] ); + } + if ( N < 8 ) { + // Use simple summation... + sum = 0.0f; + for ( i = 0; i < N; i++ ) { + sum += alpha + X[ ix ]; + ix += strideX; + } + return sum; + } + // Blocksize for pairwise summation: 128 (NOTE: decreasing the blocksize decreases rounding error as more pairs are summed, but also decreases performance. Because the inner loop is unrolled eight times, the blocksize is effectively `16`.) + if ( N <= 128 ) { + // Sum a block with 8 accumulators (by loop unrolling, we lower the effective blocksize to 16)... + s0 = alpha + X[ ix ]; + s1 = alpha + X[ ix+strideX ]; + s2 = alpha + X[ ix+(2*strideX) ]; + s3 = alpha + X[ ix+(3*strideX) ]; + s4 = alpha + X[ ix+(4*strideX) ]; + s5 = alpha + X[ ix+(5*strideX) ]; + s6 = alpha + X[ ix+(6*strideX) ]; + s7 = alpha + X[ ix+(7*strideX) ]; + ix += 8 * strideX; + + M = N % 8; + for ( i = 8; i < N-M; i += 8 ) { + s0 += alpha + X[ ix ]; + s1 += alpha + X[ ix+strideX ]; + s2 += alpha + X[ ix+(2*strideX) ]; + s3 += alpha + X[ ix+(3*strideX) ]; + s4 += alpha + X[ ix+(4*strideX) ]; + s5 += alpha + X[ ix+(5*strideX) ]; + s6 += alpha + X[ ix+(6*strideX) ]; + s7 += alpha + X[ ix+(7*strideX) ]; + ix += 8 * strideX; + } + // Pairwise sum the accumulators: + sum = ( (s0+s1) + (s2+s3) ) + ( (s4+s5) + (s6+s7) ); + + // Clean-up loop... + for (; i < N; i++ ) { + sum += alpha + X[ ix ]; + ix += strideX; + } + return sum; + } + // Recurse by dividing by two, but avoiding non-multiples of unroll factor... + n = N / 2; + n -= n % 8; + return API_SUFFIX(stdlib_strided_sapxsumpw_ndarray)( n, alpha, X, strideX, ix ) + API_SUFFIX(stdlib_strided_sapxsumpw_ndarray)( N-n, alpha, X, strideX, ix+(n*strideX) ); +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/src/sapxsumpw.c b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/src/sapxsumpw.c deleted file mode 100644 index 8871478a4eb6..000000000000 --- a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/src/sapxsumpw.c +++ /dev/null @@ -1,122 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2020 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -#include "stdlib/blas/ext/base/sapxsumpw.h" -#include - -/** -* Adds a constant to each single-precision floating-point strided array element and computes the sum using pairwise summation. -* -* ## Method -* -* - This implementation uses pairwise summation, which accrues rounding error `O(log2 N)` instead of `O(N)`. The recursion depth is also `O(log2 N)`. -* -* ## References -* -* - Higham, Nicholas J. 1993. "The Accuracy of Floating Point Summation." _SIAM Journal on Scientific Computing_ 14 (4): 783–99. doi:[10.1137/0914050](https://doi.org/10.1137/0914050). -* -* @param N number of indexed elements -* @param alpha constant -* @param X input array -* @param stride stride length -* @return output value -*/ -float stdlib_strided_sapxsumpw( const int64_t N, const float alpha, const float *X, const int64_t stride ) { - float *xp1; - float *xp2; - int64_t ix; - int64_t M; - int64_t n; - int64_t i; - float sum; - float s0; - float s1; - float s2; - float s3; - float s4; - float s5; - float s6; - float s7; - - if ( N <= 0 ) { - return 0.0f; - } - if ( N == 1 || stride == 0 ) { - return alpha + X[ 0 ]; - } - if ( stride < 0 ) { - ix = (1-N) * stride; - } else { - ix = 0; - } - if ( N < 8 ) { - // Use simple summation... - sum = 0.0f; - for ( i = 0; i < N; i++ ) { - sum += alpha + X[ ix ]; - ix += stride; - } - return sum; - } - // Blocksize for pairwise summation: 128 (NOTE: decreasing the blocksize decreases rounding error as more pairs are summed, but also decreases performance. Because the inner loop is unrolled eight times, the blocksize is effectively `16`.) - if ( N <= 128 ) { - // Sum a block with 8 accumulators (by loop unrolling, we lower the effective blocksize to 16)... - s0 = alpha + X[ ix ]; - s1 = alpha + X[ ix+stride ]; - s2 = alpha + X[ ix+(2*stride) ]; - s3 = alpha + X[ ix+(3*stride) ]; - s4 = alpha + X[ ix+(4*stride) ]; - s5 = alpha + X[ ix+(5*stride) ]; - s6 = alpha + X[ ix+(6*stride) ]; - s7 = alpha + X[ ix+(7*stride) ]; - ix += 8 * stride; - - M = N % 8; - for ( i = 8; i < N-M; i += 8 ) { - s0 += alpha + X[ ix ]; - s1 += alpha + X[ ix+stride ]; - s2 += alpha + X[ ix+(2*stride) ]; - s3 += alpha + X[ ix+(3*stride) ]; - s4 += alpha + X[ ix+(4*stride) ]; - s5 += alpha + X[ ix+(5*stride) ]; - s6 += alpha + X[ ix+(6*stride) ]; - s7 += alpha + X[ ix+(7*stride) ]; - ix += 8 * stride; - } - // Pairwise sum the accumulators: - sum = ((s0+s1) + (s2+s3)) + ((s4+s5) + (s6+s7)); - - // Clean-up loop... - for (; i < N; i++ ) { - sum += alpha + X[ ix ]; - ix += stride; - } - return sum; - } - // Recurse by dividing by two, but avoiding non-multiples of unroll factor... - n = N / 2; - n -= n % 8; - if ( stride < 0 ) { - xp1 = (float *)X + ( (n-N)*stride ); - xp2 = (float *)X; - } else { - xp1 = (float *)X; - xp2 = (float *)X + ( n*stride ); - } - return stdlib_strided_sapxsumpw( n, alpha, xp1, stride ) + stdlib_strided_sapxsumpw( N-n, alpha, xp2, stride ); -} diff --git a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/test/test.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/test/test.ndarray.js index c16278d1610d..305f05c82690 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/test/test.ndarray.js +++ b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/test/test.ndarray.js @@ -152,14 +152,14 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) t.end(); }); -tape( 'if provided a `stride` parameter equal to `0`, the function returns the first indexed element plus a constant', function test( t ) { +tape( 'if provided a `stride` parameter equal to `0`, the function returns the first element plus a constant repeated N times', function test( t ) { var x; var v; x = new Float32Array( [ 1.0, -2.0, -4.0, 5.0, 3.0 ] ); v = sapxsumpw( x.length, 5.0, x, 0, 0 ); - t.strictEqual( v, 6.0, 'returns expected value' ); + t.strictEqual( v, x.length * (x[0]+5.0), 'returns expected value' ); t.end(); }); diff --git a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/test/test.ndarray.native.js b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/test/test.ndarray.native.js index 22fa27a4bbd8..194ecf674277 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/test/test.ndarray.native.js +++ b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/test/test.ndarray.native.js @@ -161,14 +161,14 @@ tape( 'the function supports a negative `stride` parameter', opts, function test t.end(); }); -tape( 'if provided a `stride` parameter equal to `0`, the function returns the first indexed element plus a constant', opts, function test( t ) { +tape( 'if provided a `stride` parameter equal to `0`, the function returns the first element plus a constant repeated N times', opts, function test( t ) { var x; var v; x = new Float32Array( [ 1.0, -2.0, -4.0, 5.0, 3.0 ] ); v = sapxsumpw( x.length, 5.0, x, 0, 0 ); - t.strictEqual( v, 6.0, 'returns expected value' ); + t.strictEqual( v, x.length * (x[0]+5.0), 'returns expected value' ); t.end(); }); diff --git a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/test/test.sapxsumpw.js b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/test/test.sapxsumpw.js index 4b9c1dedbbec..e4aa827c9c1b 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/test/test.sapxsumpw.js +++ b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/test/test.sapxsumpw.js @@ -152,14 +152,14 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) t.end(); }); -tape( 'if provided a `stride` parameter equal to `0`, the function returns the first element plus a constant', function test( t ) { +tape( 'if provided a `stride` parameter equal to `0`, the function returns the first element plus a constant repeated N times', function test( t ) { var x; var v; x = new Float32Array( [ 1.0, -2.0, -4.0, 5.0, 3.0 ] ); v = sapxsumpw( x.length, 5.0, x, 0 ); - t.strictEqual( v, 6.0, 'returns expected value' ); + t.strictEqual( v, x.length * (x[0]+5.0), 'returns expected value' ); t.end(); }); diff --git a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/test/test.sapxsumpw.native.js b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/test/test.sapxsumpw.native.js index ea0af9baf524..3cdbccd275ab 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/test/test.sapxsumpw.native.js +++ b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/test/test.sapxsumpw.native.js @@ -270,14 +270,14 @@ tape( 'the function supports a negative `stride` parameter', opts, function test t.end(); }); -tape( 'if provided a `stride` parameter equal to `0`, the function returns the first element plus a constant', opts, function test( t ) { +tape( 'if provided a `stride` parameter equal to `0`, the function returns the first element plus a constant repeated N times', opts, function test( t ) { var x; var v; x = new Float32Array( [ 1.0, -2.0, -4.0, 5.0, 3.0 ] ); v = sapxsumpw( x.length, 5.0, x, 0 ); - t.strictEqual( v, 6.0, 'returns expected value' ); + t.strictEqual( v, x.length * (x[0]+5.0), 'returns expected value' ); t.end(); }); From 950719be3f622c6f2432c815cea7cdf2a22b622e Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Tue, 14 Jan 2025 16:00:19 +0500 Subject: [PATCH 2/3] fix: apply review suggestions Signed-off-by: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> --- lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/lib/ndarray.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/lib/ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/lib/ndarray.js index 49493d469969..f3f58f5ad6f1 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/lib/ndarray.js +++ b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/lib/ndarray.js @@ -80,7 +80,7 @@ function sapxsumpw( N, alpha, x, strideX, offsetX ) { } ix = offsetX; if ( strideX === 0 ) { - return float64ToFloat32( N * ( alpha + x[ ix ] ) ); + return float64ToFloat32( N * float64ToFloat32( alpha + x[ ix ] ) ); } if ( N < 8 ) { // Use simple summation... From e7776b5cb4ca45712028dfa1492b6ebd9c29a4f2 Mon Sep 17 00:00:00 2001 From: Athan Date: Fri, 17 Jan 2025 16:13:07 -0800 Subject: [PATCH 3/3] Apply suggestions from code review Signed-off-by: Athan --- lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/README.md b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/README.md index bc0884a453e5..d442e0562055 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/README.md +++ b/lib/node_modules/@stdlib/blas/ext/base/sapxsumpw/README.md @@ -180,7 +180,7 @@ Adds a scalar constant to each single-precision floating-point strided array ele const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f }; float v = stdlib_strided_sapxsumpw( 4, 5.0f, x, 1 ); -// returns 30.0 +// returns 30.0f ``` The function accepts the following arguments: @@ -202,7 +202,7 @@ Adds a scalar constant to each single-precision floating-point strided array ele const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f }; float v = stdlib_strided_sapxsumpw_ndarray( 4, 5.0f, x, 1, 0 ); -// returns 30.0 +// returns 30.0f ``` The function accepts the following arguments: