misc.h 5.1 KB

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