You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

219 lines
5.0KB

  1. /*
  2. * simple math operations
  3. * Copyright (c) 2001, 2002 Fabrice Bellard
  4. * Copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at> et al
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #ifndef AVCODEC_MATHOPS_H
  23. #define AVCODEC_MATHOPS_H
  24. #include <stdint.h>
  25. #include "libavutil/common.h"
  26. #include "config.h"
  27. extern const uint32_t ff_inverse[257];
  28. extern const uint8_t ff_reverse[256];
  29. extern const uint8_t ff_sqrt_tab[256];
  30. #if ARCH_ARM
  31. # include "arm/mathops.h"
  32. #elif ARCH_AVR32
  33. # include "avr32/mathops.h"
  34. #elif ARCH_BFIN
  35. # include "bfin/mathops.h"
  36. #elif ARCH_MIPS
  37. # include "mips/mathops.h"
  38. #elif ARCH_PPC
  39. # include "ppc/mathops.h"
  40. #elif ARCH_X86
  41. # include "x86/mathops.h"
  42. #endif
  43. /* generic implementation */
  44. #ifndef MUL64
  45. # define MUL64(a,b) ((int64_t)(a) * (int64_t)(b))
  46. #endif
  47. #ifndef MULL
  48. # define MULL(a,b,s) (MUL64(a, b) >> (s))
  49. #endif
  50. #ifndef MULH
  51. static av_always_inline int MULH(int a, int b){
  52. return MUL64(a, b) >> 32;
  53. }
  54. #endif
  55. #ifndef UMULH
  56. static av_always_inline unsigned UMULH(unsigned a, unsigned b){
  57. return ((uint64_t)(a) * (uint64_t)(b))>>32;
  58. }
  59. #endif
  60. #ifndef MAC64
  61. # define MAC64(d, a, b) ((d) += MUL64(a, b))
  62. #endif
  63. #ifndef MLS64
  64. # define MLS64(d, a, b) ((d) -= MUL64(a, b))
  65. #endif
  66. /* signed 16x16 -> 32 multiply add accumulate */
  67. #ifndef MAC16
  68. # define MAC16(rt, ra, rb) rt += (ra) * (rb)
  69. #endif
  70. /* signed 16x16 -> 32 multiply */
  71. #ifndef MUL16
  72. # define MUL16(ra, rb) ((ra) * (rb))
  73. #endif
  74. #ifndef MLS16
  75. # define MLS16(rt, ra, rb) ((rt) -= (ra) * (rb))
  76. #endif
  77. /* median of 3 */
  78. #ifndef mid_pred
  79. #define mid_pred mid_pred
  80. static inline av_const int mid_pred(int a, int b, int c)
  81. {
  82. #if 0
  83. int t= (a-b)&((a-b)>>31);
  84. a-=t;
  85. b+=t;
  86. b-= (b-c)&((b-c)>>31);
  87. b+= (a-b)&((a-b)>>31);
  88. return b;
  89. #else
  90. if(a>b){
  91. if(c>b){
  92. if(c>a) b=a;
  93. else b=c;
  94. }
  95. }else{
  96. if(b>c){
  97. if(c>a) b=c;
  98. else b=a;
  99. }
  100. }
  101. return b;
  102. #endif
  103. }
  104. #endif
  105. #ifndef sign_extend
  106. static inline av_const int sign_extend(int val, unsigned bits)
  107. {
  108. unsigned shift = 8 * sizeof(int) - bits;
  109. union { unsigned u; int s; } v = { (unsigned) val << shift };
  110. return v.s >> shift;
  111. }
  112. #endif
  113. #ifndef zero_extend
  114. static inline av_const unsigned zero_extend(unsigned val, unsigned bits)
  115. {
  116. return (val << ((8 * sizeof(int)) - bits)) >> ((8 * sizeof(int)) - bits);
  117. }
  118. #endif
  119. #ifndef COPY3_IF_LT
  120. #define COPY3_IF_LT(x, y, a, b, c, d)\
  121. if ((y) < (x)) {\
  122. (x) = (y);\
  123. (a) = (b);\
  124. (c) = (d);\
  125. }
  126. #endif
  127. #ifndef MASK_ABS
  128. #define MASK_ABS(mask, level) do { \
  129. mask = level >> 31; \
  130. level = (level ^ mask) - mask; \
  131. } while (0)
  132. #endif
  133. #ifndef NEG_SSR32
  134. # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
  135. #endif
  136. #ifndef NEG_USR32
  137. # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
  138. #endif
  139. #if HAVE_BIGENDIAN
  140. # ifndef PACK_2U8
  141. # define PACK_2U8(a,b) (((a) << 8) | (b))
  142. # endif
  143. # ifndef PACK_4U8
  144. # define PACK_4U8(a,b,c,d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
  145. # endif
  146. # ifndef PACK_2U16
  147. # define PACK_2U16(a,b) (((a) << 16) | (b))
  148. # endif
  149. #else
  150. # ifndef PACK_2U8
  151. # define PACK_2U8(a,b) (((b) << 8) | (a))
  152. # endif
  153. # ifndef PACK_4U2
  154. # define PACK_4U8(a,b,c,d) (((d) << 24) | ((c) << 16) | ((b) << 8) | (a))
  155. # endif
  156. # ifndef PACK_2U16
  157. # define PACK_2U16(a,b) (((b) << 16) | (a))
  158. # endif
  159. #endif
  160. #ifndef PACK_2S8
  161. # define PACK_2S8(a,b) PACK_2U8((a)&255, (b)&255)
  162. #endif
  163. #ifndef PACK_4S8
  164. # define PACK_4S8(a,b,c,d) PACK_4U8((a)&255, (b)&255, (c)&255, (d)&255)
  165. #endif
  166. #ifndef PACK_2S16
  167. # define PACK_2S16(a,b) PACK_2U16((a)&0xffff, (b)&0xffff)
  168. #endif
  169. #ifndef FASTDIV
  170. # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a) * ff_inverse[b]) >> 32))
  171. #endif /* FASTDIV */
  172. static inline av_const unsigned int ff_sqrt(unsigned int a)
  173. {
  174. unsigned int b;
  175. if (a < 255) return (ff_sqrt_tab[a + 1] - 1) >> 4;
  176. else if (a < (1 << 12)) b = ff_sqrt_tab[a >> 4] >> 2;
  177. #if !CONFIG_SMALL
  178. else if (a < (1 << 14)) b = ff_sqrt_tab[a >> 6] >> 1;
  179. else if (a < (1 << 16)) b = ff_sqrt_tab[a >> 8] ;
  180. #endif
  181. else {
  182. int s = av_log2_16bit(a >> 16) >> 1;
  183. unsigned int c = a >> (s + 2);
  184. b = ff_sqrt_tab[c >> (s + 8)];
  185. b = FASTDIV(c,b) + (b << s);
  186. }
  187. return b - (a < b * b);
  188. }
  189. #endif /* AVCODEC_MATHOPS_H */