Skip to content

Commit

Permalink
refactor: reduce code duplication in blas/base level 1 routines
Browse files Browse the repository at this point in the history
PR-URL: #2517
Reviewed-by: Athan Reines <[email protected]>
  • Loading branch information
aman-095 committed Jul 6, 2024
1 parent 243ab4d commit 271f5d5
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 645 deletions.
47 changes: 6 additions & 41 deletions lib/node_modules/@stdlib/blas/base/daxpy/lib/daxpy.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@

'use strict';

// VARIABLES //
// MODULES //

var M = 4;
var stride2offset = require( '@stdlib/strided/base/stride2offset' );
var ndarray = require('./ndarray.js');


// MAIN //
Expand Down Expand Up @@ -49,48 +50,12 @@ var M = 4;
function daxpy( N, alpha, x, strideX, y, strideY ) {
var ix;
var iy;
var m;
var i;
if ( N <= 0 || alpha === 0.0 ) {
return y;
}
// Use unrolled loops if both strides are equal to `1`...
if ( strideX === 1 && strideY === 1 ) {
m = N % M;

// If we have a remainder, run a clean-up loop...
if ( m > 0 ) {
for ( i = 0; i < m; i++ ) {
y[ i ] += alpha * x[ i ];
}
}
if ( N < M ) {
return y;
}
for ( i = m; i < N; i += M ) {
y[ i ] += alpha * x[ i ];
y[ i+1 ] += alpha * x[ i+1 ];
y[ i+2 ] += alpha * x[ i+2 ];
y[ i+3 ] += alpha * x[ i+3 ];
}
return y;
}
if ( strideX < 0 ) {
ix = (1-N) * strideX;
} else {
ix = 0;
}
if ( strideY < 0 ) {
iy = (1-N) * strideY;
} else {
iy = 0;
}
for ( i = 0; i < N; i++ ) {
y[ iy ] += alpha * x[ ix ];
ix += strideX;
iy += strideY;
}
return y;
ix = stride2offset( N, strideX );
iy = stride2offset( N, strideY );
return ndarray( N, alpha, x, strideX, ix, y, strideY, iy );
}


Expand Down
49 changes: 7 additions & 42 deletions lib/node_modules/@stdlib/blas/base/ddot/lib/ddot.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@

'use strict';

// VARIABLES //
// MODULES //

var M = 5;
var stride2offset = require( '@stdlib/strided/base/stride2offset' );
var ndarray = require( './ndarray.js' );


// MAIN //
Expand All @@ -45,50 +46,14 @@ var M = 5;
* // returns -5.0
*/
function ddot( N, x, strideX, y, strideY ) {
var dot;
var ix;
var iy;
var m;
var i;

dot = 0.0;
if ( N <= 0 ) {
return dot;
}
// Use unrolled loops if both strides are equal to `1`...
if ( strideX === 1 && strideY === 1 ) {
m = N % M;

// If we have a remainder, run a clean-up loop...
if ( m > 0 ) {
for ( i = 0; i < m; i++ ) {
dot += x[ i ] * y[ i ];
}
}
if ( N < M ) {
return dot;
}
for ( i = m; i < N; i += M ) {
dot += ( x[ i ] * y[ i ] ) + ( x[ i+1 ] * y[ i+1 ] ) + ( x[ i+2 ] * y[ i+2 ] ) + ( x[ i+3 ] * y[ i+3 ] ) + ( x[ i+4 ] * y[ i+4 ] ); // eslint-disable-line max-len
}
return dot;
}
if ( strideX < 0 ) {
ix = ( 1-N ) * strideX;
} else {
ix = 0;
}
if ( strideY < 0 ) {
iy = ( 1-N ) * strideY;
} else {
iy = 0;
}
for ( i = 0; i < N; i++ ) {
dot += ( x[ ix ] * y[ iy ] );
ix += strideX;
iy += strideY;
return 0.0;
}
return dot;
ix = stride2offset( N, strideX );
iy = stride2offset( N, strideY );
return ndarray( N, x, strideX, ix, y, strideY, iy );
}


Expand Down
39 changes: 9 additions & 30 deletions lib/node_modules/@stdlib/blas/base/drot/lib/drot.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@

'use strict';

// MODULES //

var stride2offset = require( '@stdlib/strided/base/stride2offset' );
var ndarray = require( './ndarray.js' );


// MAIN //

/**
Expand All @@ -43,42 +49,15 @@
* // y => <Float64Array>[ ~4.2, 4.4, 4.6, 4.8, 5.0 ]
*/
function drot( N, x, strideX, y, strideY, c, s ) {
var tmp;
var ix;
var iy;
var i;

if ( N <= 0 ) {
return y;
}
// If both strides are equal to `1`...
if ( strideX === 1 && strideY === 1 ) {
for ( i = 0; i < N; i++ ) {
tmp = ( c * x[ i ] ) + ( s * y[ i ] );
y[ i ] = ( c * y[ i ] ) - ( s * x[ i ] );
x[ i ] = tmp;
}
return y;
}
// If both strides are not equal to `1`...
if ( strideX < 0 ) {
ix = ( 1 - N ) * strideX;
} else {
ix = 0;
}
if ( strideY < 0 ) {
iy = ( 1 - N ) * strideY;
} else {
iy = 0;
}
for ( i = 0; i < N; i++ ) {
tmp = ( c * x[ ix ] ) + ( s * y[ iy ] );
y[ iy ] = ( c * y[ iy ] ) - ( s * x[ ix ] );
x[ ix ] = tmp;
ix += strideX;
iy += strideY;
}
return y;
ix = stride2offset( N, strideX );
iy = stride2offset( N, strideY );
return ndarray( N, x, strideX, ix, y, strideY, iy, c, s );
}


Expand Down
104 changes: 9 additions & 95 deletions lib/node_modules/@stdlib/blas/base/drotm/lib/drotm.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@

'use strict';

// MODULES //

var stride2offset = require( '@stdlib/strided/base/stride2offset' );
var ndarray = require( './ndarray.js' );


// MAIN //

/**
Expand All @@ -44,108 +50,16 @@
*/
function drotm( N, x, strideX, y, strideY, param ) {
var dflag;
var dh11;
var dh12;
var dh21;
var dh22;
var ix;
var iy;
var i;
var w;
var z;

dflag = param[ 0 ];
if ( N <= 0 || dflag === -2.0 ) {
return y;
}
if ( strideX === strideY && strideX > 0 ) {
ix = 0;
if ( dflag < 0.0 ) {
dh11 = param[ 1 ];
dh12 = param[ 3 ];
dh21 = param[ 2 ];
dh22 = param[ 4 ];
for ( i = 0; i < N; i++ ) {
w = x[ ix ];
z = y[ ix ];
x[ ix ] = ( w * dh11 ) + ( z * dh12 );
y[ ix ] = ( w * dh21 ) + ( z * dh22 );
ix += strideX;
}
return y;
}
if ( dflag === 0.0 ) {
dh12 = param[ 3 ];
dh21 = param[ 2 ];
for ( i = 0; i < N; i++ ) {
w = x[ ix ];
z = y[ ix ];
x[ ix ] = w + ( z * dh12 );
y[ ix ] = ( w * dh21 ) + z;
ix += strideX;
}
return y;
}
dh11 = param[ 1 ];
dh22 = param[ 4 ];
for ( i = 0; i < N; i++ ) {
w = x[ ix ];
z = y[ ix ];
x[ ix ] = ( w * dh11 ) + z;
y[ ix ] = -w + ( z * dh22 );
ix += strideX;
}
return y;
}
if ( strideX < 0 ) {
ix = ( 1 - N ) * strideX;
} else {
ix = 0;
}
if ( strideY < 0 ) {
iy = ( 1 - N ) * strideY;
} else {
iy = 0;
}
if ( dflag < 0.0 ) {
dh11 = param[ 1 ];
dh12 = param[ 3 ];
dh21 = param[ 2 ];
dh22 = param[ 4 ];
for ( i = 0; i < N; i++ ) {
w = x[ ix ];
z = y[ iy ];
x[ ix ] = ( w * dh11 ) + ( z * dh12 );
y[ iy ] = ( w * dh21 ) + ( z * dh22 );
ix += strideX;
iy += strideY;
}
return y;
}
if ( dflag === 0.0 ) {
dh12 = param[ 3 ];
dh21 = param[ 2 ];
for ( i = 0; i < N; i++ ) {
w = x[ ix ];
z = y[ iy ];
x[ ix ] = w + ( z * dh12 );
y[ iy ] = ( w * dh21 ) + z;
ix += strideX;
iy += strideY;
}
return y;
}
dh11 = param[ 1 ];
dh22 = param[ 4 ];
for ( i = 0; i < N; i++ ) {
w = x[ ix ];
z = y[ iy ];
x[ ix ] = ( w * dh11 ) + z;
y[ iy ] = -w + ( z * dh22 );
ix += strideX;
iy += strideY;
}
return y;
ix = stride2offset( N, strideX );
iy = stride2offset( N, strideY );
return ndarray( N, x, strideX, ix, y, strideY, iy, param );
}


Expand Down
49 changes: 7 additions & 42 deletions lib/node_modules/@stdlib/blas/base/dsdot/lib/dsdot.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@

'use strict';

// VARIABLES //
// MODULES //

var M = 5;
var stride2offset = require( '@stdlib/strided/base/stride2offset' );
var ndarray = require( './ndarray.js' );


// MAIN //
Expand All @@ -45,50 +46,14 @@ var M = 5;
* // returns -5.0
*/
function dsdot( N, x, strideX, y, strideY ) {
var dot;
var ix;
var iy;
var m;
var i;

dot = 0.0;
if ( N <= 0 ) {
return dot;
}
// Use unrolled loops if both strides are equal to `1`...
if ( strideX === 1 && strideY === 1 ) {
m = N % M;

// If we have a remainder, run a clean-up loop...
if ( m > 0 ) {
for ( i = 0; i < m; i++ ) {
dot += x[ i ] * y[ i ];
}
}
if ( N < M ) {
return dot;
}
for ( i = m; i < N; i += M ) {
dot += ( x[i]*y[i] ) + ( x[i+1]*y[i+1] ) + ( x[i+2]*y[i+2] ) + ( x[i+3]*y[i+3] ) + ( x[i+4]*y[i+4] ); // eslint-disable-line max-len
}
return dot;
}
if ( strideX < 0 ) {
ix = ( 1-N ) * strideX;
} else {
ix = 0;
}
if ( strideY < 0 ) {
iy = ( 1-N ) * strideY;
} else {
iy = 0;
}
for ( i = 0; i < N; i++ ) {
dot += x[ ix ] * y[ iy ];
ix += strideX;
iy += strideY;
return 0.0;
}
return dot;
ix = stride2offset( N, strideX );
iy = stride2offset( N, strideY );
return ndarray( N, x, strideX, ix, y, strideY, iy );
}


Expand Down
Loading

1 comment on commit 271f5d5

@stdlib-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage Report

Package Statements Branches Functions Lines
blas/base/daxpy $\color{green}431/431$
$\color{green}+100.00\%$
$\color{green}30/30$
$\color{green}+100.00\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{green}431/431$
$\color{green}+100.00\%$
blas/base/ddot $\color{green}419/419$
$\color{green}+100.00\%$
$\color{green}28/28$
$\color{green}+100.00\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{green}419/419$
$\color{green}+100.00\%$
blas/base/drot $\color{green}409/409$
$\color{green}+100.00\%$
$\color{green}19/19$
$\color{green}+100.00\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{green}409/409$
$\color{green}+100.00\%$
blas/base/drotm $\color{green}488/488$
$\color{green}+100.00\%$
$\color{green}38/38$
$\color{green}+100.00\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{green}488/488$
$\color{green}+100.00\%$
blas/base/dsdot $\color{green}419/419$
$\color{green}+100.00\%$
$\color{green}28/28$
$\color{green}+100.00\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{green}419/419$
$\color{green}+100.00\%$
blas/base/dswap $\color{green}440/440$
$\color{green}+100.00\%$
$\color{green}28/28$
$\color{green}+100.00\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{green}440/440$
$\color{green}+100.00\%$
blas/base/saxpy $\color{green}436/436$
$\color{green}+100.00\%$
$\color{green}30/30$
$\color{green}+100.00\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{green}436/436$
$\color{green}+100.00\%$
blas/base/scopy $\color{green}425/425$
$\color{green}+100.00\%$
$\color{green}29/29$
$\color{green}+100.00\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{green}425/425$
$\color{green}+100.00\%$
blas/base/sdot $\color{green}395/395$
$\color{green}+100.00\%$
$\color{green}19/19$
$\color{green}+100.00\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{green}395/395$
$\color{green}+100.00\%$
blas/base/sdsdot $\color{green}429/429$
$\color{green}+100.00\%$
$\color{green}28/28$
$\color{green}+100.00\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{green}429/429$
$\color{green}+100.00\%$
blas/base/srot $\color{green}409/409$
$\color{green}+100.00\%$
$\color{green}19/19$
$\color{green}+100.00\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{green}409/409$
$\color{green}+100.00\%$
blas/base/srotm $\color{green}493/493$
$\color{green}+100.00\%$
$\color{green}38/38$
$\color{green}+100.00\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{green}493/493$
$\color{green}+100.00\%$
blas/base/sswap $\color{green}440/440$
$\color{green}+100.00\%$
$\color{green}28/28$
$\color{green}+100.00\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{green}440/440$
$\color{green}+100.00\%$

The above coverage report was generated for the changes in this push.

Please sign in to comment.