Handle huge ranges.

Previously the end was casted to a smaller type which resulted in a wrong check and failed with values larger than handled by unsigned.

Fixes #1844