function output = #FILENAME(#INPUT_ARGS) %#codegen #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 coder.inline('always'); #LOAD_MAT_FILE #CAST_INPUT_VALUE #ASSERT_INPUT #BREAKPOINT_VALUES #TABLE_VALUES #INDEX_TYPE #FRACTION_TYPE #OUTPUT_TYPE #INTERMEDIATE_VALUE %Interpolation #STRIDE index = zeros(1,nargin,'like',idxType); frac = zeros(1,nargin,'like',fracType); for i=1:numel(#INPUT_ARG1) #PRELOOKUP #OUTPUT_LOGIC end#REMOVE_FIMATH end function [idx,frac] = preLookUp(input,bpdata,idxType,fracType) %Function to calculate index and fraction values. idx = cast(0, 'like', idxType); frac = cast(0, 'like',fracType); #DEFINE_NUMERATOR #DEFINE_DENOMINATOR %Index Search if (input <= bpdata(1)) idx(:) = 1; frac(:) = 0; elseif (input >= bpdata(end)) idx(:) = length(bpdata)-1; frac(:) = 1; else #INDEX_SEARCH %Fraction Calculation numerator(:) = subtractMonotonic(input,bpdata(idx)); bpSpace = cast(bpdata(idxLeft+1) - bpdata(idxLeft),'like',denominatorReciprocal); denominatorReciprocal(:) = 1./bpSpace; #FRACTION_CALCULATION end 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