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.

189 lines
4.2KB

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