(a*b+128)/256 Say what???
(a*b+128)/256 Say what???
I'm just trying to decode some of the library math routines. There is a routine that performs the above math and it's in the library part of the rom, I just can't seem to figure out what it does.
(a*b+128)/256
It takes 2 16 bit numbers and truncates the answer to 16 bits returning 0xffff on overflow. It seems to me that the +128 rounds the product up to the next whole number before the bottom byte is clipped off.
On another run at it I considered that despite the fact A and B are both and'd with 0xffff to guarantee they are 16 bit numbers it is possible that this routine is actually for 8 bit numbers. Also, 0xff and 0xff is actually the switch point where the result starts to climb above an 8 bit number.
Has anyone else looked at this type of routine to figure it out?
-Michael
(a*b+128)/256
It takes 2 16 bit numbers and truncates the answer to 16 bits returning 0xffff on overflow. It seems to me that the +128 rounds the product up to the next whole number before the bottom byte is clipped off.
On another run at it I considered that despite the fact A and B are both and'd with 0xffff to guarantee they are 16 bit numbers it is possible that this routine is actually for 8 bit numbers. Also, 0xff and 0xff is actually the switch point where the result starts to climb above an 8 bit number.
Has anyone else looked at this type of routine to figure it out?
-Michael
Aha, I think I got it. Took loading it into an excel spreadsheet. either A or B has to be 8 bits (because of the divide by 256). It takes A and rescales it into the range of 0..B.
So if you've got a value A and it needs to be rescaled to 0..300 instead of 0..255 you'd call this function with A and 300 as values. Kinda nifty eh?
-Michael
So if you've got a value A and it needs to be rescaled to 0..300 instead of 0..255 you'd call this function with A and 300 as values. Kinda nifty eh?
-Michael
No, not at all load related. Ignoring the fact you could bit shift to accomplish such a simple example imagine that you have an 8 bit ADC value and you want to scale it so you can compare it to a 14 bit ADC value you'd use a function like this.
The reason they don't bit shift here is that it doesn't have to be a power of 2. So let's say you wanted to generate a number from 0..100 to represent the fuel tank level you'd call this to rescale the 0..255 number from the fuel level ADC to 0..100. I've been looking and have found a whole series of functions that do this with an input level of 7 bits, 8 bits and even an arbitrary range.
I ended up renaming the series of functions to rescale_8_16up (for this one) Rescales an 8 bit number to a 16 bit range and rounds up. Will add it to my FLAIR database.
-Michael
The reason they don't bit shift here is that it doesn't have to be a power of 2. So let's say you wanted to generate a number from 0..100 to represent the fuel tank level you'd call this to rescale the 0..255 number from the fuel level ADC to 0..100. I've been looking and have found a whole series of functions that do this with an input level of 7 bits, 8 bits and even an arbitrary range.
I ended up renaming the series of functions to rescale_8_16up (for this one) Rescales an 8 bit number to a 16 bit range and rounds up. Will add it to my FLAIR database.
-Michael






