2 Haiku ATI video driver adapted from the X.org ATI driver.
4 Copyright 1997 through 2004 by Marc Aurele La France (TSI @ UQV), tsi@xfree86.org
6 Copyright 2009 Haiku, Inc. All rights reserved.
7 Distributed under the terms of the MIT license.
13 #include "accelerant.h"
17 #define MAX_INT ((int)((unsigned int)(-1) >> 2))
22 Mach64_ReduceRatio(int *numerator
, int *denominator
)
24 // Reduce a fraction by factoring out the largest common divider of the
25 // fraction's numerator and denominator.
27 int multiplier
= *numerator
;
28 int divider
= *denominator
;
31 while ((remainder
= multiplier
% divider
)) {
36 *numerator
/= divider
;
37 *denominator
/= divider
;
43 Mach64_Divide(int numerator
, int denominator
, int shift
, const int roundingKind
)
45 // Using integer arithmetic and avoiding overflows, this function finds the
46 // rounded integer that best approximates
52 // using the specified rounding (floor (<0), nearest (=0) or ceiling (>0)).
54 Mach64_ReduceRatio(&numerator
, &denominator
);
56 // Deal with left shifts but try to keep the denominator even.
57 if (denominator
& 1) {
58 if (denominator
<= MAX_INT
) {
63 while ((shift
> 0) && !(denominator
& 3)) {
69 // Deal with right shifts.
71 if ((numerator
& 1) && (denominator
<= MAX_INT
))
79 int rounding
= 0; // Default to floor
81 if (!roundingKind
) // nearest
82 rounding
= denominator
>> 1;
83 else if (roundingKind
> 0) // ceiling
84 rounding
= denominator
- 1;
86 return ((numerator
/ denominator
) << shift
) +
87 ((((numerator
% denominator
) << shift
) + rounding
) / denominator
);