%function FixPt_Dbl2StoredInt(inValue,outDT) void
%assign isFloat = FixPt_DataTypeIsFloat(outDT)
%if ISEQUAL(inValue,0.0) && ISEQUAL(outDT.Bias,0.0)
%if isFloat
%assign storedInteger = 0.0
%elseif outDT.IsSigned
%assign storedInteger = CAST("Number", 0 )
%else
%assign storedInteger = CAST("Unsigned", 0 )
%endif
%else
%assign rinValue = CAST("Real",inValue)
%assign fExp = outDT.FixedExp
%assign rBits = outDT.RequiredBits
%assign fSlope = CAST("Real",outDT.FracSlope)
%assign bias = CAST("Real",outDT.Bias)
%assign isSign = outDT.IsSigned
%assign dtPrecision = FixPt_Pow2(fExp)
%assign dtPrecision = dtPrecision*fSlope
%assign uScaled = ( rinValue - bias ) / dtPrecision
%if isFloat
%assign storedInteger = uScaled
%else
%if ( -0.5 <= uScaled ) && ( uScaled < 0.5 )
%if isSign
%assign storedInteger = CAST( "Number", 0 )
%else
%assign storedInteger = CAST( "Unsigned", 0 )
%endif
%else
%if ( !FixPt_IsInputLessThanMaxRealWorldValue(rinValue, outDT) )
%if rBits <= 32
%if isSign
%assign outMaxStoreInt = FixPt_Pow2(rBits-1)-1.0
%assign storedInteger = CAST( "Number", outMaxStoreInt )
%else
%assign outMaxStoreInt = FixPt_Pow2(rBits )-1.0
%assign storedInteger = CAST( "Unsigned", outMaxStoreInt )
%endif
%else
%assign outMaxStoreInt = FixPt_GetMaxStr(outDT)
%assign storedInteger = outMaxStoreInt
%endif
%elseif ( !FixPt_IsInputLargerThanMinRealWorldValue(rinValue, outDT) )
%if isSign
%if rBits <= 32
%assign outMinStoreInt = -1.0*FixPt_Pow2(rBits-1)
%assign storedInteger = CAST( "Number", outMinStoreInt )
%else
%assign outMinStoreInt = FixPt_GetMinStr(outDT)
%assign storedInteger = outMinStoreInt
%endif
%else
%assign storedInteger = CAST("Unsigned", 0 )
%endif
%else
%if isSign
%if uScaled >= 0.0
%assign storedInteger = CAST("Number", uScaled + 0.5 )
%else
%assign storedInteger = CAST("Number", uScaled - 0.5 )
%if ISEQUAL( 0.5, uScaled - CAST("Real",storedInteger) )
%assign storedInteger = storedInteger + 1
%endif
%endif
%else
%if uScaled >= 0.0
%assign storedInteger = CAST("Unsigned", uScaled + 0.5 )
%else
%assign storedInteger = CAST("Unsigned", uScaled - 0.5 )
%if ISEQUAL( 0.5, uScaled - CAST("Real",storedInteger) )
%assign storedInteger = storedInteger + 1
%endif
%endif
%endif
%assign realStoredInteger = CAST("Real",storedInteger)
%if ( (realStoredInteger+1.0) < uScaled ) || ...
( (realStoredInteger-1.0) > uScaled )
%openfile errTxt
Simulink Coder Fatal: Overflow occured in TLC calculation
of fixed point quantity. The number of bits required for this
calculation is more than can be handled by TLC. Reducing the
number of bits used for the blocks output and/or parameters
may work around this problem.
Block: %<MaskBlockName>
%closefile errTxt
%<LibReportFatalError(errTxt)>
%endif
%endif
%endif
%endif
%endif
%return storedInteger
%endfunction
%function SetMSNBitsStr(numSet,totalBits,isSigned) void
%if numSet < 0
%exit Simulink Coder Fatal Error: SetMSNBitsStr: numSet MUST BE NON-NEGATIVE
%elseif numSet > totalBits
%exit Simulink Coder Fatal Error: SetMSNBitsStr: numSet must be less or equal to totalBits
%elseif (totalBits % 4) != 0
%exit Simulink Coder Fatal Error: SetMSNBitsStr: totalBits MUST BE MULTIPLE OF 4
%elseif totalBits > LargestIntegerNumBits
%if IntegerSizes.LongLongMode == 0
%exit Simulink Coder Fatal Error: SetMSNBitsStr: totalBits MUST FIT IN A LONG
%else
%exit Simulink Coder Fatal Error: SetMSNBitsStr: totalBits MUST FIT IN A LONG LONG
%endif
%else
%assign bitMask = ")"
%if (totalBits > IntegerSizes.IntNumBits && totalBits <= IntegerSizes.LongNumBits)
%assign bitMask = "L"+ bitMask
%elseif (totalBits > IntegerSizes.LongNumBits && IntegerSizes.LongLongMode == 1)
%assign bitMask = "LL"+ bitMask
%endif
%if !isSigned
%assign bitMask = "U"+ bitMask
%endif
%foreach i = (totalBits/4)
%assign j = totalBits - numSet - ( i * 4 )
%if ( j >= 4 )
%assign bitMask = "0"+ bitMask
%elseif ( j == 3 )
%assign bitMask = "8"+ bitMask
%elseif ( j == 2 )
%assign bitMask = "C"+ bitMask
%elseif ( j == 1 )
%assign bitMask = "E"+ bitMask
%else
%assign bitMask = "F"+ bitMask
%endif
%endforeach
%assign bitMask = "(0x"+ bitMask
%endif
%return bitMask
%endfunction
%function SetLSNBitsStr(numSet,totalBits,isSigned) void
%if numSet < 0
%exit Simulink Coder Fatal Error: SetLSNBitsStr: numSet MUST BE NON-NEGATIVE
%elseif numSet > totalBits
%exit Simulink Coder Fatal Error: SetLSNBitsStr: numSet must be less or equal to totalBits
%elseif (totalBits % 4) != 0
%exit Simulink Coder Fatal Error: SetLSNBitsStr: totalBits MUST BE MULTIPLE OF 4
%elseif totalBits > LargestIntegerNumBits
%if IntegerSizes.LongLongMode == 0
%exit Simulink Coder Fatal Error: SetLSNBitsStr: totalBits MUST FIT IN A LONG
%else
%exit Simulink Coder Fatal Error: SetLSNBitsStr: totalBits MUST FIT IN A LONG LONG
%endif
%else
%assign bitMask = ")"
%if (totalBits > IntegerSizes.IntNumBits && totalBits <= IntegerSizes.LongNumBits)
%assign bitMask = "L"+ bitMask
%elseif (totalBits > IntegerSizes.LongNumBits && IntegerSizes.LongLongMode == 1)
%assign bitMask = "LL"+ bitMask
%endif
%if !isSigned
%assign bitMask = "U"+ bitMask
%endif
%foreach i = (totalBits/4)
%assign j = numSet - ( i * 4 )
%if ( j >= 4 )
%assign bitMask = "F"+ bitMask
%elseif ( j == 3 )
%assign bitMask = "7"+ bitMask
%elseif ( j == 2 )
%assign bitMask = "3"+ bitMask
%elseif ( j == 1 )
%assign bitMask = "1"+ bitMask
%else
%assign bitMask = "0"+ bitMask
%endif
%endforeach
%assign bitMask = "(0x"+ bitMask
%endif
%return bitMask
%endfunction
%function PowerOfTwoStr(ipow,isSigned) void
%if ipow < 0
%exit Simulink Coder Fatal Error: PowerOfTwoStr: ipow MUST BE NON-NEGATIVE
%elseif ipow >= ( IntegerSizes.LongNumBits - isSigned )
%exit Simulink Coder Fatal Error: PowerOfTwoStr: ipow must fit in a long
%else
%assign bitMask = ")"
%if ipow < ( IntegerSizes.IntNumBits - isSigned )
%assign iHi = (IntegerSizes.IntNumBits/4)
%elseif (ipow > ( IntegerSizes.IntNumBits - isSigned ) && ...
ipow < ( IntegerSizes.LongNumBits - isSigned ) && ...
IntegerSizes.LongLongMode == 0)
%assign bitMask = "L"+ bitMask
%assign iHi = (IntegerSizes.LongNumBits/4)
%else
%assign bitMask = "LL"+ bitMask
%assign iHi = (IntegerSizes.LongLongNumBits/4)
%endif
%if !isSigned
%assign bitMask = "U"+ bitMask
%endif
%foreach i = iHi
%assign j = ipow - ( i * 4 )
%if ( j == 0 )
%assign bitMask = "1"+ bitMask
%elseif ( j == 1 )
%assign bitMask = "2"+ bitMask
%elseif ( j == 2 )
%assign bitMask = "4"+ bitMask
%elseif ( j == 3 )
%assign bitMask = "8"+ bitMask
%else
%assign bitMask = "0"+ bitMask
%endif
%endforeach
%assign bitMask = "(0x"+ bitMask
%endif
%return bitMask
%endfunction
%function FixPt_GetNBitsMaxStr(requiredBits,actualBits,isSigned,containerTypeIdx) void
%if requiredBits == 8 || requiredBits == 16 || requiredBits == 32
%assign baseDT = FixPt_GetDataTypeFromIndex(containerTypeIdx)
%if ISFIELD(baseDT, "TypeMaxId")
%assign bitMask = baseDT.TypeMaxId
%else
%exit Simulink Coder Fatal Error: FixPt_GetMaxStr: TypeMaxId is not a field of base data type structure.
%endif
%elseif requiredBits < 32
%if isSigned
%assign storedIntMax = CAST("Number", FixPt_Pow2(requiredBits-1)-1.0)
%else
%assign storedIntMax = CAST("Unsigned",FixPt_Pow2(requiredBits )-1.0)
%endif
%assign bitMask = "(%<storedIntMax>)"
%else
%assign numSet = requiredBits - isSigned
%assign bitMask = SetLSNBitsStr(numSet,actualBits,isSigned)
%if actualBits > IntegerSizes.IntNumBits && actualBits <= IntegerSizes.LongNumBits
%if isSigned
%assign cast_label = FixPt_long_label
%else
%assign cast_label = FixPt_ulong_label
%endif
%elseif actualBits > IntegerSizes.LongNumBits && IntegerSizes.LongLongMode == 1
%if isSigned
%assign cast_label = FixPt_long_long_label
%else
%assign cast_label = FixPt_ulong_long_label
%endif
%else
%if isSigned
%assign cast_label = FixPt_int_label
%else
%assign cast_label = FixPt_uint_label
%endif
%endif
%assign bitMask = "((%<cast_label>)"+ bitMask + ")"
%endif
%return bitMask
%endfunction
%function FixPt_GetMaxStr(curDT) void
%if FixPt_DataTypeIsDouble(curDT)
%<FixPt_IncludeFloatDotH(0)>/
%assign bitMask = "DBL_MAX"
%elseif FixPt_DataTypeIsSingle(curDT)
%<FixPt_IncludeFloatDotH(0)>/
%assign bitMask = "FLT_MAX"
%else
%assign containerTypeId = SLibGetRecordContainerCGTypeIdx(curDT)
%assign bitMask = FixPt_GetNBitsMaxStr(curDT.RequiredBits,curDT.ActualBits,curDT.IsSigned,containerTypeId)
%endif
%return bitMask
%endfunction
%function FixPt_GetNBitsMinStr(requiredBits,actualBits,isSigned,containerTypeIdx) void
%if requiredBits == 8 || requiredBits == 16 || requiredBits == 32
%assign baseDT = FixPt_GetDataTypeFromIndex(containerTypeIdx)
%if ISFIELD(baseDT, "TypeMinId")
%assign bitMask = baseDT.TypeMinId
%else
%exit Simulink Coder Fatal Error: FixPt_GetNBitsMinStr: TypeMinId is not a field of base data type structure.
%endif
%else
%if isSigned
%if requiredBits < 32
%if requiredBits == IntegerSizes.LongNumBits
%assign storedIntMin = CAST("Number", (-1.0*FixPt_Pow2(requiredBits-1))+1.0 )
%assign bitMask = "((%<storedIntMin>)-1)"
%else
%assign storedIntMin = CAST("Number", (-1.0*FixPt_Pow2(requiredBits-1)) )
%assign bitMask = "(%<storedIntMin>)"
%endif
%else
%if isSigned
%assign numSet = 1 + actualBits - requiredBits
%else
%assign numSet = 0
%endif
%assign maskIsSigned = 0
%assign bitMask = SetMSNBitsStr(numSet,actualBits,maskIsSigned)
%if actualBits > IntegerSizes.IntNumBits && actualBits <= IntegerSizes.LongNumBits
%if isSigned
%assign cast_label = FixPt_long_label
%else
%assign cast_label = FixPt_ulong_label
%endif
%elseif actualBits > IntegerSizes.LongNumBits && IntegerSizes.LongLongMode == 1
%if isSigned
%assign cast_label = FixPt_long_long_label
%else
%assign cast_label = FixPt_ulong_long_label
%endif
%else
%if isSigned
%assign cast_label = FixPt_int_label
%else
%assign cast_label = FixPt_uint_label
%endif
%endif
%assign bitMask = "((%<cast_label>)"+ bitMask + ")"
%endif
%else
%assign bitMask = "(0)"
%endif
%endif
%return bitMask
%endfunction
%function FixPt_GetMinStr(curDT) void
%if FixPt_DataTypeIsDouble(curDT)
%<FixPt_IncludeFloatDotH(0)>/
%assign bitMask = "(-(DBL_MAX))"
%elseif FixPt_DataTypeIsSingle(curDT)
%<FixPt_IncludeFloatDotH(0)>/
%assign bitMask = "(-(FLT_MAX))"
%else
%assign containerTypeId = SLibGetRecordContainerCGTypeIdx(curDT)
%assign bitMask = FixPt_GetNBitsMinStr(curDT.RequiredBits,curDT.ActualBits,curDT.IsSigned,containerTypeId)
%endif
%return bitMask
%endfunction
%assign pow2Table = [ ...
1.0 , ...
2.0 , ...
4.0 , ...
8.0 , ...
16.0 , ...
32.0 , ...
64.0 , ...
128.0 , ...
256.0 , ...
512.0 , ...
1024.0 , ...
2048.0 , ...
4096.0 , ...
8192.0 , ...
16384.0 , ...
32768.0 , ...
65536.0 , ...
131072.0 , ...
262144.0 , ...
524288.0 , ...
1048576.0 , ...
2097152.0 , ...
4194304.0 , ...
8388608.0 , ...
16777216.0 , ...
33554432.0 , ...
67108864.0 , ...
134217728.0 , ...
268435456.0 , ...
536870912.0 , ...
1073741824.0 , ...
2147483648.0 , ...
4294967296.0 ...
]
%function FixPt_Pow2(num) void
%assign numCast = CAST("Number",num)
%if numCast != num
%exit FixPt_Pow2 was called using a non integer value
%endif
%if 0 <= numCast && numCast <= 32
%assign retVal = pow2Table[numCast]
%elseif -32 <= numCast && numCast < 0
%assign retVal = 1.0 / pow2Table[-numCast]
%else
%assign retVal = "(2^(%<numCast>))"
%assign retVal = FEVAL("eval", retVal)
%assign retVal = CAST( "Real", retVal)
%endif
%return retVal
%endfunction
%function FixPt_IsInputLessThanMaxRealWorldValue(dblValue, outDT) void
%assign totalBits = CAST("Real",outDT.RequiredBits)
%assign isSigned = CAST("Real",outDT.IsSigned)
%assign exp = CAST("Real",outDT.FixedExp)
%assign slope = CAST("Real",outDT.FracSlope)
%assign bias = CAST("Real",outDT.Bias)
%assign flag = FEVAL("fixptCompMinMaxValue", dblValue, totalBits, isSigned, exp, slope, bias)
%if flag != 1.0
%return TLC_TRUE
%else
%return TLC_FALSE
%endif
%endfunction
%function FixPt_IsInputLargerThanMinRealWorldValue(dblValue, outDT) void
%assign totalBits = CAST("Real",outDT.RequiredBits)
%assign isSigned = CAST("Real",outDT.IsSigned)
%assign exp = CAST("Real",outDT.FixedExp)
%assign slope = CAST("Real",outDT.FracSlope)
%assign bias = CAST("Real",outDT.Bias)
%assign flag = FEVAL("fixptCompMinMaxValue", dblValue, totalBits, isSigned, exp, slope, bias)
%if flag != -1.0
%return TLC_TRUE
%else
%return TLC_FALSE
%endif
%endfunction
%function FixPt_IncludeFloatDotH(junk) void
%if ::CompiledModel.IncludeFloatDotH == 0
%openfile localBuffer
#ifndef DBL_MAX
#include
#endif
%closefile localBuffer
%<LibCacheIncludes(localBuffer)>/
%assign ::CompiledModel.IncludeFloatDotH = 1
%<LibAddtoSharedUtilsIncludes("")>
%endif
%endfunction