View Single Post
Old 08-25-2013, 04:00 AM   #16 (permalink)
t vago
MPGuino Supporter
t vago's Avatar
Join Date: Oct 2010
Location: Cedar Rapids, IA
Posts: 1,766

The Karen-Mobile - '05 Dodge Magnum SXT
Team Dodge
90 day: 26.72 mpg (US)

Fiat Dakota - '00 Dodge Dakota SLT RWD Quad Cab
90 day: 16.67 mpg (US)

The Red Sled - '01 Dodge Durango SLT 4WD
90 day: 16.96 mpg (US)
Thanks: 799
Thanked 681 Times in 437 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:

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.
The Fiat Dakota

The Karen-mobile

The Red Sled
  Reply With Quote
The Following User Says Thank You to t vago For This Useful Post:
pgfpro (08-25-2013)