I am going to write this like algebra.
Let R = Ratio= # of years as rental starting from 2009 / # of total years of owner ship. ( don't worry about 2008 and before)
Let T1= taxable gain #1 = Gross gain * R
Gain subject to primary residence excursion = Gross gain *(1-R)
Let T2 = taxable gain #2 = Gross gain *(1-R) - 250K or 500K pending on your tax situation.
If T2 is 0 or negative then T2 =0
Total taxable gain = T1 + T2