Skip to content

Commit 39bf63d

Browse files
committed
refactor: update scripts
1 parent 32865ab commit 39bf63d

File tree

2 files changed

+147
-75
lines changed

2 files changed

+147
-75
lines changed

lib/node_modules/@stdlib/_tools/scaffold/math-special-unary/scripts/generate_files.js

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@
2424

2525
var join = require( 'path' ).join;
2626
var writeFileSync = require( '@stdlib/fs/write-file' ).sync;
27-
var mkdirSync = require( 'fs' ).mkdirSync;
2827
var licenseHeader = require( '@stdlib/_tools/licenses/header' );
2928
var currentYear = require( '@stdlib/time/current-year' );
3029
var capitalize = require( '@stdlib/string/capitalize' );
3130
var objectKeys = require( '@stdlib/utils/keys' );
3231
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
3332
var str2enum = require( '@stdlib/ndarray/base/dtype-str2enum' );
34-
var generateMatchesTable = require( './generate_matches_table.js' );
33+
var generateMatchesTable = require( './script.js' );
34+
var pkg = require( './../package.json' );
3535

3636

3737
// FUNCTIONS //
@@ -413,10 +413,8 @@ function generateAddonFile( matches, header, basePkg ) {
413413
* Main execution function.
414414
*
415415
* @private
416-
* @param {string} destDir - destination directory
417-
* @param {string} funcAlias - function alias
418416
*/
419-
function main( destDir, funcAlias ) {
417+
function main() {
420418
var filteredMatches;
421419
var typesJsonOut;
422420
var metaOut;
@@ -428,16 +426,8 @@ function main( destDir, funcAlias ) {
428426
var cOut;
429427
var i;
430428

431-
// Ensure directories exist:
432-
try {
433-
mkdirSync( join( destDir, 'lib' ), { 'recursive': true } );
434-
mkdirSync( join( destDir, 'src' ), { 'recursive': true } );
435-
} catch ( err ) {
436-
// Ignore errors if directories already exist
437-
}
438-
439429
// Generate and filter matches table:
440-
matches = generateMatchesTable( funcAlias );
430+
matches = generateMatchesTable();
441431

442432
// Filter out generic types for addon.c:
443433
filteredMatches = [];
@@ -448,47 +438,47 @@ function main( destDir, funcAlias ) {
448438
}
449439

450440
// Extract package information:
451-
basePkg = funcAlias;
441+
basePkg = pkg.name.split( '/' ).pop();
452442

453443
// Generate license header:
454444
header = licenseHeader( 'Apache-2.0', 'js', {
455445
'year': currentYear(),
456446
'copyright': 'The Stdlib Authors'
457447
});
458-
header += '\n/* THIS IS A GENERATED FILE. DO NOT EDIT DIRECTLY. */\n';
448+
header += '\n/* This is a generated file. Do not edit directly. */\n';
459449

460450
// Generate types.js:
461451
jsOut = generateTypesFile( matches, header );
462-
writeFileSync( join( destDir, 'lib/types.js' ), jsOut, {
452+
writeFileSync( join( __dirname, '../lib/types.js' ), jsOut, {
463453
'encoding': 'utf8'
464454
});
465455

466456
// Generate types.json:
467457
typesJsonOut = generateTypesJsonFile( matches );
468-
writeFileSync( join( destDir, 'lib/types.json' ), typesJsonOut, {
458+
writeFileSync( join( __dirname, '../lib/types.json' ), typesJsonOut, {
469459
'encoding': 'utf8'
470460
});
471461

472462
// Generate meta.json:
473463
metaOut = generateMetaFile();
474-
writeFileSync( join( destDir, 'lib/meta.json' ), metaOut, {
464+
writeFileSync( join( __dirname, '../lib/meta.json' ), metaOut, {
475465
'encoding': 'utf8'
476466
});
477467

478468
// Generate data.js:
479469
dataOut = generateDataFile( matches, header );
480-
writeFileSync( join( destDir, 'lib/data.js' ), dataOut, {
470+
writeFileSync( join( __dirname, '../lib/data.js' ), dataOut, {
481471
'encoding': 'utf8'
482472
});
483473

484474
// Generate addon.c:
485475
cOut = generateAddonFile( filteredMatches, header, basePkg );
486-
writeFileSync( join( destDir, 'src/addon.c' ), cOut, {
476+
writeFileSync( join( __dirname, '../src/addon.c' ), cOut, {
487477
'encoding': 'utf8'
488478
});
489479
}
490480

491481

492-
// EXPORTS //
482+
// MAIN //
493483

494-
module.exports = main;
484+
main();
Lines changed: 134 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,21 @@
2222

2323
// MODULES //
2424

25+
var join = require( 'path' ).join;
26+
var readJSON = require( '@stdlib/fs/read-json' ).sync;
2527
var db = require( '@stdlib/math/special/data/unary_function_database.json' );
2628
var scaffoldDatabase = require( '@stdlib/math/special/data/unary.json' );
2729
var dtypes = require( '@stdlib/ndarray/dtypes' );
2830
var dtypeChar = require( '@stdlib/ndarray/base/dtype-char' );
2931
var mostlySafeCasts = require( '@stdlib/ndarray/mostly-safe-casts' );
3032
var promotionRules = require( '@stdlib/ndarray/promotion-rules' );
31-
// Generate list of available ndarray kernels from source file names
32-
var fs = require( 'fs' );
33-
var path = require( 'path' );
34-
var NDARRAY_KERNELS = (function() {
35-
var srcDir = path.join( __dirname, '../../../../../../../lib/node_modules/@stdlib/ndarray/base/unary/src' );
36-
var files = fs.readdirSync( srcDir );
37-
var kernels = [];
38-
39-
for ( var i = 0; i < files.length; i++ ) {
40-
var file = files[ i ];
41-
if ( file.endsWith( '.c' ) && !file.startsWith( 'internal' ) ) {
42-
// Extract function name from file name (e.g., 'd_d.c' -> 'stdlib_ndarray_d_d')
43-
var baseName = file.replace( '.c', '' );
44-
kernels.push( 'stdlib_ndarray_' + baseName );
45-
}
46-
}
47-
return kernels;
48-
})();
33+
34+
35+
// VARIABLES //
36+
37+
var pkgPath = join( __dirname, '..', 'package.json' );
38+
var pkg = readJSON( pkgPath );
39+
var basePkg = pkg.name.split( '/' ).pop();
4940

5041

5142
// FUNCTIONS //
@@ -58,20 +49,36 @@ var NDARRAY_KERNELS = (function() {
5849
* @param {string} outputDtype - output dtype
5950
* @param {string} inputDtype - input dtype
6051
* @returns {string} scalar kernel name
52+
*
53+
* @example
54+
* var kernels = {
55+
* 'float64': '@stdlib/math/base/special/floor',
56+
* 'float32': '@stdlib/math/base/special/floorf',
57+
* 'generic': '@stdlib/math/base/special/floor'
58+
* };
59+
* var kernel = selectScalarKernel( kernels, 'float64', 'float32' );
60+
* // returns '@stdlib/math/base/special/floor'
6161
*/
6262
function selectScalarKernel( scalarKernels, outputDtype, inputDtype ) {
6363
var higherPrecisionDtype;
6464
var scalarKernel;
6565

66+
/*
67+
* For example:
68+
*
69+
* inputDtype = 'complex64'
70+
* outputDtype = 'float32'
71+
*/
72+
6673
if ( inputDtype === 'generic' ) {
6774
scalarKernel = scalarKernels[ outputDtype ] || scalarKernels.generic;
6875
} else if ( outputDtype === 'generic' ) {
6976
// Using the function appropriate for the input dtype, in case of generic output:
7077
scalarKernel = scalarKernels[ inputDtype ] || scalarKernels.generic;
7178
} else {
7279
// Determine which dtype has higher priority using promotion rules:
73-
higherPrecisionDtype = promotionRules( inputDtype, outputDtype );
74-
scalarKernel = scalarKernels[ higherPrecisionDtype ];
80+
higherPrecisionDtype = promotionRules( inputDtype, outputDtype ); // promotionRules( 'complex64', 'float32' ) => 'complex64'
81+
scalarKernel = scalarKernels[ higherPrecisionDtype ]; // scalarKernels[ 'complex64' ] => '@stdlib/math/base/special/cfloorf'
7582
if ( !scalarKernel ) {
7683
scalarKernel = scalarKernels.generic;
7784
}
@@ -87,36 +94,38 @@ function selectScalarKernel( scalarKernels, outputDtype, inputDtype ) {
8794
* @param {string} inputDtype - input dtype
8895
* @param {string} outputDtype - output dtype
8996
* @param {string} scalarKernel - scalar kernel name
90-
* @param {Array} kernelList - list of available kernels
9197
* @returns {string} kernel name
98+
*
99+
* @example
100+
* var kernel = selectKernelName( 'float32', 'float64', '@stdlib/math/base/special/abs' );
101+
* // returns 'stdlib_ndarray_f_d_as_d_d'
102+
*
103+
* @example
104+
* var kernel = selectKernelName( 'float64', 'float32', '@stdlib/math/base/special/absf' );
105+
* // returns 'stdlib_ndarray_d_f_as_f_f'
92106
*/
93-
function selectKernelName( inputDtype, outputDtype, scalarKernel, kernelList ) {
107+
function selectKernelName( inputDtype, outputDtype, scalarKernel ) {
94108
var scalarKernelOutputDtype;
95109
var scalarKernelInputDtype;
96110
var ndarrayKernel;
97-
var scaffoldEntry;
98-
99-
// Check if the scalar kernel exists in the scaffold database
100-
scaffoldEntry = scaffoldDatabase[ scalarKernel ];
101-
if ( !scaffoldEntry || !scaffoldEntry.parameters || scaffoldEntry.parameters.length === 0 ) {
102-
// Fallback: use input and output dtypes directly
103-
scalarKernelInputDtype = inputDtype;
104-
scalarKernelOutputDtype = outputDtype;
105-
} else {
106-
scalarKernelInputDtype = scaffoldEntry.parameters[ 0 ].type.dtype;
107-
scalarKernelOutputDtype = scaffoldEntry.returns.type.dtype;
108-
}
111+
112+
/*
113+
* For example:
114+
*
115+
* inputDtype = 'complex64'
116+
* outputDtype = 'float32'
117+
* scalarKernel = '@stdlib/math/base/special/cfloorf'
118+
*/
119+
120+
scalarKernelInputDtype = scaffoldDatabase[ scalarKernel ].parameters[ 0 ].type.dtype; // scaffoldDatabase[ '@stdlib/math/base/special/cfloorf' ].parameters[ 0 ].type.dtype => 'complex64'
121+
scalarKernelOutputDtype = scaffoldDatabase[ scalarKernel ].returns.type.dtype; // scaffoldDatabase[ '@stdlib/math/base/special/cfloorf' ].returns.type.dtype => 'complex64'
109122

110123
// Exact match:
111124
if ( inputDtype === scalarKernelInputDtype && outputDtype === scalarKernelOutputDtype ) {
112125
ndarrayKernel = 'stdlib_ndarray_' + dtypeChar( inputDtype ) + '_' + dtypeChar( outputDtype );
113126
} else {
114127
// Not an exact match:
115-
ndarrayKernel = 'stdlib_ndarray_'+dtypeChar(inputDtype)+'_'+dtypeChar(outputDtype)+'_as_'+dtypeChar(scalarKernelInputDtype)+'_'+dtypeChar(scalarKernelOutputDtype);
116-
}
117-
118-
if ( kernelList.indexOf( ndarrayKernel ) === -1 ) {
119-
return;
128+
ndarrayKernel = 'stdlib_ndarray_'+dtypeChar(inputDtype)+'_'+dtypeChar(outputDtype)+'_as_'+dtypeChar(scalarKernelInputDtype)+'_'+dtypeChar(scalarKernelOutputDtype); // => 'stdlib_ndarray_c_f_as_c_c'
120129
}
121130

122131
return ndarrayKernel;
@@ -129,10 +138,9 @@ function selectKernelName( inputDtype, outputDtype, scalarKernel, kernelList ) {
129138
* Main function to generate dtype mappings.
130139
*
131140
* @private
132-
* @param {string} funcAlias - function alias
133141
* @returns {Array} array of mappings
134142
*/
135-
function main( funcAlias ) {
143+
function main() {
136144
var needsPromotion;
137145
var cFunctionName;
138146
var allowedCasts;
@@ -152,17 +160,55 @@ function main( funcAlias ) {
152160
mappings = [];
153161

154162
// Get scalar kernels and configuration for this function:
155-
obj = db[ funcAlias ];
156-
if ( !obj ) {
157-
throw new Error( 'Function alias not found in database: ' + funcAlias );
158-
}
163+
obj = db[ basePkg ];
164+
165+
/*
166+
* obj = {
167+
* "input_dtypes": "numeric_and_generic",
168+
* "output_dtypes": "numeric_and_generic",
169+
* "output_policy": "same",
170+
* "excluded_dtypes": [ "float16", "uint8c", "complex32" ],
171+
* "scalar_kernels": {
172+
* "int8": "@stdlib/number/int8/base/identity",
173+
* "int16": "@stdlib/number/int16/base/identity",
174+
* "int32": "@stdlib/number/int32/base/identity",
175+
* "uint8": "@stdlib/number/uint8/base/identity",
176+
* "uint16": "@stdlib/number/uint16/base/identity",
177+
* "uint32": "@stdlib/number/uint32/base/identity",
178+
* "float32": "@stdlib/math/base/special/floorf",
179+
* "float64": "@stdlib/math/base/special/floor",
180+
* "complex64": "@stdlib/math/base/special/cfloorf",
181+
* "complex128": "@stdlib/math/base/special/cfloor",
182+
* "generic": "@stdlib/math/base/special/floor"
183+
* }
184+
* }
185+
*/
159186

160187
// Get input dtypes:
161188
idt = dtypes( obj.input_dtypes );
162189

163190
// Get output dtypes:
164191
odt = dtypes( obj.output_dtypes );
165192

193+
/*
194+
* idt = [
195+
* 'complex32',
196+
* 'complex64',
197+
* 'complex128',
198+
* 'float16',
199+
* 'float32',
200+
* 'float64',
201+
* 'int16',
202+
* 'int32',
203+
* 'int8',
204+
* 'uint16',
205+
* 'uint32',
206+
* 'uint8',
207+
* 'uint8c',
208+
* 'generic'
209+
* ]
210+
*/
211+
166212
// Filter out excluded dtypes:
167213
filtered = [];
168214
for ( i = 0; i < idt.length; i++ ) {
@@ -172,15 +218,48 @@ function main( funcAlias ) {
172218
}
173219
idt = filtered;
174220

221+
/*
222+
* idt = [
223+
* 'complex64',
224+
* 'complex128',
225+
* 'float32',
226+
* 'float64',
227+
* 'int16',
228+
* 'int32',
229+
* 'int8',
230+
* 'uint16',
231+
* 'uint32',
232+
* 'uint8',
233+
* 'generic'
234+
* ]
235+
*/
236+
175237
// Generate all mostly-safe cast combinations:
176238
for ( i = 0; i < idt.length; i++ ) {
177239
inputDtype = idt[ i ];
178240

241+
/*
242+
* For the first iteration:
243+
*
244+
* inputDtype = 'complex64'
245+
*/
246+
179247
if ( inputDtype === 'generic' ) {
180248
allowedCasts = [ 'generic' ];
181249
} else {
182250
// Get all dtypes this input can be mostly-safely cast to:
183251
allowedCasts = mostlySafeCasts( inputDtype );
252+
253+
/*
254+
* For the first iteration:
255+
*
256+
* allowedCasts = [
257+
* 'complex64',
258+
* 'complex128',
259+
* 'float32',
260+
* 'float64'
261+
* ]
262+
*/
184263
}
185264

186265
// Remove the dtypes which are not allowed for output, according to the output policy:
@@ -192,23 +271,26 @@ function main( funcAlias ) {
192271
}
193272
allowedCasts = filtered;
194273

274+
// console.log( 'allowedCasts for input dtype %s: %s', inputDtype, allowedCasts );
275+
195276
for ( j = 0; j < allowedCasts.length; j++ ) {
196277
outputDtype = allowedCasts[ j ];
197278

198279
if ( obj.excluded_dtypes.indexOf( outputDtype ) !== -1 ) {
199280
continue;
200281
}
201282

283+
/*
284+
* For example:
285+
*
286+
* outputDtype = 'float32'
287+
*/
288+
202289
// Get scalar kernel for this dtype combination:
203-
scalarKernel = selectScalarKernel( obj.scalar_kernels, outputDtype, inputDtype );
290+
scalarKernel = selectScalarKernel( obj.scalar_kernels, outputDtype, inputDtype ); // selectScalarKernel( kernels, 'float32', 'complex64' ) => '@stdlib/math/base/special/cfloorf'
204291

205292
// Generate ndarray kernel name:
206-
kernelName = selectKernelName( inputDtype, outputDtype, scalarKernel, NDARRAY_KERNELS );
207-
208-
// Skip if no kernel available:
209-
if ( !kernelName ) {
210-
continue;
211-
}
293+
kernelName = selectKernelName( inputDtype, outputDtype, scalarKernel ); // selectKernelName( 'complex64', 'float32', '@stdlib/math/base/special/cfloorf' ) => 'stdlib_ndarray_c_f_as_c_c'
212294

213295
// Generate mapping:
214296
needsPromotion = inputDtype !== outputDtype;

0 commit comments

Comments
 (0)