arch.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /* Copyright (c) 2003-2008 Jean-Marc Valin
  2. Copyright (c) 2007-2008 CSIRO
  3. Copyright (c) 2007-2009 Xiph.Org Foundation
  4. Written by Jean-Marc Valin */
  5. /**
  6. @file arch.h
  7. @brief Various architecture definitions for CELT
  8. */
  9. /*
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions
  12. are met:
  13. - Redistributions of source code must retain the above copyright
  14. notice, this list of conditions and the following disclaimer.
  15. - Redistributions in binary form must reproduce the above copyright
  16. notice, this list of conditions and the following disclaimer in the
  17. documentation and/or other materials provided with the distribution.
  18. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  22. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  26. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef ARCH_H
  31. #define ARCH_H
  32. #include "opus_types.h"
  33. #include "opus_defines.h"
  34. # if !defined(__GNUC_PREREQ)
  35. # if defined(__GNUC__)&&defined(__GNUC_MINOR__)
  36. # define __GNUC_PREREQ(_maj,_min) \
  37. ((__GNUC__<<16)+__GNUC_MINOR__>=((_maj)<<16)+(_min))
  38. # else
  39. # define __GNUC_PREREQ(_maj,_min) 0
  40. # endif
  41. # endif
  42. #define CELT_SIG_SCALE 32768.f
  43. #define celt_fatal(str) _celt_fatal(str, __FILE__, __LINE__);
  44. #ifdef ENABLE_ASSERTIONS
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47. #ifdef __GNUC__
  48. __attribute__((noreturn))
  49. #endif
  50. static OPUS_INLINE void _celt_fatal(const char *str, const char *file, int line)
  51. {
  52. fprintf (stderr, "Fatal (internal) error in %s, line %d: %s\n", file, line, str);
  53. abort();
  54. }
  55. #define celt_assert(cond) {if (!(cond)) {celt_fatal("assertion failed: " #cond);}}
  56. #define celt_assert2(cond, message) {if (!(cond)) {celt_fatal("assertion failed: " #cond "\n" message);}}
  57. #else
  58. #define celt_assert(cond)
  59. #define celt_assert2(cond, message)
  60. #endif
  61. #define IMUL32(a,b) ((a)*(b))
  62. #define MIN16(a,b) ((a) < (b) ? (a) : (b)) /**< Minimum 16-bit value. */
  63. #define MAX16(a,b) ((a) > (b) ? (a) : (b)) /**< Maximum 16-bit value. */
  64. #define MIN32(a,b) ((a) < (b) ? (a) : (b)) /**< Minimum 32-bit value. */
  65. #define MAX32(a,b) ((a) > (b) ? (a) : (b)) /**< Maximum 32-bit value. */
  66. #define IMIN(a,b) ((a) < (b) ? (a) : (b)) /**< Minimum int value. */
  67. #define IMAX(a,b) ((a) > (b) ? (a) : (b)) /**< Maximum int value. */
  68. #define UADD32(a,b) ((a)+(b))
  69. #define USUB32(a,b) ((a)-(b))
  70. /* Set this if opus_int64 is a native type of the CPU. */
  71. /* Assume that all LP64 architectures have fast 64-bit types; also x86_64
  72. (which can be ILP32 for x32) and Win64 (which is LLP64). */
  73. #if defined(__x86_64__) || defined(__LP64__) || defined(_WIN64)
  74. #define OPUS_FAST_INT64 1
  75. #else
  76. #define OPUS_FAST_INT64 0
  77. #endif
  78. #define PRINT_MIPS(file)
  79. #ifdef FIXED_POINT
  80. typedef opus_int16 opus_val16;
  81. typedef opus_int32 opus_val32;
  82. typedef opus_val32 celt_sig;
  83. typedef opus_val16 celt_norm;
  84. typedef opus_val32 celt_ener;
  85. #define Q15ONE 32767
  86. #define SIG_SHIFT 12
  87. #define NORM_SCALING 16384
  88. #define DB_SHIFT 10
  89. #define EPSILON 1
  90. #define VERY_SMALL 0
  91. #define VERY_LARGE16 ((opus_val16)32767)
  92. #define Q15_ONE ((opus_val16)32767)
  93. #define SCALEIN(a) (a)
  94. #define SCALEOUT(a) (a)
  95. #define ABS16(x) ((x) < 0 ? (-(x)) : (x))
  96. #define ABS32(x) ((x) < 0 ? (-(x)) : (x))
  97. static OPUS_INLINE opus_int16 SAT16(opus_int32 x) {
  98. return x > 32767 ? 32767 : x < -32768 ? -32768 : (opus_int16)x;
  99. }
  100. #ifdef FIXED_DEBUG
  101. #include "fixed_debug.h"
  102. #else
  103. #include "fixed_generic.h"
  104. #ifdef OPUS_ARM_PRESUME_AARCH64_NEON_INTR
  105. #include "arm/fixed_arm64.h"
  106. #elif OPUS_ARM_INLINE_EDSP
  107. #include "arm/fixed_armv5e.h"
  108. #elif defined (OPUS_ARM_INLINE_ASM)
  109. #include "arm/fixed_armv4.h"
  110. #elif defined (BFIN_ASM)
  111. #include "fixed_bfin.h"
  112. #elif defined (TI_C5X_ASM)
  113. #include "fixed_c5x.h"
  114. #elif defined (TI_C6X_ASM)
  115. #include "fixed_c6x.h"
  116. #endif
  117. #endif
  118. #else /* FIXED_POINT */
  119. typedef float opus_val16;
  120. typedef float opus_val32;
  121. typedef float celt_sig;
  122. typedef float celt_norm;
  123. typedef float celt_ener;
  124. #ifdef FLOAT_APPROX
  125. /* This code should reliably detect NaN/inf even when -ffast-math is used.
  126. Assumes IEEE 754 format. */
  127. static OPUS_INLINE int celt_isnan(float x)
  128. {
  129. union {float f; opus_uint32 i;} in;
  130. in.f = x;
  131. return ((in.i>>23)&0xFF)==0xFF && (in.i&0x007FFFFF)!=0;
  132. }
  133. #else
  134. #ifdef __FAST_MATH__
  135. #error Cannot build libopus with -ffast-math unless FLOAT_APPROX is defined. This could result in crashes on extreme (e.g. NaN) input
  136. #endif
  137. #define celt_isnan(x) ((x)!=(x))
  138. #endif
  139. #define Q15ONE 1.0f
  140. #define NORM_SCALING 1.f
  141. #define EPSILON 1e-15f
  142. #define VERY_SMALL 1e-30f
  143. #define VERY_LARGE16 1e15f
  144. #define Q15_ONE ((opus_val16)1.f)
  145. /* This appears to be the same speed as C99's fabsf() but it's more portable. */
  146. #define ABS16(x) ((float)fabs(x))
  147. #define ABS32(x) ((float)fabs(x))
  148. #define QCONST16(x,bits) (x)
  149. #define QCONST32(x,bits) (x)
  150. #define NEG16(x) (-(x))
  151. #define NEG32(x) (-(x))
  152. #define EXTRACT16(x) (x)
  153. #define EXTEND32(x) (x)
  154. #define SHR16(a,shift) (a)
  155. #define SHL16(a,shift) (a)
  156. #define SHR32(a,shift) (a)
  157. #define SHL32(a,shift) (a)
  158. #define PSHR32(a,shift) (a)
  159. #define VSHR32(a,shift) (a)
  160. #define PSHR(a,shift) (a)
  161. #define SHR(a,shift) (a)
  162. #define SHL(a,shift) (a)
  163. #define SATURATE(x,a) (x)
  164. #define SATURATE16(x) (x)
  165. #define ROUND16(a,shift) (a)
  166. #define HALF16(x) (.5f*(x))
  167. #define HALF32(x) (.5f*(x))
  168. #define ADD16(a,b) ((a)+(b))
  169. #define SUB16(a,b) ((a)-(b))
  170. #define ADD32(a,b) ((a)+(b))
  171. #define SUB32(a,b) ((a)-(b))
  172. #define MULT16_16_16(a,b) ((a)*(b))
  173. #define MULT16_16(a,b) ((opus_val32)(a)*(opus_val32)(b))
  174. #define MAC16_16(c,a,b) ((c)+(opus_val32)(a)*(opus_val32)(b))
  175. #define MULT16_32_Q15(a,b) ((a)*(b))
  176. #define MULT16_32_Q16(a,b) ((a)*(b))
  177. #define MULT32_32_Q31(a,b) ((a)*(b))
  178. #define MAC16_32_Q15(c,a,b) ((c)+(a)*(b))
  179. #define MAC16_32_Q16(c,a,b) ((c)+(a)*(b))
  180. #define MULT16_16_Q11_32(a,b) ((a)*(b))
  181. #define MULT16_16_Q11(a,b) ((a)*(b))
  182. #define MULT16_16_Q13(a,b) ((a)*(b))
  183. #define MULT16_16_Q14(a,b) ((a)*(b))
  184. #define MULT16_16_Q15(a,b) ((a)*(b))
  185. #define MULT16_16_P15(a,b) ((a)*(b))
  186. #define MULT16_16_P13(a,b) ((a)*(b))
  187. #define MULT16_16_P14(a,b) ((a)*(b))
  188. #define MULT16_32_P16(a,b) ((a)*(b))
  189. #define DIV32_16(a,b) (((opus_val32)(a))/(opus_val16)(b))
  190. #define DIV32(a,b) (((opus_val32)(a))/(opus_val32)(b))
  191. #define SCALEIN(a) ((a)*CELT_SIG_SCALE)
  192. #define SCALEOUT(a) ((a)*(1/CELT_SIG_SCALE))
  193. #define SIG2WORD16(x) (x)
  194. #endif /* !FIXED_POINT */
  195. #ifndef GLOBAL_STACK_SIZE
  196. #ifdef FIXED_POINT
  197. #define GLOBAL_STACK_SIZE 100000
  198. #else
  199. #define GLOBAL_STACK_SIZE 100000
  200. #endif
  201. #endif
  202. #endif /* ARCH_H */