View Single Post
Old 08-25-2013, 05:00 AM   #16 (permalink)
t vago
MPGuino Supporter
 
t vago's Avatar
 
Join Date: Oct 2010
Location: Hungary
Posts: 1,807

iNXS - '10 Opel Zafira 111 Anniversary

Suzi - '02 Suzuki Swift GL
Thanks: 830
Thanked 708 Times in 456 Posts
Finally came up with a fairly quick integer square root function. Basically, it uses the Babylonian Method to calculate the square root of an integer representation of a decimal number that has about 3 decimal places, as shown.

d = i / 4096

so, for d = 1, i = 4096. for d = 1.5, i = 6144. And so on...

Anyway, the square root function finds the square root of a number, given the above rule for i. So, for the square root of i = 6144 (d = 1.5), sqrt(i) would return 5016 (5016 / 4096 = 1.225. It uses up about 28 microseconds to do this, too, and adds 108 bytes to the code to do so.

The reason I coded the square root this way was so I could then do this:

Code:
i = manifoldPressure + fuelPressure; // calculate pressure differential seen across injector
i <<= 12; // multiply by 4096
i /= fuelPressure; // divide by fuel system pressure to generate pressure ratio
injOpenPulseTime = injOpenPulseTime * iSqrt(i); // multiply fuel injector open time by integer square root of pressure ratio
injOpenPulseTime >>= 12; // shift right to complete factor adjustment
I estimate that this little bit of code would take about 36 microseconds to execute in total. This is well within the time span between injector firings.
  Reply With Quote
The Following User Says Thank You to t vago For This Useful Post:
pgfpro (08-25-2013)