avutil/rational: use frexp rather than ad-hoc log to get floating point exponent
This simplifies and cleans up the code. Furthermore, it is much faster due to absence of the slow log computation. Reviewed-by: Michael Niedermayer <michael@niedermayer.cc> Signed-off-by: Ganesh Ajjanagadde <gajjanagadde@gmail.com>
This commit is contained in:
parent
865f6f410f
commit
8d9f86bd37
@ -106,14 +106,14 @@ AVRational av_sub_q(AVRational b, AVRational c)
|
|||||||
AVRational av_d2q(double d, int max)
|
AVRational av_d2q(double d, int max)
|
||||||
{
|
{
|
||||||
AVRational a;
|
AVRational a;
|
||||||
#define LOG2 0.69314718055994530941723212145817656807550013436025
|
|
||||||
int exponent;
|
int exponent;
|
||||||
int64_t den;
|
int64_t den;
|
||||||
if (isnan(d))
|
if (isnan(d))
|
||||||
return (AVRational) { 0,0 };
|
return (AVRational) { 0,0 };
|
||||||
if (fabs(d) > INT_MAX + 3LL)
|
if (fabs(d) > INT_MAX + 3LL)
|
||||||
return (AVRational) { d < 0 ? -1 : 1, 0 };
|
return (AVRational) { d < 0 ? -1 : 1, 0 };
|
||||||
exponent = FFMAX( (int)(log(fabs(d) + 1e-20)/LOG2), 0);
|
frexp(d, &exponent);
|
||||||
|
exponent = FFMAX(exponent-1, 0);
|
||||||
den = 1LL << (61 - exponent);
|
den = 1LL << (61 - exponent);
|
||||||
// (int64_t)rint() and llrint() do not work with gcc on ia64 and sparc64
|
// (int64_t)rint() and llrint() do not work with gcc on ia64 and sparc64
|
||||||
av_reduce(&a.num, &a.den, floor(d * den + 0.5), den, max);
|
av_reduce(&a.num, &a.den, floor(d * den + 0.5), den, max);
|
||||||
|
Loading…
Reference in New Issue
Block a user