Skip to content

Commit

Permalink
- Fix for floating point overflow/underflow in thread number of itera…
Browse files Browse the repository at this point in the history
…tions calculations.

-  Addresses #73.
  • Loading branch information
bandurvp committed Oct 2, 2017
1 parent 7dfaae0 commit 1b5acaa
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions core/fmu-import-export/src/main/resources/c-templates/FmuModel.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ fmi2Status vdmStep(fmi2Real currentCommunicationPoint, fmi2Real communicationSte
{
int i, j;
int threadRunCount;
double dtmp;

/* Call each thread the appropriate number of times. */
for(i = 0; i < PERIODIC_GENERATED_COUNT; i++)
Expand All @@ -72,7 +73,15 @@ fmi2Status vdmStep(fmi2Real currentCommunicationPoint, fmi2Real communicationSte
else if(threads[i].lastExecuted + threads[i].period <= currentCommunicationPoint + communicationStepSize)
{
/* Find number of executions to fit inside of step, allow sync. */
threadRunCount = (currentCommunicationPoint + communicationStepSize - threads[i].lastExecuted) / threads[i].period;
dtmp = (currentCommunicationPoint + communicationStepSize - threads[i].lastExecuted) / threads[i].period;

/* Underflow */
if(dtmp - ((double)(int)dtmp) >= 0.99999)
/* Overflow */
threadRunCount = dtmp + 1;
else
threadRunCount = dtmp;

syncOutAllowed = fmi2True;
}
/* Can not execute, but can sync existing values at the end of this step. */
Expand All @@ -85,9 +94,16 @@ fmi2Status vdmStep(fmi2Real currentCommunicationPoint, fmi2Real communicationSte
else
{
/* Find number of executions to fit inside of step, allow sync because need to update regardless. */
threadRunCount = (currentCommunicationPoint + communicationStepSize - threads[i].lastExecuted) / threads[i].period;
dtmp = (currentCommunicationPoint + communicationStepSize - threads[i].lastExecuted) / threads[i].period;
syncOutAllowed = fmi2True;

/* Underflow */
if(dtmp - ((double)(int)dtmp) >= 0.99999)
/* Overflow */
threadRunCount = dtmp + 1;
else
threadRunCount = dtmp;

/* Period too long for this step so postpone until next step. */
if(threadRunCount == 0)
{
Expand Down

0 comments on commit 1b5acaa

Please sign in to comment.