Libav
|
00001 /* 00002 * Copyright (c) 2010 Mans Rullgard <mans@mansr.com> 00003 * 00004 * This file is part of FFmpeg. 00005 * 00006 * FFmpeg is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Lesser General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2.1 of the License, or (at your option) any later version. 00010 * 00011 * FFmpeg is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public 00017 * License along with FFmpeg; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00019 */ 00020 00021 #ifndef AVUTIL_INTMATH_H 00022 #define AVUTIL_INTMATH_H 00023 00024 #include <stdint.h> 00025 #include "config.h" 00026 #include "attributes.h" 00027 00028 extern const uint32_t ff_inverse[257]; 00029 00030 #if ARCH_ARM 00031 # include "arm/intmath.h" 00032 #elif ARCH_X86 00033 # include "x86/intmath.h" 00034 #endif 00035 00036 #if HAVE_FAST_CLZ && AV_GCC_VERSION_AT_LEAST(3,4) 00037 00038 #ifndef av_log2 00039 00040 #define av_log2(x) (31 - __builtin_clz((x)|1)) 00041 00042 #ifndef av_log2_16bit 00043 #define av_log2_16bit av_log2 00044 #endif 00045 00046 #endif /* av_log2 */ 00047 00048 #endif /* AV_GCC_VERSION_AT_LEAST(3,4) */ 00049 00050 #ifndef FASTDIV 00051 00052 #if CONFIG_FASTDIV 00053 # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a) * ff_inverse[b]) >> 32)) 00054 #else 00055 # define FASTDIV(a,b) ((a) / (b)) 00056 #endif 00057 00058 #endif /* FASTDIV */ 00059 00060 /* 00061 * Get definition of av_log2_c from common.h. In the event we got 00062 * here through common.h including this file, including it again will 00063 * be a no-op due to multi-inclusion guards, so we must duplicate the 00064 * fallback defines here. 00065 */ 00066 00067 #include "common.h" 00068 00069 #ifndef av_log2 00070 # define av_log2 av_log2_c 00071 #endif 00072 #ifndef av_log2_16bit 00073 # define av_log2_16bit av_log2_16bit_c 00074 #endif 00075 00076 extern const uint8_t ff_sqrt_tab[256]; 00077 00078 static inline av_const unsigned int ff_sqrt(unsigned int a) 00079 { 00080 unsigned int b; 00081 00082 if (a < 255) return (ff_sqrt_tab[a + 1] - 1) >> 4; 00083 else if (a < (1 << 12)) b = ff_sqrt_tab[a >> 4] >> 2; 00084 #if !CONFIG_SMALL 00085 else if (a < (1 << 14)) b = ff_sqrt_tab[a >> 6] >> 1; 00086 else if (a < (1 << 16)) b = ff_sqrt_tab[a >> 8] ; 00087 #endif 00088 else { 00089 int s = av_log2_16bit(a >> 16) >> 1; 00090 unsigned int c = a >> (s + 2); 00091 b = ff_sqrt_tab[c >> (s + 8)]; 00092 b = FASTDIV(c,b) + (b << s); 00093 } 00094 00095 return b - (a < b * b); 00096 } 00097 00098 #endif /* AVUTIL_INTMATH_H */