gfill.js (1873B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2020 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 'use strict'; 20 21 // VARIABLES // 22 23 var M = 8; 24 25 26 // MAIN // 27 28 /** 29 * Fills a strided array with a specified scalar constant. 30 * 31 * @param {PositiveInteger} N - number of indexed elements 32 * @param {number} alpha - scalar 33 * @param {NumericArray} x - input array 34 * @param {integer} stride - index increment 35 * @returns {NumericArray} input array 36 * 37 * @example 38 * var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ]; 39 * 40 * gfill( x.length, 5.0, x, 1 ); 41 * // x => [ 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0 ] 42 */ 43 function gfill( N, alpha, x, stride ) { 44 var ix; 45 var i; 46 var m; 47 48 if ( N <= 0 ) { 49 return x; 50 } 51 // Use loop unrolling if the stride is equal to `1`... 52 if ( stride === 1 ) { 53 m = N % M; 54 55 // If we have a remainder, run a clean-up loop... 56 if ( m > 0 ) { 57 for ( i = 0; i < m; i++ ) { 58 x[ i ] = alpha; 59 } 60 } 61 if ( N < M ) { 62 return x; 63 } 64 for ( i = m; i < N; i += M ) { 65 x[ i ] = alpha; 66 x[ i+1 ] = alpha; 67 x[ i+2 ] = alpha; 68 x[ i+3 ] = alpha; 69 x[ i+4 ] = alpha; 70 x[ i+5 ] = alpha; 71 x[ i+6 ] = alpha; 72 x[ i+7 ] = alpha; 73 } 74 return x; 75 } 76 if ( stride < 0 ) { 77 ix = (1-N) * stride; 78 } else { 79 ix = 0; 80 } 81 for ( i = 0; i < N; i++ ) { 82 x[ ix ] = alpha; 83 ix += stride; 84 } 85 return x; 86 } 87 88 89 // EXPORTS // 90 91 module.exports = gfill;