8bb376cf6b
The previous implementation was behaving incorrectly in some corner cases. Signed-off-by: Anton Khirnov <anton@khirnov.net>