I'm running on a platform with no floating-point support, and the largest integer type being 32 bits.
Given a pair of values, I need to ensure that none of them exceeds 16 bits before I continue to process them.
If either one of them does, then I need to reduce both while maintaining their ratio as accurately as possible.
Here is what I've done so far:
#define MAX_VAL 0x0000FFFF
typedef unsigned int uint32;
void func(uint32 x, uint32 y) {
if (x > MAX_VAL && y <= MAX_VAL) {
y = y * MAX_VAL / x;
x = MAX_VAL;
}
if (x <= MAX_VAL && y > MAX_VAL) {
x = x * MAX_VAL / y;
y = MAX_VAL;
}
while (x > MAX_VAL || y > MAX_VAL) {
x >>= 1;
y >>= 1;
}
...
}
This seems to work well in terms of accuracy.
However, I would still like to improve performance, specifically with regards to the while
loop, while maintaining the level of accuracy of course.
Any ideas how to approach that?
User contributions licensed under CC BY-SA 3.0