%%
%%
%%
%%Copyright1994-2013TheMathWorks,Inc.
%%
%%Abstract:2D-Lookupblocktargetfile
 
%implements Lookup2D "C"
 
%include "fixptlook_support.tlc"
%include "fixptlook_search.tlc"
%include "fixptlook2D.tlc"
 
%%Function:BlockTypeSetup===================================================
%%Abstract:
%%
%function BlockTypeSetup(block, system) void
 
%endfunction
 
%%Function:BlockFixptInstanceSetup===============================================
%%Abstract:
%%Pre-codegenerationwork
%%Thisfunctioniscalledwheninfixptmode
%%
%function BlockFixptInstanceSetup(block, system) void
    %%
    %% Call the fixed-point setup function
    %%
    %<FixPt_Setup(block, system)>
    %%
%endfunction
 
 
%%Function:BlockClassicInstanceSetup================================================
%%Abstract:
%%Thisfunctioniscalledwheninclassicmode
%%
%function BlockClassicInstanceSetup(block, system) void
  %<LibBlockSetIsExpressionCompliant(block)>
  %% If generating code for Embedded-C code formats, inform user
  %% that they can generate more efficient code by removing repeated zero
  %% values.
  %if (SLibIsERTCodeFormat())
    %if !(ParamSettings.ColZeroTechnique == "NormalInterp" && ...
      ParamSettings.RowZeroTechnique == "NormalInterp")
      %if (block.InMask == "yes")
        %assign blockName = LibParentMaskBlockName(block)
      %else
        %assign blockName = LibGetFormattedBlockPath(block)
      %endif
      %selectfile STDOUT
 
Note: Removing repeated zero values from the X and Y axes will produce
more efficient code for block: %<blockName>. To locate this block, type
 
hilite_system('%<blockName>')
 
at the MATLAB command prompt.
 
      %selectfile NULL_FILE
    %endif
  %endif
%endfunction
 
%%Function:BlockInstanceSetup==============================================
%%Abstract:
%%Pre-codegenerationwork
%%
%function BlockInstanceSetup(block, system) void
  %if block.InFixptMode
    %<BlockFixptInstanceSetup(block, system)>
  %else
    %<BlockClassicInstanceSetup(block, system)>
  %endif
  %if GenerateASAP2
    %% Create a parameter group for ASAP2 data definition
    %assign group = SLibCreateParameterGroup(block, "Lookup2D")
    %assign tmpVar = SLibAddMember(block, group, RowIndex)
    %assign tmpVar = SLibAddMember(block, group, ColumnIndex)
    %assign tmpVar = SLibAddMember(block, group, Table)
  %endif
%endfunction
 
%%Function:genLookup2DGeneralFcnFromTfl=============================================
%%
%function genLookup2DGeneralFcnFromTfl(outputDataType, addrRowValues, numRowValues, ...
                                        addrColValues, numColValues, outputValues, ...
                                        uX, uY, ParamSettingsDotColZeroIndex, colZeroTechnique, ...
                                        outputAtRowZero) void
 
    %createrecord FcnRec{Name "rt_Lookup2D_General"; RetTypeId outputDataType; NumArgs 10}
    %addtorecord FcnRec ArgList{Expr "%<addrRowValues>"; TypeId outputDataType; IsPtr 1; IsCplx 0; IsConst 0}
    %addtorecord FcnRec ArgList{Expr "%<numRowValues>"; TypeId tSS_INTEGER; IsPtr 0; IsCplx 0; IsConst 0}
    %addtorecord FcnRec ArgList{Expr "%<addrColValues>"; TypeId outputDataType; IsPtr 1; IsCplx 0; IsConst 0}
    %addtorecord FcnRec ArgList{Expr "%<numColValues>"; TypeId tSS_INTEGER; IsPtr 0; IsCplx 0; IsConst 0}
    %addtorecord FcnRec ArgList{Expr "%<outputValues>"; TypeId outputDataType; IsPtr 1; IsCplx 0; IsConst 0}
    %addtorecord FcnRec ArgList{Expr "%<uX>"; TypeId outputDataType; IsPtr 0; IsCplx 0; IsConst 0}
    %addtorecord FcnRec ArgList{Expr "%<uY>"; TypeId outputDataType; IsPtr 0; IsCplx 0; IsConst 0}
    %addtorecord FcnRec ArgList{Expr "%<ParamSettingsDotColZeroIndex>"; TypeId tSS_INTEGER; IsPtr 0; IsCplx 0; IsConst 0}
    %addtorecord FcnRec ArgList{Expr "%<colZeroTechnique>"; TypeId tSS_INTEGER; IsPtr 0; IsCplx 0; IsConst 0}
    %addtorecord FcnRec ArgList{Expr "%<outputAtRowZero>"; TypeId outputDataType; IsPtr 1; IsCplx 0; IsConst 0}
 
    %return "%<LibGenFcnCall(FcnRec)>"
 
%endfunction %% genLookup2DGeneralFcnFromTfl
 
%%Function:genLookup2DNormalFcnFromTfl=============================================
%%
%function genLookup2DNormalFcnFromTfl(outputDataType, addrRowValues, numRowValues, ...
                                        addrColValues, numColValues, outputValues, ...
                                        uX, uY) void
    %createrecord FcnRec{Name "rt_Lookup2D_Normal"; RetTypeId outputDataType; NumArgs 7}
    %addtorecord FcnRec ArgList{Expr "%<addrRowValues>"; TypeId outputDataType; IsPtr 1; IsCplx 0; IsConst 0}
    %addtorecord FcnRec ArgList{Expr "%<numRowValues>"; TypeId tSS_INTEGER; IsPtr 0; IsCplx 0; IsConst 0}
    %addtorecord FcnRec ArgList{Expr "%<addrColValues>"; TypeId outputDataType; IsPtr 1; IsCplx 0; IsConst 0}
    %addtorecord FcnRec ArgList{Expr "%<numColValues>"; TypeId tSS_INTEGER; IsPtr 0; IsCplx 0; IsConst 0}
    %addtorecord FcnRec ArgList{Expr "%<outputValues>"; TypeId outputDataType; IsPtr 1; IsCplx 0; IsConst 0}
    %addtorecord FcnRec ArgList{Expr "%<uX>"; TypeId outputDataType; IsPtr 0; IsCplx 0; IsConst 0}
    %addtorecord FcnRec ArgList{Expr "%<uY>"; TypeId outputDataType; IsPtr 0; IsCplx 0; IsConst 0}
 
    %return "%<LibGenFcnCall(FcnRec)>"
 
%endfunction %% genLookup2DNormalFcnFromTfl
 
%%Function:FixptOutputs==========================================================
%%Abstract:
%%Thisfunctioniscalledwheninfixptmode
%%
%function FixptOutputs(block, system) Output
    %%
    %assign y0IsComplex = LibBlockOutputSignalIsComplex(0)
    %%
    %% Get input data types
    %%
    %assign u0DT = FixPt_GetInputDataTypeWithBoolReplace(0)
    %%
    %assign u1DT = FixPt_GetInputDataTypeWithBoolReplace(1)
    %%
    %% Get output data type
    %%
    %assign y0DT = FixPt_GetOutputDataType(0)
    %%
    %% create header comment
    %%
    %openfile commentBuffer
    %%
    %% add general comments
    %%
    %<FixPt_GeneralComments()>/
    %%
    %% comment on modes
    %%
     * Lookup Method: %<FixPtLookUpMethodStr>
     *
    %%
    %% add comments about parameters
    %%
     * Row Data parameter uses the same data type and scaling as Input0
     * Column Data parameter uses the same data type and scaling as Input1
     * Table Data parameter uses the same data type and scaling as Output0
    %%
    %% END: header comment
    %%
    %closefile commentBuffer
    %%
    %<LibCacheBlockComment(block,commentBuffer)>/
    %%
    %% determine if the Row and Col Data is inlined and evenly spaced
    %%
    %if FixPt_ParameterCouldBeInlined(RowIndex, "", "", 0)
        %assign rowDataEvenSpaceInfo = FixPt_GetBreakPointInfo(RowIndex.Value)
    %else
        %assign rowDataEvenSpaceInfo = FixPt_BreakPointInfoDefault()
    %endif
    %%
    %if FixPt_ParameterCouldBeInlined(ColumnIndex, "", "", 0)
        %assign colDataEvenSpaceInfo = FixPt_GetBreakPointInfo(ColumnIndex.Value)
    %else
        %assign colDataEvenSpaceInfo = FixPt_BreakPointInfoDefault()
    %endif
    %%
    %if FixPtLookUpMethodStr != "Linear_Endpoint"
    {
    %endif
    %%
    %% declare local variables as needed
    %%
    %if FixPtLookUpMethodStr == "Linear_Endpoint"
        %%
        %% no index vars needed
        %%
    %elseif FixPtLookUpMethodStr == "Below"
        %%
        %<FixPt_uint_label> iRowIndex1, iColumnIndex1;
 
    %elseif FixPtLookUpMethodStr == "Above"
        %%
        %<FixPt_uint_label> iRowIndex2, iColumnIndex2;
 
    %elseif FixPtLookUpMethodStr == "Nearest"
        %%
        %<FixPt_uint_label> iRowIndex1, iColumnIndex1;
 
    %else
      %openfile errTxt
 
        The lookup method: %<FixPtLookUpMethodStr> is not supported
        for code generation.
 
        Block: '%<LibGetBlockPath(block)>'
 
      %closefile errTxt
      %exit %<errTxt>
      %%
    %endif
    %%
    %assign u0BreakpointWidth = LibBlockParameterWidth(RowIndex)
    %%
    %% Roll around signal width
    %%
    %assign rollVars = ["U", "Y"]
    %%
    %roll sigIdx = RollRegions, lcv = RollThreshold, block, "Roller", rollVars
        %%
        %% Get input 0
        %%
        %assign u0Label = LibBlockInputSignal(0, "", lcv, sigIdx)
        %%
        %% Get input 1
        %%
        %assign u1Label = LibBlockInputSignal(1, "", lcv, sigIdx)
        %%
        %% Get output
        %%
        %if FixPtLookUpMethodStr == "Linear_Endpoint"
            %%
            %% if complex, get the whole structure
            %%
            %assign y0Label = LibBlockOutputSignal(0, "", lcv, sigIdx)
        %else
            %%
            %assign reSigIdx = tRealPart + STRING(sigIdx)
            %assign imSigIdx = tImagPart + STRING(sigIdx)
            %%
            %assign y0ReLabel = LibBlockOutputSignal(0, "", lcv, reSigIdx)
            %if y0IsComplex
                %assign y0ImLabel = LibBlockOutputSignal(0, "", lcv, imSigIdx)
            %endif
        %endif
        %%
        %% lookup method Linear_Endpoint
        %%
        %if FixPtLookUpMethodStr == "Linear_Endpoint"
            %<FixPt_SearchAndInterpolate2D(...
               y0Label, y0DT,...
                                    u0Label,u0DT,...
                                    u1Label,u1DT,...
                                    Table,y0DT,-1,...
               RowIndex, u0DT, rowDataEvenSpaceInfo, -1,...
               ColumnIndex, u1DT, colDataEvenSpaceInfo, -1,...
                                    FixPtRoundingMode,FixPtSaturationMode)>/
        %%
        %% lookup method BELOW
        %%
        %elseif FixPtLookUpMethodStr == "Below"
            %%
            %% find row indices
            %%
            %<FixPt_IndexSearch("iRowIndex1","",...
                                u0Label,u0DT,...
                                RowIndex,u0DT,...
                                rowDataEvenSpaceInfo,-1,...
                                "Below")>
            %%
            %% find col indices
            %%
            %<FixPt_IndexSearch("iColumnIndex1","",...
                                u1Label,u1DT,...
                                ColumnIndex,u1DT,...
                                colDataEvenSpaceInfo,-1,...
                                "Below")>
            %%
            %% set output to proper Table Data Value
            %%
            %<y0ReLabel> = %<LibBlockParameter(Table,...
                                               "iRowIndex1+%<u0BreakpointWidth>*iColumnIndex1", "", "re0")>;
            %if y0IsComplex
                %<y0ImLabel> = %<LibBlockParameter(Table,...
                                                   "iRowIndex1+%<u0BreakpointWidth>*iColumnIndex1", "", "im0")>;
            %endif
        %%
        %% lookup method ABOVE
        %%
        %elseif FixPtLookUpMethodStr == "Above"
            %%
            %% find row indices
            %%
            %<FixPt_IndexSearch("","iRowIndex2",...
                                u0Label,u0DT,...
                                RowIndex,u0DT,...
                                rowDataEvenSpaceInfo,-1,...
                                "Above")>
            %%
            %% find col indices
            %%
            %<FixPt_IndexSearch("","iColumnIndex2",...
                                u1Label,u1DT,...
                                ColumnIndex,u1DT,...
                                colDataEvenSpaceInfo,-1,...
                                "Above")>
            %%
            %% set output to proper Table Data Value
            %%
            %<y0ReLabel> = %<LibBlockParameter(Table,...
                                               "iRowIndex2+%<u0BreakpointWidth>*iColumnIndex2", "", "re0")>;
            %if y0IsComplex
                %<y0ImLabel> = %<LibBlockParameter(Table,...
                                                   "iRowIndex2+%<u0BreakpointWidth>*iColumnIndex2", "", "im0")>;
            %endif
        %%
        %% lookup method NEAREST
        %%
        %else %% if FixPtLookUpMethodStr == "Nearest"
            %%
            %% find row indices
            %%
            %<FixPt_IndexSearch("iRowIndex1","",...
                                u0Label,u0DT,...
                                RowIndex,u0DT,...
                                rowDataEvenSpaceInfo,-1,...
                                "Near")>
            %%
            %% find col indices
            %%
            %<FixPt_IndexSearch("iColumnIndex1","",...
                                u1Label,u1DT,...
                                ColumnIndex,u1DT,...
                                colDataEvenSpaceInfo,-1,...
                                "Near")>
            %%
            %% set output to proper Table Data Value
            %%
            %<y0ReLabel> = %<LibBlockParameter(Table,...
                                               "iRowIndex1+%<u0BreakpointWidth>*iColumnIndex1", "", "re0")>;
            %if y0IsComplex
                %<y0ImLabel> = %<LibBlockParameter(Table,...
                                                   "iRowIndex1+%<u0BreakpointWidth>*iColumnIndex1", "", "im0")>;
            %endif
        %endif
    %endroll
    %if FixPtLookUpMethodStr != "Linear_Endpoint"
    }
    %endif
    %%
    %% blank line for formating
 
%endfunction %% FixptOutputs
 
%%[EOF]sfix_look2.tlc
 
 
%%Function:ClassicOutputs==========================================================
%%Abstract:
%%Outputforclassicmode
%%Yz=LookUp2D(Ux,Uy)
%%Thisfunctioniscalledwheninclassicmode
%function ClassicOutputs(block, system) Output
  %assign outputDataType = LibBlockOutputSignalAliasedThruDataTypeId(0)
  %%
  %assign addrColValues = LibBlockParameterBaseAddr(ColumnIndex)
  %assign numColValues = SIZE(ColumnIndex.Value, 1)
  %%
  %assign addrRowValues = LibBlockParameterBaseAddr(RowIndex)
  %assign numRowValues = SIZE(RowIndex.Value, 1)
  %assign outputValues = LibBlockParameterBaseAddr(Table)
  %%
  %assign rollVars = ["U", "Y"]
  %roll sigIdx = RollRegions, lcv = RollThreshold, block, "Roller", rollVars
    %%
    %assign uX = LibBlockInputSignal(0, "", lcv, sigIdx)
    %assign uY = LibBlockInputSignal(1, "", lcv, sigIdx)
    %assign yZ = LibBlockOutputSignal(0, "", lcv, sigIdx)
    %%
    %if (ParamSettings.ColZeroTechnique == "NormalInterp" && ...
      ParamSettings.RowZeroTechnique == "NormalInterp")
      %<yZ> = %<genLookup2DNormalFcnFromTfl(outputDataType, addrRowValues, numRowValues, ...
                                        addrColValues, numColValues, outputValues, ...
                                        uX, uY)>;
    %else
      %assign outputAtRowZero = LibBlockParameterBaseAddr(OutputAtRowZero)
      %switch ParamSettings.ColZeroTechnique
    %case "NormalInterp"
      %assign colZeroTechnique = "NORMAL_INTERP"
      %break
    %case "AverageValue"
      %assign colZeroTechnique = "AVERAGE_VALUE"
      %break
    %case "MiddleValue"
      %assign colZeroTechnique = "MIDDLE_VALUE"
      %break
    %default
      %%START_ASSERT
      %assign errTxt = "Internal error: ParamSetting 'ColZeroTechnique' " ...
        "cannot be '%<ParamSettings.ColZeroTechnique>'."
      %<LibBlockReportFatalError(block,errTxt)>
      %%END_ASSERT
      %endswitch
      %<yZ> = %<genLookup2DGeneralFcnFromTfl(outputDataType, addrRowValues, numRowValues, ...
                                        addrColValues, numColValues, outputValues, ...
                                        uX, uY, ParamSettings.ColZeroIndex, colZeroTechnique, ...
                                        outputAtRowZero)>;
    %endif
  %endroll
 
%endfunction
 
%%Function:Outputs==============================================
%%Abstract:
%%
%function Outputs(block, system) Output
  %if block.InFixptMode
    %<FixptOutputs(block, system)>
  %else
    %<ClassicOutputs(block, system)>
  %endif
%endfunction
 
%%Function:BlockOutputSignal=================================================
%%Abstract:
%%Returnanoutputexpression.Thisfunction*may*
%%beusedbySimulinkwhenoptimizingtheBlockIOdatastructure.
%%
%function BlockOutputSignal(block,system,portIdx,ucv,lcv,idx,retType) void
  %if !block.InFixptMode
    %switch retType
      %case "Signal"
    %assign outputDataType = LibBlockOutputSignalAliasedThruDataTypeId(0)
    %%
    %assign addrColValues = LibBlockParameterBaseAddr(ColumnIndex)
    %assign numColValues = SIZE(ColumnIndex.Value, 1)
    %%
    %assign addrRowValues = LibBlockParameterBaseAddr(RowIndex)
    %assign numRowValues = SIZE(RowIndex.Value, 1)
    %assign outputValues = LibBlockParameterBaseAddr(Table)
    %%
    %assign uX = LibBlockInputSignal(0, ucv, lcv, idx)
    %assign uY = LibBlockInputSignal(1, ucv, lcv, idx)
    %%
    %if (ParamSettings.ColZeroTechnique == "NormalInterp" && ...
      ParamSettings.RowZeroTechnique == "NormalInterp")
        %return "%<genLookup2DNormalFcnFromTfl(outputDataType, ...
                 addrRowValues, numRowValues, addrColValues, ...
                 numColValues, outputValues, uX, uY)>"
    %else
      %%START_ASSERT
      %assign errTxt = "Expression output is not valid."
      %<LibBlockReportFatalError(block,errTxt)>
      %%END_ASSERT
    %endif
    %%START_ASSERT
      %default
    %assign errTxt = "Unsupported return type: %<retType>"
    %<LibBlockReportError(block,errTxt)>
    %%END_ASSERT
    %endswitch
  %endif
%endfunction
 
%%[EOF]lookup2d.tlc