arch.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. #if OPUS_GNUC_PREREQ(3, 0)
  43. #define opus_likely(x) (__builtin_expect(!!(x), 1))
  44. #define opus_unlikely(x) (__builtin_expect(!!(x), 0))
  45. #else
  46. #define opus_likely(x) (!!(x))
  47. #define opus_unlikely(x) (!!(x))
  48. #endif
  49. #define CELT_SIG_SCALE 32768.f
  50. #define CELT_FATAL(str) celt_fatal(str, __FILE__, __LINE__);
  51. #if defined(ENABLE_ASSERTIONS) || defined(ENABLE_HARDENING)
  52. #ifdef __GNUC__
  53. __attribute__((noreturn))
  54. #endif
  55. void celt_fatal(const char *str, const char *file, int line);
  56. #if defined(CELT_C) && !defined(OVERRIDE_celt_fatal)
  57. #include <stdio.h>
  58. #include <stdlib.h>
  59. #ifdef __GNUC__
  60. __attribute__((noreturn))
  61. #endif
  62. void celt_fatal(const char *str, const char *file, int line)
  63. {
  64. fprintf (stderr, "Fatal (internal) error in %s, line %d: %s\n", file, line, str);
  65. abort();
  66. }
  67. #endif
  68. #define celt_assert(cond) {if (!(cond)) {CELT_FATAL("assertion failed: " #cond);}}
  69. #define celt_assert2(cond, message) {if (!(cond)) {CELT_FATAL("assertion failed: " #cond "\n" message);}}
  70. #define MUST_SUCCEED(call) celt_assert((call) == OPUS_OK)
  71. #else
  72. #define celt_assert(cond)
  73. #define celt_assert2(cond, message)
  74. #define MUST_SUCCEED(call) do {if((call) != OPUS_OK) {RESTORE_STACK; return OPUS_INTERNAL_ERROR;} } while (0)
  75. #endif
  76. #if defined(ENABLE_ASSERTIONS)
  77. #define celt_sig_assert(cond) {if (!(cond)) {CELT_FATAL("signal assertion failed: " #cond);}}
  78. #else
  79. #define celt_sig_assert(cond)
  80. #endif
  81. #define IMUL32(a,b) ((a)*(b))
  82. #define MIN16(a,b) ((a) < (b) ? (a) : (b)) /**< Minimum 16-bit value. */
  83. #define MAX16(a,b) ((a) > (b) ? (a) : (b)) /**< Maximum 16-bit value. */
  84. #define MIN32(a,b) ((a) < (b) ? (a) : (b)) /**< Minimum 32-bit value. */
  85. #define MAX32(a,b) ((a) > (b) ? (a) : (b)) /**< Maximum 32-bit value. */
  86. #define IMIN(a,b) ((a) < (b) ? (a) : (b)) /**< Minimum int value. */
  87. #define IMAX(a,b) ((a) > (b) ? (a) : (b)) /**< Maximum int value. */
  88. #define UADD32(a,b) ((a)+(b))
  89. #define USUB32(a,b) ((a)-(b))
  90. /* Set this if opus_int64 is a native type of the CPU. */
  91. /* Assume that all LP64 architectures have fast 64-bit types; also x86_64
  92. (which can be ILP32 for x32) and Win64 (which is LLP64). */
  93. #if defined(__x86_64__) || defined(__LP64__) || defined(_WIN64)
  94. #define OPUS_FAST_INT64 1
  95. #else
  96. #define OPUS_FAST_INT64 0
  97. #endif
  98. #define PRINT_MIPS(file)
  99. #ifdef FIXED_POINT
  100. typedef opus_int16 opus_val16;
  101. typedef opus_int32 opus_val32;
  102. typedef opus_int64 opus_val64;
  103. typedef opus_val32 celt_sig;
  104. typedef opus_val16 celt_norm;
  105. typedef opus_val32 celt_ener;
  106. #define celt_isnan(x) 0
  107. #define Q15ONE 32767
  108. #define SIG_SHIFT 12
  109. /* Safe saturation value for 32-bit signals. Should be less than
  110. 2^31*(1-0.85) to avoid blowing up on DC at deemphasis.*/
  111. #define SIG_SAT (300000000)
  112. #define NORM_SCALING 16384
  113. #define DB_SHIFT 10
  114. #define EPSILON 1
  115. #define VERY_SMALL 0
  116. #define VERY_LARGE16 ((opus_val16)32767)
  117. #define Q15_ONE ((opus_val16)32767)
  118. #define SCALEIN(a) (a)
  119. #define SCALEOUT(a) (a)
  120. #define ABS16(x) ((x) < 0 ? (-(x)) : (x))
  121. #define ABS32(x) ((x) < 0 ? (-(x)) : (x))
  122. static OPUS_INLINE opus_int16 SAT16(opus_int32 x) {
  123. return x > 32767 ? 32767 : x < -32768 ? -32768 : (opus_int16)x;
  124. }
  125. #ifdef FIXED_DEBUG
  126. #include "fixed_debug.h"
  127. #else
  128. #include "fixed_generic.h"
  129. #ifdef OPUS_ARM_PRESUME_AARCH64_NEON_INTR
  130. #include "arm/fixed_arm64.h"
  131. #elif defined (OPUS_ARM_INLINE_EDSP)
  132. #include "arm/fixed_armv5e.h"
  133. #elif defined (OPUS_ARM_INLINE_ASM)
  134. #include "arm/fixed_armv4.h"
  135. #elif defined (BFIN_ASM)
  136. #include "fixed_bfin.h"
  137. #elif defined (TI_C5X_ASM)
  138. #include "fixed_c5x.h"
  139. #elif defined (TI_C6X_ASM)
  140. #include "fixed_c6x.h"
  141. #endif
  142. #endif
  143. #else /* FIXED_POINT */
  144. typedef float opus_val16;
  145. typedef float opus_val32;
  146. typedef float opus_val64;
  147. typedef float celt_sig;
  148. typedef float celt_norm;
  149. typedef float celt_ener;
  150. #ifdef FLOAT_APPROX
  151. /* This code should reliably detect NaN/inf even when -ffast-math is used.
  152. Assumes IEEE 754 format. */
  153. static OPUS_INLINE int celt_isnan(float x)
  154. {
  155. union {float f; opus_uint32 i;} in;
  156. in.f = x;
  157. return ((in.i>>23)&0xFF)==0xFF && (in.i&0x007FFFFF)!=0;
  158. }
  159. #else
  160. #ifdef __FAST_MATH__
  161. #error Cannot build libopus with -ffast-math unless FLOAT_APPROX is defined. This could result in crashes on extreme (e.g. NaN) input
  162. #endif
  163. #define celt_isnan(x) ((x)!=(x))
  164. #endif
  165. #define Q15ONE 1.0f
  166. #define NORM_SCALING 1.f
  167. #define EPSILON 1e-15f
  168. #define VERY_SMALL 1e-30f
  169. #define VERY_LARGE16 1e15f
  170. #define Q15_ONE ((opus_val16)1.f)
  171. /* This appears to be the same speed as C99's fabsf() but it's more portable. */
  172. #define ABS16(x) ((float)fabs(x))
  173. #define ABS32(x) ((float)fabs(x))
  174. #define QCONST16(x,bits) (x)
  175. #define QCONST32(x,bits) (x)
  176. #define NEG16(x) (-(x))
  177. #define NEG32(x) (-(x))
  178. #define NEG32_ovflw(x) (-(x))
  179. #define EXTRACT16(x) (x)
  180. #define EXTEND32(x) (x)
  181. #define SHR16(a,shift) (a)
  182. #define SHL16(a,shift) (a)
  183. #define SHR32(a,shift) (a)
  184. #define SHL32(a,shift) (a)
  185. #define PSHR32(a,shift) (a)
  186. #define VSHR32(a,shift) (a)
  187. #define PSHR(a,shift) (a)
  188. #define SHR(a,shift) (a)
  189. #define SHL(a,shift) (a)
  190. #define SATURATE(x,a) (x)
  191. #define SATURATE16(x) (x)
  192. #define ROUND16(a,shift) (a)
  193. #define SROUND16(a,shift) (a)
  194. #define HALF16(x) (.5f*(x))
  195. #define HALF32(x) (.5f*(x))
  196. #define ADD16(a,b) ((a)+(b))
  197. #define SUB16(a,b) ((a)-(b))
  198. #define ADD32(a,b) ((a)+(b))
  199. #define SUB32(a,b) ((a)-(b))
  200. #define ADD32_ovflw(a,b) ((a)+(b))
  201. #define SUB32_ovflw(a,b) ((a)-(b))
  202. #define MULT16_16_16(a,b) ((a)*(b))
  203. #define MULT16_16(a,b) ((opus_val32)(a)*(opus_val32)(b))
  204. #define MAC16_16(c,a,b) ((c)+(opus_val32)(a)*(opus_val32)(b))
  205. #define MULT16_32_Q15(a,b) ((a)*(b))
  206. #define MULT16_32_Q16(a,b) ((a)*(b))
  207. #define MULT32_32_Q31(a,b) ((a)*(b))
  208. #define MAC16_32_Q15(c,a,b) ((c)+(a)*(b))
  209. #define MAC16_32_Q16(c,a,b) ((c)+(a)*(b))
  210. #define MULT16_16_Q11_32(a,b) ((a)*(b))
  211. #define MULT16_16_Q11(a,b) ((a)*(b))
  212. #define MULT16_16_Q13(a,b) ((a)*(b))
  213. #define MULT16_16_Q14(a,b) ((a)*(b))
  214. #define MULT16_16_Q15(a,b) ((a)*(b))
  215. #define MULT16_16_P15(a,b) ((a)*(b))
  216. #define MULT16_16_P13(a,b) ((a)*(b))
  217. #define MULT16_16_P14(a,b) ((a)*(b))
  218. #define MULT16_32_P16(a,b) ((a)*(b))
  219. #define DIV32_16(a,b) (((opus_val32)(a))/(opus_val16)(b))
  220. #define DIV32(a,b) (((opus_val32)(a))/(opus_val32)(b))
  221. #define SCALEIN(a) ((a)*CELT_SIG_SCALE)
  222. #define SCALEOUT(a) ((a)*(1/CELT_SIG_SCALE))
  223. #define SIG2WORD16(x) (x)
  224. #endif /* !FIXED_POINT */
  225. #ifndef GLOBAL_STACK_SIZE
  226. #ifdef FIXED_POINT
  227. #define GLOBAL_STACK_SIZE 120000
  228. #else
  229. #define GLOBAL_STACK_SIZE 120000
  230. #endif
  231. #endif
  232. #endif /* ARCH_H */