mathops.h 5.2 KB

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