Haiku ATI video driver adapted from the X.org ATI driver.
Copyright 1997 through 2004 by Marc Aurele La France (TSI @ UQV), tsi@xfree86.org
Copyright 2009 Haiku, Inc. All rights reserved.
Distributed under the terms of the MIT license.
Authors:
Gerald Zajac 2009
*/
#include "accelerant.h"
#include "mach64.h"
#define MAX_INT ((int)((unsigned int)(-1) >> 2))
void
Mach64_ReduceRatio(int *numerator, int *denominator)
{
int multiplier = *numerator;
int divider = *denominator;
int remainder;
while ((remainder = multiplier % divider)) {
multiplier = divider;
divider = remainder;
}
*numerator /= divider;
*denominator /= divider;
}
int
Mach64_Divide(int numerator, int denominator, int shift, const int roundingKind)
{
Mach64_ReduceRatio(&numerator, &denominator);
if (denominator & 1) {
if (denominator <= MAX_INT) {
denominator <<= 1;
shift++;
}
} else {
while ((shift > 0) && !(denominator & 3)) {
denominator >>= 1;
shift--;
}
}
while (shift < 0) {
if ((numerator & 1) && (denominator <= MAX_INT))
denominator <<= 1;
else
numerator >>= 1;
shift++;
}
int rounding = 0;
if (!roundingKind)
rounding = denominator >> 1;
else if (roundingKind > 0)
rounding = denominator - 1;
return ((numerator / denominator) << shift) +
((((numerator % denominator) << shift) + rounding) / denominator);
}