misc.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /************************************************************************
  2. * Copyright (C) 2002-2009, Xiph.org Foundation
  3. * Copyright (C) 2010, Robin Watts for Pinknoise Productions Ltd
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the names of the Xiph.org Foundation nor Pinknoise
  17. * Productions Ltd nor the names of its contributors may be used to
  18. * endorse or promote products derived from this software without
  19. * specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ************************************************************************
  33. function: miscellaneous math and prototypes
  34. ************************************************************************/
  35. #ifndef _V_RANDOM_H_
  36. #define _V_RANDOM_H_
  37. #include "ivorbiscodec.h"
  38. #include "os_types.h"
  39. /*#define _VDBG_GRAPHFILE "_0.m"*/
  40. #ifdef _VDBG_GRAPHFILE
  41. extern void *_VDBG_malloc(void *ptr,long bytes,char *file,long line);
  42. extern void _VDBG_free(void *ptr);
  43. #undef _ogg_malloc
  44. #undef _ogg_calloc
  45. #undef _ogg_realloc
  46. #undef _ogg_free
  47. #define _ogg_malloc(x) _VDBG_malloc(NULL,(x),__FILE__,__LINE__)
  48. #define _ogg_calloc(x,y) _VDBG_malloc(NULL,(x)*(y),__FILE__,__LINE__)
  49. #define _ogg_realloc(x,y) _VDBG_malloc((x),(y),__FILE__,__LINE__)
  50. #define _ogg_free(x) _VDBG_free((x))
  51. #endif
  52. #include "asm_arm.h"
  53. #ifndef _V_WIDE_MATH
  54. #define _V_WIDE_MATH
  55. #ifndef _LOW_ACCURACY_
  56. /* 64 bit multiply */
  57. #include <endian.h>
  58. #include <sys/types.h>
  59. #if BYTE_ORDER==LITTLE_ENDIAN
  60. union magic {
  61. struct {
  62. ogg_int32_t lo;
  63. ogg_int32_t hi;
  64. } halves;
  65. ogg_int64_t whole;
  66. };
  67. #endif
  68. #if BYTE_ORDER==BIG_ENDIAN
  69. union magic {
  70. struct {
  71. ogg_int32_t hi;
  72. ogg_int32_t lo;
  73. } halves;
  74. ogg_int64_t whole;
  75. };
  76. #endif
  77. static inline ogg_int32_t MULT32(ogg_int32_t x, ogg_int32_t y) {
  78. union magic magic;
  79. magic.whole = (ogg_int64_t)x * y;
  80. return magic.halves.hi;
  81. }
  82. static inline ogg_int32_t MULT31(ogg_int32_t x, ogg_int32_t y) {
  83. return MULT32(x,y)<<1;
  84. }
  85. static inline ogg_int32_t MULT31_SHIFT15(ogg_int32_t x, ogg_int32_t y) {
  86. union magic magic;
  87. magic.whole = (ogg_int64_t)x * y;
  88. return ((ogg_uint32_t)(magic.halves.lo)>>15) | ((magic.halves.hi)<<17);
  89. }
  90. #else
  91. /* 32 bit multiply, more portable but less accurate */
  92. /*
  93. * Note: Precision is biased towards the first argument therefore ordering
  94. * is important. Shift values were chosen for the best sound quality after
  95. * many listening tests.
  96. */
  97. /*
  98. * For MULT32 and MULT31: The second argument is always a lookup table
  99. * value already preshifted from 31 to 8 bits. We therefore take the
  100. * opportunity to save on text space and use unsigned char for those
  101. * tables in this case.
  102. */
  103. static inline ogg_int32_t MULT32(ogg_int32_t x, ogg_int32_t y) {
  104. return (x >> 9) * y; /* y preshifted >>23 */
  105. }
  106. static inline ogg_int32_t MULT31(ogg_int32_t x, ogg_int32_t y) {
  107. return (x >> 8) * y; /* y preshifted >>23 */
  108. }
  109. static inline ogg_int32_t MULT31_SHIFT15(ogg_int32_t x, ogg_int32_t y) {
  110. return (x >> 6) * y; /* y preshifted >>9 */
  111. }
  112. #endif
  113. /*
  114. * This should be used as a memory barrier, forcing all cached values in
  115. * registers to wr writen back to memory. Might or might not be beneficial
  116. * depending on the architecture and compiler.
  117. */
  118. #define MB()
  119. /*
  120. * The XPROD functions are meant to optimize the cross products found all
  121. * over the place in mdct.c by forcing memory operation ordering to avoid
  122. * unnecessary register reloads as soon as memory is being written to.
  123. * However this is only beneficial on CPUs with a sane number of general
  124. * purpose registers which exclude the Intel x86. On Intel, better let the
  125. * compiler actually reload registers directly from original memory by using
  126. * macros.
  127. */
  128. #ifdef __i386__
  129. #define XPROD32(_a, _b, _t, _v, _x, _y) \
  130. { *(_x)=MULT32(_a,_t)+MULT32(_b,_v); \
  131. *(_y)=MULT32(_b,_t)-MULT32(_a,_v); }
  132. #define XPROD31(_a, _b, _t, _v, _x, _y) \
  133. { *(_x)=MULT31(_a,_t)+MULT31(_b,_v); \
  134. *(_y)=MULT31(_b,_t)-MULT31(_a,_v); }
  135. #define XNPROD31(_a, _b, _t, _v, _x, _y) \
  136. { *(_x)=MULT31(_a,_t)-MULT31(_b,_v); \
  137. *(_y)=MULT31(_b,_t)+MULT31(_a,_v); }
  138. #else
  139. static inline void XPROD32(ogg_int32_t a, ogg_int32_t b,
  140. ogg_int32_t t, ogg_int32_t v,
  141. ogg_int32_t *x, ogg_int32_t *y)
  142. {
  143. *x = MULT32(a, t) + MULT32(b, v);
  144. *y = MULT32(b, t) - MULT32(a, v);
  145. }
  146. static inline void XPROD31(ogg_int32_t a, ogg_int32_t b,
  147. ogg_int32_t t, ogg_int32_t v,
  148. ogg_int32_t *x, ogg_int32_t *y)
  149. {
  150. *x = MULT31(a, t) + MULT31(b, v);
  151. *y = MULT31(b, t) - MULT31(a, v);
  152. }
  153. static inline void XNPROD31(ogg_int32_t a, ogg_int32_t b,
  154. ogg_int32_t t, ogg_int32_t v,
  155. ogg_int32_t *x, ogg_int32_t *y)
  156. {
  157. *x = MULT31(a, t) - MULT31(b, v);
  158. *y = MULT31(b, t) + MULT31(a, v);
  159. }
  160. #endif
  161. #endif
  162. #ifndef _V_CLIP_MATH
  163. #define _V_CLIP_MATH
  164. static inline ogg_int32_t CLIP_TO_15(ogg_int32_t x) {
  165. int ret=x;
  166. ret-= ((x<=32767)-1)&(x-32767);
  167. ret-= ((x>=-32768)-1)&(x+32768);
  168. return(ret);
  169. }
  170. #endif
  171. #endif