Are you concerned about counting days for visa purposes or counting how many days you were resident where? For some of this (residency or physical presence) IIRC what matters is whether you either enter or cross over a different country before midnight on the day of departure from the USA, while visa length surely depends on the date on which you are "stamped in/out," which would be when an immigration officer approves your entrance or exit.
I suspect that there are different answers for different purposes. For instance, IANAL but if you murder someone before clearing USA immigration but after landing and deplaning, I would expect USA laws to apply regardless of the carrier of your arrival, but if you murder someone after doing preclearance at a Canadian airport but before boarding a flight to the USA, I would expect Canada to prosecute.