%%
%%spclib.tlc-HelperfunctionsforDSPSystemToolboxcodegeneration
%%
%%Copyright1995-2016TheMathWorks,Inc.
%%
 
%if EXISTS("_DSPLIB_") == 0
%assign _DSPLIB_ = 1
 
 
%%===========================================================================
 
%assign ::MATLAB_ROOT_TMP = FEVAL("matlabroot")
%assign ::MATLAB_ROOT = FEVAL("strrep", ::MATLAB_ROOT_TMP, "//", "/")
 
%%IncludethestandardDSPSystemToolboxCheaderfileinallgeneratedcode:
%%
%openfile buffer
    #include "dsp_rt.h" /* DSP System Toolbox general run time support functions */
%closefile buffer
%<LibCacheDefine(buffer)>
 
%%IncludeDSPSystemToolboxfixed-pointTLCarithmeticfunctionlibrary
%include FEVAL("matlabroot")+"/toolbox/shared/dsp/vision/matlab/utilities/mex/tlc_c/spcfixptlib.tlc"
 
 
%%===========================================================================
%%DSPSystemToolboxcode-genframework(fcndefinitions,etc.):
%%===========================================================================
 
 
%%Function:JustCreatedCompiledModelRecord==================================
%%Abstract:
%%ChecksforCompiledModel.recordName
%%Ifrecordalreadyexists,returnwitha0(false)
%%Ifrecorddoesnotexist,
%%createrecordNameunderCompiledModel
%%returnwith1(true)
%%
%%Exampleusage:
%%
%%%ifJustCreatedCompiledModelRecord("my_cache")
%%%%Thisisthefirsttimetherecordwascreated
%%%%codegoeshere>
%%%else
%%%%2ndorsubsequentcall(recordalreadyexists!)
%%%%codegoeshere>
%%%endif
%%
%function JustCreatedCompiledModelRecord(recordName) void
 
    %assign fullRecordName = "::CompiledModel." + recordName
    %%
    %if !EXISTS("%<fullRecordName>")
        %assign retval = 1 %% First call
 
        %% Create record since it does not yet exist:
        %assign %<recordName> = 1
        %assign ::CompiledModel = ::CompiledModel + %<recordName>
        %undef %<recordName> %% Remove from current scope
    %else
        %assign retval = 0 %% Subsequent call
 
    %endif
    %return retval
 
%endfunction %% JustCreatedCompiledModelRecord
 
 
%%Function:Include_rt_header===============================================
%%
%%Abstract:
%%Cachetheincludefileforrun-timesupportlibrary
%%
%%Exampleusage:
%%%<Include_rt_header(block,"dspfft_rt")>
%%
%%Thiswilladd:
%%
%%#include"dspfft_rt.h"
%%
%%tothemodel_common.hfile.
%%
%function Include_rt_header(block, libName) void
 
    %assign recName = libName + "_include"
 
    %if JustCreatedCompiledModelRecord(recName)
        %% Include the standard DSP System Toolbox C header file in all generated code:
        %%
        %openfile buffer
            %assign fileDotH = libName + ".h"
            #include "%<fileDotH>" /* DSP System Toolbox run time support library */
        %closefile buffer
        %<LibCacheDefine(buffer)>
    %endif
 
%endfunction %% Include_rt_header
 
 
%%Function:getModelName
%%Abstract:returnsthenameofthecurrentmodel
%function getModelName() void
  %if EXISTS(::CompiledModel.OrigName)
    %assign modelName = ::CompiledModel.OrigName
  %else
    %assign modelName = CompiledModel.Name
  %endif
  %return modelName
%endfunction
 
 
%%Function:ModifyBuildInfo
%%Abstract:ThismethodiscalledtoaddfilestotheBuildInfostructure
%%withthefollowingparameters:
%%1)Theproduct
%%2)Thepath(relativetotheproduct)
%%3)headerfiles
%%4)sourcefiles
%%5)non-buildfiles
%%6)linkflags
%%ExampleusagefromwithinTLC:
%%%<ModifyBuildInfo("vipblks","viphoughlines","","houghlines_d_rt.c")>
%%
%function ModifyBuildInfo(product, path, hdrFiles, srcFiles, nonBuildFiles, linkFlags) void
  %assign foobar = FEVAL("dspModifyBuildInfo", "%<getModelName()>", "%<product>", "%<path>", "%<hdrFiles>", "%<srcFiles>", "%<nonBuildFiles>", "%<linkFlags>")
%endfunction
 
 
%%Function:MAX=============================================================
%%
%%Abstract:Returnthemaxoftwovariables
%%
%function MAX(a,b)
%return ((a > b) ? a : b)
%endfunction
 
 
%%Function:MIN=============================================================
%%
%%Abstract:Returntheminoftwovariables
%function MIN(a,b)
%return ((a < b) ? a : b)
%endfunction
 
 
%%Function:LibTaskSFcnComment==============================================
%%Abstract:
%%GenerateaC-stylecommentindicatingthesample-andoffset-
%%timesfortheS-FunctionTID.TheTIDmustbeanyofthe
%%validTIDinputsthatLibGetGlobalTIDFromLocalSFcnTIDaccepts.
%%Stringincludesthecommentdelimiters(both/*and*/)
%%
%%Exampleoutput:
%%/*Sampletime:[1.0,0.0]*/
%%
%%Examplecalls:
%%Forport-basedsampletimes:
%%LibTaskSFcnComment("InputPortIdx0")
%%LibTaskSFcnComment("OutputPortIdx0")
%%
%%Forblock-basedsampletimes:
%%LibTaskSFcnComment(0)
%%
%function LibTaskSFcnComment(sfcnTID) Output
    %<LibTaskComment(LibGetGlobalTIDFromLocalSFcnTID(sfcnTID))>/
%endfunction %% LibTaskSFcnComment
 
 
%%Function:LibBlockCopyInputsToOutputs=====================================
%%
%%Abstract:
%%Copyinputstooutputs,usingtheINPUTwidth.
%%
%%Assumptions:
%%Ifoutputdatatypematchesinputdatatype,
%%thennofixed-pointcastisrequired,
%%copyisperformedusingmemcpy()ordirectassignment.
%%
%%Outputwidthisgreaterthanorequaltoinputwidth.Theremainingpartof
%%theoutputbufferisnottouched.
%%
%%Inputandoutputcomplexitycandiffer.
%%
%function LibBlockCopyInputsToOutputs(INPORT, OUTPORT) Output
  %assign inDT = FixPt_GetInputDataType(INPORT)
  %assign DTYPE_ID = LibBlockInputSignalDataTypeId(INPORT)
  %assign DTYPE_STR = LibBlockInputSignalDataTypeName(INPORT,"%<tRealPart>")
  %assign OUT_COMPLEX = LibBlockOutputSignalIsComplex(OUTPORT)
  %assign INP_COMPLEX = LibBlockInputSignalIsComplex(INPORT)
  %assign INP_WIDTH = LibBlockInputSignalWidth(INPORT)
  %assign NUM_REAL_INP_ELEMENTS = INP_WIDTH * (1 + INP_COMPLEX)
  %assign outputDTSameAsInput = (DTYPE_ID == LibBlockOutputSignalDataTypeId(OUTPORT))
  %%
  %if (outputDTSameAsInput)
    %if (INP_COMPLEX == OUT_COMPLEX)
      %%
      %% Copy every input sample
      %% to every output sample
      %%
      %if INP_WIDTH > 1
        memcpy( %<LibBlockOutputSignalAddr(OUTPORT, "", "", 0)>, /
          %<LibBlockInputSignalAddr( INPORT, "", "", 0)>, /
          (%<NUM_REAL_INP_ELEMENTS> * sizeof(%<DTYPE_STR>)) );
      %else
        %<LibBlockOutputSignal(OUTPORT, "", "", 0)> = %<LibBlockInputSignal( INPORT, "", "", 0)>;
      %endif
    %else
      %%
      %% Input and output differ in complexity
      %%
      %if INP_COMPLEX
        %%
        %% Copy real part of each
        %% complex input sample
        %% to each real output sample.
        %%
        %if INP_WIDTH > 1
          {
            const byte_T *uTemp = (const byte_T *)%<LibBlockInputSignalAddr( INPORT, "", "", 0)>;
            byte_T *yTemp = (byte_T *)%<LibBlockOutputSignalAddr(OUTPORT, "", "", 0)>;
            int_T inpIdx = %<INP_WIDTH>;
            while (inpIdx-- > 0) {
              memcpy(yTemp, uTemp, sizeof(%<DTYPE_STR>));
              yTemp += sizeof(%<DTYPE_STR>);
              uTemp += 2*sizeof(%<DTYPE_STR>); /* Skip imaginary part */
            }
          }
        %else
          %<LibBlockOutputSignal(OUTPORT, "", "", 0)> = %<LibBlockInputSignal( INPORT, "", "", "%<tRealPart>0")>;
        %endif
      %else
        %%
        %% Input is real, output is complex
        %% Copy each real input sample
        %% to the real part of each
        %% complex output sample.
        %% Interleave output samples with
        %% data type zero representation
        %% in each imaginary part.
        %%
        %if INP_WIDTH > 1
          {
            %if LibIsDataTypeMultiWordFixpt(DTYPE_ID)
              const %<DTYPE_STR> zero = %<FixPt_GetGroundValueOrNameFromDataTypeRec(FixPt_GetOutputDataType(OUTPORT), TLC_FALSE)>;
            %else
              const %<DTYPE_STR> zero = %<SLibGetGroundValueFromId(DTYPE_ID)>;
            %endif
            const byte_T *uTemp = (const byte_T *)%<LibBlockInputSignalAddr( INPORT, "", "", 0)>;
            byte_T *yTemp = (byte_T *)%<LibBlockOutputSignalAddr(OUTPORT, "", "", 0)>;
            int_T inpIdx = %<INP_WIDTH>;
            while (inpIdx-- > 0) {
              memcpy(yTemp, uTemp, sizeof(%<DTYPE_STR>));
              uTemp += sizeof(%<DTYPE_STR>);
              yTemp += sizeof(%<DTYPE_STR>);
              memcpy(yTemp, &zero, sizeof(%<DTYPE_STR>));
              yTemp += sizeof(%<DTYPE_STR>);
            }
          }
        %else
          %% Scalar I-O (input is real, output is complex)
          %<LibBlockOutputSignal(OUTPORT, "", "", "%<tRealPart>0")> = %<LibBlockInputSignal( INPORT, "", "", 0)>;
          %if LibIsDataTypeMultiWordFixpt(DTYPE_ID)
            %<LibBlockOutputSignal(OUTPORT, "", "", "%<tImagPart>0")> = %<FixPt_GetGroundValueOrNameFromDataTypeRec(FixPt_GetOutputDataType(OUTPORT), TLC_FALSE)>;
          %else
            %<LibBlockOutputSignal(OUTPORT, "", "", "%<tImagPart>0")> = %<SLibGetGroundValueFromId(DTYPE_ID)>;
          %endif
        %endif
      %endif
    %endif
  %else
    %% Assuming fixed point input
    %assign outLabel_re = LibBlockOutputSignal(OUTPORT, "idxSPL", "", "%<tRealPart>0")
    %assign outLabel_im = LibBlockOutputSignal(OUTPORT, "idxSPL", "", "%<tImagPart>0")
    %assign inLabel_re = LibBlockInputSignal( INPORT, "idxSPL", "", "%<tRealPart>0")
    %assign inLabel_im = LibBlockInputSignal( INPORT, "idxSPL", "", "%<tImagPart>0")
    %assign outDT = FixPt_GetOutputDataType(OUTPORT)
    {
      int idxSPL=0;
      for (; idxSPL<%<INP_WIDTH>; idxSPL++) {
        %<FixPt_Fix2FixAlwaysOutput(outLabel_re, outDT, inLabel_re, inDT, FixPtRoundingMode, FixPtSaturationMode)>/
        %if OUT_COMPLEX
          %if INP_COMPLEX
            %<FixPt_Fix2FixAlwaysOutput(outLabel_im, outDT, inLabel_im, inDT, FixPtRoundingMode, FixPtSaturationMode)>/
          %else
            %% Assign zero into imaginary part (input is real, output is complex)
            %<outLabel_im> = %<FixPt_GetGroundValueOrNameFromDataTypeRec(outDT, TLC_FALSE)>;
          %endif
        %else %% OUT_REAL
          %if INP_COMPLEX
            /* Discarding imaginary part of input */
          %endif
        %endif
      }
    }
  %endif
%endfunction %% LibBlockCopyInputsToOutputs
 
 
%endif %% _DSPLIB_
 
%%[EOF]spclib.tlc