arch.h 8.3 KB

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