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.

218 lines
4.9KB

  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_sqrt_tab[256];
  29. #if ARCH_ARM
  30. # include "arm/mathops.h"
  31. #elif ARCH_AVR32
  32. # include "avr32/mathops.h"
  33. #elif ARCH_BFIN
  34. # include "bfin/mathops.h"
  35. #elif ARCH_MIPS
  36. # include "mips/mathops.h"
  37. #elif ARCH_PPC
  38. # include "ppc/mathops.h"
  39. #elif ARCH_X86
  40. # include "x86/mathops.h"
  41. #endif
  42. /* generic implementation */
  43. #ifndef MUL64
  44. # define MUL64(a,b) ((int64_t)(a) * (int64_t)(b))
  45. #endif
  46. #ifndef MULL
  47. # define MULL(a,b,s) (MUL64(a, b) >> (s))
  48. #endif
  49. #ifndef MULH
  50. static av_always_inline int MULH(int a, int b){
  51. return MUL64(a, b) >> 32;
  52. }
  53. #endif
  54. #ifndef UMULH
  55. static av_always_inline unsigned UMULH(unsigned a, unsigned b){
  56. return ((uint64_t)(a) * (uint64_t)(b))>>32;
  57. }
  58. #endif
  59. #ifndef MAC64
  60. # define MAC64(d, a, b) ((d) += MUL64(a, b))
  61. #endif
  62. #ifndef MLS64
  63. # define MLS64(d, a, b) ((d) -= MUL64(a, b))
  64. #endif
  65. /* signed 16x16 -> 32 multiply add accumulate */
  66. #ifndef MAC16
  67. # define MAC16(rt, ra, rb) rt += (ra) * (rb)
  68. #endif
  69. /* signed 16x16 -> 32 multiply */
  70. #ifndef MUL16
  71. # define MUL16(ra, rb) ((ra) * (rb))
  72. #endif
  73. #ifndef MLS16
  74. # define MLS16(rt, ra, rb) ((rt) -= (ra) * (rb))
  75. #endif
  76. /* median of 3 */
  77. #ifndef mid_pred
  78. #define mid_pred mid_pred
  79. static inline av_const int mid_pred(int a, int b, int c)
  80. {
  81. #if 0
  82. int t= (a-b)&((a-b)>>31);
  83. a-=t;
  84. b+=t;
  85. b-= (b-c)&((b-c)>>31);
  86. b+= (a-b)&((a-b)>>31);
  87. return b;
  88. #else
  89. if(a>b){
  90. if(c>b){
  91. if(c>a) b=a;
  92. else b=c;
  93. }
  94. }else{
  95. if(b>c){
  96. if(c>a) b=c;
  97. else b=a;
  98. }
  99. }
  100. return b;
  101. #endif
  102. }
  103. #endif
  104. #ifndef sign_extend
  105. static inline av_const int sign_extend(int val, unsigned bits)
  106. {
  107. unsigned shift = 8 * sizeof(int) - bits;
  108. union { unsigned u; int s; } v = { (unsigned) val << shift };
  109. return v.s >> shift;
  110. }
  111. #endif
  112. #ifndef zero_extend
  113. static inline av_const unsigned zero_extend(unsigned val, unsigned bits)
  114. {
  115. return (val << ((8 * sizeof(int)) - bits)) >> ((8 * sizeof(int)) - bits);
  116. }
  117. #endif
  118. #ifndef COPY3_IF_LT
  119. #define COPY3_IF_LT(x, y, a, b, c, d)\
  120. if ((y) < (x)) {\
  121. (x) = (y);\
  122. (a) = (b);\
  123. (c) = (d);\
  124. }
  125. #endif
  126. #ifndef MASK_ABS
  127. #define MASK_ABS(mask, level) do { \
  128. mask = level >> 31; \
  129. level = (level ^ mask) - mask; \
  130. } while (0)
  131. #endif
  132. #ifndef NEG_SSR32
  133. # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
  134. #endif
  135. #ifndef NEG_USR32
  136. # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
  137. #endif
  138. #if HAVE_BIGENDIAN
  139. # ifndef PACK_2U8
  140. # define PACK_2U8(a,b) (((a) << 8) | (b))
  141. # endif
  142. # ifndef PACK_4U8
  143. # define PACK_4U8(a,b,c,d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
  144. # endif
  145. # ifndef PACK_2U16
  146. # define PACK_2U16(a,b) (((a) << 16) | (b))
  147. # endif
  148. #else
  149. # ifndef PACK_2U8
  150. # define PACK_2U8(a,b) (((b) << 8) | (a))
  151. # endif
  152. # ifndef PACK_4U2
  153. # define PACK_4U8(a,b,c,d) (((d) << 24) | ((c) << 16) | ((b) << 8) | (a))
  154. # endif
  155. # ifndef PACK_2U16
  156. # define PACK_2U16(a,b) (((b) << 16) | (a))
  157. # endif
  158. #endif
  159. #ifndef PACK_2S8
  160. # define PACK_2S8(a,b) PACK_2U8((a)&255, (b)&255)
  161. #endif
  162. #ifndef PACK_4S8
  163. # define PACK_4S8(a,b,c,d) PACK_4U8((a)&255, (b)&255, (c)&255, (d)&255)
  164. #endif
  165. #ifndef PACK_2S16
  166. # define PACK_2S16(a,b) PACK_2U16((a)&0xffff, (b)&0xffff)
  167. #endif
  168. #ifndef FASTDIV
  169. # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a) * ff_inverse[b]) >> 32))
  170. #endif /* FASTDIV */
  171. static inline av_const unsigned int ff_sqrt(unsigned int a)
  172. {
  173. unsigned int b;
  174. if (a < 255) return (ff_sqrt_tab[a + 1] - 1) >> 4;
  175. else if (a < (1 << 12)) b = ff_sqrt_tab[a >> 4] >> 2;
  176. #if !CONFIG_SMALL
  177. else if (a < (1 << 14)) b = ff_sqrt_tab[a >> 6] >> 1;
  178. else if (a < (1 << 16)) b = ff_sqrt_tab[a >> 8] ;
  179. #endif
  180. else {
  181. int s = av_log2_16bit(a >> 16) >> 1;
  182. unsigned int c = a >> (s + 2);
  183. b = ff_sqrt_tab[c >> (s + 8)];
  184. b = FASTDIV(c,b) + (b << s);
  185. }
  186. return b - (a < b * b);
  187. }
  188. #endif /* AVCODEC_MATHOPS_H */