/media/libtremor/lib/misc.h

http://github.com/zpao/v8monkey · C Header · 250 lines · 163 code · 43 blank · 44 comment · 10 complexity · 578475bf245c6be56e2afbd357643272 MD5 · raw file

  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE OggVorbis 'TREMOR' CODEC SOURCE CODE. *
  4. * *
  5. * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
  6. * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
  7. * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  8. * *
  9. * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2002 *
  10. * BY THE Xiph.Org FOUNDATION http://www.xiph.org/ *
  11. * *
  12. ********************************************************************
  13. function: miscellaneous math and prototypes
  14. ********************************************************************/
  15. #ifndef _V_RANDOM_H_
  16. #define _V_RANDOM_H_
  17. #include "ivorbiscodec.h"
  18. #include "os.h"
  19. #ifdef _LOW_ACCURACY_
  20. # define X(n) (((((n)>>22)+1)>>1) - ((((n)>>22)+1)>>9))
  21. # define LOOKUP_T const unsigned char
  22. #else
  23. # define X(n) (n)
  24. # define LOOKUP_T const ogg_int32_t
  25. #endif
  26. #include "asm_arm.h"
  27. #include <stdlib.h> /* for abs() */
  28. #ifndef _V_WIDE_MATH
  29. #define _V_WIDE_MATH
  30. #ifndef _LOW_ACCURACY_
  31. /* 64 bit multiply */
  32. #if !(defined WIN32 && defined WINCE)
  33. #include <sys/types.h>
  34. #endif
  35. #if BYTE_ORDER==LITTLE_ENDIAN
  36. union magic {
  37. struct {
  38. ogg_int32_t lo;
  39. ogg_int32_t hi;
  40. } halves;
  41. ogg_int64_t whole;
  42. };
  43. #elif BYTE_ORDER==BIG_ENDIAN
  44. union magic {
  45. struct {
  46. ogg_int32_t hi;
  47. ogg_int32_t lo;
  48. } halves;
  49. ogg_int64_t whole;
  50. };
  51. #endif
  52. STIN ogg_int32_t MULT32(ogg_int32_t x, ogg_int32_t y) {
  53. union magic magic;
  54. magic.whole = (ogg_int64_t)x * y;
  55. return magic.halves.hi;
  56. }
  57. STIN ogg_int32_t MULT31(ogg_int32_t x, ogg_int32_t y) {
  58. return MULT32(x,y)<<1;
  59. }
  60. STIN ogg_int32_t MULT31_SHIFT15(ogg_int32_t x, ogg_int32_t y) {
  61. union magic magic;
  62. magic.whole = (ogg_int64_t)x * y;
  63. return ((ogg_uint32_t)(magic.halves.lo)>>15) | ((magic.halves.hi)<<17);
  64. }
  65. #else
  66. /* 32 bit multiply, more portable but less accurate */
  67. /*
  68. * Note: Precision is biased towards the first argument therefore ordering
  69. * is important. Shift values were chosen for the best sound quality after
  70. * many listening tests.
  71. */
  72. /*
  73. * For MULT32 and MULT31: The second argument is always a lookup table
  74. * value already preshifted from 31 to 8 bits. We therefore take the
  75. * opportunity to save on text space and use unsigned char for those
  76. * tables in this case.
  77. */
  78. STIN ogg_int32_t MULT32(ogg_int32_t x, ogg_int32_t y) {
  79. return (x >> 9) * y; /* y preshifted >>23 */
  80. }
  81. STIN ogg_int32_t MULT31(ogg_int32_t x, ogg_int32_t y) {
  82. return (x >> 8) * y; /* y preshifted >>23 */
  83. }
  84. STIN ogg_int32_t MULT31_SHIFT15(ogg_int32_t x, ogg_int32_t y) {
  85. return (x >> 6) * y; /* y preshifted >>9 */
  86. }
  87. #endif
  88. /*
  89. * This should be used as a memory barrier, forcing all cached values in
  90. * registers to wr writen back to memory. Might or might not be beneficial
  91. * depending on the architecture and compiler.
  92. */
  93. #define MB()
  94. /*
  95. * The XPROD functions are meant to optimize the cross products found all
  96. * over the place in mdct.c by forcing memory operation ordering to avoid
  97. * unnecessary register reloads as soon as memory is being written to.
  98. * However this is only beneficial on CPUs with a sane number of general
  99. * purpose registers which exclude the Intel x86. On Intel, better let the
  100. * compiler actually reload registers directly from original memory by using
  101. * macros.
  102. */
  103. #ifdef __i386__
  104. #define XPROD32(_a, _b, _t, _v, _x, _y) \
  105. { *(_x)=MULT32(_a,_t)+MULT32(_b,_v); \
  106. *(_y)=MULT32(_b,_t)-MULT32(_a,_v); }
  107. #define XPROD31(_a, _b, _t, _v, _x, _y) \
  108. { *(_x)=MULT31(_a,_t)+MULT31(_b,_v); \
  109. *(_y)=MULT31(_b,_t)-MULT31(_a,_v); }
  110. #define XNPROD31(_a, _b, _t, _v, _x, _y) \
  111. { *(_x)=MULT31(_a,_t)-MULT31(_b,_v); \
  112. *(_y)=MULT31(_b,_t)+MULT31(_a,_v); }
  113. #else
  114. STIN void XPROD32(ogg_int32_t a, ogg_int32_t b,
  115. ogg_int32_t t, ogg_int32_t v,
  116. ogg_int32_t *x, ogg_int32_t *y)
  117. {
  118. *x = MULT32(a, t) + MULT32(b, v);
  119. *y = MULT32(b, t) - MULT32(a, v);
  120. }
  121. STIN void XPROD31(ogg_int32_t a, ogg_int32_t b,
  122. ogg_int32_t t, ogg_int32_t v,
  123. ogg_int32_t *x, ogg_int32_t *y)
  124. {
  125. *x = MULT31(a, t) + MULT31(b, v);
  126. *y = MULT31(b, t) - MULT31(a, v);
  127. }
  128. STIN void XNPROD31(ogg_int32_t a, ogg_int32_t b,
  129. ogg_int32_t t, ogg_int32_t v,
  130. ogg_int32_t *x, ogg_int32_t *y)
  131. {
  132. *x = MULT31(a, t) - MULT31(b, v);
  133. *y = MULT31(b, t) + MULT31(a, v);
  134. }
  135. #endif
  136. #endif
  137. #ifndef _V_CLIP_MATH
  138. #define _V_CLIP_MATH
  139. STIN ogg_int32_t CLIP_TO_15(ogg_int32_t x) {
  140. int ret=x;
  141. ret-= ((x<=32767)-1)&(x-32767);
  142. ret-= ((x>=-32768)-1)&(x+32768);
  143. return(ret);
  144. }
  145. #endif
  146. STIN ogg_int32_t VFLOAT_MULT(ogg_int32_t a,ogg_int32_t ap,
  147. ogg_int32_t b,ogg_int32_t bp,
  148. ogg_int32_t *p){
  149. if(a && b){
  150. #ifndef _LOW_ACCURACY_
  151. *p=ap+bp+32;
  152. return MULT32(a,b);
  153. #else
  154. *p=ap+bp+31;
  155. return (a>>15)*(b>>16);
  156. #endif
  157. }else
  158. return 0;
  159. }
  160. int _ilog(unsigned int);
  161. STIN ogg_int32_t VFLOAT_MULTI(ogg_int32_t a,ogg_int32_t ap,
  162. ogg_int32_t i,
  163. ogg_int32_t *p){
  164. int ip=_ilog(abs(i))-31;
  165. return VFLOAT_MULT(a,ap,i<<-ip,ip,p);
  166. }
  167. STIN ogg_int32_t VFLOAT_ADD(ogg_int32_t a,ogg_int32_t ap,
  168. ogg_int32_t b,ogg_int32_t bp,
  169. ogg_int32_t *p){
  170. if(!a){
  171. *p=bp;
  172. return b;
  173. }else if(!b){
  174. *p=ap;
  175. return a;
  176. }
  177. /* yes, this can leak a bit. */
  178. if(ap>bp){
  179. int shift=ap-bp+1;
  180. *p=ap+1;
  181. a>>=1;
  182. if(shift<32){
  183. b=(b+(1<<(shift-1)))>>shift;
  184. }else{
  185. b=0;
  186. }
  187. }else{
  188. int shift=bp-ap+1;
  189. *p=bp+1;
  190. b>>=1;
  191. if(shift<32){
  192. a=(a+(1<<(shift-1)))>>shift;
  193. }else{
  194. a=0;
  195. }
  196. }
  197. a+=b;
  198. if((a&0xc0000000)==0xc0000000 ||
  199. (a&0xc0000000)==0){
  200. a<<=1;
  201. (*p)--;
  202. }
  203. return(a);
  204. }
  205. #endif