%function FixPt_Fix2Fix_Param_Expr(yDT,uLabel,uDT) void
%assign retVecStr = FixPt_Fix2Fix_Old("#error fatal", yDT, uLabel, uDT )
%if SIZE(retVecStr,1) == 3
%return retVecStr[1]
%else
%assign errTxt = "Parameter '%<uLabel>' is defined to be of data type " + /
"'%<uDT.DTName>', but this block is using it with a data type of " + /
"'%<yDT.DTName>'. Simulink Coder is unable to generate a run-time " + /
"casting expression in cases where there is a mismatch in the bias or " + /
"fractional slope. NOTE: One way to fix this problem is to remove the " + /
"parameter's '%<uDT.DTName>' data type specification, which will " + /
"result in it being declared with the block's context-sensitive data type."
%<LibBlockReportError([], errTxt)>
%endif
%endfunction
%function FixPt_Fix2Fix_Old(yLabel,yDT,uLabel,uDT) Output
%assign uLabel = SLibProcessSafeExpression(Name, uLabel, 1)
%<FixPt_WordLengthLimitationCheck(uDT)>/
%<FixPt_WordLengthLimitationCheck(yDT)>/
%assign vecCGIR = FixPt_UnaryOp("Fix2Fix", yLabel, yDT, uLabel, uDT, ...
"Nearest", "Saturate")
%if !ISEMPTY(vecCGIR[1])
%return ["%", "%", "%"]
%endif
%if FixPt_DataTypesSame(yDT,uDT)
%<LibReportFatalError("Trivial conversion from same data type should have been handled by CGIR callback")>
%endif
%assign retVecStr = ""
%assign yIsFloat = FixPt_DataTypeIsFloat(yDT)
%assign uIsFloat = FixPt_DataTypeIsFloat(uDT)
%if yIsFloat
%if uIsFloat
%assign retVecStr = FixPt_Dbl2Dbl(yLabel,yDT,uLabel,uDT)
%return retVecStr
%else
%assign retVecStr = FixPt_Fix2Dbl(yLabel,yDT,uLabel,uDT)
%return retVecStr
%endif
%else
%if uIsFloat
%assign retVecStr = FixPt_Dbl2Fix(yLabel,yDT,...
uLabel,uDT)
%return retVecStr
%else
%if ( ( yDT.FracSlope != uDT.FracSlope ) || ...
( yDT.Bias != uDT.Bias ) )
/* using CGIR generated code */
%
%else
%copyrecord yRadixDT yDT
%assign yRadixDT.FracSlope = 1.0
%assign yRadixDT.Bias = 0.0
%assign yTempLabel = "yTemp"
%copyrecord uRadixDT uDT
%assign uRadixDT.FracSlope = 1.0
%assign uRadixDT.Bias = 0.0
%assign retVecStr = FixPt_Fix2FixRad(yLabel,yRadixDT,...
uLabel,uRadixDT)
%endif
%endif
%endif
%return retVecStr
%endfunction
%function FixPt_Fix2FixRad(CLabel,CDT,BLabel,BDT) Output
%assign BLabel = SLibProcessSafeExpression(Name, BLabel, 1)
%<FixPt_FloatNotSupportedNamed(CDT,"CDT","FixPt_Fix2FixRad")>/
%<FixPt_FloatNotSupportedNamed(BDT,"BDT","FixPt_Fix2FixRad")>/
%<FixPt_Error_If_BinPtCast_Mismatched(CDT,BDT)>/
%assign ExPrec = BDT.FixedExp - CDT.FixedExp
%assign cHiExp = CDT.RequiredBits - CDT.IsSigned
%if fxpIsDataTypeBoolean(BDT)
%assign bRequiredBits = 1
%else
%assign bRequiredBits = BDT.RequiredBits
%endif
%assign bHiExp = bRequiredBits - BDT.IsSigned
%assign outHiExp = bHiExp + ExPrec
%if ( cHiExp < outHiExp ) || ( !CDT.IsSigned && BDT.IsSigned ) || ...
( ( cHiExp == outHiExp ) && ( ExPrec < 0 ) )
%assign overflowPossible = 1
%assign includeSaturationCode = 1
%else
%assign overflowPossible = 0
%assign includeSaturationCode = 0
%endif
%if ( overflowPossible )
%assign satModeUsed = "Saturate"
%else
%assign satModeUsed = "Wrap"
%endif
%assign includeRoundingCode = 0
%if ( ExPrec < 0 )
%assign includeRoundingCode = 1
%else
%assign includeRoundingCode = 0
%endif
%assign utilityName = FixPt_UtilityMakeName("TuneParamCast")
%assign utilityName = FixPt_UtilityNameAppendDT(utilityName,CDT)
%assign utilityName = FixPt_UtilityNameAppendDT(utilityName,BDT)
%if ExPrec > 0
%assign utilityName = utilityName + "_SL"
%assign utilityName = utilityName + STRING(ExPrec)
%elseif ExPrec < 0
%assign utilityName = utilityName + "_SR"
%assign utilityName = utilityName + STRING(-ExPrec)
%endif
%if includeSaturationCode
%assign utilityName = utilityName + "_SAT"
%endif
%if includeRoundingCode
%assign utilityName = utilityName + "_NEAR"
%endif
%if SLibInBlockScope()
%assign block = SLibGetCurrentBlock()
%else
%assign block = ""
%endif
%assign utilityName = SLibRegisterSharedUtility(block, utilityName)
%assign rightSideStr = "%<utilityName>(%<BLabel>)"
%assign effortStr = "simple"
%assign completeStr = "%<CLabel> = %<rightSideStr>;"
%assign retVec = ["%<effortStr>", "%<rightSideStr>", "%<completeStr>"]
%if !(ISFIELD(FixPtUtils,utilityName))
%<LibPushEmptyStackSharedUtils()>/
%assign tmpRet = SETFIELD(FixPtUtils,utilityName,1)
%openfile fcnAbstract
Tunable Parameter Data Type and Scaling Conversion Utility
%<utilityName>
This utility was generated because a tunable parameter was
declared using one data type and scaling, but a user of that
parameter (typically a block) requires the parameter's value to
be represented using a different data type and scaling.
This usage must be in the form of an expression. In order to
provide an expression, the statements required for the conversion
have been wrapped in this function.
%if ISEQUAL(::CompiledModel.RTWCGIR,0)
This function does not serve other conversion roles. It only
exists to support conversions of tunable parameters.
%endif
%closefile fcnAbstract
%openfile utilityDef
%assign fcnName = utilityName
%assign fcnReturns = CDT.NativeType
%assign fcnParams = BDT.NativeType + " B"
%assign funcSignature = ...
fcnReturns + " " + fcnName + "(" + fcnParams + ")"
%openfile funcProto
%<funcSignature>;
%closefile funcProto
%selectfile utilityDef
%createrecord fcnRec {Name fcnName; Returns fcnReturns; Params fcnParams; ...
Abstract fcnAbstract; Category "utility"; GeneratedBy "fixpt_tunable_param_cast.tlc"}
%<SLibDumpFunctionBanner(fcnRec)>
%undef fcnRec
%<funcSignature>
{
%<FixPt_WordLengthLimitationCheck(CDT)>/
%<FixPt_WordLengthLimitationCheck(BDT)>/
%assign vecCGIR2 = FixPt_UnaryOp("Fix2Fix", "C", CDT, "B", BDT, ...
"Nearest", satModeUsed)
%if ISEMPTY(vecCGIR2[1])
%<CDT.NativeType> C;
%/
return (C);
%else
return (%);
%endif
}
%closefile utilityDef
%<SLibAddMathIncludeForUtilsSourceCode(utilityDef)>
%<SLibDumpUtilsSourceCodeAndCacheFunctionPrototype(utilityName,funcProto,utilityDef)>/
%assign GSUStackBuf = LibPopStackSharedUtilsIncludes()
%<LibAddtoSharedUtilsIncludesVector(GSUStackBuf)>
%endif
%<FcnTrackSharedUtilHeaderFileUsage(utilityName + ".h", TLC_FALSE)>
%return retVec
%endfunction
%function FixPt_Dbl2Fix(outLabel,outDT,inLabel,inDT) Output
%<FixPt_FloatNotSupportedNamed(outDT,"outDT","FixPt_Dbl2Fix")>/
%<FixPt_FixptNotSupportedNamed(inDT, "inDT", "FixPt_Dbl2Fix")>/
%assign retVecStr = ""
%assign inLabel = SLibProcessSafeExpression(Name, inLabel, 1)
%assign inDTName = inDT.NativeType
%assign fSlopeNet = CAST("Real",inDT.FracSlope) / CAST("Real",outDT.FracSlope)
%assign biasUmY = CAST("Real",inDT.Bias) - CAST("Real",outDT.Bias)
%assign inIsDouble = FixPt_DataTypeIsDouble(inDT)
%if !inIsDouble
%assign uCast = "((double)%<inLabel>)"
%else
%assign uCast = inLabel
%endif
%assign y = outLabel
%assign yStoreType = outDT.NativeType
%if ( inDT.FracSlope == 1.0 && inDT.FixedExp == 0 )
%if biasUmY > 0.0
%assign uScaled = "(%<uCast>+%<biasUmY>)"
%elseif biasUmY < 0.0
%assign uScaled = "(%<uCast>-%<-1.0*biasUmY>)"
%else
%assign uScaled = uCast
%endif
%if outDT.FracSlope != 1.0
%assign fSlopeY = CAST("Real",outDT.FracSlope)
%assign dtPrecision = FixPt_Pow2(outDT.FixedExp)
%assign dtPrecision = dtPrecision*fSlopeY
%assign uScaled = "(%<uScaled>/%<dtPrecision>)"
%elseif outDT.FixedExp != 0
%assign negFixExp = -1*outDT.FixedExp
%assign uScaled = LibGenSharedMathFcnCall("ldexp",tSS_DOUBLE,"%<uScaled>","%<negFixExp>")
%endif
%else
%assign fixExpNet = inDT.FixedExp - outDT.FixedExp
%if fSlopeNet != 1.0
%assign totalSlopeNet = FixPt_Pow2(fixExpNet)
%assign totalSlopeNet = fSlopeNet*totalSlopeNet
%assign uScaled = "(%<uCast>*%<totalSlopeNet>)"
%elseif fixExpNet != 0
%assign uScaled = LibGenSharedMathFcnCall("ldexp",tSS_DOUBLE,"%<uCast>","%<fixExpNet>")
%else
%assign uScaled = uCast
%endif
%if biasUmY != 0.0
%assign biasNet = biasUmY / CAST("Real",outDT.FracSlope)
%if outDT.FixedExp != 0
%assign biasNet = biasNet * FixPt_Pow2(-outDT.FixedExp)
%endif
%if biasNet > 0.0
%assign uScaled = "(%<uScaled>+%<biasNet>)"
%else
%assign uScaled = "(%<uScaled>-%<-1.0*biasNet>)"
%endif
%endif
%endif
%assign rightSideStr = LibGenSharedMathFcnCall("floor",tSS_DOUBLE,"%<uScaled>+0.5","")
%assign rBits = outDT.RequiredBits
%assign isSign = outDT.IsSigned
%assign pseudoMax = CAST("Real",FixPt_Pow2(rBits-isSign))
%assign outMaxAsFloat = pseudoMax-1.0
%if isSign
%assign outMinAsFloat = -1.0*pseudoMax
%else
%assign outMinAsFloat = 0.0
%endif
%assign outMaxAsFloat = CAST("Real",outMaxAsFloat)
%assign outMinAsFloat = CAST("Real",outMinAsFloat)
%assign outMaxAsInt = FixPt_GetMaxStr(outDT)
%assign outMinAsInt = FixPt_GetMinStr(outDT)
%assign outMaxAsInt = "((%<outDT.NativeType>)%<outMaxAsInt>)"
%assign outMinAsInt = "((%<outDT.NativeType>)%<outMinAsInt>)"
%assign rightSideStr = "( ( %<rightSideStr> >= %<outMaxAsFloat> ) ? %<outMaxAsInt> : ( ( %<rightSideStr> <= %<outMinAsFloat> ) ? %<outMinAsInt> : ((%<outDT.NativeType>)%<rightSideStr>) ) )"
%assign effortStr = "simple"
%assign completeStr = "%<y> = %<rightSideStr>;"
%assign retVecStr = ["%<effortStr>", "%<rightSideStr>", "%<completeStr>"]
%return retVecStr
%endfunction
%function FixPt_Fix2Dbl(outLabel,outDT,inLabel,inDT) void
%<FixPt_FixptNotSupportedNamed(outDT,"outDT","FixPt_Fix2Dbl")>/
%<FixPt_FloatNotSupportedNamed(inDT, "inDT", "FixPt_Fix2Dbl")>/
%assign inLabel = SLibProcessSafeExpression(Name, inLabel, 1)
%assign outDTName = outDT.NativeType
%assign u = inLabel
%assign y = outLabel
%assign fExp = inDT.FixedExp - outDT.FixedExp
%assign fSlope = CAST("Real",inDT.FracSlope) / CAST("Real",outDT.FracSlope)
%assign bias = ( CAST("Real",inDT.Bias) - CAST("Real",outDT.Bias) ) / CAST("Real",outDT.FracSlope)
%assign isSign = inDT.IsSigned
%if outDT.FixedExp != 0
%assign bias = bias * FixPt_Pow2(-outDT.FixedExp)
%endif
%if !FixPt_DataTypeIsDouble(outDT)
%assign d2ycastpre = "((" + STRING(outDTName) + ")"
%assign d2ycastpost = ")"
%else
%assign d2ycastpre = ""
%assign d2ycastpost = ""
%endif
%if ( fSlope != 1.0 ) || ( fExp != 0.0 )
%if fSlope != 1.0
%assign dtPrecision = FixPt_Pow2(fExp)
%assign dtPrecision = dtPrecision*fSlope
%if bias > 0.0
%assign rightSideStr = "%<d2ycastpre>(%<dtPrecision>*(%<FixPt_double_label>)%<u>+%<bias>)%<d2ycastpost>"
%elseif bias < 0.0
%assign rightSideStr = "%<d2ycastpre>(%<dtPrecision>*(%<FixPt_double_label>)%<u>-%<-1.0*bias>)%<d2ycastpost>"
%else
%assign rightSideStr = "%<d2ycastpre>(%<dtPrecision>*(%<FixPt_double_label>)%<u>)%<d2ycastpost>"
%endif
%else
%if bias > 0.0
%assign rightSideStr = "%<d2ycastpre>(%<LibGenSharedMathFcnCall("ldexp",tSS_DOUBLE,"(%<FixPt_double_label>)%<u>","%<fExp>")>+%<bias>)%<d2ycastpost>"
%elseif bias < 0.0
%assign rightSideStr = "%<d2ycastpre>(%<LibGenSharedMathFcnCall("ldexp",tSS_DOUBLE,"(%<FixPt_double_label>)%<u>","%<fExp>")>-%<-1.0*bias>)%<d2ycastpost>"
%else
%assign rightSideStr = "%<d2ycastpre>%<LibGenSharedMathFcnCall("ldexp",tSS_DOUBLE,"(%<FixPt_double_label>)%<u>","%<fExp>")>%<d2ycastpost>"
%endif
%endif
%else
%if bias > 0.0
%assign rightSideStr = "%<d2ycastpre>((%<FixPt_double_label>)%<u>+%<bias>)%<d2ycastpost>"
%elseif bias < 0.0
%assign rightSideStr = "%<d2ycastpre>((%<FixPt_double_label>)%<u>-%<-1.0*bias>)%<d2ycastpost>"
%else
%if d2ycastpre == ""
%if FixPt_DataTypeIsDouble(inDT)
%assign rightSideStr = u
%else
%assign rightSideStr = "((%<FixPt_double_label>)%<u>)"
%endif
%else
%assign rightSideStr = "%<d2ycastpre>%<u>%<d2ycastpost>"
%endif
%endif
%endif
%assign effortStr = "simple"
%assign completeStr = "%<y> = %<rightSideStr>;"
%assign retVecStr = ["%<effortStr>", "%<rightSideStr>", "%<completeStr>"]
%return retVecStr
%endfunction
%function FixPt_Dbl2Dbl(yLabel,yDT,uLabel,uDT) void
%<FixPt_FixptNotSupportedNamed(yDT,"yDT","FixPt_Dbl2Dbl")>/
%<FixPt_FixptNotSupportedNamed(uDT, "uDT", "FixPt_Dbl2Dbl")>/
%assign uLabel = SLibProcessSafeExpression(Name, uLabel, 1)
%if yDT.FixedExp != uDT.FixedExp
%assign slopeCorrect = FixPt_Pow2(uDT.FixedExp-yDT.FixedExp)
%else
%assign slopeCorrect = 1.0
%endif
%if yDT.FracSlope != uDT.FracSlope
%assign slopeCorrect = slopeCorrect * uDT.FracSlope / yDT.FracSlope
%endif
%assign biasCorrect = uDT.Bias - yDT.Bias
%if biasCorrect != 0.0
%assign biasCorrect = biasCorrect * FixPt_Pow2(-yDT.FixedExp) / yDT.FracSlope
%if biasCorrect >= 0
%assign biasCorrectStr = "%<biasCorrect>"
%else
%assign biasCorrectStr = "(%<biasCorrect>)"
%endif
%if FixPt_IsZeroNumericOrString(uLabel)
%assign rightSideStr = "( %<biasCorrectStr> )"
%assign effortStr = "simple"
%else
%if slopeCorrect != 1.0
%assign rightSideStr = "( %<biasCorrectStr> + %<slopeCorrect> * %<uLabel> )"
%assign effortStr = "simple"
%else
%assign rightSideStr = "( %<biasCorrectStr> + %<uLabel> )"
%assign effortStr = "simple"
%endif
%endif
%else
%if slopeCorrect != 1.0
%if FixPt_IsZeroNumericOrString(uLabel)
%if FixPt_DataTypeIsSingle(yDT)
%assign rightSideStr = "0.0F"
%else
%assign rightSideStr = "0.0"
%endif
%assign effortStr = "trivial"
%else
%assign rightSideStr = "( %<slopeCorrect> * %<uLabel> )"
%assign effortStr = "simple"
%endif
%else
%assign rightSideStr = uLabel
%assign effortStr = "trivial"
%endif
%endif
%if FixPt_DataTypeIsSingle">FixPt_DataTypeIsSingle(yDT) && ( !FixPt_DataTypeIsSingle">FixPt_DataTypeIsSingle(uDT) || biasCorrect != 0.0 || slopeCorrect != 1.0 )
%assign rightSideStr = "((float)%<rightSideStr>)"
%assign effortStr = "simple"
%else
%assign rightSideStr = FixPt_ProtectDataTypeOfExpression(rightSideStr,yDT,uDT)
%endif
%assign completeStr = "%<yLabel> = %<rightSideStr>;"
%assign retVecStr = ["%<effortStr>", "%<rightSideStr>", "%<completeStr>"]
%return retVecStr
%endfunction
%function FixPt_ProtectDataTypeOfExpression(rightSideStr,yDT,uDT) void
%assign protect = 0
%if FixPt_DataTypeIsDouble">FixPt_DataTypeIsDouble(yDT) && !FixPt_DataTypeIsDouble">FixPt_DataTypeIsDouble(uDT)
%assign protect = 1
%elseif FixPt_DataTypeIsSingle">FixPt_DataTypeIsSingle(yDT) && !FixPt_DataTypeIsSingle">FixPt_DataTypeIsSingle(uDT)
%assign protect = 1
%elseif FixPt_DataTypeIsFloat(uDT)
%assign protect = 1
%elseif yDT.ActualBits < IntegerSizes.IntNumBits || ...
( yDT.ActualBits == IntegerSizes.IntNumBits && uDT.ActualBits > IntegerSizes.IntNumBits )
%assign protect = 1
%endif
%if protect
%assign rightSideStr = SLibProcessSafeExpression(Name, rightSideStr, 1)
%assign rightSideStr = "((%<yDT.NativeType>)%<rightSideStr>)"
%endif
%return rightSideStr
%endfunction
%function FixPt_Error_If_BinPtCast_Mismatched(outDT,inDT) void
%if !FixPt_FracSlopesSame( outDT.FracSlope, inDT.FracSlope ) || ...
( outDT.Bias != inDT.Bias )
%openfile errTxt
Incorrect use of FixPt_Fix2FixRad
The fractional slopes must be equal
inDT.FracSlope = %<inDT.FracSlope>
outDT.FracSlope = %<outDT.FracSlope>
and the biases must be equal too
inDT.Bias = %<inDT.Bias>
outDT.Bias = %<outDT.Bias>
%closefile errTxt
%<LibReportFatalError(errTxt)>
%endif
%return
%endfunction