Move mod calculations to a separate helper

We use mod calculations on multiple places, so make a separate helper
from it with proper types.

Signed-off-by: Anton Lundin <glance@acc.umu.se>