Ported from https://github.com/google/periph/issues/124. Having a higher quality implementation is blocked on issue #32.