matsstaff / stc1000p

Programmable thermostat firmware and arduino based uploader for the STC-1000 thermostat
GNU General Public License v3.0
261 stars 47 forks source link

shave 8 instructions out of read_ad #82

Closed seanmcveigh closed 8 years ago

seanmcveigh commented 8 years ago

here's an amusing one. I went in to optimize the adfilter range checking again, and noticed that if you make a union of an int and 2 chars, you can more efficiently access the hi & lo bytes when assembling the 16-bit value. this saves 8 instructions. might be able to repeat this pattern elsewhere where you do something like: int blah = (hi << 8) | lo; the only odd part I noticed is that the allocation for a function-local version of this union ends up getting it's own slot in the UDL_page0_0 compiler-defined variables section. I think that's okay though. I toyed with the idea of making an array of 2 of this union globally, and having read_ad() just take a 0/1 arg for which one to update, but this ended up leading to larger code due to the indirect addressing into the array. meh.

I should probably have merged newpb2 into optimization and then made the optimization on that branch, but I guess I'm working slightly out of order here optimizing your new code :)

I'll go back to optimizing the older code that is unlikely to change with your new effort. BTW, I think we've now optimized over 11% back!

(hopefully I didn't get lo & hi reversed in the union)

matsstaff commented 8 years ago

That is effing brilliant!!!!! That trick could be most handy :)