From d9580f56677f81425930dad6a0ed65c71f665314 Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Wed, 18 Sep 2024 00:02:08 +0500 Subject: [PATCH] refactor: reduce code duplication in `blas/ext/base/sfill` PR-URL: https://github.com/stdlib-js/stdlib/pull/2916 Reviewed-by: Athan Reines --- .../@stdlib/blas/ext/base/sfill/lib/sfill.js | 48 ++----------------- 1 file changed, 4 insertions(+), 44 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/sfill/lib/sfill.js b/lib/node_modules/@stdlib/blas/ext/base/sfill/lib/sfill.js index 90df4981e8e..f70cf47c2a1 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/sfill/lib/sfill.js +++ b/lib/node_modules/@stdlib/blas/ext/base/sfill/lib/sfill.js @@ -18,9 +18,10 @@ 'use strict'; -// VARIABLES // +// MODULES // -var M = 8; +var stride2offset = require( '@stdlib/strided/base/stride2offset' ); +var ndarray = require( './ndarray.js' ); // MAIN // @@ -43,48 +44,7 @@ var M = 8; * // x => [ 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0 ] */ function sfill( N, alpha, x, stride ) { - var ix; - var i; - var m; - - if ( N <= 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; - x[ i+5 ] = alpha; - x[ i+6 ] = alpha; - x[ i+7 ] = 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; + return ndarray( N, alpha, x, stride, stride2offset( N, stride ) ); }