Skip to content

Commit

Permalink
feat: add boolean dtype support to strided/base/binary-addon-dispatch
Browse files Browse the repository at this point in the history
PR-URL: #2526
Ref: #2500
Reviewed-by: Athan Reines <kgryte@gmail.com>
  • Loading branch information
Jaysukh-409 authored Jul 6, 2024
1 parent 599f5e4 commit 082d2e9
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2021 The Stdlib Authors.
* Copyright (c) 2024 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.
Expand All @@ -27,13 +27,15 @@ var isTypedArrayLike = require( '@stdlib/assert/is-typed-array-like' );
var resolve = require( '@stdlib/strided/base/dtype-resolve-enum' );
var reinterpretComplex64 = require( '@stdlib/strided/base/reinterpret-complex64' );
var reinterpretComplex128 = require( '@stdlib/strided/base/reinterpret-complex128' );
var reinterpretBoolean = require( '@stdlib/strided/base/reinterpret-boolean' );
var format = require( '@stdlib/string/format' );


// VARIABLES //

var COMPLEX64 = resolve( 'complex64' );
var COMPLEX128 = resolve( 'complex128' );
var BOOLEAN = resolve( 'bool' );


// MAIN //
Expand Down Expand Up @@ -173,20 +175,26 @@ function dispatch( addon, fallback ) {
viewX = reinterpretComplex64( x, 0 );
} else if ( dtypeX === COMPLEX128 ) {
viewX = reinterpretComplex128( x, 0 );
} else if ( dtypeX === BOOLEAN ) {
viewX = reinterpretBoolean( x, 0 );
} else {
viewX = x;
}
if ( dtypeY === COMPLEX64 ) {
viewY = reinterpretComplex64( y, 0 );
} else if ( dtypeY === COMPLEX128 ) {
viewY = reinterpretComplex128( y, 0 );
} else if ( dtypeY === BOOLEAN ) {
viewY = reinterpretBoolean( y, 0 );
} else {
viewY = y;
}
if ( dtypeZ === COMPLEX64 ) {
viewZ = reinterpretComplex64( z, 0 );
} else if ( dtypeZ === COMPLEX128 ) {
viewZ = reinterpretComplex128( z, 0 );
} else if ( dtypeZ === BOOLEAN ) {
viewZ = reinterpretBoolean( z, 0 );
} else {
viewZ = z;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2021 The Stdlib Authors.
* Copyright (c) 2024 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.
Expand All @@ -28,6 +28,7 @@ var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).is
var resolve = require( '@stdlib/strided/base/dtype-resolve-enum' );
var reinterpretComplex64 = require( '@stdlib/strided/base/reinterpret-complex64' );
var reinterpretComplex128 = require( '@stdlib/strided/base/reinterpret-complex128' );
var reinterpretBoolean = require( '@stdlib/strided/base/reinterpret-boolean' );
var offsetView = require( '@stdlib/strided/base/offset-view' );
var minViewBufferIndex = require( '@stdlib/strided/base/min-view-buffer-index' );
var format = require( '@stdlib/string/format' );
Expand All @@ -37,6 +38,7 @@ var format = require( '@stdlib/string/format' );

var COMPLEX64 = resolve( 'complex64' );
var COMPLEX128 = resolve( 'complex128' );
var BOOLEAN = resolve( 'bool' );


// MAIN //
Expand Down Expand Up @@ -200,20 +202,26 @@ function dispatch( addon, fallback ) {
viewX = reinterpretComplex64( x, offsetX );
} else if ( dtypeX === COMPLEX128 ) {
viewX = reinterpretComplex128( x, offsetX );
} else if ( dtypeX === BOOLEAN ) {
viewX = reinterpretBoolean( x, offsetX );
} else {
viewX = offsetView( x, offsetX );
}
if ( dtypeY === COMPLEX64 ) {
viewY = reinterpretComplex64( y, offsetY );
} else if ( dtypeY === COMPLEX128 ) {
viewY = reinterpretComplex128( y, offsetY );
} else if ( dtypeY === BOOLEAN ) {
viewY = reinterpretBoolean( y, offsetY );
} else {
viewY = offsetView( y, offsetY );
}
if ( dtypeZ === COMPLEX64 ) {
viewZ = reinterpretComplex64( z, offsetZ );
} else if ( dtypeZ === COMPLEX128 ) {
viewZ = reinterpretComplex128( z, offsetZ );
} else if ( dtypeZ === BOOLEAN ) {
viewZ = reinterpretBoolean( z, offsetZ );
} else {
viewZ = offsetView( z, offsetZ );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2021 The Stdlib Authors.
* Copyright (c) 2024 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.
Expand All @@ -25,8 +25,10 @@ var noop = require( '@stdlib/utils/noop' );
var Float64Array = require( '@stdlib/array/float64' );
var Complex64Array = require( '@stdlib/array/complex64' );
var Complex128Array = require( '@stdlib/array/complex128' );
var BooleanArray = require( '@stdlib/array/bool' );
var isFloat32Array = require( '@stdlib/assert/is-float32array' );
var isFloat64Array = require( '@stdlib/assert/is-float64array' );
var isUint8Array = require( '@stdlib/assert/is-uint8array' );
var resolve = require( '@stdlib/strided/base/dtype-resolve-enum' );
var dispatch = require( './../lib' );

Expand Down Expand Up @@ -134,6 +136,44 @@ tape( 'the function returns a function which dispatches to an addon function whe
}
});

tape( 'the function returns a function which dispatches to an addon function when provided typed arrays (bool)', function test( t ) {
var f;
var x;
var y;
var z;

f = dispatch( addon, fallback );

x = new BooleanArray( 2 );
y = new BooleanArray( x.length );
z = new BooleanArray( x.length );

f( x.length, 'bool', x, 1, 'bool', y, 1, 'bool', z, 1 );

t.end();

function addon( N, dx, ax, sx, dy, ay, sy, dz, az, sz ) {
t.ok( true, 'called addon' );
t.strictEqual( N, x.length, 'returns expected value' );
t.strictEqual( dx, resolve( 'bool' ), 'returns expected value' );
t.strictEqual( isUint8Array( ax ), true, 'returns expected value' );
t.strictEqual( ax.buffer, x.buffer, 'returns expected value' );
t.strictEqual( sx, 1, 'returns expected value' );
t.strictEqual( dy, resolve( 'bool' ), 'returns expected value' );
t.strictEqual( isUint8Array( ay ), true, 'returns expected value' );
t.strictEqual( ay.buffer, y.buffer, 'returns expected value' );
t.strictEqual( sy, 1, 'returns expected value' );
t.strictEqual( dz, resolve( 'bool' ), 'returns expected value' );
t.strictEqual( isUint8Array( az ), true, 'returns expected value' );
t.strictEqual( az.buffer, z.buffer, 'returns expected value' );
t.strictEqual( sz, 1, 'returns expected value' );
}

function fallback() {
t.ok( false, 'called fallback' );
}
});

tape( 'the function returns a function which dispatches to an addon function when provided typed arrays (complex64)', function test( t ) {
var f;
var x;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2021 The Stdlib Authors.
* Copyright (c) 2024 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.
Expand All @@ -27,8 +27,10 @@ var noop = require( '@stdlib/utils/noop' );
var Float64Array = require( '@stdlib/array/float64' );
var Complex64Array = require( '@stdlib/array/complex64' );
var Complex128Array = require( '@stdlib/array/complex128' );
var BooleanArray = require( '@stdlib/array/bool' );
var isFloat32Array = require( '@stdlib/assert/is-float32array' );
var isFloat64Array = require( '@stdlib/assert/is-float64array' );
var isUint8Array = require( '@stdlib/assert/is-uint8array' );
var resolve = require( '@stdlib/strided/base/dtype-resolve-enum' );
var dispatch = require( './../lib/ndarray.js' );

Expand Down Expand Up @@ -256,6 +258,44 @@ tape( 'the function returns a function which dispatches to an addon function whe
}
});

tape( 'the function returns a function which dispatches to an addon function when provided typed arrays (bool)', function test( t ) {
var f;
var x;
var y;
var z;

f = dispatch( addon, fallback );

x = new BooleanArray( 2 );
y = new BooleanArray( x.length );
z = new BooleanArray( x.length );

f( x.length, 'bool', x, 1, 0, 'bool', y, 1, 0, 'bool', z, 1, 0 );

t.end();

function addon( N, dx, ax, sx, dy, ay, sy, dz, az, sz ) {
t.ok( true, 'called addon' );
t.strictEqual( N, x.length, 'returns expected value' );
t.strictEqual( dx, resolve( 'bool' ), 'returns expected value' );
t.strictEqual( isUint8Array( ax ), true, 'returns expected value' );
t.strictEqual( ax.buffer, x.buffer, 'returns expected value' );
t.strictEqual( sx, 1, 'returns expected value' );
t.strictEqual( dy, resolve( 'bool' ), 'returns expected value' );
t.strictEqual( isUint8Array( ay ), true, 'returns expected value' );
t.strictEqual( ay.buffer, y.buffer, 'returns expected value' );
t.strictEqual( sy, 1, 'returns expected value' );
t.strictEqual( dz, resolve( 'bool' ), 'returns expected value' );
t.strictEqual( isUint8Array( az ), true, 'returns expected value' );
t.strictEqual( az.buffer, z.buffer, 'returns expected value' );
t.strictEqual( sz, 1, 'returns expected value' );
}

function fallback() {
t.ok( false, 'called fallback' );
}
});

tape( 'the function returns a function which dispatches to an addon function when provided typed arrays (complex64)', function test( t ) {
var f;
var x;
Expand Down

1 comment on commit 082d2e9

@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
strided/base/binary-addon-dispatch $\color{green}506/506$
$\color{green}+100.00\%$
$\color{green}79/79$
$\color{green}+100.00\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{green}506/506$
$\color{green}+100.00\%$

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

Please # to comment.