%% $RCSFile:$ %% File : sfun_idxsearch.tlc generated from sfun_idxsearch.ttlc revsion 1.4 %% $Date: 2000/03/02 22:39:23 $ %% %% Rob Aberg, 14-Aug-1999 %% Copyright 1990-2000 The MathWorks, Inc. %% %% Abstract: Index search and fraction calculation for breakpoint set. %implements sfun_idxsearch "Ada" %% Start ====================================================================== %% Abstract: %% Initialize the state information (first input & time) %% %function Start(block, system) Output -- PreLook-Up Index Search Block: % %assign rollVars = ["Mode"] %assign rollRegion = [0:%] %roll idx = rollRegion, lcv = RollThreshold, block, "Roller", rollVars %assign m = LibBlockMode("", lcv, idx) % := 0; %endroll %endfunction %% Function: Outputs ========================================================== %% Abstract: %% Set up to perform linear, binary search or direct calculation. %% Determines index of lower side of interval plus distance fraction %% into the interval [0,1) %% %function Outputs(block, system) Output -- PreLook-Up Index Search Block: % %assign otype = LibBlockOutputSignalDataTypeName(0,"") %assign wtype = LibBlockParameterDataTypeName(bpData,"") %if SFcnParamSettings.outputMode == 0 %assign itype = "TMW_Types.Uint_T" %else %assign itype = otype %endif %assign numEl = DataInputPort[0].Width declare %if numEl > 1 %assign bpSize = LibBlockParameterSize(bpData) %assign declbpData = "bpData : %_Access" % := %; maxIdx : uint_T := %; %if ( SFcnParamSettings.outputMode == 0 && ... SFcnParamSettings.searchMode == 3 ) || ... SFcnParamSettings.searchMode == 2 || ... SFcnParamSettings.extrapMethod != 1 lambda : %; %endif I : uint_T; begin for I in 0 .. % loop %assign istr = "i" %assign istr2 = "2*i" %assign istr21 = "2*i+1" %else %assign istr = "0" %assign istr2 = "0" %assign istr21 = "1" %endif %assign u = LibBlockInputSignal(0, istr, "", "") %if SFcnParamSettings.outputMode == 0 %assign y0 = LibBlockOutputSignal(0, istr2, "", "") %assign py0 = LibBlockOutputSignalAddr(0, istr2, "", "") %assign y1 = LibBlockOutputSignal(0, istr21, "", "") %else %assign y0 = LibBlockOutputSignal(0, istr, "", "") %endif %% %if SFcnParamSettings.rangeErrorMode < 3 || ... SFcnParamSettings.searchMode > 1 %if numEl == 1 %assign bpSize = LibBlockParameterSize(bpData) %assign declbpData = "bpData : %_Access" % := %; maxIdx : uint_T := %; %if ( SFcnParamSettings.outputMode == 0 && ... SFcnParamSettings.searchMode == 3 ) || ... SFcnParamSettings.searchMode == 2 || ... SFcnParamSettings.extrapMethod != 1 lambda : %; %endif %endif %if SFcnParamSettings.searchMode != 1 || ... SFcnParamSettings.extrapMethod != 1 bpFound : boolean := false; %endif u : % := %; %if SFcnParamSettings.searchMode > 1 %% an actual search? %if SFcnParamSettings.cacheBpFlag == 1 index : % := %; \ -- get previous index %else index : % := 0; %endif %else index : %; %endif %endif %if SFcnParamSettings.extrapMethod == 1 -- clipping %if SFcnParamSettings.rangeErrorMode != 3 %if SFcnParamSettings.searchMode == 1 if (u < bpData(0)) then u := bpData(0); elsif (u > bpData(maxIdx)) then u := bpData(maxIdx); end if; %else if (u < bpData(0)) then index := 0; %if SFcnParamSettings.outputMode == 0 lambda := 0.0; %endif bpFound := true; elsif (u > bpData(MaxIdx)) then %if SFcnParamSettings.outputMode == 0 index := maxIdx - 1; lambda := 1.0; %else index := maxIdx; %endif bpFound := true; end if; %endif %endif %else %% %% possible only if outputting index and fraction -- extrapolate input if needed (linear) if (u < bpData(0)) then index := 0; lambda := (u - bpData(0)) / (bpData(1) - bpData(0)); bpFound := true; elsif (u > bpData(MaxIdx)) then index := maxIdx - 1; lambda := (u - bpData(MaxIdx-1)) / ... (bpData(MaxIdx) - bpData(MaxIdx-1)); bpFound := true; end if; %endif %% %switch (SFcnParamSettings.searchMode) %case 1 %if SFcnParamSettings.extrapMethod > 1 if (bpFound = false) then %endif -- directly calculate index and fraction %if SFcnParamSettings.outputMode == 0 % := (u - %) / (% - ... %); % := %(%(%)); % := % - %; %else % := %(%( ... (u - %) / (% - ... %))); %endif %if SFcnParamSettings.extrapMethod > 1 else %if SFcnParamSettings.outputMode == 0 if ( index = maxIdx ) then % := %((index-1)); % := 1.0; else % := %(index); % := lambda; end if; %else % := index; %endif end if; %endif %break %case 2 %% lambda is always part of the algorithm, even if %% it isn't part of the output -- perform in-range linear search for [ u ) while (bpFound = false) loop lambda := (u - bpData(Index)) / (bpData(Index+1) - bpData(Index)); -- check result if (lambda >= 0.0 and then lambda < 1.0 ) then bpFound := true; elsif (lambda > 1.0) then index := index + 1; elsif (lambda < 0.0) then index := index - 1; elsif (lambda = 1.0) then bpFound := true; if ( index < (maxIdx-1) ) then index := index + 1; lambda := 0.0; end if; end if; end loop; % := %(index); %if SFcnParamSettings.outputMode == 0 % := lambda; %endif %break %case 3 -- perform in-range binary search for [ u ) if (bpFound = false) then declare bottom : uint_T := 0; top : uint_T := maxIdx; begin while (bpFound = false) loop if (u < bpData(Index)) then top := index - 1; index := (top + bottom) / 2; elsif (u <= bpData(Index+1)) then bpFound := true; if ( u = bpData(Index+1) ) then index := index + 1; end if; else bottom := index + 1; index := (top + bottom) / 2; end if; end loop; %if SFcnParamSettings.outputMode == 0 if ( index = maxIdx ) then index := index - 1; lambda := 1.0; else lambda := (u - bpData(Index)) / (bpData(Index+1) - bpData(Index)); end if; %endif end; end if; %if SFcnParamSettings.outputMode == 0 % := %(index); % := lambda; %else % := index; %endif %endswitch %% %if SFcnParamSettings.searchMode > 1 && ... SFcnParamSettings.cacheBpFlag == 1 % := index; %endif %% %if numEl > 1 end; %endif end; %endfunction