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.

181 lines
4.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 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. return (val << ((8 * sizeof(int)) - bits)) >> ((8 * sizeof(int)) - bits);
  105. }
  106. #endif
  107. #ifndef zero_extend
  108. static inline av_const unsigned zero_extend(unsigned val, unsigned bits)
  109. {
  110. return (val << ((8 * sizeof(int)) - bits)) >> ((8 * sizeof(int)) - bits);
  111. }
  112. #endif
  113. #ifndef COPY3_IF_LT
  114. #define COPY3_IF_LT(x, y, a, b, c, d)\
  115. if ((y) < (x)) {\
  116. (x) = (y);\
  117. (a) = (b);\
  118. (c) = (d);\
  119. }
  120. #endif
  121. #ifndef NEG_SSR32
  122. # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
  123. #endif
  124. #ifndef NEG_USR32
  125. # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
  126. #endif
  127. #if HAVE_BIGENDIAN
  128. # ifndef PACK_2U8
  129. # define PACK_2U8(a,b) (((a) << 8) | (b))
  130. # endif
  131. # ifndef PACK_4U8
  132. # define PACK_4U8(a,b,c,d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
  133. # endif
  134. # ifndef PACK_2U16
  135. # define PACK_2U16(a,b) (((a) << 16) | (b))
  136. # endif
  137. #else
  138. # ifndef PACK_2U8
  139. # define PACK_2U8(a,b) (((b) << 8) | (a))
  140. # endif
  141. # ifndef PACK_4U2
  142. # define PACK_4U8(a,b,c,d) (((d) << 24) | ((c) << 16) | ((b) << 8) | (a))
  143. # endif
  144. # ifndef PACK_2U16
  145. # define PACK_2U16(a,b) (((b) << 16) | (a))
  146. # endif
  147. #endif
  148. #ifndef PACK_2S8
  149. # define PACK_2S8(a,b) PACK_2U8((a)&255, (b)&255)
  150. #endif
  151. #ifndef PACK_4S8
  152. # define PACK_4S8(a,b,c,d) PACK_4U8((a)&255, (b)&255, (c)&255, (d)&255)
  153. #endif
  154. #ifndef PACK_2S16
  155. # define PACK_2S16(a,b) PACK_2U16((a)&0xffff, (b)&0xffff)
  156. #endif
  157. #endif /* AVCODEC_MATHOPS_H */