ffmath.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * copyright (c) 2016 Ganesh Ajjanagadde <gajjanag@gmail.com>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * internal math functions header
  23. */
  24. #ifndef AVUTIL_FFMATH_H
  25. #define AVUTIL_FFMATH_H
  26. #include "attributes.h"
  27. #include "libm.h"
  28. /**
  29. * Compute 10^x for floating point values. Note: this function is by no means
  30. * "correctly rounded", and is meant as a fast, reasonably accurate approximation.
  31. * For instance, maximum relative error for the double precision variant is
  32. * ~ 1e-13 for very small and very large values.
  33. * This is ~2x faster than GNU libm's approach, which is still off by 2ulp on
  34. * some inputs.
  35. * @param x exponent
  36. * @return 10^x
  37. */
  38. static av_always_inline double ff_exp10(double x)
  39. {
  40. return exp2(M_LOG2_10 * x);
  41. }
  42. static av_always_inline float ff_exp10f(float x)
  43. {
  44. return exp2f(M_LOG2_10 * x);
  45. }
  46. /**
  47. * Compute x^y for floating point x, y. Note: this function is faster than the
  48. * libm variant due to mainly 2 reasons:
  49. * 1. It does not handle any edge cases. In particular, this is only guaranteed
  50. * to work correctly for x > 0.
  51. * 2. It is not as accurate as a standard nearly "correctly rounded" libm variant.
  52. * @param x base
  53. * @param y exponent
  54. * @return x^y
  55. */
  56. static av_always_inline float ff_fast_powf(float x, float y)
  57. {
  58. return expf(logf(x) * y);
  59. }
  60. #endif /* AVUTIL_FFMATH_H */