All files main.js

100% Statements 155/155
100% Branches 26/26
100% Functions 1/1
100% Lines 155/155

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 1561x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 17x 17x 17x 29x 9x 9x 29x 29x 29x 29x 7x 7x 7x 7x 22x 29x 9x 9x 6x 6x 9x 9x 9x 9x 9x 13x 13x 13x 29x 6x 6x 3006x 3006x 2505x 2505x 3006x 3006x 3000x 3000x 3006x 6x 6x 6x 6x 6x 6x 6x 7x 7x 29x 6x 6x 7x 29x 22x 22x 15x 15x 22x 22x 14x 14x 22x 22x 7x 29x 37x 37x 7x 29x 6x 6x 7x 7x 7x 7x 7x 29x 1x 1x 1x 1x 1x  
/**
* @license Apache-2.0
*
* Copyright (c) 2022 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 join = require( 'path' ).join;
var readFile = require( '@stdlib/fs/read-file' ).sync;
var replace = require( '@stdlib/string/replace' );
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
var uppercase = require( '@stdlib/string/base/uppercase' );
 
 
// VARIABLES //
 
var opts = {
	'encoding': 'utf8'
};
var dir = join( __dirname, 'templates' );
 
// Templates:
var SINGLE_COEFFICIENT_TEMPLATE = readFile( join( dir, 'single_coefficient.c.txt' ), opts ); // eslint-disable-line id-length
var EVALPOLY_TEMPLATE = readFile( join( dir, 'evalpoly.c.txt' ), opts );
var EMPTY_TEMPLATE = readFile( join( dir, 'empty.c.txt' ), opts );
var LOOP_TEMPLATE = readFile( join( dir, 'loop.c.txt' ), opts );
 
 
// MAIN //
 
/**
* Compiles a C function string for evaluating a polynomial.
*
* @param {NumericArray} c - polynomial coefficients sorted in ascending degree
* @param {Options} [options] - function options
* @param {string} [options.dtype='double'] - input value floating-point data type
* @param {string} [options.name='evalpoly'] - function name
* @returns {string} function string for evaluating a polynomial
*
* @example
* var str = compile( [ 3.0, 2.0, 1.0 ] );
* // returns <string>
*/
function compile( c, options ) {
	var horner;
	var opts;
	var str;
	var n;
	var m;
	var i;
 
	opts = {
		'dtype': 'double',
		'name': 'evalpoly',
		'suffix': ''
	};
	if ( arguments.length > 1 ) {
		opts.dtype = options.dtype || opts.dtype;
		opts.name = options.name || opts.name;
	}
	if ( opts.dtype === 'float' ) {
		opts.suffix = 'f';
	}
	n = c.length;
 
	// If no coefficients, the function always returns 0...
	if ( n === 0 ) {
		str = replace( EMPTY_TEMPLATE, '{{dtype}}', opts.dtype );
		str = replace( str, '{{dtype_suffix}}', opts.suffix );
		return replace( str, '{{fname}}', opts.name );
	}
	// If only one coefficient, the function always returns that coefficient...
	if ( n === 1 ) {
		str = c[ 0 ].toString();
		if ( isInteger( c[ 0 ] ) ) {
			str += '.0';
		}
		str = replace( SINGLE_COEFFICIENT_TEMPLATE, '{{coefficient}}', str );
		str = replace( str, '{{dtype}}', opts.dtype );
		str = replace( str, '{{dtype_suffix}}', opts.suffix );
		return replace( str, '{{fname}}', opts.name );
	}
	m = n - 1;
 
	// Avoid exceeding the maximum stack size on V8 by using a simple loop :(. Note that the choice of `500` was empirically determined...
	if ( n > 500 ) {
		str = '';
		for ( i = 0; i < n; i++ ) {
			str += '\t' + c[ i ].toString();
			if ( isInteger( c[ i ] ) ) {
				str += '.0';
			}
			str += '{{dtype_suffix}}';
			if ( i < m ) {
				str += ',\n';
			}
		}
		str = replace( LOOP_TEMPLATE, '{{coefficients}}', str );
		str = replace( str, '{{num_coefficients}}', n.toString() );
		str = replace( str, '{{dtype}}', opts.dtype );
		str = replace( str, '{{dtype_suffix}}', opts.suffix );
		str = replace( str, '{{fname}}', opts.name );
		return replace( str, '{{FNAME}}', uppercase( opts.name ) );
	}
	// If more than one coefficient, apply Horner's method...
	horner = c[ 0 ].toString();
	if ( isInteger( c[ 0 ] ) ) {
		horner += '.0';
	}
	horner += '{{dtype_suffix}}';
	for ( i = 1; i < n; i++ ) {
		horner += ' + (x * ';
		if ( i < m ) {
			horner += '(';
		}
		horner += c[ i ].toString();
		if ( isInteger( c[ i ] ) ) {
			horner += '.0';
		}
		horner += '{{dtype_suffix}}';
	}
	// Close all the parentheses...
	for ( i = 0; i < (2*(n-1))-1; i++ ) {
		horner += ')';
	}
	str = c[ 0 ].toString();
	if ( isInteger( c[ 0 ] ) ) {
		str += '.0';
	}
	str = replace( EVALPOLY_TEMPLATE, '{{coefficient}}', str );
	str = replace( str, '{{horner}}', horner );
	str = replace( str, '{{dtype}}', opts.dtype );
	str = replace( str, '{{dtype_suffix}}', opts.suffix );
	return replace( str, '{{fname}}', opts.name );
}
 
 
// EXPORTS //
 
module.exports = compile;