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 | 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 53x 53x 53x 12x 7x 7x 5x 5x 53x 5x 5x 53x 10x 10x 4x 4x 6x 10x 1x 1x 5x 5x 53x 5x 4x 4x 1x 5x 5x 1x 1x 1x 1x 21x 53x 7x 7x 14x 53x 1x 1x 1x 1x 1x | /** * @license Apache-2.0 * * Copyright (c) 2025 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. */ /* eslint-disable id-length, function-paren-newline */ 'use strict'; // MODULES // var isFloatingPointDataType = require( '@stdlib/ndarray/base/assert/is-floating-point-data-type' ); var isSignedIntegerDataType = require( '@stdlib/ndarray/base/assert/is-signed-integer-data-type' ); var isUnsignedIntegerDataType = require( '@stdlib/ndarray/base/assert/is-unsigned-integer-data-type' ); var isDataType = require( '@stdlib/ndarray/base/assert/is-data-type' ); var promotionRules = require( '@stdlib/ndarray/promotion-rules' ); var defaults = require( '@stdlib/ndarray/defaults' ); var format = require( '@stdlib/string/format' ); // VARIABLES // var DEFAULT_SIGNED_INTEGER_DTYPE = defaults.get( 'dtypes.signed_integer' ); var DEFAULT_UNSIGNED_INTEGER_DTYPE = defaults.get( 'dtypes.unsigned_integer' ); var DEFAULT_REAL_FLOATING_POINT_DTYPE = defaults.get( 'dtypes.real_floating_point' ); // MAIN // /** * Resolves the input ndarray casting data type for a binary function. * * @param {string} idtype1 - first input ndarray data type * @param {string} idtype2 - second input ndarray data type * @param {string} odtype - output ndarray data type * @param {string} policy - input ndarray data type casting policy * @throws {TypeError} fourth argument must be a recognized data type policy * @throws {Error} unexpected error * @returns {string} data type * * @example * var dt = resolve( 'float32', 'float32', 'float64', 'none' ); * // returns <string> */ function resolve( idtype1, idtype2, odtype, policy ) { var dt; if ( policy === 'none' ) { if (idtype1 !== idtype2) { throw new Error( format( 'Inputs must match under "none" policy. Got %s and %s.', idtype1, idtype2 ) ); } return idtype1; } if ( policy === 'output' ) { return odtype; } if ( policy === 'promoted' ) { dt = promotionRules( idtype1, idtype2 ); if ( dt === -1 ) { throw new Error( format( 'invalid operation. Unable to promote the first input and second input data types. First input data type: %s. Second input data type: %s.', idtype1, idtype2 ) ); } dt = promotionRules( dt, odtype ); if ( dt === -1 ) { throw new Error( format( 'invalid operation. Unable to promote the input and output data types. Input data type: %s. Output data type: %s.', dt, odtype ) ); } return dt; } if ( policy === 'accumulation' ) { if ( isFloatingPointDataType( idtype1 ) || isFloatingPointDataType( idtype2 ) || idtype1 === 'generic' || idtype2 === 'generic' ) { // NOTE: we may want to revisit this in the future for float16/complex32, where the value range is much more limited return promotionRules(idtype1, idtype2); } // Unless the input data type value range is larger than the default un/signed integer data type, accumulate in the default un/signed integer data type, as accumulating in smaller range integer data types (e.g., `int8`) are at high risk for overflow, especially for ndarrays containing many elements... if ( isUnsignedIntegerDataType( idtype1 ) && isUnsignedIntegerDataType( idtype2 ) ) { // eslint-disable-line max-len return promotionRules( promotionRules(idtype1, idtype2), DEFAULT_UNSIGNED_INTEGER_DTYPE ); } if ( isSignedIntegerDataType( idtype1 ) && isSignedIntegerDataType( idtype2 ) ) { // eslint-disable-line max-len return promotionRules( promotionRules(idtype1, idtype2), DEFAULT_SIGNED_INTEGER_DTYPE ); } // For all other input data types, accumulate in the default real-valued floating-point data type... return DEFAULT_REAL_FLOATING_POINT_DTYPE; } // Check for an explicit data type... if ( isDataType( policy ) ) { return policy; } throw new TypeError( format( 'invalid argument. Fourth argument must be a supported casting policy. Value: `%s`.', policy ) ); } // EXPORTS // module.exports = resolve; |