misc.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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-2003 *
  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_types.h"
  19. /*#define _VDBG_GRAPHFILE "_0.m"*/
  20. #ifdef _VDBG_GRAPHFILE
  21. extern void *_VDBG_malloc(void *ptr,long bytes,char *file,long line);
  22. extern void _VDBG_free(void *ptr,char *file,long line);
  23. #undef _ogg_malloc
  24. #undef _ogg_calloc
  25. #undef _ogg_realloc
  26. #undef _ogg_free
  27. #define _ogg_malloc(x) _VDBG_malloc(NULL,(x),__FILE__,__LINE__)
  28. #define _ogg_calloc(x,y) _VDBG_malloc(NULL,(x)*(y),__FILE__,__LINE__)
  29. #define _ogg_realloc(x,y) _VDBG_malloc((x),(y),__FILE__,__LINE__)
  30. #define _ogg_free(x) _VDBG_free((x),__FILE__,__LINE__)
  31. #endif
  32. #include "asm_arm.h"
  33. #ifndef _V_WIDE_MATH
  34. #define _V_WIDE_MATH
  35. #ifndef _LOW_ACCURACY_
  36. /* 64 bit multiply */
  37. #include <sys/types.h>
  38. #if BYTE_ORDER==LITTLE_ENDIAN
  39. union magic {
  40. struct {
  41. ogg_int32_t lo;
  42. ogg_int32_t hi;
  43. } halves;
  44. ogg_int64_t whole;
  45. };
  46. #endif
  47. #if BYTE_ORDER==BIG_ENDIAN
  48. union magic {
  49. struct {
  50. ogg_int32_t hi;
  51. ogg_int32_t lo;
  52. } halves;
  53. ogg_int64_t whole;
  54. };
  55. #endif
  56. static inline ogg_int32_t MULT32(ogg_int32_t x, ogg_int32_t y) {
  57. union magic magic;
  58. magic.whole = (ogg_int64_t)x * y;
  59. return magic.halves.hi;
  60. }
  61. static inline ogg_int32_t MULT31(ogg_int32_t x, ogg_int32_t y) {
  62. return MULT32(x,y)<<1;
  63. }
  64. static inline ogg_int32_t MULT31_SHIFT15(ogg_int32_t x, ogg_int32_t y) {
  65. union magic magic;
  66. magic.whole = (ogg_int64_t)x * y;
  67. return ((ogg_uint32_t)(magic.halves.lo)>>15) | ((magic.halves.hi)<<17);
  68. }
  69. #else
  70. /* 32 bit multiply, more portable but less accurate */
  71. /*
  72. * Note: Precision is biased towards the first argument therefore ordering
  73. * is important. Shift values were chosen for the best sound quality after
  74. * many listening tests.
  75. */
  76. /*
  77. * For MULT32 and MULT31: The second argument is always a lookup table
  78. * value already preshifted from 31 to 8 bits. We therefore take the
  79. * opportunity to save on text space and use unsigned char for those
  80. * tables in this case.
  81. */
  82. static inline ogg_int32_t MULT32(ogg_int32_t x, ogg_int32_t y) {
  83. return (x >> 9) * y; /* y preshifted >>23 */
  84. }
  85. static inline ogg_int32_t MULT31(ogg_int32_t x, ogg_int32_t y) {
  86. return (x >> 8) * y; /* y preshifted >>23 */
  87. }
  88. static inline ogg_int32_t MULT31_SHIFT15(ogg_int32_t x, ogg_int32_t y) {
  89. return (x >> 6) * y; /* y preshifted >>9 */
  90. }
  91. #endif
  92. /*
  93. * This should be used as a memory barrier, forcing all cached values in
  94. * registers to wr writen back to memory. Might or might not be beneficial
  95. * depending on the architecture and compiler.
  96. */
  97. #define MB()
  98. /*
  99. * The XPROD functions are meant to optimize the cross products found all
  100. * over the place in mdct.c by forcing memory operation ordering to avoid
  101. * unnecessary register reloads as soon as memory is being written to.
  102. * However this is only beneficial on CPUs with a sane number of general
  103. * purpose registers which exclude the Intel x86. On Intel, better let the
  104. * compiler actually reload registers directly from original memory by using
  105. * macros.
  106. */
  107. #ifdef __i386__
  108. #define XPROD32(_a, _b, _t, _v, _x, _y) \
  109. { *(_x)=MULT32(_a,_t)+MULT32(_b,_v); \
  110. *(_y)=MULT32(_b,_t)-MULT32(_a,_v); }
  111. #define XPROD31(_a, _b, _t, _v, _x, _y) \
  112. { *(_x)=MULT31(_a,_t)+MULT31(_b,_v); \
  113. *(_y)=MULT31(_b,_t)-MULT31(_a,_v); }
  114. #define XNPROD31(_a, _b, _t, _v, _x, _y) \
  115. { *(_x)=MULT31(_a,_t)-MULT31(_b,_v); \
  116. *(_y)=MULT31(_b,_t)+MULT31(_a,_v); }
  117. #else
  118. static inline void XPROD32(ogg_int32_t a, ogg_int32_t b,
  119. ogg_int32_t t, ogg_int32_t v,
  120. ogg_int32_t *x, ogg_int32_t *y)
  121. {
  122. *x = MULT32(a, t) + MULT32(b, v);
  123. *y = MULT32(b, t) - MULT32(a, v);
  124. }
  125. static inline void XPROD31(ogg_int32_t a, ogg_int32_t b,
  126. ogg_int32_t t, ogg_int32_t v,
  127. ogg_int32_t *x, ogg_int32_t *y)
  128. {
  129. *x = MULT31(a, t) + MULT31(b, v);
  130. *y = MULT31(b, t) - MULT31(a, v);
  131. }
  132. static inline void XNPROD31(ogg_int32_t a, ogg_int32_t b,
  133. ogg_int32_t t, ogg_int32_t v,
  134. ogg_int32_t *x, ogg_int32_t *y)
  135. {
  136. *x = MULT31(a, t) - MULT31(b, v);
  137. *y = MULT31(b, t) + MULT31(a, v);
  138. }
  139. #endif
  140. #endif
  141. #ifndef _V_CLIP_MATH
  142. #define _V_CLIP_MATH
  143. static inline ogg_int32_t CLIP_TO_15(ogg_int32_t x) {
  144. int ret=x;
  145. ret-= ((x<=32767)-1)&(x-32767);
  146. ret-= ((x>=-32768)-1)&(x+32768);
  147. return(ret);
  148. }
  149. #endif
  150. #endif