Shrink sensors code size by 5k using local pow

This commit is contained in:
Theo Arends 2020-05-15 14:17:11 +02:00
parent 6573802f83
commit bafe8a137f

View File

@ -327,6 +327,30 @@ int16_t MutichannelGasSensor::readR(void)
** Returns:
float value - concentration of the gas
*********************************************************************************************************/
float MutichannelGasSensor_pow(float a, float b)
{
// https://martin.ankerl.com/2012/01/25/optimized-approximative-pow-in-c-and-cpp/
// calculate approximation with fraction of the exponent
int e = abs((int)b);
union {
double d;
int x[2];
} u = { a };
u.x[1] = (int)((b - e) * (u.x[1] - 1072632447) + 1072632447);
u.x[0] = 0;
// exponentiation by squaring with the exponent's integer part
// double r = u.d makes everything much slower, not sure why
double r = 1.0;
while (e) {
if (e & 1) {
r *= a;
}
a *= a;
e >>= 1;
}
return r * u.d;
}
float MutichannelGasSensor::calcGas(int gas)
{
@ -370,42 +394,42 @@ float MutichannelGasSensor::calcGas(int gas)
{
case CO:
{
c = pow(ratio1, -1.179)*4.385; //mod by jack
c = MutichannelGasSensor_pow(ratio1, -1.179)*4.385; //mod by jack
break;
}
case NO2:
{
c = pow(ratio2, 1.007)/6.855; //mod by jack
c = MutichannelGasSensor_pow(ratio2, 1.007)/6.855; //mod by jack
break;
}
case NH3:
{
c = pow(ratio0, -1.67)/1.47; //modi by jack
c = MutichannelGasSensor_pow(ratio0, -1.67)/1.47; //modi by jack
break;
}
case C3H8: //add by jack
{
c = pow(ratio0, -2.518)*570.164;
c = MutichannelGasSensor_pow(ratio0, -2.518)*570.164;
break;
}
case C4H10: //add by jack
{
c = pow(ratio0, -2.138)*398.107;
c = MutichannelGasSensor_pow(ratio0, -2.138)*398.107;
break;
}
case GAS_CH4: //add by jack
{
c = pow(ratio1, -4.363)*630.957;
c = MutichannelGasSensor_pow(ratio1, -4.363)*630.957;
break;
}
case H2: //add by jack
{
c = pow(ratio1, -1.8)*0.73;
c = MutichannelGasSensor_pow(ratio1, -1.8)*0.73;
break;
}
case C2H5OH: //add by jack
{
c = pow(ratio1, -1.552)*1.622;
c = MutichannelGasSensor_pow(ratio1, -1.552)*1.622;
break;
}
default: