-
-
Notifications
You must be signed in to change notification settings - Fork 813
/
Copy pathloops.js
801 lines (688 loc) · 19.4 KB
/
loops.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
#!/usr/bin/env node
/**
* @license Apache-2.0
*
* 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.
* 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.
*/
'use strict';
// MODULES //
var path = require( 'path' );
var logger = require( 'debug' );
var readDir = require( '@stdlib/fs/read-dir' ).sync;
var unlink = require( '@stdlib/fs/unlink' ).sync;
var readFile = require( '@stdlib/fs/read-file' ).sync;
var readJSON = require( '@stdlib/fs/read-json' ).sync;
var writeFile = require( '@stdlib/fs/write-file' ).sync;
var replace = require( '@stdlib/string/replace' );
var substringBefore = require( '@stdlib/string/substring-before' );
var substringAfter = require( '@stdlib/string/substring-after' );
var uppercase = require( '@stdlib/string/uppercase' );
var dtypes = require( '@stdlib/ndarray/dtypes' );
var mostlySafeCasts = require( '@stdlib/ndarray/mostly-safe-casts' );
var dtypeChar = require( '@stdlib/ndarray/base/dtype-char' );
var dtypeDesc = require( '@stdlib/ndarray/base/dtype-desc' );
var dtype2c = require( '@stdlib/ndarray/base/dtype2c' );
var char2dtype = require( '@stdlib/ndarray/base/char2dtype' );
var bytesPerElement = require( '@stdlib/ndarray/base/bytes-per-element' );
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
var numel = require( '@stdlib/ndarray/base/numel' );
var gscal = require( '@stdlib/blas/base/gscal' );
var filledarray = require( '@stdlib/array/filled' );
var currentYear = require( '@stdlib/time/current-year' );
var format = require( '@stdlib/string/format' );
// VARIABLES //
var FOPTS = {
'encoding': 'utf8'
};
// Logger:
var debug = logger( 'ndarray-assign-loops:script' );
// Get the current year:
var CURRENT_YEAR = currentYear().toString();
// Specify the copyright holder:
var COPYRIGHT = 'The Stdlib Authors';
// Templates:
var TMPL_HEADER = readFile( path.join( __dirname, 'templates', 'header.txt' ), FOPTS );
var TMPL_SOURCE = readFile( path.join( __dirname, 'templates', 'source.txt' ), FOPTS );
var TMPL_README = readFile( path.join( __dirname, 'templates', 'docs.txt' ), FOPTS );
// Output directories:
var INCLUDE_DIR = path.resolve( __dirname, '..', 'include', 'stdlib', 'ndarray', 'base', 'assign' );
var SRC_DIR = path.resolve( __dirname, '..', 'src' );
// Main header file:
var INCLUDE_MAIN = INCLUDE_DIR + '.h';
// Manifest file:
var MANIFEST = path.resolve( __dirname, '..', 'manifest.json' );
// README file:
var README = path.resolve( __dirname, '..', 'README.md' );
// Data types to exclude when generating loops:
var EXCLUDE_DTYPES = [ 'binary', 'generic', 'uint8c' ];
// Resolve a list of dtypes for which we want to create loops:
var DTYPES = filter( dtypes(), EXCLUDE_DTYPES );
// Define "special" loops, which cannot be readily generated according to standardized rules:
var SPECIAL_LOOPS = [
// Support callbacks which operate on signed integers, but whose return values are always positive and can be cast to unsigned integers of the same or greater bit width:
'i_u',
'k_t',
'k_u',
's_b',
's_t',
's_u'
];
// Hash containing C macro names:
var MACROS = {
'nocast': 'NOCAST',
'cast': 'CAST',
'fcast': 'CAST_FCN'
};
// Regular expression to test for a "loop" file:
var RE_LOOP_FILE = /^[a-z]_[a-z]\.(?:h|c)$/;
// Regular expression to test for a "loop" file in the manifest.json:
var RE_MANIFEST_LOOP_FILE = /\.\/src\/[a-z]_[a-z]\.c$/;
// Regular expression to match input and output array dtype characters:
var RE_SIGNATURE = /^([a-z])_([a-z])/;
// Specify array shapes:
var SHAPES = [
[],
[ 3 ],
[ 2, 2 ],
[ 2, 2, 2 ],
[ 1, 2, 2, 2 ],
[ 1, 1, 2, 2, 2 ],
[ 1, 1, 1, 2, 2, 2 ],
[ 1, 1, 1, 1, 2, 2, 2 ],
[ 1, 1, 1, 1, 1, 2, 2, 2 ],
[ 1, 1, 1, 1, 1, 1, 2, 2, 2 ],
[ 1, 1, 1, 1, 1, 1, 1, 2, 2, 2 ]
];
// Array order:
var ORDER = 'row-major';
// FUNCTIONS //
/**
* Removes a list of elements from a provided list.
*
* @private
* @param {ArrayLikeObject} src - list to filter
* @param {ArrayLikeObject} list - items to remove
* @returns {Array} filtered list
*
* @example
* var src = [ 'a', 'b', 'c', 'd' ];
* var list = [ 'b', 'd' ];
*
* var out = filter( src, list );
* // returns [ 'a', 'c' ]
*/
function filter( src, list ) {
var out;
var M;
var N;
var v;
var i;
var j;
M = src.length;
N = list.length;
out = [];
for ( i = 0; i < M; i++ ) {
v = src[ i ];
for ( j = 0; j < N; j++ ) {
if ( v === list[ j ] ) {
break;
}
}
if ( j === N ) {
out.push( v );
}
}
return out;
}
/**
* Removes loop files from a directory.
*
* @private
* @param {string} dir - directory
*/
function removeLoopFiles( dir ) {
var list;
var i;
list = readDir( dir );
for ( i = 0; i < list.length; i++ ) {
if ( RE_LOOP_FILE.test( list[ i ] ) ) {
unlink( path.join( dir, list[ i ] ) );
}
}
}
/**
* Generates a list of loop signatures from a list of data types.
*
* @private
* @param {StringArray} dtypes - list of data types
* @returns {StringArray} list of loop signatures
*/
function signatures( dtypes ) {
var casts;
var out;
var ch1;
var ch2;
var t1;
var t2;
var N;
var s;
var i;
var j;
N = dtypes.length;
// Generate the list of signatures:
out = [];
for ( i = 0; i < N; i++ ) {
t1 = dtypes[ i ];
// Resolve single-letter dtype abbreviation:
ch1 = dtypeChar( t1 );
// Generate the input/output array signature:
s = ch1+'_'+ch1; // e.g., d_d
out.push( s );
// Resolve the list of (mostly) safe casts for the input dtype:
casts = mostlySafeCasts( t1 );
// Remove the excluded dtypes:
casts = filter( casts, EXCLUDE_DTYPES );
// Generate signatures for allowed casts:
for ( j = 0; j < casts.length; j++ ) {
t2 = casts[ j ];
if ( t2 === t1 ) {
continue;
}
ch2 = dtypeChar( t2 );
s = ch1+'_'+ch2;
out.push( s ); // e.g., `b_i`
}
}
// Append any special loops:
for ( i = 0; i < SPECIAL_LOOPS.length; i++ ) {
out.push( SPECIAL_LOOPS[ i ] );
}
return out.sort();
}
/**
* Defines byte arrays in a provided template string.
*
* @private
* @param {string} tmpl - template string
* @param {PositiveInteger} nb1 - bytes per element for input array
* @param {PositiveInteger} nb2 - bytes per element for output array
* @returns {string} updated string
*/
function defineByteArrays( tmpl, nb1, nb2 ) {
var bytes;
var tmp;
var N;
var i;
bytes = filledarray( 0, nb1, 'generic' ).join( ', ' );
tmpl = replace( tmpl, '{{INPUT_NDARRAY_1_BYTES_0D}}', bytes );
bytes = filledarray( 0, nb2, 'generic' ).join( ', ' );
tmpl = replace( tmpl, '{{OUTPUT_NDARRAY_BYTES_0D}}', bytes );
for ( i = 1; i < SHAPES.length; i++ ) {
N = numel( SHAPES[ i ] );
tmp = '{{INPUT_NDARRAY_1_BYTES_'+i+'D}}';
bytes = filledarray( 0, nb1*N, 'generic' ).join( ', ' );
tmpl = replace( tmpl, tmp, bytes );
tmp = '{{OUTPUT_NDARRAY_BYTES_'+i+'D}}';
bytes = filledarray( 0, nb2*N, 'generic' ).join( ', ' );
tmpl = replace( tmpl, tmp, bytes );
}
return tmpl;
}
/**
* Defines array strides in a provided template string.
*
* @private
* @param {string} tmpl - template string
* @param {PositiveInteger} nb1 - bytes per element for input array
* @param {PositiveInteger} nb2 - bytes per element for output array
* @returns {string} updated string
*/
function defineStrides( tmpl, nb1, nb2 ) {
var strides;
var tmp;
var st;
var i;
for ( i = 1; i < SHAPES.length; i++ ) {
strides = shape2strides( SHAPES[ i ], ORDER );
tmp = '{{INPUT_NDARRAY_1_STRIDES_'+i+'D}}';
st = gscal( strides.length, nb1, strides.slice(), 1 );
tmpl = replace( tmpl, tmp, st.join( ', ' ) );
tmp = '{{OUTPUT_NDARRAY_STRIDES_'+i+'D}}';
st = gscal( strides.length, nb2, strides.slice(), 1 );
tmpl = replace( tmpl, tmp, st.join( ', ' ) );
}
return tmpl;
}
/**
* Defines array shapes in a provided template string.
*
* @private
* @param {string} tmpl - template string
* @returns {string} updated string
*/
function defineShapes( tmpl ) {
var i;
for ( i = 1; i < SHAPES.length; i++ ) {
tmpl = replace( tmpl, '{{NDARRAY_SHAPE_'+i+'D}}', SHAPES[ i ].join( ', ' ) );
}
return tmpl;
}
/**
* Creates a header file for a provided loop signature.
*
* @private
* @param {string} signature - loop signature
* @throws {Error} unexpected error
*/
function createHeaderFile( signature ) {
var fpath;
var file;
var err;
file = replace( TMPL_HEADER, '{{YEAR}}', CURRENT_YEAR );
file = replace( file, '{{COPYRIGHT}}', COPYRIGHT );
file = replace( file, '{{INCLUDE_GUARD}}', uppercase( signature ) );
file = replace( file, '{{SIGNATURE}}', signature );
fpath = path.join( INCLUDE_DIR, signature+'.h' );
debug( 'Creating header file: %s', fpath );
err = writeFile( fpath, file, FOPTS );
if ( err ) {
throw err;
}
}
/**
* Creates header files for a list of loop signatures.
*
* @private
* @param {StringArray} signatures - list of loop signatures
*/
function createHeaderFiles( signatures ) {
var i;
for ( i = 0; i < signatures.length; i++ ) {
createHeaderFile( signatures[ i ] );
}
}
/**
* Creates a source file for a provided loop signature.
*
* @private
* @param {string} signature - loop signature
* @throws {Error} unexpected error
*/
function createSourceFile( signature ) {
var match1;
var macro;
var fpath;
var file;
var args;
var err;
var inc;
var tmp;
var ch1;
var ch2;
var ct1;
var ct2;
var nb1;
var nb2;
var t1;
var t2;
file = replace( TMPL_SOURCE, '{{YEAR}}', CURRENT_YEAR );
file = replace( file, '{{COPYRIGHT}}', COPYRIGHT );
file = replace( file, '{{SIGNATURE}}', signature );
// Ensure the appropriate header files are included in source files:
inc = [];
if ( /c/.test( signature ) ) {
inc.push( '#include "stdlib/complex/float32/ctor.h"' );
}
if ( /z/.test( signature ) ) {
inc.push( '#include "stdlib/complex/float64/ctor.h"' );
}
if ( /x/.test( signature ) ) {
inc.push( '#include <stdbool.h>' );
}
if ( inc.length ) {
file = replace( file, '{{INCLUDES}}', '\n'+inc.join( '\n' ) );
} else {
file = replace( file, '{{INCLUDES}}', '' );
}
// Ensure the appropriate header files are included in source documentation examples:
file = replace( file, '{{EXAMPLE_INCLUDES}}', '' );
// Resolve the array data types:
match1 = signature.match( RE_SIGNATURE );
ch1 = match1[ 1 ];
t1 = char2dtype( ch1 );
ch2 = match1[ 2 ];
t2 = char2dtype( ch2 );
// Define array data types:
file = replace( file, '{{INPUT_NDARRAY_1_DTYPE_UPPER}}', uppercase( t1 ) );
file = replace( file, '{{OUTPUT_NDARRAY_DTYPE_UPPER}}', uppercase( t2 ) );
file = replace( file, '{{INPUT_NDARRAY_1_DTYPE_LOWER}}', t1 );
file = replace( file, '{{OUTPUT_NDARRAY_DTYPE_LOWER}}', t2 );
// Define the input array C data type:
file = replace( file, '{{INPUT_NDARRAY_1_CTYPE}}', dtype2c( t1 ) );
// Define the number of bytes per element for the respective arrays:
nb1 = bytesPerElement( t1 );
file = replace( file, '{{INPUT_NDARRAY_1_BYTES_PER_ELEMENT}}', nb1.toString() );
nb2 = bytesPerElement( t2 );
file = replace( file, '{{OUTPUT_NDARRAY_BYTES_PER_ELEMENT}}', nb2.toString() );
// Define the array shapes:
file = defineShapes( file );
// Define underlying byte arrays:
file = defineByteArrays( file, nb1, nb2 );
// Define array strides:
file = defineStrides( file, nb1, nb2 );
// Resolve C data types:
ct1 = dtype2c( t1 );
ct2 = dtype2c( t2 );
// Resolve the 0D expression:
if ( /[cz]/.test( signature ) ) {
if ( ch1 === ch2 ) { // e.g., z_z, c_c
tmp = 'v';
} else if ( ch2 === 'z' ) { // e.g., c_z, d_z, u_z
tmp = format( 'stdlib_complex128_from_%s( v )', char2dtype( ch1 ) );// e.g., stdlib_complex128_from_float64( v )
} else if ( ch2 === 'c' ) {
if ( ch1 === 'z' ) { // e.g., z_c
tmp = 'stdlib_complex128_to_complex64( v )';
} else { // e.g., f_c
tmp = format( 'stdlib_complex64_from_%s( v )', char2dtype( ch1 ) );
}
} else { // e.g., z_d, c_f
// Based on type promotion rules and conventions, we shouldn't reach here.
}
} else if ( ch1 === ch2 ) { // e.g., d_d, f_f, b_b
tmp = 'v';
} else { // e.g., b_d, f_d
tmp = format( '(%s)v', ct2 ); // e.g., (double)v
}
file = replace( file, '{{EXPRESSION_0D}}', tmp );
// Resolve the loop macro:
if ( /[cz]/.test( signature ) ) {
// E.g., z_z, c_c, c_z, f_c, d_z, u_z, #_(c|z), etc.
if ( ch1 === ch2 ) { // e.g., z_z, c_c
macro = MACROS.nocast;
args = [ ct1, ct2 ];
} else if ( ch2 === 'z' ) { // e.g., c_z, d_z, u_z
macro = MACROS.fcast;
args = [ ct1, ct2, 'stdlib_complex128_from_'+char2dtype( ch1 ) ];
} else if ( ch2 === 'c' ) {
macro = MACROS.fcast;
args = [ ct1, ct2 ];
if ( ch1 === 'z' ) { // e.g., z_c
args.push( 'stdlib_complex128_to_complex64' );
} else { // e.g., f_c
args.push( 'stdlib_complex64_from_'+char2dtype( ch1 ) );
}
} else { // e.g., z_d
throw new Error( 'unexpected error. Should not reach this branch according to type promotion rules.' );
}
} else {
macro = MACROS.cast;
args = [ ct1, ct2 ];
}
file = replace( file, '{{LOOP_MACRO}}', macro );
file = replace( file, '{{LOOP_MACRO_ARGUMENTS}}', args.join( ', ' ) );
// Create the source file:
fpath = path.join( SRC_DIR, signature+'.c' );
debug( 'Creating source file: %s', fpath );
err = writeFile( fpath, file, FOPTS );
if ( err ) {
throw err;
}
}
/**
* Creates source files for a list of loop signatures.
*
* @private
* @param {StringArray} signatures - list of loop signatures
*/
function createSourceFiles( signatures ) {
var i;
for ( i = 0; i < signatures.length; i++ ) {
createSourceFile( signatures[ i ] );
}
}
/**
* Generates README documentation for a loop interface.
*
* @private
* @param {string} signature - loop signature
* @returns {string} documentation
*/
function createDoc( signature ) {
var match;
var doc;
var ch1;
var ch2;
var nb1;
var nb2;
var t1;
var t2;
doc = replace( TMPL_README, '{{SIGNATURE}}', signature );
// Ensure appropriate header files are included in documentation examples:
doc = replace( doc, '{{EXAMPLE_INCLUDES}}', '' );
// Resolve the array data types:
match = signature.match( RE_SIGNATURE );
ch1 = match[ 1 ];
t1 = char2dtype( ch1 );
ch2 = match[ 2 ];
t2 = char2dtype( ch2 );
// Define array data types:
doc = replace( doc, '{{INPUT_NDARRAY_1_DTYPE_UPPER}}', uppercase( t1 ) );
doc = replace( doc, '{{OUTPUT_NDARRAY_DTYPE_UPPER}}', uppercase( t2 ) );
// Define the number of bytes per element for the respective arrays:
nb1 = bytesPerElement( t1 );
doc = replace( doc, '{{INPUT_NDARRAY_1_BYTES_PER_ELEMENT}}', nb1.toString() );
nb2 = bytesPerElement( t2 );
doc = replace( doc, '{{OUTPUT_NDARRAY_BYTES_PER_ELEMENT}}', nb2.toString() );
// Define the array shapes:
doc = defineShapes( doc );
// Define underlying byte arrays:
doc = defineByteArrays( doc, nb1, nb2 );
// Define array strides:
doc = defineStrides( doc, nb1, nb2 );
return doc;
}
/**
* Returns a character code list item.
*
* @private
* @param {string} dtype - data type
* @returns {string} list item
*/
function charCodeListItem( dtype ) {
return [
'- **',
dtypeChar( dtype ),
'**: `',
dtype,
'` (',
dtypeDesc( dtype ),
').'
].join( '' );
}
/**
* Returns a list of character code descriptions.
*
* @private
* @param {StringArray} dtypes - list of data types
* @returns {StringArray} list of character code descriptions
*/
function charCodeList( dtypes ) {
var out;
var i;
out = [];
for ( i = 0; i < dtypes.length; i++ ) {
out.push( charCodeListItem( dtypes[ i ] ) );
}
return out;
}
/**
* Update the package README.
*
* @private
* @param {StringArray} signatures - list of (sorted) loop signatures
* @throws {Error} unexpected error
*/
function updateREADME( signatures ) {
var parts;
var file;
var docs;
var out;
var err;
var i;
file = readFile( README, FOPTS );
if ( file instanceof Error ) {
throw file;
}
docs = [];
for ( i = 0; i < signatures.length; i++ ) {
docs.push( createDoc( signatures[ i ] ) );
}
out = [];
parts = file.split( '\n<!-- charcodes -->' );
out.push( parts[ 0 ] );
out.push( '<!-- charcodes -->' );
out.push( '' );
out.push( charCodeList( DTYPES ).join( '\n' ) );
out.push( '' );
parts = parts[ 1 ].split( '<!-- ./charcodes -->\n' );
out.push( '<!-- ./charcodes -->' );
parts = parts[ 1 ].split( '\n<!-- loops -->' );
out.push( parts[ 0 ] );
out.push( '<!-- loops -->' );
out.push( '' );
out.push( docs.join( '\n' ) );
parts = parts[ 1 ].split( '<!-- ./loops -->\n' );
out.push( '<!-- ./loops -->' );
out.push( parts[ 1 ] );
err = writeFile( README, out.join( '\n' ), FOPTS );
if ( err ) {
throw err;
}
}
/**
* Updates the main header file.
*
* @private
* @param {StringArray} signatures - list of (sorted) loop signatures
* @throws {Error} unexpected error
*/
function updateMainHeader( signatures ) {
var file;
var list;
var err;
var sig;
var ch;
var i;
file = readFile( INCLUDE_MAIN, FOPTS );
if ( file instanceof Error ) {
throw file;
}
list = [];
ch = signatures[ 0 ].charAt( 0 );
for ( i = 0; i < signatures.length; i++ ) {
sig = signatures[ i ];
if ( sig.charAt( 0 ) !== ch ) {
ch = sig.charAt( 0 );
list.push( '' );
}
list.push( '#include "assign/'+sig+'.h"' );
}
file = [
substringBefore( file, '\n// BEGIN LOOPS' ),
'// BEGIN LOOPS',
list.join( '\n' ),
'// END LOOPS',
substringAfter( file, '// END LOOPS\n' )
].join( '\n' );
err = writeFile( INCLUDE_MAIN, file, FOPTS );
if ( err ) {
throw err;
}
}
/**
* Updates the package manifest.
*
* @private
* @param {StringArray} signatures - list of (sorted) loop signatures
* @throws {Error} unexpected error
*/
function updateManifest( signatures ) {
var file;
var list;
var tmp;
var err;
var l;
var i;
var j;
file = readJSON( MANIFEST, FOPTS );
if ( file instanceof Error ) {
throw file;
}
list = [];
for ( i = 0; i < signatures.length; i++ ) {
list.push( './src/'+signatures[ i ]+'.c' );
}
for ( j = 0; j < file.confs.length; j++ ) {
l = list.slice();
// Copy over non-signature source files...
tmp = file.confs[ j ].src;
for ( i = 0; i < tmp.length; i++ ) {
if ( RE_MANIFEST_LOOP_FILE.test( tmp[ i ] ) === false ) {
l.push( tmp[ i ] );
}
}
// Replace the list of source files:
file.confs[ j ].src = l;
}
err = writeFile( MANIFEST, JSON.stringify( file, null, 2 )+'\n', FOPTS );
if ( err ) {
throw err;
}
}
// MAIN //
/**
* Main execution sequence.
*
* @private
*/
function main() {
var sigs;
debug( 'Data types: %s', DTYPES.join( ', ' ) );
// Generate the list of loop signatures:
sigs = signatures( DTYPES );
debug( 'Signatures: %s', '\n'+sigs.join( '\n' ) );
// Remove loop files from output directories:
debug( 'Clearing include directory: %s', INCLUDE_DIR );
removeLoopFiles( INCLUDE_DIR );
debug( 'Clearing source directory: %s', SRC_DIR );
removeLoopFiles( SRC_DIR );
// Create header files for the list of loop signatures:
debug( 'Creating header files...' );
createHeaderFiles( sigs );
// Create source files for the list of loop signatures:
debug( 'Creating source files...' );
createSourceFiles( sigs );
// Update the main header file to include the loop header files:
debug( 'Updating main header file: %s', INCLUDE_MAIN );
updateMainHeader( sigs );
// Update the package manifest to include the loop source files:
debug( 'Updating manifest file: %s', MANIFEST );
updateManifest( sigs );
// Update the package README to include the loop interfaces:
debug( 'Updating README file: %s', README );
updateREADME( sigs );
debug( 'Finished.' );
}
main();