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.

183 lines
4.1KB

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