Merge pull request #273 from brunob45/dev/table2Dspeed
improve table2D_getValue() function speed
This commit is contained in:
commit
88c8e3f8b5
|
@ -225,25 +225,10 @@ int table2D_getValue(struct table2D *fromTable, int X_in)
|
|||
int returnValue = 0;
|
||||
bool valueFound = false;
|
||||
|
||||
//Copy the from table values (bytes or ints) into common int temp arrays
|
||||
int tempAxisX[fromTable->xSize];
|
||||
int tempValues[fromTable->xSize];
|
||||
for(byte x=0; x< fromTable->xSize; x++)
|
||||
{
|
||||
if(fromTable->valueSize == SIZE_INT) { tempValues[x] = ((int16_t*)((*fromTable).values))[x]; }
|
||||
else if(fromTable->valueSize == SIZE_BYTE) { tempValues[x] = ((uint8_t*)((*fromTable).values))[x]; }
|
||||
|
||||
if(fromTable->axisSize == SIZE_INT) { tempAxisX[x] = ((int16_t*)((*fromTable).axisX))[x]; }
|
||||
else if(fromTable->axisSize == SIZE_BYTE) { tempAxisX[x] = ((uint8_t*)((*fromTable).axisX))[x]; }
|
||||
}
|
||||
|
||||
int X = X_in;
|
||||
int xMinValue, xMaxValue;
|
||||
int xMin = 0;
|
||||
int xMax = 0;
|
||||
|
||||
xMinValue = tempAxisX[0];
|
||||
xMaxValue = tempAxisX[fromTable->xSize-1];
|
||||
int xMax = fromTable->xSize-1;
|
||||
|
||||
//Check whether the X input is the same as last time this ran
|
||||
if( (X_in == fromTable->lastInput) && (fromTable->cacheTime == currentStatus.secl) )
|
||||
|
@ -251,15 +236,15 @@ int table2D_getValue(struct table2D *fromTable, int X_in)
|
|||
returnValue = fromTable->lastOutput;
|
||||
valueFound = true;
|
||||
}
|
||||
//If the requested X value is greater/small than the maximum/minimum bin, reset X to be that value
|
||||
else if(X > xMaxValue)
|
||||
//If the requested X value is greater/small than the maximum/minimum bin, simply return that value
|
||||
else if(X >= table2D_getAxisValue(fromTable, xMax))
|
||||
{
|
||||
returnValue = tempValues[fromTable->xSize-1];
|
||||
returnValue = table2D_getRawValue(fromTable, xMax);
|
||||
valueFound = true;
|
||||
}
|
||||
else if(X < xMinValue)
|
||||
else if(X <= table2D_getAxisValue(fromTable, xMin))
|
||||
{
|
||||
returnValue = tempValues[0];
|
||||
returnValue = table2D_getRawValue(fromTable, xMin);
|
||||
valueFound = true;
|
||||
}
|
||||
//Finally if none of that is found
|
||||
|
@ -267,46 +252,40 @@ int table2D_getValue(struct table2D *fromTable, int X_in)
|
|||
{
|
||||
fromTable->cacheTime = currentStatus.secl; //As we're not using the cache value, set the current secl value to track when this new value was calc'd
|
||||
|
||||
//If the requested X value is greater/small than the maximum/minimum bin, reset X to be that value
|
||||
//Failsafe, this should've been handled above
|
||||
if(X > xMaxValue) { X = xMaxValue; }
|
||||
if(X < xMinValue) { X = xMinValue; }
|
||||
|
||||
|
||||
//1st check is whether we're still in the same X bin as last time
|
||||
if ( (X <= tempAxisX[fromTable->lastXMax]) && (X > tempAxisX[fromTable->lastXMin]) )
|
||||
xMaxValue = table2D_getAxisValue(fromTable, fromTable->lastXMax);
|
||||
xMinValue = table2D_getAxisValue(fromTable, fromTable->lastXMin);
|
||||
if ( (X <= xMaxValue) && (X > xMinValue) )
|
||||
{
|
||||
xMaxValue = tempAxisX[fromTable->lastXMax];
|
||||
xMinValue = tempAxisX[fromTable->lastXMin];
|
||||
xMax = fromTable->lastXMax;
|
||||
xMin = fromTable->lastXMin;
|
||||
}
|
||||
else
|
||||
{
|
||||
//If we're not in the same bin, loop through to find where we are
|
||||
for (int x = fromTable->xSize-1; x >= 0; x--)
|
||||
xMaxValue = table2D_getAxisValue(fromTable, fromTable->xSize-1); // init xMaxValue in preparation for loop.
|
||||
for (int x = fromTable->xSize-1; x > 0; x--)
|
||||
{
|
||||
xMinValue = table2D_getAxisValue(fromTable, x-1); // fetch next Min
|
||||
|
||||
//Checks the case where the X value is exactly what was requested
|
||||
if ( (X == tempAxisX[x]) || (x == 0) )
|
||||
if (X == xMaxValue)
|
||||
{
|
||||
returnValue = tempValues[x]; //Simply return the coresponding value
|
||||
returnValue = table2D_getRawValue(fromTable, x); //Simply return the coresponding value
|
||||
valueFound = true;
|
||||
break;
|
||||
}
|
||||
else
|
||||
else if (X > xMinValue)
|
||||
{
|
||||
//Normal case
|
||||
if ( (X <= tempAxisX[x]) && (X > tempAxisX[x-1]) )
|
||||
{
|
||||
xMaxValue = tempAxisX[x];
|
||||
xMinValue = tempAxisX[x-1];
|
||||
xMax = x;
|
||||
fromTable->lastXMax = xMax;
|
||||
xMin = x-1;
|
||||
fromTable->lastXMin = xMin;
|
||||
break;
|
||||
}
|
||||
// Value is in the current bin
|
||||
xMax = x;
|
||||
fromTable->lastXMax = xMax;
|
||||
xMin = x-1;
|
||||
fromTable->lastXMin = xMin;
|
||||
break;
|
||||
}
|
||||
// Otherwise, continue to next bin
|
||||
xMaxValue = xMinValue; // for the next bin, our Min is their Max
|
||||
}
|
||||
}
|
||||
} //X_in same as last time
|
||||
|
@ -316,18 +295,20 @@ int table2D_getValue(struct table2D *fromTable, int X_in)
|
|||
int16_t m = X - xMinValue;
|
||||
int16_t n = xMaxValue - xMinValue;
|
||||
|
||||
int16_t yMax = table2D_getRawValue(fromTable, xMax);
|
||||
int16_t yMin = table2D_getRawValue(fromTable, xMin);
|
||||
|
||||
//Float version
|
||||
/*
|
||||
int yVal = (m / n) * (abs(fromTable.values[xMax] - fromTable.values[xMin]));
|
||||
int yVal = (m / n) * (abs(yMax - yMin));
|
||||
*/
|
||||
|
||||
//Non-Float version
|
||||
int16_t yVal;
|
||||
yVal = ((long)(m << 6) / n) * (abs(tempValues[xMax] - tempValues[xMin]));
|
||||
int16_t yVal = ((long)(m << 6) / n) * (abs(yMax - yMin));
|
||||
yVal = (yVal >> 6);
|
||||
|
||||
if (tempValues[xMax] > tempValues[xMin]) { yVal = tempValues[xMin] + yVal; }
|
||||
else { yVal = tempValues[xMin] - yVal; }
|
||||
if (yMax > yMin) { yVal = yMin + yVal; }
|
||||
else { yVal = yMin - yVal; }
|
||||
|
||||
returnValue = yVal;
|
||||
}
|
||||
|
@ -349,8 +330,8 @@ int16_t table2D_getAxisValue(struct table2D *fromTable, byte X_in)
|
|||
{
|
||||
int returnValue = 0;
|
||||
|
||||
if(fromTable->axisSize == SIZE_INT) { returnValue = ((int16_t*)((*fromTable).axisX))[X_in]; }
|
||||
else if(fromTable->axisSize == SIZE_BYTE) { returnValue = ((uint8_t*)((*fromTable).axisX))[X_in]; }
|
||||
if(fromTable->axisSize == SIZE_INT) { returnValue = ((int16_t*)fromTable->axisX)[X_in]; }
|
||||
else if(fromTable->axisSize == SIZE_BYTE) { returnValue = ((uint8_t*)fromTable->axisX)[X_in]; }
|
||||
|
||||
return returnValue;
|
||||
}
|
||||
|
@ -366,8 +347,8 @@ int16_t table2D_getRawValue(struct table2D *fromTable, byte X_index)
|
|||
{
|
||||
int returnValue = 0;
|
||||
|
||||
if(fromTable->valueSize == SIZE_INT) { returnValue = ((int16_t*)((*fromTable).values))[X_index]; }
|
||||
else if(fromTable->valueSize == SIZE_BYTE) { returnValue = ((uint8_t*)((*fromTable).values))[X_index]; }
|
||||
if(fromTable->valueSize == SIZE_INT) { returnValue = ((int16_t*)fromTable->values)[X_index]; }
|
||||
else if(fromTable->valueSize == SIZE_BYTE) { returnValue = ((uint8_t*)fromTable->values)[X_index]; }
|
||||
|
||||
return returnValue;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue