function output = #FILENAME(#INPUT_ARGS) #HEADER_COMMENT #PROBLEM_DEFINITION_COMMENT %Steps to evaluate function: %Check input type %Create constants for table value and breakpoint values %Initialize variables and set their type %Perform pre-lookup to get the index and fraction corresponding to input %Calculate the output value #LOAD_MAT_FILE #CAST_INPUT_VALUE #BREAKPOINT_VALUES #TABLE_VALUES #INDEX_TYPE #FRACTION_TYPE #OUTPUT_TYPE #INTERMEDIATE_VALUE %Interpolation #STRIDE tableValues = cast(tableValues,'like',intermediateValue); index = zeros(length(inputValues1),nargin,'like',idxType); frac = zeros(length(inputValues1),nargin,'like',fracType); intermediateValue = zeros(length(inputValues1),nargin,'like',intermediateValue); #PRELOOKUP #OUTPUT_LOGIC #REMOVE_FIMATH end function [idx,frac] = preLookUp(input,bpdata,idxType,fracType) %Function to calculate index and fraction values. idx = zeros(size(input), 'like',idxType); frac = zeros(size(input), 'like',fracType); %Index Search #INDEX_SEARCH idx(input >= bpdata(end)) = length(bpdata)-1; idx(idx == 0) = 1; %Fraction Calculation #DEFINE_NUMERATOR #DEFINE_DENOMINATOR numerator = zeros(size(input),'like',numerator); denominatorReciprocal = zeros(size(input),'like',denominatorReciprocal); numerator(:) = subtractMonotonic(input,bpdata(idxLeft)); bpSpace = cast(bpdata(idxLeft+1) - bpdata(idxLeft),'like',denominatorReciprocal); denominatorReciprocal(:) = 1./bpSpace; frac(:) = numerator .* denominatorReciprocal; frac(input <= bpdata(1)) = 0; frac(input >= bpdata(end)) = 1; end function output = subtractMonotonic(input1, input2) %#codegen coder.inline('always'); if isfloat(input1) || isfloat(input2) if isdouble(input1) || isdouble(input2) output = double(input1) - double(input2); else output = single(input1) - single(input2); end else assert(input1.SlopeAdjustmentFactor == input2.SlopeAdjustmentFactor); nt1 = fixed.extractNumericType(input1); nt2 = fixed.extractNumericType(input2); ntNew1 = copyTrivialSlopeAdjustBias(nt1); ntNew2 = copyTrivialSlopeAdjustBias(nt2); in1 = reinterpretcast(input1,ntNew1); in2 = reinterpretcast(input2,ntNew2); ntOver = fixed.aggregateType(ntNew1,ntNew2); fmLean = fimath('RoundingMethod','Floor','OverflowAction','Wrap'); if ntOver.SignednessBool tempOut = in1 - in2; ntOverUnsigned = copyUnsigned(ntOver); tempOut = removefimath( fi(tempOut, ntOverUnsigned, fmLean) ); else in1 = fi(in1,ntOver); in2 = fi(in2,ntOver); fmUnsigned = fimath(... 'RoundingMethod','Floor',... 'OverflowAction','Wrap',... 'SumMode', 'KeepLSB', ... 'SumWordLength', ntOver.WordLength, ... 'CastBeforeSum', true ... ); tempOut = setfimath(in1,fmUnsigned) - setfimath(in2,fmUnsigned); tempOut = removefimath(tempOut); end newBias = input1.Bias - input2.Bias; ntTempOut = fixed.extractNumericType(tempOut); ntOutput = copyNewSlopeAdjustBias(ntTempOut,input1.SlopeAdjustmentFactor,newBias); output = reinterpretcast(tempOut,ntOutput); end end function ntNew = copyTrivialSlopeAdjustBias(u) %#codegen coder.inline('always'); ntOrig = fixed.extractNumericType(u); if (1 == ntOrig.SlopeAdjustmentFactor) && (0 == ntOrig.Bias) ntNew = ntOrig; else ntNew = numerictype(... ntOrig.Signed,... ntOrig.WordLength,... 1.0,... ntOrig.FixedExponent,... 0.0,... 'DataType',ntOrig.DataType); end end function ntNew = copyUnsigned(u) %#codegen coder.inline('always'); ntOrig = fixed.extractNumericType(u); needChange = ntOrig.isfixed && ~ntOrig.SignednessBool; if ~needChange ntNew = ntOrig; else ntNew = numerictype(... false,... ntOrig.WordLength,... newSlopeAdjustmentFactor,... ntOrig.FixedExponent,... ntOrig.Bias,... 'DataType',ntOrig.DataType); end end function ntNew = copyNewSlopeAdjustBias(u,newSlopeAdjustmentFactor,newBias) %#codegen coder.inline('always'); ntOrig = fixed.extractNumericType(u); if (newBias == ntOrig.Bias) && ... (newSlopeAdjustmentFactor == ntOrig.SlopeAdjustmentFactor) ntNew = ntOrig; else ntNew = numerictype(... ntOrig.Signed,... ntOrig.WordLength,... newSlopeAdjustmentFactor,... ntOrig.FixedExponent,... newBias,... 'DataType',ntOrig.DataType); end end