From b316d4579695c32082eaf7e23949ea7b70105d94 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sun, 19 Jan 2025 09:02:45 +0000 Subject: [PATCH] Auto-generated commit --- .editorconfig | 1 - CHANGELOG.md | 28 ++++- CONTRIBUTORS | 5 +- NOTICE | 2 +- README.md | 146 +++++++++++++++++++++++--- benchmark/benchmark.js | 9 +- benchmark/benchmark.native.js | 9 +- benchmark/benchmark.ndarray.js | 9 +- benchmark/benchmark.ndarray.native.js | 9 +- benchmark/c/benchmark.length.c | 50 ++++++++- dist/index.js | 14 +-- dist/index.js.map | 8 +- docs/repl.txt | 32 +++--- docs/types/index.d.ts | 24 ++--- examples/c/example.c | 10 +- examples/index.js | 8 +- include/stdlib/blas/ext/base/sapx.h | 11 +- lib/index.js | 2 +- lib/ndarray.js | 21 ++-- lib/ndarray.native.js | 22 ++-- lib/sapx.js | 53 ++-------- lib/sapx.native.js | 10 +- manifest.json | 32 +++--- package.json | 10 +- src/addon.c | 27 ++++- src/main.c | 84 +++++++++++++++ src/sapx.c | 70 ------------ 27 files changed, 448 insertions(+), 258 deletions(-) create mode 100644 src/main.c delete mode 100644 src/sapx.c diff --git a/.editorconfig b/.editorconfig index 0779e8a..dab5d2a 100644 --- a/.editorconfig +++ b/.editorconfig @@ -86,7 +86,6 @@ indent_style = tab [*.{f,f.txt}] indent_style = space indent_size = 2 -insert_final_newline = false # Set properties for shell files: [*.{sh,sh.txt}] diff --git a/CHANGELOG.md b/CHANGELOG.md index 4cb601e..f92ddee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,29 @@
-## Unreleased (2024-12-26) +## Unreleased (2025-01-19) + +
+ +### Features + +- [`2ea4452`](https://github.com/stdlib-js/stdlib/commit/2ea4452fa0f63499be526f392fa7fdd647d1a9b5) - add C `ndarray` API and refactor `blas/ext/base/sapx` [(#4696)](https://github.com/stdlib-js/stdlib/pull/4696) + +
+ + + +
+ +### BREAKING CHANGES + +- [`2ea4452`](https://github.com/stdlib-js/stdlib/commit/2ea4452fa0f63499be526f392fa7fdd647d1a9b5): rename `c_sapx` to `stdlib_strided_sapx` + + - To migrate, users should replace all instances of `c_sapx` with `stdlib_strided_sapx`. + +
+ +
@@ -12,6 +34,7 @@
+- [`2ea4452`](https://github.com/stdlib-js/stdlib/commit/2ea4452fa0f63499be526f392fa7fdd647d1a9b5) - **feat:** add C `ndarray` API and refactor `blas/ext/base/sapx` [(#4696)](https://github.com/stdlib-js/stdlib/pull/4696) _(by Muhammad Haris, Athan Reines)_ - [`a6f3921`](https://github.com/stdlib-js/stdlib/commit/a6f3921560705503dcb5ee2575dd2f11417f58fb) - **docs:** update related packages sections [(#4242)](https://github.com/stdlib-js/stdlib/pull/4242) _(by stdlib-bot, Athan Reines)_ - [`62364f6`](https://github.com/stdlib-js/stdlib/commit/62364f62ea823a3b52c2ad25660ecd80c71f8f36) - **style:** fix C comment alignment _(by Philipp Burckhardt)_ - [`272ae7a`](https://github.com/stdlib-js/stdlib/commit/272ae7ac5c576c68cfab1b6e304c86407faa20cd) - **docs:** remove comment _(by Athan Reines)_ @@ -27,9 +50,10 @@ ### Contributors -A total of 2 people contributed to this release. Thank you to the following contributors: +A total of 3 people contributed to this release. Thank you to the following contributors: - Athan Reines +- Muhammad Haris - Philipp Burckhardt
diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 173c07b..aedb148 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -27,6 +27,8 @@ Daniel Killenberger Daniel Yu <40680511+Daniel777y@users.noreply.github.com> Debashis Maharana Desh Deepak Kant <118960904+DeshDeepakKant@users.noreply.github.com> +Dev Goel <135586571+corsairier@users.noreply.github.com> +Dhruv Arvind Singh <154677013+DhruvArvindSingh@users.noreply.github.com> Divyansh Seth <59174836+sethdivyansh@users.noreply.github.com> Dominic Lim <46486515+domlimm@users.noreply.github.com> Dominik Moritz @@ -49,6 +51,7 @@ Joey Reed Jordan Gallivan <115050475+Jordan-Gallivan@users.noreply.github.com> Joris Labie Justin Dennison +Karan Anand <119553199+anandkaranubc@users.noreply.github.com> Karthik Prakash <116057817+skoriop@users.noreply.github.com> Kohantika Nath <145763549+kohantikanath@users.noreply.github.com> Krishnendu Das <86651039+itskdhere@users.noreply.github.com> @@ -117,7 +120,7 @@ UtkershBasnet <119008923+UtkershBasnet@users.noreply.github.com> Vaibhav Patel <98279986+noobCoderVP@users.noreply.github.com> Varad Gupta Vinit Pandit <106718914+MeastroZI@users.noreply.github.com> -Vivek maurya <155618190+vivekmaurya001@users.noreply.github.com> +Vivek Maurya Xiaochuan Ye Yaswanth Kosuru <116426380+yaswanthkosuru@users.noreply.github.com> Yernar Yergaziyev diff --git a/NOTICE b/NOTICE index e6e7482..cbd3a29 100644 --- a/NOTICE +++ b/NOTICE @@ -1 +1 @@ -Copyright (c) 2016-2024 The Stdlib Authors. +Copyright (c) 2016-2025 The Stdlib Authors. diff --git a/README.md b/README.md index 377d222..b932f8c 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ limitations under the License. [![NPM version][npm-image]][npm-url] [![Build Status][test-image]][test-url] [![Coverage Status][coverage-image]][coverage-url] -> Add a constant to each element in a single-precision floating-point strided array. +> Add a scalar constant to each element in a single-precision floating-point strided array.
@@ -63,9 +63,9 @@ To view installation and usage instructions specific to each branch build, be su var sapx = require( '@stdlib/blas-ext-base-sapx' ); ``` -#### sapx( N, alpha, x, stride ) +#### sapx( N, alpha, x, strideX ) -Adds a constant `alpha` to each element in a single-precision floating-point strided array `x`. +Adds a scalar constant to each element in a single-precision floating-point strided array. ```javascript var Float32Array = require( '@stdlib/array-float32' ); @@ -81,9 +81,9 @@ The function has the following parameters: - **N**: number of indexed elements. - **alpha**: scalar constant. - **x**: input [`Float32Array`][@stdlib/array/float32]. -- **stride**: index increment. +- **strideX**: stride length. -The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to add a constant to every other element +The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to add a constant to every other element: ```javascript var Float32Array = require( '@stdlib/array-float32' ); @@ -110,9 +110,9 @@ sapx( 3, 5.0, x1, 2 ); // x0 => [ 1.0, 3.0, 3.0, 1.0, 5.0, -1.0 ] ``` -#### sapx.ndarray( N, alpha, x, stride, offset ) +#### sapx.ndarray( N, alpha, x, strideX, offsetX ) -Adds a constant `alpha` to each element in a single-precision floating-point strided array `x` using alternative indexing semantics. +Adds a scalar constant to each element in a single-precision floating-point strided array using alternative indexing semantics. ```javascript var Float32Array = require( '@stdlib/array-float32' ); @@ -125,9 +125,9 @@ sapx.ndarray( x.length, 5.0, x, 1, 0 ); The function has the following additional parameters: -- **offset**: starting index. +- **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 only the last three elements of the strided array +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 only the last three elements of the strided array: ```javascript var Float32Array = require( '@stdlib/array-float32' ); @@ -159,11 +159,12 @@ sapx.ndarray( 3, 5.0, x, 1, x.length-3 ); ```javascript -var uniform = require( '@stdlib/random-base-uniform' ).factory; -var filledarrayBy = require( '@stdlib/array-filled-by' ); +var discreteUniform = require( '@stdlib/random-array-discrete-uniform' ); var sapx = require( '@stdlib/blas-ext-base-sapx' ); -var x = filledarrayBy( 10, 'float32', uniform( -100.0, 100.0 ) ); +var x = discreteUniform( 10, -100, 100, { + 'dtype': 'float32' +}); console.log( x ); sapx( x.length, 5.0, x, 1 ); @@ -174,6 +175,125 @@ console.log( x ); + + +* * * + +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### Usage + +```c +#include "stdlib/blas/ext/base/sapx.h" +``` + +#### stdlib_strided_sapx( N, alpha, \*X, strideX ) + +Adds a scalar constant to each element in a single-precision floating-point strided array. + +```c +float x[] = { 1.0f, 2.0f, 3.0f, 4.0f }; + +stdlib_strided_sapx( 4, 5.0f, x, 1 ); +``` + +The function accepts the following arguments: + +- **N**: `[in] CBLAS_INT` number of indexed elements. +- **alpha**: `[in] float` scalar constant. +- **X**: `[inout] float*` input array. +- **strideX**: `[in] CBLAS_INT` stride length. + +```c +void stdlib_strided_sapx( const CBLAS_INT N, const float alpha, float *X, const CBLAS_INT strideX ); +``` + +#### stdlib_strided_sapx_ndarray( N, alpha, \*X, strideX, offsetX ) + +Adds a scalar constant to each element in a single-precision floating-point strided array using alternative indexing semantics. + +```c +float x[] = { 1.0f, 2.0f, 3.0f, 4.0f }; + +stdlib_strided_sapx_ndarray( 4, 5.0f, x, 1, 0 ); +``` + +The function accepts the following arguments: + +- **N**: `[in] CBLAS_INT` number of indexed elements. +- **alpha**: `[in] float` scalar constant. +- **X**: `[inout] float*` input array. +- **strideX**: `[in] CBLAS_INT` stride length. +- **offsetX**: `[in] CBLAS_INT` starting index. + +```c +void stdlib_strided_sapx_ndarray( const CBLAS_INT N, const float alpha, float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ); +``` + +
+ + + + + +
+ +
+ + + + + +
+ +### Examples + +```c +#include "stdlib/blas/ext/base/sapx.h" +#include + +int main( void ) { + // Create a strided array: + 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; + + // Add a constant to each element: + stdlib_strided_sapx( N, 5.0f, x, strideX ); + + // Print the result: + for ( int i = 0; i < 8; i++ ) { + printf( "x[ %i ] = %f\n", i, x[ i ] ); + } +} +``` + +
+ + + +
+ + + diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js index eecfeb0..ca0a785 100644 --- a/benchmark/benchmark.js +++ b/benchmark/benchmark.js @@ -21,8 +21,7 @@ // MODULES // var bench = require( '@stdlib/bench-harness' ); -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; @@ -31,7 +30,9 @@ var sapx = require( './../lib/sapx.js' ); // VARIABLES // -var rand = uniform( -100.0, 100.0 ); +var options = { + 'dtype': 'float32' +}; // FUNCTIONS // @@ -44,7 +45,7 @@ var rand = uniform( -100.0, 100.0 ); * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x = filledarrayBy( len, 'float32', rand ); + var x = uniform( len, -100, 100, options ); return benchmark; function benchmark( b ) { diff --git a/benchmark/benchmark.native.js b/benchmark/benchmark.native.js index 4f4bd7b..8ac64a5 100644 --- a/benchmark/benchmark.native.js +++ b/benchmark/benchmark.native.js @@ -22,8 +22,7 @@ var resolve = require( 'path' ).resolve; var bench = require( '@stdlib/bench-harness' ); -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,7 +35,9 @@ var sapx = tryRequire( resolve( __dirname, './../lib/sapx.native.js' ) ); var opts = { 'skip': ( sapx instanceof Error ) }; -var rand = uniform( -100.0, 100.0 ); +var options = { + 'dtype': 'float32' +}; // FUNCTIONS // @@ -49,7 +50,7 @@ var rand = uniform( -100.0, 100.0 ); * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x = filledarrayBy( len, 'float32', rand ); + var x = uniform( len, -100, 100, options ); return benchmark; function benchmark( b ) { diff --git a/benchmark/benchmark.ndarray.js b/benchmark/benchmark.ndarray.js index 9aebcd4..ff3cd75 100644 --- a/benchmark/benchmark.ndarray.js +++ b/benchmark/benchmark.ndarray.js @@ -21,8 +21,7 @@ // MODULES // var bench = require( '@stdlib/bench-harness' ); -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; @@ -31,7 +30,9 @@ var sapx = require( './../lib/ndarray.js' ); // VARIABLES // -var rand = uniform( -100.0, 100.0 ); +var options = { + 'dtype': 'float32' +}; // FUNCTIONS // @@ -44,7 +45,7 @@ var rand = uniform( -100.0, 100.0 ); * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x = filledarrayBy( len, 'float32', rand ); + var x = uniform( len, -100, 100, options ); return benchmark; function benchmark( b ) { diff --git a/benchmark/benchmark.ndarray.native.js b/benchmark/benchmark.ndarray.native.js index f264f3b..cff885e 100644 --- a/benchmark/benchmark.ndarray.native.js +++ b/benchmark/benchmark.ndarray.native.js @@ -22,8 +22,7 @@ var resolve = require( 'path' ).resolve; var bench = require( '@stdlib/bench-harness' ); -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,7 +35,9 @@ var sapx = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' ) ); var opts = { 'skip': ( sapx instanceof Error ) }; -var rand = uniform( -100.0, 100.0 ); +var options = { + 'dtype': 'float32' +}; // FUNCTIONS // @@ -49,7 +50,7 @@ var rand = uniform( -100.0, 100.0 ); * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x = filledarrayBy( len, 'float32', rand ); + var x = uniform( len, -100, 100, options ); return benchmark; function benchmark( b ) { diff --git a/benchmark/c/benchmark.length.c b/benchmark/c/benchmark.length.c index 0b4366b..ae841c2 100644 --- a/benchmark/c/benchmark.length.c +++ b/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 ]; double t; @@ -105,7 +105,40 @@ static double benchmark( int iterations, int len ) { } t = tic(); for ( i = 0; i < iterations; i++ ) { - c_sapx( len, 5.0f, x, 1 ); + // cppcheck-suppress uninitvar + stdlib_strided_sapx( len, 5.0f, x, 1 ); + if ( x[ 0 ] != x[ 0 ] ) { + printf( "should not return NaN\n" ); + break; + } + } + elapsed = tic() - t; + if ( x[ 0 ] != x[ 0 ] ) { + printf( "should not return NaN\n" ); + } + 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 ]; + double t; + int i; + + for ( i = 0; i < len; i++ ) { + x[ i ] = ( rand_float()*200.0f ) - 100.0f; + } + t = tic(); + for ( i = 0; i < iterations; i++ ) { + // cppcheck-suppress uninitvar + stdlib_strided_sapx_ndarray( len, 5.0f, x, 1, 0 ); if ( x[ 0 ] != x[ 0 ] ) { printf( "should not return NaN\n" ); break; @@ -140,7 +173,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/dist/index.js b/dist/index.js index fbba652..76715a9 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,9 +1,9 @@ -"use strict";var o=function(u,i){return function(){return i||u((i={exports:{}}).exports,i),i.exports}};var y=o(function(D,m){ -var q=5;function O(u,i,e,f){var v,r,t;if(u<=0||i===0)return e;if(f===1){if(t=u%q,t>0)for(r=0;r0)for(n=0;n0)for(u=0;u [ 3.0, 6.0, 8.0, 0.0, 9.0, 5.0, 4.0, 2.0 ]\n*/\nfunction sapx( N, alpha, x, stride ) {\n\tvar ix;\n\tvar i;\n\tvar m;\n\n\tif ( N <= 0 || alpha === 0.0 ) {\n\t\treturn x;\n\t}\n\t// Use loop unrolling if the stride is equal to `1`...\n\tif ( stride === 1 ) {\n\t\tm = N % M;\n\n\t\t// If we have a remainder, run a clean-up loop...\n\t\tif ( m > 0 ) {\n\t\t\tfor ( i = 0; i < m; i++ ) {\n\t\t\t\tx[ i ] += alpha;\n\t\t\t}\n\t\t}\n\t\tif ( N < M ) {\n\t\t\treturn x;\n\t\t}\n\t\tfor ( i = m; i < N; i += M ) {\n\t\t\tx[ i ] += alpha;\n\t\t\tx[ i+1 ] += alpha;\n\t\t\tx[ i+2 ] += alpha;\n\t\t\tx[ i+3 ] += alpha;\n\t\t\tx[ i+4 ] += alpha;\n\t\t}\n\t\treturn x;\n\t}\n\tif ( stride < 0 ) {\n\t\tix = (1-N) * stride;\n\t} else {\n\t\tix = 0;\n\t}\n\tfor ( i = 0; i < N; i++ ) {\n\t\tx[ ix ] += alpha;\n\t\tix += stride;\n\t}\n\treturn x;\n}\n\n\n// EXPORTS //\n\nmodule.exports = sapx;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2020 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// VARIABLES //\n\nvar M = 5;\n\n\n// MAIN //\n\n/**\n* Adds a constant to each element in a single-precision floating-point strided array.\n*\n* @param {PositiveInteger} N - number of indexed elements\n* @param {number} alpha - scalar\n* @param {Float32Array} x - input array\n* @param {integer} stride - index increment\n* @param {NonNegativeInteger} offset - starting index\n* @returns {Float32Array} input array\n*\n* @example\n* var Float32Array = require( '@stdlib/array-float32' );\n*\n* var x = new Float32Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] );\n* var alpha = 5.0;\n*\n* sapx( 3, alpha, x, 1, x.length-3 );\n* // x => [ 1.0, -2.0, 3.0, 1.0, 10.0, -1.0 ]\n*/\nfunction sapx( N, alpha, x, stride, offset ) {\n\tvar ix;\n\tvar m;\n\tvar i;\n\n\tif ( N <= 0 || alpha === 0.0 ) {\n\t\treturn x;\n\t}\n\tix = offset;\n\n\t// Use loop unrolling if the stride is equal to `1`...\n\tif ( stride === 1 ) {\n\t\tm = N % M;\n\n\t\t// If we have a remainder, run a clean-up loop...\n\t\tif ( m > 0 ) {\n\t\t\tfor ( i = 0; i < m; i++ ) {\n\t\t\t\tx[ ix ] += alpha;\n\t\t\t\tix += stride;\n\t\t\t}\n\t\t}\n\t\tif ( N < M ) {\n\t\t\treturn x;\n\t\t}\n\t\tfor ( i = m; i < N; i += M ) {\n\t\t\tx[ ix ] += alpha;\n\t\t\tx[ ix+1 ] += alpha;\n\t\t\tx[ ix+2 ] += alpha;\n\t\t\tx[ ix+3 ] += alpha;\n\t\t\tx[ ix+4 ] += alpha;\n\t\t\tix += M;\n\t\t}\n\t\treturn x;\n\t}\n\tfor ( i = 0; i < N; i++ ) {\n\t\tx[ ix ] += alpha;\n\t\tix += stride;\n\t}\n\treturn x;\n}\n\n\n// EXPORTS //\n\nmodule.exports = sapx;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2020 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );\nvar sapx = require( './sapx.js' );\nvar ndarray = require( './ndarray.js' );\n\n\n// MAIN //\n\nsetReadOnly( sapx, 'ndarray', ndarray );\n\n\n// EXPORTS //\n\nmodule.exports = sapx;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2020 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Add a constant to each element in a single-precision floating-point strided array.\n*\n* @module @stdlib/blas-ext-base-sapx\n*\n* @example\n* var Float32Array = require( '@stdlib/array-float32' );\n* var sapx = require( '@stdlib/blas-ext-base-sapx' );\n*\n* var x = new Float32Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] );\n*\n* sapx( x.length, 5.0, x, 1 );\n* // x => [ 3.0, 6.0, 8.0, 0.0, 9.0, 5.0, 4.0, 2.0 ]\n*\n* @example\n* var Float32Array = require( '@stdlib/array-float32' );\n* var sapx = require( '@stdlib/blas-ext-base-sapx' );\n*\n* var x = new Float32Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] );\n*\n* sapx.ndarray( x.length, 5.0, x, 1, 0 );\n* // x => [ 3.0, 6.0, 8.0, 0.0, 9.0, 5.0, 4.0, 2.0 ]\n*/\n\n// MODULES //\n\nvar join = require( 'path' ).join;\nvar tryRequire = require( '@stdlib/utils-try-require' );\nvar isError = require( '@stdlib/assert-is-error' );\nvar main = require( './main.js' );\n\n\n// MAIN //\n\nvar sapx;\nvar tmp = tryRequire( join( __dirname, './native.js' ) );\nif ( isError( tmp ) ) {\n\tsapx = main;\n} else {\n\tsapx = tmp;\n}\n\n\n// EXPORTS //\n\nmodule.exports = sapx;\n\n// exports: { \"ndarray\": \"sapx.ndarray\" }\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAI,EAsBR,SAASC,EAAMC,EAAGC,EAAOC,EAAGC,EAAS,CACpC,IAAIC,EACAC,EACAC,EAEJ,GAAKN,GAAK,GAAKC,IAAU,EACxB,OAAOC,EAGR,GAAKC,IAAW,EAAI,CAInB,GAHAG,EAAIN,EAAIF,EAGHQ,EAAI,EACR,IAAMD,EAAI,EAAGA,EAAIC,EAAGD,IACnBH,EAAGG,CAAE,GAAKJ,EAGZ,GAAKD,EAAIF,EACR,OAAOI,EAER,IAAMG,EAAIC,EAAGD,EAAIL,EAAGK,GAAKP,EACxBI,EAAGG,CAAE,GAAKJ,EACVC,EAAGG,EAAE,CAAE,GAAKJ,EACZC,EAAGG,EAAE,CAAE,GAAKJ,EACZC,EAAGG,EAAE,CAAE,GAAKJ,EACZC,EAAGG,EAAE,CAAE,GAAKJ,EAEb,OAAOC,CACR,CAMA,IALKC,EAAS,EACbC,GAAM,EAAEJ,GAAKG,EAEbC,EAAK,EAEAC,EAAI,EAAGA,EAAIL,EAAGK,IACnBH,EAAGE,CAAG,GAAKH,EACXG,GAAMD,EAEP,OAAOD,CACR,CAKAL,EAAO,QAAUE,ICzFjB,IAAAQ,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAI,EAwBR,SAASC,EAAMC,EAAGC,EAAOC,EAAGC,EAAQC,EAAS,CAC5C,IAAIC,EACAC,EACAC,EAEJ,GAAKP,GAAK,GAAKC,IAAU,EACxB,OAAOC,EAKR,GAHAG,EAAKD,EAGAD,IAAW,EAAI,CAInB,GAHAG,EAAIN,EAAIF,EAGHQ,EAAI,EACR,IAAMC,EAAI,EAAGA,EAAID,EAAGC,IACnBL,EAAGG,CAAG,GAAKJ,EACXI,GAAMF,EAGR,GAAKH,EAAIF,EACR,OAAOI,EAER,IAAMK,EAAID,EAAGC,EAAIP,EAAGO,GAAKT,EACxBI,EAAGG,CAAG,GAAKJ,EACXC,EAAGG,EAAG,CAAE,GAAKJ,EACbC,EAAGG,EAAG,CAAE,GAAKJ,EACbC,EAAGG,EAAG,CAAE,GAAKJ,EACbC,EAAGG,EAAG,CAAE,GAAKJ,EACbI,GAAMP,EAEP,OAAOI,CACR,CACA,IAAMK,EAAI,EAAGA,EAAIP,EAAGO,IACnBL,EAAGG,CAAG,GAAKJ,EACXI,GAAMF,EAEP,OAAOD,CACR,CAKAL,EAAO,QAAUE,IC1FjB,IAAAS,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAc,QAAS,uDAAwD,EAC/EC,EAAO,IACPC,EAAU,IAKdF,EAAaC,EAAM,UAAWC,CAAQ,EAKtCH,EAAO,QAAUE,ICYjB,IAAIE,EAAO,QAAS,MAAO,EAAE,KACzBC,EAAa,QAAS,2BAA4B,EAClDC,EAAU,QAAS,yBAA0B,EAC7CC,EAAO,IAKPC,EACAC,EAAMJ,EAAYD,EAAM,UAAW,aAAc,CAAE,EAClDE,EAASG,CAAI,EACjBD,EAAOD,EAEPC,EAAOC,EAMR,OAAO,QAAUD", - "names": ["require_sapx", "__commonJSMin", "exports", "module", "M", "sapx", "N", "alpha", "x", "stride", "ix", "i", "m", "require_ndarray", "__commonJSMin", "exports", "module", "M", "sapx", "N", "alpha", "x", "stride", "offset", "ix", "m", "i", "require_main", "__commonJSMin", "exports", "module", "setReadOnly", "sapx", "ndarray", "join", "tryRequire", "isError", "main", "sapx", "tmp"] + "sources": ["../lib/ndarray.js", "../lib/sapx.js", "../lib/main.js", "../lib/index.js"], + "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2020 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// VARIABLES //\n\nvar M = 5;\n\n\n// MAIN //\n\n/**\n* Adds a scalar constant to each element in a single-precision floating-point strided array.\n*\n* @param {PositiveInteger} N - number of indexed elements\n* @param {number} alpha - scalar constant\n* @param {Float32Array} x - input array\n* @param {integer} strideX - stride length\n* @param {NonNegativeInteger} offsetX - starting index\n* @returns {Float32Array} input array\n*\n* @example\n* var Float32Array = require( '@stdlib/array-float32' );\n*\n* var x = new Float32Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] );\n*\n* sapx( 3, 5.0, x, 1, x.length-3 );\n* // x => [ 1.0, -2.0, 3.0, 1.0, 10.0, -1.0 ]\n*/\nfunction sapx( N, alpha, x, strideX, offsetX ) {\n\tvar ix;\n\tvar m;\n\tvar i;\n\n\tif ( N <= 0 || alpha === 0.0 ) {\n\t\treturn x;\n\t}\n\tix = offsetX;\n\n\t// Use loop unrolling if the stride is equal to `1`...\n\tif ( strideX === 1 ) {\n\t\tm = N % M;\n\n\t\t// If we have a remainder, run a clean-up loop...\n\t\tif ( m > 0 ) {\n\t\t\tfor ( i = 0; i < m; i++ ) {\n\t\t\t\tx[ ix ] += alpha;\n\t\t\t\tix += strideX;\n\t\t\t}\n\t\t}\n\t\tif ( N < M ) {\n\t\t\treturn x;\n\t\t}\n\t\tfor ( i = m; i < N; i += M ) {\n\t\t\tx[ ix ] += alpha;\n\t\t\tx[ ix+1 ] += alpha;\n\t\t\tx[ ix+2 ] += alpha;\n\t\t\tx[ ix+3 ] += alpha;\n\t\t\tx[ ix+4 ] += alpha;\n\t\t\tix += M;\n\t\t}\n\t\treturn x;\n\t}\n\tfor ( i = 0; i < N; i++ ) {\n\t\tx[ ix ] += alpha;\n\t\tix += strideX;\n\t}\n\treturn x;\n}\n\n\n// EXPORTS //\n\nmodule.exports = sapx;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2020 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar stride2offset = require( '@stdlib/strided-base-stride2offset' );\nvar ndarray = require( './ndarray.js' );\n\n\n// MAIN //\n\n/**\n* Adds a scalar constant to each element in a single-precision floating-point strided array.\n*\n* @param {PositiveInteger} N - number of indexed elements\n* @param {number} alpha - scalar constant\n* @param {Float32Array} x - input array\n* @param {integer} strideX - stride length\n* @returns {Float32Array} input array\n*\n* @example\n* var Float32Array = require( '@stdlib/array-float32' );\n*\n* var x = new Float32Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] );\n*\n* sapx( x.length, 5.0, x, 1 );\n* // x => [ 3.0, 6.0, 8.0, 0.0, 9.0, 5.0, 4.0, 2.0 ]\n*/\nfunction sapx( N, alpha, x, strideX ) {\n\treturn ndarray( N, alpha, x, strideX, stride2offset( N, strideX ) );\n}\n\n\n// EXPORTS //\n\nmodule.exports = sapx;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2020 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );\nvar sapx = require( './sapx.js' );\nvar ndarray = require( './ndarray.js' );\n\n\n// MAIN //\n\nsetReadOnly( sapx, 'ndarray', ndarray );\n\n\n// EXPORTS //\n\nmodule.exports = sapx;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2020 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Add a scalar constant to each element in a single-precision floating-point strided array.\n*\n* @module @stdlib/blas-ext-base-sapx\n*\n* @example\n* var Float32Array = require( '@stdlib/array-float32' );\n* var sapx = require( '@stdlib/blas-ext-base-sapx' );\n*\n* var x = new Float32Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] );\n*\n* sapx( x.length, 5.0, x, 1 );\n* // x => [ 3.0, 6.0, 8.0, 0.0, 9.0, 5.0, 4.0, 2.0 ]\n*\n* @example\n* var Float32Array = require( '@stdlib/array-float32' );\n* var sapx = require( '@stdlib/blas-ext-base-sapx' );\n*\n* var x = new Float32Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] );\n*\n* sapx.ndarray( x.length, 5.0, x, 1, 0 );\n* // x => [ 3.0, 6.0, 8.0, 0.0, 9.0, 5.0, 4.0, 2.0 ]\n*/\n\n// MODULES //\n\nvar join = require( 'path' ).join;\nvar tryRequire = require( '@stdlib/utils-try-require' );\nvar isError = require( '@stdlib/assert-is-error' );\nvar main = require( './main.js' );\n\n\n// MAIN //\n\nvar sapx;\nvar tmp = tryRequire( join( __dirname, './native.js' ) );\nif ( isError( tmp ) ) {\n\tsapx = main;\n} else {\n\tsapx = tmp;\n}\n\n\n// EXPORTS //\n\nmodule.exports = sapx;\n\n// exports: { \"ndarray\": \"sapx.ndarray\" }\n"], + "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAI,EAuBR,SAASC,EAAMC,EAAGC,EAAOC,EAAGC,EAASC,EAAU,CAC9C,IAAIC,EACAC,EACAC,EAEJ,GAAKP,GAAK,GAAKC,IAAU,EACxB,OAAOC,EAKR,GAHAG,EAAKD,EAGAD,IAAY,EAAI,CAIpB,GAHAG,EAAIN,EAAIF,EAGHQ,EAAI,EACR,IAAMC,EAAI,EAAGA,EAAID,EAAGC,IACnBL,EAAGG,CAAG,GAAKJ,EACXI,GAAMF,EAGR,GAAKH,EAAIF,EACR,OAAOI,EAER,IAAMK,EAAID,EAAGC,EAAIP,EAAGO,GAAKT,EACxBI,EAAGG,CAAG,GAAKJ,EACXC,EAAGG,EAAG,CAAE,GAAKJ,EACbC,EAAGG,EAAG,CAAE,GAAKJ,EACbC,EAAGG,EAAG,CAAE,GAAKJ,EACbC,EAAGG,EAAG,CAAE,GAAKJ,EACbI,GAAMP,EAEP,OAAOI,CACR,CACA,IAAMK,EAAI,EAAGA,EAAIP,EAAGO,IACnBL,EAAGG,CAAG,GAAKJ,EACXI,GAAMF,EAEP,OAAOD,CACR,CAKAL,EAAO,QAAUE,ICzFjB,IAAAS,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAgB,QAAS,oCAAqC,EAC9DC,EAAU,IAsBd,SAASC,EAAMC,EAAGC,EAAOC,EAAGC,EAAU,CACrC,OAAOL,EAASE,EAAGC,EAAOC,EAAGC,EAASN,EAAeG,EAAGG,CAAQ,CAAE,CACnE,CAKAP,EAAO,QAAUG,ICpDjB,IAAAK,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAc,QAAS,uDAAwD,EAC/EC,EAAO,IACPC,EAAU,IAKdF,EAAaC,EAAM,UAAWC,CAAQ,EAKtCH,EAAO,QAAUE,ICYjB,IAAIE,EAAO,QAAS,MAAO,EAAE,KACzBC,EAAa,QAAS,2BAA4B,EAClDC,EAAU,QAAS,yBAA0B,EAC7CC,EAAO,IAKPC,EACAC,EAAMJ,EAAYD,EAAM,UAAW,aAAc,CAAE,EAClDE,EAASG,CAAI,EACjBD,EAAOD,EAEPC,EAAOC,EAMR,OAAO,QAAUD", + "names": ["require_ndarray", "__commonJSMin", "exports", "module", "M", "sapx", "N", "alpha", "x", "strideX", "offsetX", "ix", "m", "i", "require_sapx", "__commonJSMin", "exports", "module", "stride2offset", "ndarray", "sapx", "N", "alpha", "x", "strideX", "require_main", "__commonJSMin", "exports", "module", "setReadOnly", "sapx", "ndarray", "join", "tryRequire", "isError", "main", "sapx", "tmp"] } diff --git a/docs/repl.txt b/docs/repl.txt index 42e084c..f62676a 100644 --- a/docs/repl.txt +++ b/docs/repl.txt @@ -1,7 +1,7 @@ -{{alias}}( N, alpha, x, stride ) - Adds a constant to each element in a single-precision floating-point strided - array. +{{alias}}( N, alpha, x, strideX ) + Adds a scalar constant to each element in a single-precision floating-point + strided array. The `N` and stride parameters determine which elements in the strided array are accessed at runtime. @@ -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,7 +37,7 @@ > {{alias}}( x.length, 5.0, x, 1 ) [ 3.0, 6.0, 8.0, 0.0, 9.0, 4.0, 2.0 ] - // Using `N` and `stride` parameters: + // Using `N` and stride parameters: > x = new {{alias:@stdlib/array/float32}}( [ -2.0, 1.0, 3.0, -5.0, 4.0, -1.0, -3.0 ] ); > {{alias}}( 3, 5.0, x, 2 ) [ 3.0, 1.0, 8.0, -5.0, 9.0, -1.0, -3.0 ] @@ -51,13 +51,13 @@ [ 1.0, 3.0, 3.0, 1.0, 5.0, -1.0 ] -{{alias}}.ndarray( N, alpha, x, stride, offset ) - Adds a constant to each element in a single-precision floating-point strided - array using alternative indexing semantics. +{{alias}}.ndarray( N, alpha, x, strideX, offsetX ) + Adds a scalar constant to each element in a single-precision floating-point + strided array using 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 - starting index. + buffer, the offset parameter supports indexing semantics based on a starting + index. Parameters ---------- @@ -65,15 +65,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/docs/types/index.d.ts b/docs/types/index.d.ts index 33b95f8..4762c27 100644 --- a/docs/types/index.d.ts +++ b/docs/types/index.d.ts @@ -23,12 +23,12 @@ */ interface Routine { /** - * Adds a constant to each element in a single-precision floating-point strided array. + * Adds a scalar constant to each element in a single-precision floating-point strided array. * * @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 `x` * * @example @@ -39,16 +39,16 @@ interface Routine { * sapx( x.length, 5.0, x, 1 ); * // x => [ 3.0, 6.0, 8.0, 0.0, 9.0, 5.0, 4.0, 2.0 ] */ - ( N: number, alpha: number, x: Float32Array, stride: number ): Float32Array; + ( N: number, alpha: number, x: Float32Array, strideX: number ): Float32Array; /** - * Adds a constant to each element in a single-precision floating-point strided array using alternative indexing semantics. + * Adds a scalar constant to each element in a single-precision floating-point strided array using 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 `x` * * @example @@ -59,16 +59,16 @@ interface Routine { * sapx.ndarray( x.length, 5.0, x, 1, 0 ); * // x => [ 3.0, 6.0, 8.0, 0.0, 9.0, 5.0, 4.0, 2.0 ] */ - ndarray( N: number, alpha: number, x: Float32Array, stride: number, offset: number ): Float32Array; + ndarray( N: number, alpha: number, x: Float32Array, strideX: number, offsetX: number ): Float32Array; } /** -* Adds a constant to each element in a single-precision floating-point strided array. +* Adds a scalar constant to each element in a single-precision floating-point strided array. * * @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 `x` * * @example diff --git a/examples/c/example.c b/examples/c/example.c index d925811..b1e0e48 100644 --- a/examples/c/example.c +++ b/examples/c/example.c @@ -21,16 +21,16 @@ int main( void ) { // Create a strided array: - float x[] = { 1.0, -2.0, 3.0, -4.0, 5.0, -6.0, 7.0, -8.0 }; + float x[] = { 1.0f, -2.0f, 3.0f, -4.0f, 5.0f, -6.0f, 7.0f, -8.0f }; - // Specify the number of elements: - int N = 8; + // Specify the number of indexed elements: + const int N = 8; // Specify a stride: - int strideX = 1; + const int strideX = 1; // Add a constant to each element: - c_sapx( N, 5.0f, x, strideX ); + stdlib_strided_sapx( N, 5.0f, x, strideX ); // Print the result: for ( int i = 0; i < 8; i++ ) { diff --git a/examples/index.js b/examples/index.js index 27be014..0e6ea34 100644 --- a/examples/index.js +++ b/examples/index.js @@ -18,12 +18,12 @@ 'use strict'; -var uniform = require( '@stdlib/random-base-uniform' ).factory; -var filledarrayBy = require( '@stdlib/array-filled-by' ); +var discreteUniform = require( '@stdlib/random-array-discrete-uniform' ); var sapx = require( './../lib' ); -var x = filledarrayBy( 10, 'float32', uniform( -100.0, 100.0 ) ); - +var x = discreteUniform( 10, -100, 100, { + 'dtype': 'float32' +}); console.log( x ); sapx( x.length, 5.0, x, 1 ); diff --git a/include/stdlib/blas/ext/base/sapx.h b/include/stdlib/blas/ext/base/sapx.h index 91ed15d..1ef0813 100644 --- a/include/stdlib/blas/ext/base/sapx.h +++ b/include/stdlib/blas/ext/base/sapx.h @@ -19,7 +19,7 @@ #ifndef STDLIB_BLAS_EXT_BASE_SAPX_H #define STDLIB_BLAS_EXT_BASE_SAPX_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. @@ -29,9 +29,14 @@ extern "C" { #endif /** -* Adds a constant to each element in a single-precision floating-point strided array. +* Adds a scalar constant to each element in a single-precision floating-point strided array. */ -void c_sapx( const int64_t N, const float alpha, float *X, const int64_t stride ); +void API_SUFFIX(stdlib_strided_sapx)( const CBLAS_INT N, const float alpha, float *X, const CBLAS_INT strideX ); + +/** +* Adds a scalar constant to each element in a single-precision floating-point strided array using alternative indexing semantics. +*/ +void API_SUFFIX(stdlib_strided_sapx_ndarray)( const CBLAS_INT N, const float alpha, float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ); #ifdef __cplusplus } diff --git a/lib/index.js b/lib/index.js index f1f35b4..0c52db3 100644 --- a/lib/index.js +++ b/lib/index.js @@ -19,7 +19,7 @@ 'use strict'; /** -* Add a constant to each element in a single-precision floating-point strided array. +* Add a scalar constant to each element in a single-precision floating-point strided array. * * @module @stdlib/blas-ext-base-sapx * diff --git a/lib/ndarray.js b/lib/ndarray.js index a661b39..b01defc 100644 --- a/lib/ndarray.js +++ b/lib/ndarray.js @@ -26,25 +26,24 @@ var M = 5; // MAIN // /** -* Adds a constant to each element in a single-precision floating-point strided array. +* Adds a scalar constant to each element in a single-precision floating-point strided array. * * @param {PositiveInteger} N - number of indexed elements -* @param {number} alpha - scalar +* @param {number} alpha - scalar constant * @param {Float32Array} x - input array -* @param {integer} stride - index increment -* @param {NonNegativeInteger} offset - starting index +* @param {integer} strideX - stride length +* @param {NonNegativeInteger} offsetX - starting index * @returns {Float32Array} input array * * @example * var Float32Array = require( '@stdlib/array-float32' ); * * var x = new Float32Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] ); -* var alpha = 5.0; * -* sapx( 3, alpha, x, 1, x.length-3 ); +* sapx( 3, 5.0, x, 1, x.length-3 ); * // x => [ 1.0, -2.0, 3.0, 1.0, 10.0, -1.0 ] */ -function sapx( N, alpha, x, stride, offset ) { +function sapx( N, alpha, x, strideX, offsetX ) { var ix; var m; var i; @@ -52,17 +51,17 @@ function sapx( N, alpha, x, stride, offset ) { if ( N <= 0 || alpha === 0.0 ) { return x; } - ix = offset; + ix = offsetX; // Use loop unrolling if the stride is equal to `1`... - if ( stride === 1 ) { + if ( strideX === 1 ) { m = N % M; // If we have a remainder, run a clean-up loop... if ( m > 0 ) { for ( i = 0; i < m; i++ ) { x[ ix ] += alpha; - ix += stride; + ix += strideX; } } if ( N < M ) { @@ -80,7 +79,7 @@ function sapx( N, alpha, x, stride, offset ) { } for ( i = 0; i < N; i++ ) { x[ ix ] += alpha; - ix += stride; + ix += strideX; } return x; } diff --git a/lib/ndarray.native.js b/lib/ndarray.native.js index a785ec7..ee8eed9 100644 --- a/lib/ndarray.native.js +++ b/lib/ndarray.native.js @@ -20,37 +20,31 @@ // MODULES // -var minViewBufferIndex = require( '@stdlib/strided-base-min-view-buffer-index' ); -var offsetView = require( '@stdlib/strided-base-offset-view' ); -var addon = require( './sapx.native.js' ); +var addon = require( './../src/addon.node' ); // MAIN // /** -* Adds a constant to each element in a single-precision floating-point strided array. +* Adds a scalar constant to each element in a single-precision floating-point strided array. * * @param {PositiveInteger} N - number of indexed elements -* @param {number} alpha - scalar +* @param {number} alpha - scalar constant * @param {Float32Array} x - input array -* @param {integer} stride - index increment -* @param {NonNegativeInteger} offset - starting index +* @param {integer} strideX - stride length +* @param {NonNegativeInteger} offsetX - starting index * @returns {Float32Array} input array * * @example * var Float32Array = require( '@stdlib/array-float32' ); * * var x = new Float32Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] ); -* var alpha = 5.0; * -* sapx( 3, alpha, x, 1, x.length-3 ); +* sapx( 3, 5.0, x, 1, x.length-3 ); * // x => [ 1.0, -2.0, 3.0, 1.0, 10.0, -1.0 ] */ -function sapx( N, alpha, x, stride, offset ) { - var view; - offset = minViewBufferIndex( N, stride, offset ); - view = offsetView( x, offset ); - addon( N, alpha, view, stride ); +function sapx( N, alpha, x, strideX, offsetX ) { + addon.ndarray( N, alpha, x, strideX, offsetX ); return x; } diff --git a/lib/sapx.js b/lib/sapx.js index cbc2031..26f3a3f 100644 --- a/lib/sapx.js +++ b/lib/sapx.js @@ -18,20 +18,21 @@ 'use strict'; -// VARIABLES // +// MODULES // -var M = 5; +var stride2offset = require( '@stdlib/strided-base-stride2offset' ); +var ndarray = require( './ndarray.js' ); // MAIN // /** -* Adds a constant to each element in a single-precision floating-point strided array. +* Adds a scalar constant to each element in a single-precision floating-point strided array. * * @param {PositiveInteger} N - number of indexed elements -* @param {number} alpha - scalar +* @param {number} alpha - scalar constant * @param {Float32Array} x - input array -* @param {integer} stride - index increment +* @param {integer} strideX - stride length * @returns {Float32Array} input array * * @example @@ -42,46 +43,8 @@ var M = 5; * sapx( x.length, 5.0, x, 1 ); * // x => [ 3.0, 6.0, 8.0, 0.0, 9.0, 5.0, 4.0, 2.0 ] */ -function sapx( N, alpha, x, stride ) { - var ix; - var i; - var m; - - if ( N <= 0 || alpha === 0.0 ) { - return x; - } - // Use loop unrolling if the stride is equal to `1`... - if ( stride === 1 ) { - m = N % M; - - // If we have a remainder, run a clean-up loop... - if ( m > 0 ) { - for ( i = 0; i < m; i++ ) { - x[ i ] += alpha; - } - } - if ( N < M ) { - return x; - } - for ( i = m; i < N; i += M ) { - x[ i ] += alpha; - x[ i+1 ] += alpha; - x[ i+2 ] += alpha; - x[ i+3 ] += alpha; - x[ i+4 ] += alpha; - } - return x; - } - if ( stride < 0 ) { - ix = (1-N) * stride; - } else { - ix = 0; - } - for ( i = 0; i < N; i++ ) { - x[ ix ] += alpha; - ix += stride; - } - return x; +function sapx( N, alpha, x, strideX ) { + return ndarray( N, alpha, x, strideX, stride2offset( N, strideX ) ); } diff --git a/lib/sapx.native.js b/lib/sapx.native.js index 273b07b..b5206a8 100644 --- a/lib/sapx.native.js +++ b/lib/sapx.native.js @@ -26,12 +26,12 @@ var addon = require( './../src/addon.node' ); // MAIN // /** -* Adds a constant to each element in a single-precision floating-point strided array. +* Adds a scalar constant to each element in a single-precision floating-point strided array. * * @param {PositiveInteger} N - number of indexed elements -* @param {number} alpha - scalar +* @param {number} alpha - scalar constant * @param {Float32Array} x - input array -* @param {integer} stride - index increment +* @param {integer} strideX - stride length * @returns {Float32Array} input array * * @example @@ -42,8 +42,8 @@ var addon = require( './../src/addon.node' ); * sapx( x.length, 5.0, x, 1 ); * // x => [ 3.0, 6.0, 8.0, 0.0, 9.0, 5.0, 4.0, 2.0 ] */ -function sapx( N, alpha, x, stride ) { - addon( N, alpha, x, stride ); +function sapx( N, alpha, x, strideX ) { + addon( N, alpha, x, strideX ); return x; } diff --git a/manifest.json b/manifest.json index a5cac6e..340f352 100644 --- a/manifest.json +++ b/manifest.json @@ -28,50 +28,52 @@ { "task": "build", "src": [ - "./src/sapx.c" + "./src/main.c" ], "include": [ "./include" ], - "libraries": [ - "-lm" - ], + "libraries": [], "libpath": [], "dependencies": [ "@stdlib/napi-export", "@stdlib/napi-argv", "@stdlib/napi-argv-int64", "@stdlib/napi-argv-float", - "@stdlib/napi-argv-strided-float32array" + "@stdlib/napi-argv-strided-float32array", + "@stdlib/blas-base-shared", + "@stdlib/strided-base-stride2offset" ] }, { "task": "benchmark", "src": [ - "./src/sapx.c" + "./src/main.c" ], "include": [ "./include" ], - "libraries": [ - "-lm" - ], + "libraries": [], "libpath": [], - "dependencies": [] + "dependencies": [ + "@stdlib/blas-base-shared", + "@stdlib/strided-base-stride2offset" + ] }, { "task": "examples", "src": [ - "./src/sapx.c" + "./src/main.c" ], "include": [ "./include" ], - "libraries": [ - "-lm" - ], + "libraries": [], "libpath": [], - "dependencies": [] + "dependencies": [ + "@stdlib/blas-base-shared", + "@stdlib/strided-base-stride2offset" + ] } ] } diff --git a/package.json b/package.json index 11e78ba..3aeb584 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@stdlib/blas-ext-base-sapx", "version": "0.2.2", - "description": "Add a constant to each element in a single-precision floating-point strided array.", + "description": "Add a scalar constant to each element in a single-precision floating-point strided array.", "license": "Apache-2.0", "author": { "name": "The Stdlib Authors", @@ -42,24 +42,24 @@ }, "dependencies": { "@stdlib/assert-is-error": "^0.2.2", + "@stdlib/blas-base-shared": "^0.1.0", "@stdlib/napi-argv": "^0.2.2", "@stdlib/napi-argv-float": "^0.2.2", "@stdlib/napi-argv-int64": "^0.2.2", "@stdlib/napi-argv-strided-float32array": "^0.2.2", "@stdlib/napi-export": "^0.2.2", + "@stdlib/strided-base-stride2offset": "^0.1.0", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2", "@stdlib/utils-library-manifest": "^0.2.2", "@stdlib/utils-try-require": "^0.2.2" }, "devDependencies": { - "@stdlib/array-filled-by": "^0.2.1", "@stdlib/array-float32": "^0.2.2", "@stdlib/assert-is-browser": "^0.2.2", "@stdlib/math-base-assert-is-nan": "^0.2.2", "@stdlib/math-base-special-pow": "^0.3.0", - "@stdlib/random-base-uniform": "^0.2.1", - "@stdlib/strided-base-min-view-buffer-index": "^0.2.2", - "@stdlib/strided-base-offset-view": "^0.2.2", + "@stdlib/random-array-discrete-uniform": "^0.2.1", + "@stdlib/random-array-uniform": "^0.2.1", "proxyquire": "^2.0.0", "tape": "git+https://github.com/kgryte/tape.git#fix/globby", "istanbul": "^0.4.1", diff --git a/src/addon.c b/src/addon.c index 9acbeab..38b9d49 100644 --- a/src/addon.c +++ b/src/addon.c @@ -17,6 +17,7 @@ */ #include "stdlib/blas/ext/base/sapx.h" +#include "stdlib/blas/base/shared.h" #include "stdlib/napi/export.h" #include "stdlib/napi/argv.h" #include "stdlib/napi/argv_int64.h" @@ -35,10 +36,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, strideX, argv, 3 ); - STDLIB_NAPI_ARGV_STRIDED_FLOAT32ARRAY( env, X, N, strideX, argv, 2 ) - c_sapx( N, alpha, X, strideX ); + STDLIB_NAPI_ARGV_INT64( env, strideX, argv, 3 ); + STDLIB_NAPI_ARGV_STRIDED_FLOAT32ARRAY( env, X, N, strideX, argv, 2 ); + API_SUFFIX(stdlib_strided_sapx)( N, alpha, X, strideX ); return NULL; } -STDLIB_NAPI_MODULE_EXPORT_FCN( addon ) +/** +* 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 ); + API_SUFFIX(stdlib_strided_sapx_ndarray)( N, alpha, X, strideX, offsetX ); + return NULL; +} + +STDLIB_NAPI_MODULE_EXPORT_FCN_WITH_METHOD( addon, "ndarray", addon_method ) diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..9a1d348 --- /dev/null +++ b/src/main.c @@ -0,0 +1,84 @@ +/** +* @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/sapx.h" +#include "stdlib/strided/base/stride2offset.h" +#include "stdlib/blas/base/shared.h" + +/** +* Adds a scalar constant to each element in a single-precision floating-point strided array. +* +* @param N number of indexed elements +* @param alpha scalar constant +* @param X input array +* @param strideX stride length +*/ +void API_SUFFIX(stdlib_strided_sapx)( const CBLAS_INT N, const float alpha, float *X, const CBLAS_INT strideX ) { + CBLAS_INT ox = stdlib_strided_stride2offset( N, strideX ); + API_SUFFIX(stdlib_strided_sapx_ndarray)( N, alpha, X, strideX, ox ); +} + +/** +* Adds a scalar constant to each element in a single-precision floating-point strided array using alternative indexing semantics. +* +* @param N number of indexed elements +* @param alpha scalar constant +* @param X input array +* @param strideX stride length +* @param offsetX starting index +*/ +void API_SUFFIX(stdlib_strided_sapx_ndarray)( const CBLAS_INT N, const float alpha, float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ) { + CBLAS_INT ix; + CBLAS_INT m; + CBLAS_INT i; + + if ( N <= 0 || alpha == 0.0 ) { + return; + } + ix = offsetX; + + // Use loop unrolling if the stride is equal to `1`... + if ( strideX == 1 ) { + m = N % 5; + + // If we have a remainder, run a clean-up loop... + if ( m > 0 ) { + for ( i = 0; i < m; i++ ) { + X[ ix ] += alpha; + ix += strideX; + } + } + if ( N < 5 ) { + return; + } + for ( i = m; i < N; i += 5 ) { + X[ ix ] += alpha; + X[ ix+1 ] += alpha; + X[ ix+2 ] += alpha; + X[ ix+3 ] += alpha; + X[ ix+4 ] += alpha; + ix += 5; + } + return; + } + for ( i = 0; i < N; i++ ) { + X[ ix ] += alpha; + ix += strideX; + } + return; +} diff --git a/src/sapx.c b/src/sapx.c deleted file mode 100644 index 2771b85..0000000 --- a/src/sapx.c +++ /dev/null @@ -1,70 +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/sapx.h" -#include - -/** -* Adds a constant to each element in a single-precision floating-point strided array. -* -* @param N number of indexed elements -* @param alpha scalar -* @param X input array -* @param stride index increment -*/ -void c_sapx( const int64_t N, const float alpha, float *X, const int64_t stride ) { - int64_t ix; - int64_t m; - int64_t i; - - if ( N <= 0 || alpha == 0.0 ) { - return; - } - // Use loop unrolling if the stride is equal to `1`... - if ( stride == 1 ) { - m = N % 5; - - // If we have a remainder, run a clean-up loop... - if ( m > 0 ) { - for ( i = 0; i < m; i++ ) { - X[ i ] += alpha; - } - } - if ( N < 5 ) { - return; - } - for ( i = m; i < N; i += 5 ) { - X[ i ] += alpha; - X[ i+1 ] += alpha; - X[ i+2 ] += alpha; - X[ i+3 ] += alpha; - X[ i+4 ] += alpha; - } - return; - } - if ( stride < 0 ) { - ix = (1-N) * stride; - } else { - ix = 0; - } - for ( i = 0; i < N; i++ ) { - X[ ix ] += alpha; - ix += stride; - } - return; -}