[FFmpeg-devel] [PATCH] lavu/libm: change macros to functions

wm4 nfxjfg at googlemail.com
Sun Dec 27 14:16:39 CET 2015


On Thu, 24 Dec 2015 19:52:21 +0100
Hendrik Leppkes <h.leppkes at gmail.com> wrote:

> Am 24.12.2015 19:34 schrieb "Ganesh Ajjanagadde" <gajjanagadde at gmail.com>:
> >
> > In the standard library, these are functions. We should match it; there
> > is no reason for these to be macros.
> >
> > While at it, add some trivial comments for readability and correct an
> > incorrect (at standard double precision) M_LN2 constant used in the exp2
> > fallback.
> >
> > Signed-off-by: Ganesh Ajjanagadde <gajjanagadde at gmail.com>
> > ---
> >  libavutil/libm.h | 108  
> ++++++++++++++++++++++++++++++++++---------------------
> >  1 file changed, 67 insertions(+), 41 deletions(-)
> >
> > diff --git a/libavutil/libm.h b/libavutil/libm.h
> > index 6f9ac1b..ac05613 100644
> > --- a/libavutil/libm.h
> > +++ b/libavutil/libm.h
> > @@ -36,33 +36,39 @@
> >  #endif /* HAVE_MIPSFPU && HAVE_INLINE_ASM*/
> >
> >  #if !HAVE_ATANF
> > -#undef atanf
> > -#define atanf(x) ((float)atan(x))
> > -#endif
> > +static av_always_inline float atanf(float x)
> > +{
> > +    return atan(x);
> > +}
> > +#endif /* HAVE_ATANF */
> >
> >  #if !HAVE_ATAN2F
> > -#undef atan2f
> > -#define atan2f(y, x) ((float)atan2(y, x))
> > -#endif
> > +static av_always_inline float atan2f(float y, float x)
> > +{
> > +    return atan2(y, x);
> > +}
> > +#endif /* HAVE_ATAN2F */
> >
> >  #if !HAVE_POWF
> > -#undef powf
> > -#define powf(x, y) ((float)pow(x, y))
> > -#endif
> > +static av_always_inline float powf(float x, float y)
> > +{
> > +    return pow(x, y);
> > +}
> > +#endif /* HAVE_POWF */
> >
> >  #if !HAVE_CBRT
> >  static av_always_inline double cbrt(double x)
> >  {
> >      return x < 0 ? -pow(-x, 1.0 / 3.0) : pow(x, 1.0 / 3.0);
> >  }
> > -#endif
> > +#endif /* HAVE_CBRT */
> >
> >  #if !HAVE_CBRTF
> >  static av_always_inline float cbrtf(float x)
> >  {
> >      return x < 0 ? -powf(-x, 1.0 / 3.0) : powf(x, 1.0 / 3.0);
> >  }
> > -#endif
> > +#endif /* HAVE_CBRTF */
> >
> >  #if !HAVE_COPYSIGN
> >  static av_always_inline double copysign(double x, double y)
> > @@ -71,12 +77,13 @@ static av_always_inline double copysign(double x,  
> double y)
> >      uint64_t vy = av_double2int(y);
> >      return av_int2double((vx & UINT64_C(0x7fffffffffffffff)) | (vy &  
> UINT64_C(0x8000000000000000)));
> >  }
> > -#endif
> > +#endif /* HAVE_COPYSIGN */
> >
> >  #if !HAVE_COSF
> > -#undef cosf
> > -#define cosf(x) ((float)cos(x))
> > -#endif
> > +static av_always_inline float cosf(float x) {
> > +    return cos(x);
> > +}
> > +#endif /* HAVE_COSF */
> >
> >  #if !HAVE_ERF
> >  static inline double ff_eval_poly(const double *coeff, int size, double  
> x) {
> > @@ -279,18 +286,24 @@ static inline double erf(double z)
> >  #endif
> >
> >  #if !HAVE_EXPF
> > -#undef expf
> > -#define expf(x) ((float)exp(x))
> > -#endif
> > +static av_always_inline float expf(float x)
> > +{
> > +    return exp(x);
> > +}
> > +#endif /* HAVE_EXPF */
> >
> >  #if !HAVE_EXP2
> > -#undef exp2
> > -#define exp2(x) exp((x) * 0.693147180559945)
> > +static av_always_inline double exp2(double x)
> > +{
> > +    return exp(x * M_LN2);
> > +}
> >  #endif /* HAVE_EXP2 */
> >
> >  #if !HAVE_EXP2F
> > -#undef exp2f
> > -#define exp2f(x) ((float)exp2(x))
> > +static av_always_inline float exp2f(float x)
> > +{
> > +    return exp2(x);
> > +}
> >  #endif /* HAVE_EXP2F */
> >
> >  /* Somewhat inaccurate fallbacks, relative error ~ 1e-13 concentrated on  
> very
> > @@ -362,7 +375,6 @@ static av_always_inline av_const int  
> avpriv_isnan(double x)
> >  #endif /* HAVE_ISNAN */
> >
> >  #if !HAVE_HYPOT
> > -#undef hypot
> >  static inline av_const double hypot(double x, double y)
> >  {
> >      double ret, temp;
> > @@ -385,39 +397,53 @@ static inline av_const double hypot(double x,  
> double y)
> >  #endif /* HAVE_HYPOT */
> >
> >  #if !HAVE_LDEXPF
> > -#undef ldexpf
> > -#define ldexpf(x, exp) ((float)ldexp(x, exp))
> > -#endif
> > +static av_always_inline float ldexpf(float x, int exp)
> > +{
> > +    return ldexp(x, exp);
> > +}
> > +#endif /* HAVE_LDEXPF */
> >
> >  #if !HAVE_LLRINT
> > -#undef llrint
> > -#define llrint(x) ((long long)rint(x))
> > +static av_always_inline long long llrint(double x)
> > +{
> > +    return rint(x);
> > +}
> >  #endif /* HAVE_LLRINT */
> >
> >  #if !HAVE_LLRINTF
> > -#undef llrintf
> > -#define llrintf(x) ((long long)rint(x))
> > -#endif /* HAVE_LLRINT */
> > +static av_always_inline long long llrintf(float x)
> > +{
> > +    return rint(x);
> > +}
> > +#endif /* HAVE_LLRINTF */
> >
> >  #if !HAVE_LOG2
> > -#undef log2
> > -#define log2(x) (log(x) * 1.44269504088896340736)
> > +static av_always_inline double log2(double x)
> > +{
> > +    return log(x * 1.44269504088896340736);
> > +}
> >  #endif /* HAVE_LOG2 */
> >
> >  #if !HAVE_LOG2F
> > -#undef log2f
> > -#define log2f(x) ((float)log2(x))
> > +static av_always_inline float log2f(float x)
> > +{
> > +    return log2(x);
> > +}
> >  #endif /* HAVE_LOG2F */
> >
> >  #if !HAVE_LOG10F
> > -#undef log10f
> > -#define log10f(x) ((float)log10(x))
> > -#endif
> > +static av_always_inline float log10f(float x)
> > +{
> > +    return log10(x);
> > +}
> > +#endif /* HAVE_LOG10F */
> >
> >  #if !HAVE_SINF
> > -#undef sinf
> > -#define sinf(x) ((float)sin(x))
> > -#endif
> > +static av_always_inline float sinf(float x)
> > +{
> > +    return sin(x);
> > +}
> > +#endif /* HAVE_SINF */
> >
> >  #if !HAVE_RINT
> >  static inline double rint(double x)
> > --
> > 2.6.4
> >  
> 
> Could this cause linkage issues if presence of such a function is
> mis-detected? Previously this worked fine. There was at least one such case
> where log2 IIRC was explicitly disabled in configure because its in the
> libc library but forgotten in the headers in one specific libc.
> 
> Not sure what the rules for static inline functions are in that regard.

Not adding functions to headers is perfectly allowed, and might
actually be done in practice to keep ABI for deprecated/officially
removed functions. For example, a libc could choose to remove gets()
from its headers, but keep it in the .so to maintain backwards
compatibility with older binaries.


More information about the ffmpeg-devel mailing list