mercredi 6 novembre 2019

Map integer range onto another range

In runtime I have 2 ranges defined by their uint32_t borders a..b and c..d. The first range tends to be much greater than the second.
I need a routine that performs linear mapping of an integer from the first range onto the second one: f(x) -> round_to_uint32_t((float)(x - a) / (b - a) * (d - c) + c)

Sounds like a simple ratio problem and was already answered in many questions like
Convert a number range to another range, maintaining ratio
but here I need a really really fast solution.

This routine is a pivotal part of a specialized sorting algorithm and will be called at least once for every element of a sorted array.

SIMD solution is also acceptable if it doesn't use heavy AVX instructions.

Aucun commentaire:

Enregistrer un commentaire