mirror of
https://github.com/hrydgard/ppsspp.git
synced 2024-11-23 21:39:52 +00:00
314 lines
8.1 KiB
C
314 lines
8.1 KiB
C
/*
|
|
* copyright (c) 2005-2012 Michael Niedermayer <michaelni@gmx.at>
|
|
*
|
|
* This file is part of FFmpeg.
|
|
*
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
* License as published by the Free Software Foundation; either
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
*
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
* Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <stdint.h>
|
|
#include <math.h>
|
|
#include <limits.h>
|
|
|
|
#include "compat.h"
|
|
|
|
|
|
#if HAVE_FAST_CLZ
|
|
#if AV_GCC_VERSION_AT_LEAST(3,4)
|
|
#ifndef ff_log2
|
|
# define ff_log2(x) (31 - __builtin_clz((x)|1))
|
|
# ifndef ff_log2_16bit
|
|
# define ff_log2_16bit av_log2
|
|
# endif
|
|
#endif /* ff_log2 */
|
|
#endif /* AV_GCC_VERSION_AT_LEAST(3,4) */
|
|
#endif
|
|
|
|
int av_log2(unsigned int v);
|
|
int av_log2_16bit(unsigned int v);
|
|
|
|
/**
|
|
* @addtogroup lavu_math
|
|
* @{
|
|
*/
|
|
|
|
#if HAVE_FAST_CLZ
|
|
#if AV_GCC_VERSION_AT_LEAST(3,4)
|
|
#ifndef ff_ctz
|
|
#define ff_ctz(v) __builtin_ctz(v)
|
|
#endif
|
|
#ifndef ff_ctzll
|
|
#define ff_ctzll(v) __builtin_ctzll(v)
|
|
#endif
|
|
#ifndef ff_clz
|
|
#define ff_clz(v) __builtin_clz(v)
|
|
#endif
|
|
#endif
|
|
#endif
|
|
|
|
#ifndef ff_ctz
|
|
#define ff_ctz ff_ctz_c
|
|
/**
|
|
* Trailing zero bit count.
|
|
*
|
|
* @param v input value. If v is 0, the result is undefined.
|
|
* @return the number of trailing 0-bits
|
|
*/
|
|
/* We use the De-Bruijn method outlined in:
|
|
* http://supertech.csail.mit.edu/papers/debruijn.pdf. */
|
|
static av_always_inline av_const int ff_ctz_c(int v)
|
|
{
|
|
static const uint8_t debruijn_ctz32[32] = {
|
|
0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
|
|
31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
|
|
};
|
|
return debruijn_ctz32[(uint32_t)((v & -v) * 0x077CB531U) >> 27];
|
|
}
|
|
#endif
|
|
|
|
#ifndef ff_ctzll
|
|
#define ff_ctzll ff_ctzll_c
|
|
/* We use the De-Bruijn method outlined in:
|
|
* http://supertech.csail.mit.edu/papers/debruijn.pdf. */
|
|
static av_always_inline av_const int ff_ctzll_c(long long v)
|
|
{
|
|
static const uint8_t debruijn_ctz64[64] = {
|
|
0, 1, 2, 53, 3, 7, 54, 27, 4, 38, 41, 8, 34, 55, 48, 28,
|
|
62, 5, 39, 46, 44, 42, 22, 9, 24, 35, 59, 56, 49, 18, 29, 11,
|
|
63, 52, 6, 26, 37, 40, 33, 47, 61, 45, 43, 21, 23, 58, 17, 10,
|
|
51, 25, 36, 32, 60, 20, 57, 16, 50, 31, 19, 15, 30, 14, 13, 12
|
|
};
|
|
return debruijn_ctz64[(uint64_t)((v & -v) * 0x022FDD63CC95386DU) >> 58];
|
|
}
|
|
#endif
|
|
|
|
static inline int sign_extend(int val, unsigned bits)
|
|
{
|
|
unsigned shift = 8 * sizeof(int) - bits;
|
|
union { unsigned u; int s; } v = { (unsigned)val << shift };
|
|
return v.s >> shift;
|
|
}
|
|
|
|
static inline unsigned zero_extend(unsigned val, unsigned bits)
|
|
{
|
|
return (val << ((8 * sizeof(int)) - bits)) >> ((8 * sizeof(int)) - bits);
|
|
}
|
|
|
|
#ifndef NEG_SSR32
|
|
# define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
|
|
#endif
|
|
|
|
#ifndef NEG_USR32
|
|
# define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
|
|
#endif
|
|
|
|
#ifndef M_LOG2_10
|
|
#define M_LOG2_10 3.32192809488736234787 /* log_2 10 */
|
|
#endif
|
|
#ifndef M_PHI
|
|
#define M_PHI 1.61803398874989484820 /* phi / golden ratio */
|
|
#endif
|
|
#ifndef M_PI
|
|
#define M_PI 3.14159265358979323846 /* pi */
|
|
#endif
|
|
#ifndef M_PI_2
|
|
#define M_PI_2 1.57079632679489661923 /* pi/2 */
|
|
#endif
|
|
#ifndef M_SQRT1_2
|
|
#define M_SQRT1_2 0.70710678118654752440 /* 1/sqrt(2) */
|
|
#endif
|
|
#ifndef M_SQRT2
|
|
#define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
|
|
#endif
|
|
|
|
/**
|
|
* @addtogroup lavu_math
|
|
* @{
|
|
*/
|
|
|
|
enum AVRounding {
|
|
AV_ROUND_ZERO = 0, ///< Round toward zero.
|
|
AV_ROUND_INF = 1, ///< Round away from zero.
|
|
AV_ROUND_DOWN = 2, ///< Round toward -infinity.
|
|
AV_ROUND_UP = 3, ///< Round toward +infinity.
|
|
AV_ROUND_NEAR_INF = 5, ///< Round to nearest and halfway cases away from zero.
|
|
AV_ROUND_PASS_MINMAX = 8192, ///< Flag to pass INT64_MIN/MAX through instead of rescaling, this avoids special cases for AV_NOPTS_VALUE
|
|
};
|
|
|
|
/**
|
|
* rational number numerator/denominator
|
|
*/
|
|
typedef struct AVRational {
|
|
int num; ///< numerator
|
|
int den; ///< denominator
|
|
} AVRational;
|
|
|
|
/**
|
|
* Compute the greatest common divisor of a and b.
|
|
*
|
|
* @return gcd of a and b up to sign; if a >= 0 and b >= 0, return value is >= 0;
|
|
* if a == 0 and b == 0, returns 0.
|
|
*/
|
|
int64_t av_gcd(int64_t a, int64_t b);
|
|
|
|
/**
|
|
* Rescale a 64-bit integer with rounding to nearest.
|
|
* A simple a*b/c isn't possible as it can overflow.
|
|
*/
|
|
int64_t av_rescale(int64_t a, int64_t b, int64_t c);
|
|
|
|
/**
|
|
* Rescale a 64-bit integer with specified rounding.
|
|
* A simple a*b/c isn't possible as it can overflow.
|
|
*
|
|
* @return rescaled value a, or if AV_ROUND_PASS_MINMAX is set and a is
|
|
* INT64_MIN or INT64_MAX then a is passed through unchanged.
|
|
*/
|
|
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, AVRounding);
|
|
|
|
/**
|
|
* Rescale a 64-bit integer by 2 rational numbers.
|
|
*/
|
|
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq);
|
|
|
|
/**
|
|
* Rescale a 64-bit integer by 2 rational numbers with specified rounding.
|
|
*
|
|
* @return rescaled value a, or if AV_ROUND_PASS_MINMAX is set and a is
|
|
* INT64_MIN or INT64_MAX then a is passed through unchanged.
|
|
*/
|
|
int64_t av_rescale_q_rnd(int64_t a, AVRational bq, AVRational cq, AVRounding);
|
|
|
|
|
|
/**
|
|
* Create a rational.
|
|
* Useful for compilers that do not support compound literals.
|
|
* @note The return value is not reduced.
|
|
*/
|
|
static inline AVRational av_make_q(int num, int den)
|
|
{
|
|
AVRational r = { num, den };
|
|
return r;
|
|
}
|
|
|
|
/**
|
|
* Compare two rationals.
|
|
* @param a first rational
|
|
* @param b second rational
|
|
* @return 0 if a==b, 1 if a>b, -1 if a<b, and INT_MIN if one of the
|
|
* values is of the form 0/0
|
|
*/
|
|
static inline int av_cmp_q(AVRational a, AVRational b) {
|
|
const int64_t tmp = a.num * (int64_t)b.den - b.num * (int64_t)a.den;
|
|
|
|
if (tmp) return (int)((tmp ^ a.den ^ b.den) >> 63) | 1;
|
|
else if (b.den && a.den) return 0;
|
|
else if (a.num && b.num) return (a.num >> 31) - (b.num >> 31);
|
|
else return INT_MIN;
|
|
}
|
|
|
|
/**
|
|
* Convert rational to double.
|
|
* @param a rational to convert
|
|
* @return (double) a
|
|
*/
|
|
static inline double av_q2d(AVRational a) {
|
|
return a.num / (double)a.den;
|
|
}
|
|
|
|
/**
|
|
* Reduce a fraction.
|
|
* This is useful for framerate calculations.
|
|
* @param dst_num destination numerator
|
|
* @param dst_den destination denominator
|
|
* @param num source numerator
|
|
* @param den source denominator
|
|
* @param max the maximum allowed for dst_num & dst_den
|
|
* @return 1 if exact, 0 otherwise
|
|
*/
|
|
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max);
|
|
|
|
/**
|
|
* Multiply two rationals.
|
|
* @param b first rational
|
|
* @param c second rational
|
|
* @return b*c
|
|
*/
|
|
AVRational av_mul_q(AVRational b, AVRational c);
|
|
|
|
/**
|
|
* Divide one rational by another.
|
|
* @param b first rational
|
|
* @param c second rational
|
|
* @return b/c
|
|
*/
|
|
AVRational av_div_q(AVRational b, AVRational c);
|
|
|
|
/**
|
|
* Add two rationals.
|
|
* @param b first rational
|
|
* @param c second rational
|
|
* @return b+c
|
|
*/
|
|
AVRational av_add_q(AVRational b, AVRational c);
|
|
|
|
/**
|
|
* Invert a rational.
|
|
* @param q value
|
|
* @return 1 / q
|
|
*/
|
|
static inline AVRational av_inv_q(AVRational q)
|
|
{
|
|
AVRational r = { q.den, q.num };
|
|
return r;
|
|
}
|
|
|
|
/**
|
|
* Clear high bits from an unsigned integer starting with specific bit position
|
|
* @param a value to clip
|
|
* @param p bit position to clip at
|
|
* @return clipped value
|
|
*/
|
|
static inline unsigned av_mod_uintp2(unsigned a, unsigned p)
|
|
{
|
|
return a & ((1 << p) - 1);
|
|
}
|
|
|
|
/**
|
|
* Count number of bits set to one in x
|
|
* @param x value to count bits of
|
|
* @return the number of bits set to one in x
|
|
*/
|
|
static inline int av_popcount(uint32_t x)
|
|
{
|
|
x -= (x >> 1) & 0x55555555;
|
|
x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
|
|
x = (x + (x >> 4)) & 0x0F0F0F0F;
|
|
x += x >> 8;
|
|
return (x + (x >> 16)) & 0x3F;
|
|
}
|
|
|
|
/**
|
|
* Count number of bits set to one in x
|
|
* @param x value to count bits of
|
|
* @return the number of bits set to one in x
|
|
*/
|
|
static inline int av_popcount64(uint64_t x)
|
|
{
|
|
return av_popcount((uint32_t)x) + av_popcount((uint32_t)(x >> 32));
|
|
}
|