mathops.h 4.0 KB

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