%%
%%
%%
%%
%%Thisfilecontainstlccodeforgenerationoffixedpoint
%%lookuptables
%%
%%Copyright1994-2014TheMathWorks,Inc.
%%
 
%%
%%createvariabletoindicatethatthisfilehasalreadybeenloaded
%%
%if EXISTS("_FIXPTLOOK_") == 0
%assign _FIXPTLOOK_ = 1
 
%include "fixptlook_support.tlc"
 
%%Function:fxpInterpEvenBigProd=======================================
%%
%function fxpInterpEvenBigProd(...
            yAddrLabel, yDT ,...
            yLeftLabel, yRghtLabel, yDataDT,...
            xMinusXLeftLabel, xDT,...
            spacingLabel, ...
            roundMode,satMode) Output
  %%
  %<FixPt_FloatingPointNotSupported(yDT)>/
  %<FixPt_FloatingPointNotSupported(yDataDT)>/
  %<FixPt_FloatingPointNotSupported(xDT)>/
  %%
  %assign yRadixDT = FixPt_CopyDataTypeWithBoolReplace(yDT)
  %%
  %assign yRadixDT.Bias = 0.0
  %assign yRadixDT.FracSlope = 1.0
  %%
  %assign yDataRadixDT = FixPt_CopyDataTypeWithBoolReplace(yDataDT)
  %%
  %assign yDataRadixDT.Bias = 0.0
  %assign yDataRadixDT.FracSlope = 1.0
  %%
  %assign xRadixDT = FixPt_CopyDataTypeWithBoolReplace(xDT)
  %%
  %assign xRadixDT.Bias = 0.0
  %assign xRadixDT.FracSlope = 1.0
  %%
  %% Create a string to represent the utility
  %%
  %assign utilityName = FixPt_UtilityMakeName("INTERPOLATE_EVEN")
  %%
  %% identify output storage type
  %%
  %assign utilityName = FixPt_UtilityNameAppendDTPair(utilityName,yRadixDT,yDataRadixDT)
  %%
  %% identify NUMERATOR storage type
  %%
  %assign utilityName = FixPt_UtilityNameAppendDT(utilityName,xRadixDT)
  %%
  %assign utilityName = FixPt_UtilityNameAppendMode(utilityName,roundMode,satMode)
  %%
  %% END: Create a string to represent the utility
   
  %% Register utility name in the Shared Code Manager
  %if SLibInBlockScope()
    %assign block = SLibGetCurrentBlock()
  %else
    %assign block = ""
  %endif
  %assign utilityName = SLibRegisterSharedUtility(block, utilityName)
   
  %%
  %%
  %% Output a "call" to the required utility
  %%
  %<FcnTrackSharedUtilHeaderFileUsage(utilityName + ".h", TLC_FALSE)>
  %<utilityName>( %<yAddrLabel>, %<yLeftLabel>, %<yRghtLabel>, %<xMinusXLeftLabel>, %<spacingLabel>);
  %%
  %% determine if the required utility has already been defined
  %% If it has not, then create the definition.
  %%
  %if !(ISFIELD(FixPtUtils,utilityName))
    %%
    %% register that utility is being defined
    %%
    %assign tmpRet = SETFIELD(FixPtUtils,utilityName,1)
    %<LibPushEmptyStackSharedUtils()>/
    %%
    %% Get fixpt records for generic variables INSIDE this util
    %%
    %assign yn = yRadixDT.NativeType
    %assign xn = xRadixDT.NativeType
    %assign ydn = yDataRadixDT.NativeType
    %%
    %assign yLocalLabel = "*pY"
    %assign yLeftLocalLabel = "yL"
    %assign yRghtLocalLabel = "yR"
    %assign xLocalLabel = "xMinusxL"
    %assign xSpacingLabel = "xSpacing"
    %assign xSpacingType = xn
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %% open a buffer to hold the utility definition
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %openfile utilityDef
    %%
    %% create first line of utility
    %% case of C function
    %% including prototype definition
    %%
    %assign fcnAbstract = "Lookup Evenly Spaced Interpolation " + utilityName
    %assign fcnName = utilityName
    %assign fcnReturns = "void"
    %assign fcnParams = "%<yn> %<yLocalLabel>, " + ...
      "%<ydn> %<yLeftLocalLabel>, " + ...
      "%<ydn> %<yRghtLocalLabel>, " + ...
      "%<xn> %<xLocalLabel>, " + ...
      "%<xSpacingType> %<xSpacingLabel>"
    %assign funcDecRoot = fcnReturns + " " + fcnName + "(" + fcnParams + ")"
    %openfile funcProto
    %<funcDecRoot>;
    %closefile funcProto
    %selectfile utilityDef
    %createrecord fcnRec {Name fcnName; Returns fcnReturns; Params fcnParams; ...
      Abstract fcnAbstract; Category "utility"; GeneratedBy "fixptlook.tlc"}
    %<SLibDumpFunctionBanner(fcnRec)>
    %undef fcnRec
    %<funcDecRoot>
    {
      %%
      %% prep bigProduct variable
      %%
      %assign bigProdBits = yDataRadixDT.RequiredBits + xRadixDT.RequiredBits
      %%
      %assign isSign = yDataRadixDT.IsSigned || xRadixDT.IsSigned
      %%
      %if ( bigProdBits <= IntegerSizes.IntNumBits )
        %assign freeBits = IntegerSizes.IntNumBits - bigProdBits
        %assign bigProdBits = IntegerSizes.IntNumBits
      %else
        %assign freeBits = IntegerSizes.LongNumBits - bigProdBits
        %assign bigProdBits = IntegerSizes.LongNumBits
      %endif
      %%
      %assign fixExp = yDataRadixDT.FixedExp + xRadixDT.FixedExp
      %%
      %if freeBits > 0
        %%
        %assign bigProdExtraShift = ...
          ( yDataDT.FixedExp - yDT.FixedExp )
        %% - ( xDataDT.FixedExp - xDT.FixedExp ) No DataDT for even space case
        %%
        %if bigProdExtraShift > 0
          %%
          %if bigProdExtraShift > freeBits
            %%
            %assign fixExp = fixExp - freeBits
            %%
          %else
            %%
            %assign fixExp = fixExp - bigProdExtraShift
            %%
          %endif
        %endif
      %endif
      %%
      %assign dataTypeName = LibFixPointFormDataTypeName(isSign,bigProdBits,fixExp)
      %%
      %createrecord bigProdDT { ...
        DataTypeName dataTypeName; ...
        IsSigned isSign; ...
        RequiredBits bigProdBits; ...
        ActualBits bigProdBits; ...
        FixedExp fixExp; ...
        FracSlope 1.0; ...
        Bias 0.0; ...
        NativeType "#error x" ...
      }
      %%
      %<FixPt_DefineDataType(bigProdDT)>/
      %%
      %assign bigProdLocalLabel = "bigProd"
      %%
      %% create variable to hold the diffs
      %%
      %assign yDiffLocalLabel = "yDiff"
      %%
      %assign xDenLocalLabel = "xDen"
      %%
      %% declare variables
      %%
      %<bigProdDT.NativeType> %<bigProdLocalLabel>;
      %<yDataRadixDT.NativeType> %<yDiffLocalLabel>;
      %%
      %if FixPt_DataTypesSame(yRadixDT,yDataRadixDT)
        %%
        %assign yAdjustLocalLabel = yDiffLocalLabel
      %else
        %%
        %assign yAdjustLocalLabel = "yAjust"
        %%
        %<yRadixDT.NativeType> %<yAdjustLocalLabel>;
      %endif
      %%
      %<FixPt_Fix2FixAlwaysOutput(yLocalLabel,yRadixDT,yLeftLocalLabel,yDataRadixDT,roundMode,satMode)>/
       
      %if yRadixDT.IsSigned
        %%
        %<yDiffLocalLabel> = %<yRghtLocalLabel>;
        %<FixPt_AccumNeg(yDiffLocalLabel, yDataRadixDT,yLeftLocalLabel, yDataRadixDT,satMode)>
         
        %<FixPt_Multiply(bigProdLocalLabel, bigProdDT,yDiffLocalLabel, yDataRadixDT,xLocalLabel, xRadixDT,roundMode,satMode)>
         
        %<FixPt_Division(yAdjustLocalLabel, yRadixDT,bigProdLocalLabel, bigProdDT,xSpacingLabel, xRadixDT,roundMode,satMode)>
         
        %<FixPt_AccumPos(yLocalLabel, yRadixDT,yAdjustLocalLabel, yRadixDT,satMode)>
      %else
        %%
        if ( %<yRghtLocalLabel> >= %<yLeftLocalLabel> )
        {
          %<yDiffLocalLabel> = %<yRghtLocalLabel>;
          %<FixPt_AccumNeg(yDiffLocalLabel, yDataRadixDT,yLeftLocalLabel, yDataRadixDT,satMode)>
        }
        else
        {
          %<yDiffLocalLabel> = %<yLeftLocalLabel>;
          %<FixPt_AccumNeg(yDiffLocalLabel, yDataRadixDT,yRghtLocalLabel, yDataRadixDT,satMode)>
        }
         
        %<FixPt_Multiply(bigProdLocalLabel, bigProdDT,yDiffLocalLabel, yDataRadixDT,xLocalLabel, xRadixDT,roundMode,satMode)>
         
        %<FixPt_Division(yAdjustLocalLabel, yRadixDT,bigProdLocalLabel, bigProdDT,xSpacingLabel, xRadixDT,roundMode,satMode)>
         
        %%
        if ( %<yRghtLocalLabel> >= %<yLeftLocalLabel> )
        {
          %<FixPt_AccumPos(yLocalLabel, yRadixDT,yAdjustLocalLabel, yRadixDT,satMode)>
        }
        else
        {
          %<FixPt_AccumNeg(yLocalLabel, yRadixDT,yAdjustLocalLabel, yRadixDT,satMode)>
        }
        %%
      %endif
    }
    %closefile utilityDef
    %<SLibAddMathIncludeForUtilsSourceCode(utilityDef)>
    %<SLibDumpUtilsSourceCodeAndCacheFunctionPrototype(utilityName,funcProto,utilityDef)>/
    %%
    %assign GSUStackBuf = LibPopStackSharedUtilsIncludes()
  %endif %% definition of utility
  %%
%endfunction %% fxpInterpEvenBigProd
 
 
 
%%Function:fxpInterpUnevenBigProd==============================
%%
%%
%function fxpInterpUnevenBigProd(...
            yAddrLabel, yDT, ...
            yLeftLabel, yRghtLabel, yDataDT, ...
            xLabel, xDT, ...
            xLeftLabel, xRghtLabel, xDataDT, ...
            roundMode,satMode) Output
    %%
    %<FixPt_FloatingPointNotSupported(yDT)>/
    %<FixPt_FloatingPointNotSupported(yDataDT)>/
    %<FixPt_FloatingPointNotSupported(xDT)>/
    %<FixPt_FloatingPointNotSupported(xDataDT)>/
    %%
    %assign yRadixDT = FixPt_CopyDataTypeWithBoolReplace(yDT)
    %%
    %assign yRadixDT.Bias = 0.0
    %assign yRadixDT.FracSlope = 1.0
    %%
    %assign yDataRadixDT = FixPt_CopyDataTypeWithBoolReplace(yDataDT)
    %%
    %assign yDataRadixDT.Bias = 0.0
    %assign yDataRadixDT.FracSlope = 1.0
    %%
    %assign xRadixDT = FixPt_CopyDataTypeWithBoolReplace(xDT)
    %%
    %assign xRadixDT.Bias = 0.0
    %assign xRadixDT.FracSlope = 1.0
    %%
    %assign xDataRadixDT = FixPt_CopyDataTypeWithBoolReplace(xDataDT)
    %%
    %assign xDataRadixDT.Bias = 0.0
    %assign xDataRadixDT.FracSlope = 1.0
    %%
    %% Create a string to represent the utility
    %%
    %assign utilityName = FixPt_UtilityMakeName("INTERPOLATE")
    %%
    %% identify output storage type
    %%
    %assign utilityName = FixPt_UtilityNameAppendDTPair(utilityName,yRadixDT,yDataRadixDT)
    %%
    %% identify NUMERATOR storage type
    %%
    %assign utilityName = FixPt_UtilityNameAppendDTPair(utilityName,xRadixDT,xDataRadixDT)
    %%
    %assign utilityName = FixPt_UtilityNameAppendMode(utilityName,roundMode,satMode)
    %%
    %% END: Create a string to represent the utility
     
    %% Register utility name in the Shared Code Manager
    %if SLibInBlockScope()
      %assign block = SLibGetCurrentBlock()
    %else
      %assign block = ""
    %endif
    %assign utilityName = SLibRegisterSharedUtility(block, utilityName)
     
    %%
    %%
    %% Output a "call" to the required utility
    %%
    %<FcnTrackSharedUtilHeaderFileUsage(utilityName + ".h", TLC_FALSE)>
    %<utilityName>( %<yAddrLabel>, %<yLeftLabel>, %<yRghtLabel>, %<xLabel>, %<xLeftLabel>, %<xRghtLabel>);
    %%
    %% determine if the required utility has already been defined
    %% If it has not, then create the definition.
    %%
    %if !(ISFIELD(FixPtUtils,utilityName))
      %%
      %% register that utility is being defined
      %%
      %assign tmpRet = SETFIELD(FixPtUtils,utilityName,1)
      %<LibPushEmptyStackSharedUtils()>/
      %%
      %% Get fixpt records for generic variables INSIDE this util
      %%
      %assign yLocalLabel = "*pY"
      %assign yLeftLocalLabel = "yL"
      %assign yRghtLocalLabel = "yR"
      %assign xLocalLabel = "x"
      %assign xLeftLocalLabel = "xL"
      %assign xRghtLocalLabel = "xR"
      %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      %% open a buffer to hold the utility definition
      %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      %openfile utilityDef
      %%
      %% create first line of utility
      %% case of C function
      %% including prototype definition
      %%
      %assign yn = yRadixDT.NativeType
      %assign xn = xRadixDT.NativeType
      %assign ydn = yDataRadixDT.NativeType
      %assign xdn = xDataRadixDT.NativeType
      %%
      %assign fcnAbstract = "Lookup Interpolation " + utilityName
      %assign fcnName = utilityName
      %assign fcnReturns = "void"
      %assign fcnParams = "%<yn> %<yLocalLabel>, " + ...
        "%<ydn> %<yLeftLocalLabel>, " + ...
        "%<ydn> %<yRghtLocalLabel>, " + ...
        "%<xn> %<xLocalLabel>, " + ...
        "%<xdn> %<xLeftLocalLabel>, " + ...
        "%<xdn> %<xRghtLocalLabel>"
      %assign funcDecRoot = fcnReturns + " " + fcnName + "(" + fcnParams + ")"
      %%
      %openfile funcProto
      %<funcDecRoot>;
      %closefile funcProto
      %%
      %selectfile utilityDef
      %createrecord fcnRec {Name fcnName; Returns fcnReturns; Params fcnParams; ...
        Abstract fcnAbstract; Category "utility"; GeneratedBy "fixptlook.tlc"}
      %<SLibDumpFunctionBanner(fcnRec)>
      %undef fcnRec
      %<funcDecRoot>
      {
        %%
        %% prep bigProduct variable
        %%
        %if xRadixDT.RequiredBits > xDataRadixDT.RequiredBits
          %assign bigProdBits = yDataRadixDT.RequiredBits + xRadixDT.RequiredBits
        %else
          %assign bigProdBits = yDataRadixDT.RequiredBits + xDataRadixDT.RequiredBits
        %endif
        %%
        %assign isSign = yDataRadixDT.IsSigned || xRadixDT.IsSigned
        %%
        %if ( bigProdBits <= IntegerSizes.IntNumBits )
          %assign freeBits = IntegerSizes.IntNumBits - bigProdBits
          %assign bigProdBits = IntegerSizes.IntNumBits
        %else
          %assign freeBits = IntegerSizes.LongNumBits - bigProdBits
          %assign bigProdBits = IntegerSizes.LongNumBits
        %endif
        %%
        %assign fixExp = yDataRadixDT.FixedExp + xRadixDT.FixedExp
        %%
        %if freeBits > 0
          %%
          %assign bigProdExtraShift = ...
            ( yDataDT.FixedExp - yDT.FixedExp ) - ...
            ( xDataDT.FixedExp - xDT.FixedExp )
          %%
          %if bigProdExtraShift > 0
            %%
            %if bigProdExtraShift > freeBits
              %%
              %assign fixExp = fixExp - freeBits
              %%
            %else
              %%
              %assign fixExp = fixExp - bigProdExtraShift
              %%
            %endif
          %endif
        %endif
        %%
        %assign dataTypeName = LibFixPointFormDataTypeName(isSign,bigProdBits,fixExp)
        %%
        %createrecord bigProdDT { ...
          DataTypeName dataTypeName; ...
          IsSigned isSign; ...
          RequiredBits bigProdBits; ...
          ActualBits bigProdBits; ...
          FixedExp fixExp; ...
          FracSlope 1.0; ...
          Bias 0.0; ...
          NativeType "#error x" ...
        }
        %%
        %<FixPt_DefineDataType(bigProdDT)>/
        %%
        %assign bigProdLocalLabel = "bigProd"
        %%
        %% create variable to hold the diffs
        %%
        %assign yDiffLocalLabel = "yDiff"
        %%
        %assign xNumLocalLabel = "xNum"
        %%
        %assign xDenLocalLabel = "xDen"
        %%
        %% declare variables
        %%
        %<bigProdDT.NativeType> %<bigProdLocalLabel>;
        %<yDataRadixDT.NativeType> %<yDiffLocalLabel>;
        %<xRadixDT.NativeType> %<xNumLocalLabel>;
        %<xDataRadixDT.NativeType> %<xDenLocalLabel>;
        %%
        %if FixPt_DataTypesSame(yRadixDT,yDataRadixDT)
          %%
          %assign yAdjustLocalLabel = yDiffLocalLabel
        %else
          %%
          %assign yAdjustLocalLabel = "yAdjust"
          %%
          %<yRadixDT.NativeType> %<yAdjustLocalLabel>;
        %endif
        %%
        %if FixPt_DataTypesSame(xRadixDT,xDataRadixDT)
          %%
          %assign castXLeftLocalLabel = xLeftLocalLabel
        %else
          %%
          %assign castXLeftLocalLabel = "castXLeft"
          %%
          %<xRadixDT.NativeType> %<castXLeftLocalLabel>;
          %%
          %<FixPt_Fix2FixAlwaysOutput(castXLeftLocalLabel,xRadixDT,xLeftLocalLabel,xDataRadixDT,roundMode,satMode)>/
        %endif
         
        %<FixPt_Fix2FixAlwaysOutput(yLocalLabel,yRadixDT,yLeftLocalLabel,yDataRadixDT,roundMode,satMode)>/
         
        /* If %<xLocalLabel> is not strictly between %<xRghtLocalLabel> and %<xLeftLocalLabel>
         * then an interpolation calculation is not necessary %<xLocalLabel> == %<xLeftLocalLabel>
         * or not valid. The invalid situation is expected when the input
         * is beyond the left or right end of the table. The design is
         * that %<yLeftLocalLabel> holds the correct value for %<yLocalLabel>
         * in invalid situations.
         */
        if ( (%<xRghtLocalLabel> > %<xLeftLocalLabel>) && (%<xLocalLabel> > %<castXLeftLocalLabel>) )
        {
          %<xDenLocalLabel> = %<xRghtLocalLabel>;
          %<FixPt_AccumNeg(xDenLocalLabel, xDataRadixDT,xLeftLocalLabel, xDataRadixDT,satMode)>
           
          %<xNumLocalLabel> = %<xLocalLabel>;
          %<FixPt_AccumNeg(xNumLocalLabel, xRadixDT,castXLeftLocalLabel, xRadixDT,satMode)>
           
          %if yRadixDT.IsSigned
            %%
            %<yDiffLocalLabel> = %<yRghtLocalLabel>;
            %<FixPt_AccumNeg(yDiffLocalLabel, yDataRadixDT,yLeftLocalLabel, yDataRadixDT,satMode)>
             
            %<FixPt_Multiply(bigProdLocalLabel, bigProdDT,yDiffLocalLabel, yDataRadixDT,xNumLocalLabel, xRadixDT,roundMode,satMode)>
             
            %<FixPt_Division(yAdjustLocalLabel, yRadixDT,bigProdLocalLabel, bigProdDT,xDenLocalLabel, xDataRadixDT,roundMode,satMode)>
             
            %<FixPt_AccumPos(yLocalLabel, yRadixDT,yAdjustLocalLabel, yRadixDT,satMode)>
          %else
            %%
            if ( %<yRghtLocalLabel> >= %<yLeftLocalLabel> )
            {
              %<yDiffLocalLabel> = %<yRghtLocalLabel>;
              %<FixPt_AccumNeg(yDiffLocalLabel, yDataRadixDT,yLeftLocalLabel, yDataRadixDT,satMode)>
            }
            else
            {
              %<yDiffLocalLabel> = %<yLeftLocalLabel>;
              %<FixPt_AccumNeg(yDiffLocalLabel, yDataRadixDT,yRghtLocalLabel, yDataRadixDT,satMode)>
            }
             
            %<FixPt_Multiply(bigProdLocalLabel, bigProdDT,yDiffLocalLabel, yDataRadixDT,xNumLocalLabel, xRadixDT,roundMode,satMode)>
             
            %<FixPt_Division(yAdjustLocalLabel, yRadixDT,bigProdLocalLabel, bigProdDT,xDenLocalLabel, xDataRadixDT,roundMode,satMode)>
             
            %%
            if ( %<yRghtLocalLabel> >= %<yLeftLocalLabel> )
            {
              %<FixPt_AccumPos(yLocalLabel, yRadixDT,yAdjustLocalLabel, yRadixDT,satMode)>
            }
            else
            {
              %<FixPt_AccumNeg(yLocalLabel, yRadixDT,yAdjustLocalLabel, yRadixDT,satMode)>
            }
            %%
          %endif
        }
      }
      %closefile utilityDef
      %%
      %<SLibDumpUtilsSourceCodeAndCacheFunctionPrototype(utilityName,funcProto,utilityDef)>/
      %%
      %assign GSUStackBuf = LibPopStackSharedUtilsIncludes()
    %endif %% definition of utility
%endfunction %% fxpInterpUnevenBigProd
 
 
 
%%Function:fxpInterpolateGetLambdaDT================================
%%
%function fxpInterpolateGetLambdaDT( ...
            yDT, yDataDT, xDT, xDataDT, ...
            spacingIsPow2, spacingPow2Exp, ...
            allowPow2Optimization ) void
    %%
    %% prep lambda variable
    %% the definition of the data type will be completed in the calc func
    %%
    %if FixPt_DataTypeIsFloat(yDT) || ...
        FixPt_DataTypeIsFloat(yDataDT) || ...
        FixPt_DataTypeIsFloat(xDT) || ...
        FixPt_DataTypeIsFloat(xDataDT)
      %%
      %assign doFloat = 1
      %%
      %if !FixPt_DataTypeIsSingle(yDT) || ...
          !FixPt_DataTypeIsSingle(yDataDT) || ...
          !FixPt_DataTypeIsSingle(xDT) || ...
          !FixPt_DataTypeIsSingle(xDataDT)
        %%
        %assign lambdaDT = FixPt_GetDataTypeFromIndex(0)
      %else
        %%
        %assign lambdaDT = FixPt_GetDataTypeFromIndex(1)
      %endif
      %%
    %else
      %%
      %if yDT.RequiredBits > yDataDT.RequiredBits
        %%
        %assign maxOutReqBits = yDT.RequiredBits
        %assign maxOutActBits = yDT.ActualBits
      %else
        %%
        %assign maxOutReqBits = yDataDT.RequiredBits
        %assign maxOutActBits = yDataDT.ActualBits
      %endif
      %%
      %if xDT.RequiredBits > xDataDT.RequiredBits
        %%
        %assign maxInReqBits = xDT.RequiredBits
        %assign maxInActBits = xDT.ActualBits
      %else
        %%
        %assign maxInReqBits = xDataDT.RequiredBits
        %assign maxInActBits = xDataDT.ActualBits
      %endif
      %%
      %if maxInReqBits > maxOutReqBits
        %%
        %assign lambdaReqBits = maxInReqBits
        %assign lambdaActBits = maxInActBits
      %else
        %%
        %assign lambdaReqBits = maxOutReqBits
        %assign lambdaActBits = maxOutActBits
      %endif
      %%
      %assign isSigned = 0
      %%
      %if allowPow2Optimization && spacingIsPow2
        %%
        %% for evenly space powers of 2
        %% on-line division is NOT needed
        %% off-line the fixed exponent is set to account
        %% for that conceptual division by a power of 2
        %%
        %assign fixedExp = -1*spacingPow2Exp
        %%
      %else
        %%
        %% set the fixed exponent to make lambda a "fractional" type
        %% for the case when actual on-line division will take place
        %%
        %assign fixedExp = (1+isSigned-lambdaReqBits)
        %%
      %endif
      %%
      %assign dataTypeName = LibFixPointFormDataTypeName(isSigned,lambdaReqBits,fixedExp)
      %%
      %createrecord lambdaDT { ...
        DataTypeName dataTypeName; ...
        IsSigned isSigned; ...
        RequiredBits lambdaReqBits; ...
        ActualBits lambdaActBits; ...
        FixedExp fixedExp; ...
        FracSlope 1.0; ...
        Bias 0.0; ...
        NativeType "#error x" ...
      }
      %%
      %<FixPt_DefineDataType(lambdaDT)>/
      %%
    %endif
    %%
    %return lambdaDT
    %%
%endfunction %% fxpInterpolateGetLambdaDT
 
 
 
%%Function:fxpInterpolateGetLambdaDTUneven=============================
%%
%function fxpInterpolateGetLambdaDTUneven( ...
            yDT, yDataDT, xDT, xDataDT, ...
            allowPow2Optimization ) void
  %%
  %assign mootBreakPointInfo = FixPt_BreakPointInfoDefault()
  %%
  %assign lambdaDT = fxpInterpolateGetLambdaDT( ...
            yDT, yDataDT, xDT, xDataDT, ...
            mootBreakPointInfo.spacingIsPow2, ...
            mootBreakPointInfo.spacingPow2Exp, ...
            allowPow2Optimization )
  %%
  %return lambdaDT
  %%
%endfunction %% fxpInterpolateGetLambdaDTUneven
             
             
             
%%Function:fxpInterpolateCalcLambdaPow2================================
%%
%function fxpInterpolateCalcLambdaPow2( ...
           uAdjLabel, u0DT,...
           lambdaLabel, lambdaDT,...
           pow2ExpValue, ...
           roundMode, satMode) Output
  %%
  %assert !FixPt_DataTypeIsFloat(lambdaDT)
  %%
  %assert pow2ExpValue == -lambdaDT.FixedExp
  %%
  %% rather THAN do a remainder operation
  %% because the divisor is a constant power of 2
  %% use mask to quickly get lambda
  %%
  /* The table is inlined with even spacing 2^%<pow2ExpValue>, so
   * the interpolation factor is the least significant %<pow2ExpValue> bits. */
  %%
  %assign mask = SetLSNBitsStr(pow2ExpValue,u0DT.ActualBits,0)
  %%
  %<lambdaLabel> = %<uAdjLabel> & %<mask>;
  %%
%endfunction %% fxpInterpolateCalcLambdaPow2
 
 
 
%%Function:fxpInterpolateCalcLambdaEven================================
%%
%function fxpInterpolateCalcLambdaEven( ...
           uAdjLabel, u0DT,...
           lambdaLabel, lambdaDT,...
           spacingValue, ...
           iLeftLabel,...
           roundMode, satMode) Output
  %%
  %% create a data type record for the den and num
  %%
  %assign deltaDT = FixPt_CopyDataTypeWithBoolReplace(u0DT)
  %%
  %assign deltaDT.Bias = 0.0
  %%
  %% Get numerator rec
  %%
  %assign numLabel = "num"
  %%
  %assign unsignType = utilFloatOrUnsignedTypeFromDT(u0DT)
  %%
  %% Note it would be easier to subtract of Xleft, but
  %% we are not storing the vector of breakpoints for the evenly
  %% spaced case inorder to save ROM
  %%
  {
    %<u0DT.NativeType> %<numLabel> = (%<unsignType>)%<uAdjLabel> - ( %<iLeftLabel> * %<spacingValue> );
   
    %%
    %% initialize denominator to delta
    %%
    %assign denLabel = STRING(spacingValue)
    %%
    %% lambda = num/den
    %%
    %% Division by zero is NOT possible
    %% equal x values is protected above
    %%
    %<FixPt_Division(lambdaLabel,lambdaDT,numLabel,deltaDT,denLabel,deltaDT,roundMode,satMode)>/
    %%
  }
%endfunction %% fxpInterpolateCalcLambdaEven
 
 
 
%%Function:fxpInterpolateCalcLambdaEven2================================
%%
%function fxpInterpolateCalcLambdaEven2( ...
           uMinusLeftLabel, u0DT,...
           lambdaLabel, lambdaDT,...
           spacingValue, ...
           roundMode, satMode) Output
  %%
  %% create a data type record for the den and num
  %%
  %assign deltaDT = FixPt_CopyDataTypeWithBoolReplace(u0DT)
  %%
  %assign deltaDT.Bias = 0.0
  %%
  %% lambda = num/den
  %%
  %% Division by zero is NOT possible
  %% equal x values is protected above
  %%
  %<FixPt_Division(...
    lambdaLabel, lambdaDT, ...
    uMinusLeftLabel, deltaDT, ...
    STRING(spacingValue), deltaDT, ...
    roundMode,satMode)>/
  %%
%endfunction %% fxpInterpolateCalcLambdaEven2
 
%%Function:fxpInterpolateGetNumDataType================================
%%
%function fxpInterpolateGetNumDataType( ...
  u0DT, ...
  u0DataDT, ...
  lambdaDT) Output
  %if FixPt_DataTypeIsFloat">FixPt_DataTypeIsFloat(u0DT) || FixPt_DataTypeIsFloat">FixPt_DataTypeIsFloat(u0DataDT)
    %return lambdaDT
  %else
    %return u0DT
  %endif
%endfunction %% fxpInterpolateGetNumDenDataType
 
%%Function:fxpInterpolateGetDenDataType================================
%%
%function fxpInterpolateGetDenDataType( ...
  u0DT, ...
  u0DataDT, ...
  lambdaDT) Output
  %if FixPt_DataTypeIsFloat">FixPt_DataTypeIsFloat(u0DT) || FixPt_DataTypeIsFloat">FixPt_DataTypeIsFloat(u0DataDT)
    %return lambdaDT
  %else
    %return u0DataDT
  %endif
%endfunction %% fxpInterpolateGetNumDenDataType
 
%%Function:fxpInterpolateCalcLambdaUneven================================
%%
%function fxpInterpolateCalcLambdaUneven( ...
           u0Label, u0DT,...
           lambdaLabel, lambdaDT,...
           u0LeftLabel, u0RghtLabel, u0DataDT, ...
           roundMode,satMode) Output
    %%
    %% Check that storage types and scaling are supported
    %% and biases are zero
    %%
    %<FixPt_CheckInputBreakpointDataTypes(u0DT,u0DataDT)>/
    %%
    if ( %<u0RghtLabel> > %<u0LeftLabel> )
    {
      %% find the data type used for the den/num.
       
      %%
      %%
      %assign numDT = fxpInterpolateGetNumDataType(u0DT, u0DataDT, lambdaDT)
      %assign denDT = fxpInterpolateGetDenDataType(u0DT, u0DataDT, lambdaDT)
      %% create a data type record for the den and num
      %%
      %assign deltaDT = FixPt_CopyDataTypeWithBoolReplace(numDT)
      %%
      %assign deltaDT.Bias = 0.0
      %%
      %% Get numerator rec
      %%
      %assign numLabel = "num"
      %%
      %% Get denominator rec
      %%
      %assign denLabel = "den"
      %%
      %if FixPt_DataTypesSame(numDT,denDT)
        %assign deltaDenDT = deltaDT
      %else
        %%
        %% create a data type record for the den and num
        %%
        %assign deltaDenDT = FixPt_CopyDataTypeWithBoolReplace(denDT)
        %%
        %assign deltaDenDT.Bias = 0.0
      %endif
      %%
      %% declare local variables
      %%
      %<numDT.NativeType> %<numLabel>;
      %<denDT.NativeType> %<denLabel>;
       
      %%
      %% initialize denominator to right value
      %%
      %<denLabel> = %<u0RghtLabel>;
      %%
      %% subtract left value to make denominator equal to the
      %% delta = right - left
      %%
      %% for the evenly spaced non-power of 2 case
      %% the whole subtraction could be eliminated
      %% but there is the possibility u is signed and delta is
      %% actually bigger than would fit in a signed number
      %% Keeping overflow behavior consistent with what happened
      %% in simulation could be cumbersome. For now, the Accumulation
      %% function will be employed to maintain Bit True simulations.
      %%
      %<FixPt_AccumNeg(denLabel,deltaDenDT,u0LeftLabel,deltaDenDT,satMode)>/
      %%
      %%
      %% initialize numerator to current value
      %if FixPt_DataTypeIsFloat">FixPt_DataTypeIsFloat(u0DataDT) && !FixPt_DataTypeIsFloat">FixPt_DataTypeIsFloat(u0DT)
        %%It is not allowed when input is float but bp is fixed-point.
        %%no saturation code or rounding code are generated.
        %%The block requires thay the input data type has less precision than the breakpoint data type.
        /* cast current input to the data type/scaling of the breakpoint data */
        %<FixPt_Fix2FixAlwaysOutput(numLabel,u0DataDT,u0Label,u0DT,"Floor","Saturate")>
      %else
        %%when both are fixed-point. only stored integers are in the operations.
        %%no casts are needed.
        %<numLabel> = %<u0Label>;
      %endif
      %%
      %%
      %% subtract left value to make numerator equal to the
      %% delta = current - left
      %%
      %if FixPt_DataTypesSame(deltaDT,deltaDenDT)
        %<FixPt_AccumNeg(numLabel,deltaDT,u0LeftLabel,deltaDenDT,satMode)>/
      %else
        %assign tempVarName = "tempConversion"
        %openfile captureConvert
        %assign retVecStr = FixPt_Fix2Fix(tempVarName,deltaDT,u0LeftLabel,deltaDenDT,roundMode,satMode)
        %closefile captureConvert
        %%
        %if SIZE(retVecStr,1) == 3
          %<FixPt_AccumNeg(numLabel,deltaDT,retVecStr[1],deltaDT,satMode)>/
        %else
          {
            %<deltaDT.NativeType> %<tempVarName>;
            %<captureConvert>/
            %<FixPt_AccumNeg(numLabel,deltaDT,tempVarName,deltaDT,satMode)>/
          }
        %endif
      %endif
      %%
      %% lambda = num/den
      %%
      %% Division by zero is NOT possible
      %% equal x values is protected above
      %%
      %<FixPt_Division(lambdaLabel,lambdaDT,numLabel,deltaDT,denLabel,deltaDenDT,roundMode,satMode)>/
      %%
    }
    else
    {
      %<lambdaLabel> = %<FixPt_GetGroundValueOrNameFromDataTypeRec(lambdaDT, TLC_FALSE)>;
    }
%endfunction %% fxpInterpolateCalcLambdaUneven
 
 
 
%%Function:fxpInterpolateApplyLambda======================================
%%
%%Abstract:Generatecodeforfindingthelambdamultipliertobeusedin
%%aninterpolation
%%
%%Synopsis:
%%
%%Ifthedatatypeandscalingfortheinputandthebreakpointsarenot
%%thesame,thentheinputsdatatypeandscalingwilldominate.
%%
%function fxpInterpolateApplyLambda(...
           yLabel, yDT,...
           yLeftLabel, yRghtLabel, yDataDT,...
           lambdaLabel,lambdaDT,...
           roundMode,satMode) Output
    %%
    %assign yRadixDT = FixPt_CopyDataTypeWithBoolReplace(yDT)
    %%
    %assign yRadixDT.Bias = 0.0
    %assign yRadixDT.FracSlope = 1.0
    %%
    %assign yDataRadixDT = FixPt_CopyDataTypeWithBoolReplace(yDataDT)
    %%
    %assign yDataRadixDT.Bias = 0.0
    %assign yDataRadixDT.FracSlope = 1.0
    %%
    %% do lambda interp
    %%
    %if FixPt_DataTypeIsFloat(lambdaDT)
        %%
        %% assuming lambda is an unscaled double or single
        %%
        {
            %%
            %% This needs to be more efficient
            %assign tempLabela = "yLeftCast"
            %assign tempLabelb = "yRghtCast"
            %%
            %<lambdaDT.NativeType> %<tempLabela>;
            %<lambdaDT.NativeType> %<tempLabelb>;
            %%
            %<FixPt_Fix2FixAlwaysOutput(tempLabela,lambdaDT,yLeftLabel,yDataRadixDT,roundMode,satMode)>/
            %<FixPt_Fix2FixAlwaysOutput(tempLabelb,lambdaDT,yRghtLabel,yDataRadixDT,roundMode,satMode)>/
            %%
            %<tempLabela> += %<lambdaLabel> * ( %<tempLabelb> - %<tempLabela> );
            %%
            %<FixPt_Fix2FixAlwaysOutput(yLabel,yRadixDT,tempLabela,lambdaDT,roundMode,satMode)>/
        }
    %elseif FixPt_DataTypeIsFloat(yRadixDT) || (yRadixDT.IsSigned && yDataRadixDT.IsSigned)
        %%
        %if FixPt_DataTypesSame(yRadixDT,yDataRadixDT) && yLabel != yLeftLabel
            %%
            %% SAME DATA TYPE case
            %%
            %if yLabel != yRghtLabel
                %<yLabel> = %<yRghtLabel>;
            %endif
            %%
            %<FixPt_AccumNeg(yLabel,yRadixDT,yLeftLabel,yRadixDT,satMode)>/
            %%
            %% get first half of output
            %%
            %<FixPt_Multiply(yLabel,yRadixDT,lambdaLabel,lambdaDT,yLabel,yRadixDT,roundMode,satMode)>/
            %%
            %% combine two halfs of output
            %% normally rounding is NOT an issue
            %% possibly in very rare cases rounding could case extra
            %% bits that lead to overflow
            %%
            %<FixPt_AccumPos(yLabel,yRadixDT,yLeftLabel,yRadixDT,satMode)>/
        %else
            %%
            %% NOT the same DATA TYPE case
            %% OR
            %% yLabel and yLeftLabel are the same beast so we must
            %% be careful not to wipe out value before we're through with it
            %%
            %assign tempDiffVarName = "tempDiff"
            %assign tempConvertVarName = "tempConversion"
            %%
            %openfile captureConvert
                %%
                %assign retVecStr = FixPt_Fix2Fix(tempConvertVarName,yRadixDT,yLeftLabel,yDataRadixDT,roundMode,satMode)
                %%
                %if SIZE(retVecStr,1) != 3 || yLabel == yLeftLabel
                    %assign needTempConvertVar = 1
                %else
                    %assign needTempConvertVar = 0
                %endif
            %closefile captureConvert
            %%
            {
                %<yDataRadixDT.NativeType> %<tempDiffVarName>;
                %if needTempConvertVar
                    %<yRadixDT.NativeType> %<tempConvertVarName>;
                %endif
                %%
                %if needTempConvertVar
                    %<captureConvert>/
                    %if SIZE(retVecStr,1) == 3
                        %
                    %endif
                %endif
                %%
                %<tempDiffVarName> = %<yRghtLabel>;
                %%
                %<FixPt_AccumNeg(tempDiffVarName,yDataRadixDT,yLeftLabel,yDataRadixDT,satMode)>/
                %%
                %% get first half of output
                %%
                %<FixPt_Multiply(yLabel,yRadixDT,lambdaLabel,lambdaDT,tempDiffVarName,yDataRadixDT,roundMode,satMode)>/
                %%
                %% combine two halfs of output
                %% normally rounding is NOT an issue
                %% possibly in very rare cases rounding could case extra
                %% bits that lead to overflow
                %%
                %if needTempConvertVar
                    %<FixPt_AccumPos(yLabel,yRadixDT,tempConvertVarName,yRadixDT,satMode)>/
                %else
                    %<FixPt_AccumPos(yLabel,yRadixDT,retVecStr[1],yRadixDT,satMode)>/
                %endif
            }
        %endif
    %else
        {
            %assign yTempLabel2 = "yTemp2"
            %assign absDiffLabel = "absDiff"
            %%
            %<yRadixDT.NativeType> %<yTempLabel2>;
            %<yDataRadixDT.NativeType> %<absDiffLabel>;
            %%
            %if yLabel != yLeftLabel
                %<FixPt_Fix2FixAlwaysOutput(yLabel,yRadixDT,yLeftLabel,yDataRadixDT,roundMode,satMode)>/
            %endif
            %%
            if ( %<yRghtLabel> >= %<yLeftLabel> )
            {
                %<absDiffLabel> = %<yRghtLabel>;
                %%
                %<FixPt_AccumNeg(absDiffLabel,yDataRadixDT,yLeftLabel,yDataRadixDT,satMode)>/
                %%
                %<FixPt_Multiply(yTempLabel2,yRadixDT,lambdaLabel,lambdaDT,absDiffLabel,yDataRadixDT,roundMode,satMode)>/
                %%
                %<FixPt_AccumPos(yLabel,yRadixDT,yTempLabel2,yRadixDT,satMode)>/
            }
            else
            {
                %<absDiffLabel> = %<yLeftLabel>;
                %%
                %<FixPt_AccumNeg(absDiffLabel,yDataRadixDT,yRghtLabel,yDataRadixDT,satMode)>/
                %%
                %<FixPt_Multiply(yTempLabel2,yRadixDT,lambdaLabel,lambdaDT,absDiffLabel,yDataRadixDT,roundMode,satMode)>/
                %%
                %<FixPt_AccumNeg(yLabel,yRadixDT,yTempLabel2,yRadixDT,satMode)>/
            }
        }
    %endif
    %%
%endfunction %% fxpInterpolateApplyLambda
 
 
%function utilDecideSearchMethod(xDT,xDataDT,xDataBreakPointInfo)
  %%
  %<FixPt_CheckInputBreakpointDataTypes(xDT,xDataDT)>/
  %%
  %if !FixPt_DataTypeIsFloat(xDT) && ...
      !FixPt_DataTypeIsFloat(xDataDT) && ...
      ISEQUAL(xDataBreakPointInfo.spacingValue,1) && ...
      (xDT.FixedExp == xDataDT.FixedExp)
    %%
    %assign searchMethod = searchTrivial
    %%
  %else
    %%
    %if xDataBreakPointInfo.evenlySpaced
      %%
      %if !FixPt_DataTypeIsFloat(xDT) && ...
          !FixPt_DataTypeIsFloat(xDataDT) && ...
          xDataBreakPointInfo.spacingIsPow2
        %%
        %assign searchMethod = searchPow2
      %else
        %assign searchMethod = searchEven
      %endif
    %else
      %assign searchMethod = searchUneven
    %endif
  %endif
  %%
  %return searchMethod
  %%
%endfunction %% utilDecideSearchMethod
 
 
 
%function utilDecideInterpolateMethod(...
           yDT,yDataDT,xDT,xDataDT,...
           evenlySpaced,...
           spacingValue,...
           spacingIsPow2)
  %%
  %<FixPt_CheckInputBreakpointDataTypes(xDT,xDataDT)>/
  %%
  %if !FixPt_DataTypeIsFloat(xDT) && ...
      !FixPt_DataTypeIsFloat(xDataDT) && ...
      ISEQUAL(spacingValue,1) && ...
      (xDT.FixedExp == xDataDT.FixedExp)
    %%
    %assign interpolateMethod = interpolateTrivial
    %%
  %elseif FixPt_DataTypeIsFloat(yDT) || ...
          FixPt_DataTypeIsFloat(xDT) || ...
          FixPt_DataTypeIsFloat(yDataDT) || ...
          FixPt_DataTypeIsFloat(xDataDT)
    %%
    %assign interpolateMethod = interpolateLambda
  %else
    %if evenlySpaced && spacingIsPow2
      %%
      %assign interpolateMethod = interpolatePow2
    %else
      %if xDT.RequiredBits > xDataDT.RequiredBits
        %%
        %assign maxInReqBits = xDT.RequiredBits
      %else
        %assign maxInReqBits = xDataDT.RequiredBits
      %endif
      %%
      %% bigProduct is (yH-yL)*(x-xL)
      %% note it does not depend on the number of bits in y
      %%
      %assign bigProdBits = yDataDT.RequiredBits + maxInReqBits
      %%
      %assign bigProdWithExtraShift = bigProdBits + ...
                  ( yDataDT.FixedExp - yDT.FixedExp ) - ...
                  ( xDataDT.FixedExp - xDT.FixedExp )
                 
      %%
      %if ( bigProdBits <= IntegerSizes.LongNumBits ) && ...
          ( bigProdBits <= 32 ) && ...
          ( bigProdWithExtraShift <= IntegerSizes.LongNumBits ) && ...
          ( bigProdWithExtraShift <= 32 )
        %%
        %assign interpolateMethod = interpolateBigProduct
      %else
        %assign interpolateMethod = interpolateLambda
      %endif
    %endif
  %endif
  %%
  %return interpolateMethod
  %%
%endfunction %% utilDecideInterpolateMethod
 
%function utilDecideInterpolateMethodUneven(yDT,yDataDT,xDT,xDataDT)
  %%
  %assign mootBreakPointInfo = FixPt_BreakPointInfoDefault()
  %%
  %assign interpolateMethod = utilDecideInterpolateMethod(...
                                yDT,yDataDT,xDT,xDataDT,...
                                mootBreakPointInfo.evenlySpaced,...
                                mootBreakPointInfo.spacingValue,...
                                mootBreakPointInfo.spacingIsPow2)
  %%
  %return interpolateMethod
  %%
%endfunction %% utilDecideInterpolateMethodUneven
 
%endif %% _FIXPTLOOK_