Zanduino / BME680

Arduino Library to access the Bosch BME680 - temperature, pressure, humidity and gas sensor
GNU General Public License v3.0
37 stars 10 forks source link

Update Zanshin_BME680.cpp #32

Closed Alain2019 closed 3 years ago

Alain2019 commented 3 years ago

readout sensor registers : use bit shift instead of * / temp sensor compensation changed shift's so that 32-bit variables can be used --> less use off 64-bit

results are the same

SV-Zanshin commented 3 years ago

The c++ compiler automatically optimized these integer divisions to shift operations, so changing "x / 16" to "x >> 4" produces the same runtime code. I haven't checked to see if all values remain in range for the variables you changed from 64 to 32 bit; but they are temporary variables inside of functions so the space lost/gained is only temporary during the execution of the routines so I am not certain that the gains are worth it.

Alain2019 commented 3 years ago

The c++ compiler automatically optimized these integer divisions to shift operations, so changing "x / 16" to "x >> 4" produces >the same runtime code.

Those changes where for clarity, when bitshifting it's clearer to use the bit shift operators.

I haven't checked to see if all values remain in range for the variables you changed from 64 to 32 bit; but they are temporary >variables inside of functions so the space lost/gained is only temporary during the execution of the routines so I am not certain >that the gains are worth it.

There won't be a space advantage, but 64-bit operations are normaly a lot more time consuming that 32-bit operations.

SV-Zanshin commented 3 years ago

I see your point about bit-shifting rather than multiplying and dividing. I'll change the corresponding locations directly in version 10 under development. But I still see some multiplying that might exceed the 32-bit values, so I'll take a look at that separately.

Alain2019 commented 3 years ago

But I still see some multiplying that might exceed the 32-bit values, so I'll take a look at that separately.

I've checked with either the Bosch library, that only uses 64-bit for some measurements and 32-bit for others.

The changes are to the temp calculation, that could have needed 64-bit at the start with 1 or 2 bits. I moved a >>4 to the first calculation, which would be enough.

SV-Zanshin commented 3 years ago

I've added issue #34 with 2 additions that cover what changes you made, plus integrated the changes in computation to the source code; there would have been too many many changes to this pull request