gsm.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*****************************************************************************/
  2. /* GSM.H v8.2.2 */
  3. /* */
  4. /* Copyright (c) 1998-2017 Texas Instruments Incorporated */
  5. /* http://www.ti.com/ */
  6. /* */
  7. /* Redistribution and use in source and binary forms, with or without */
  8. /* modification, are permitted provided that the following conditions */
  9. /* are met: */
  10. /* */
  11. /* Redistributions of source code must retain the above copyright */
  12. /* notice, this list of conditions and the following disclaimer. */
  13. /* */
  14. /* Redistributions in binary form must reproduce the above copyright */
  15. /* notice, this list of conditions and the following disclaimer in */
  16. /* the documentation and/or other materials provided with the */
  17. /* distribution. */
  18. /* */
  19. /* Neither the name of Texas Instruments Incorporated nor the names */
  20. /* of its contributors may be used to endorse or promote products */
  21. /* derived from this software without specific prior written */
  22. /* permission. */
  23. /* */
  24. /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
  25. /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
  26. /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR */
  27. /* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
  28. /* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
  29. /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */
  30. /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, */
  31. /* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY */
  32. /* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
  33. /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE */
  34. /* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
  35. /* */
  36. /*****************************************************************************/
  37. #ifndef _GSMHDR
  38. #define _GSMHDR
  39. /*---------------------------------------------------------------------------*/
  40. /* Definitions for the Basic DSP Operations defined by the European */
  41. /* Telecommunications Standards Institute (ETSI). */
  42. /* */
  43. /* Details about these functions may be found in Chapter 13 (BASOP: ITU-T */
  44. /* Basic Operators) of "ITU-T Software Tool Library 2005 User's Manual" */
  45. /* available at //www.itu.int/rec/T-REC-G.191-200508-I/en. */
  46. /*---------------------------------------------------------------------------*/
  47. #include <stdlib.h>
  48. #include <linkage.h>
  49. #define MAX_32 (int)0x7fffffffL
  50. #define MIN_32 (int)0x80000000L
  51. #define MAX_16 (short)0x7fff
  52. #define MIN_16 (short)0x8000
  53. extern _DATA_ACCESS int Overflow;
  54. extern _DATA_ACCESS int Carry;
  55. /******************************************************************************/
  56. /* Macros for GSM ETSI math operations */
  57. /* Note: The shift ETSI operations defined here do not support shift */
  58. /* amounts larger than 31. For instance, if you code calls -- */
  59. /* "L_shl(x, 500);" these macros are not designed to handle a SSHL */
  60. /* by 500 bits. These macros can be modified accordingly for these */
  61. /* situations. */
  62. /******************************************************************************/
  63. #define L_add(a,b) (_sadd((a),(b))) /* int sat addition */
  64. #define L_sub(a,b) (_ssub((a),(b))) /* int sat subtract */
  65. #define L_sub_c(a,b) L_add_c((a),~(b)) /* integer subtraction */
  66. #define L_negate(a) (_ssub(0,(a))) /* integer negation */
  67. #define L_deposit_h(a) ((a)<<16) /* put short in upper 16 */
  68. #define L_deposit_l(a) ((int)(a)) /* put short in lower 16 */
  69. #define L_abs(a) (_abs(a)) /* int absolute value */
  70. #define L_mult(a,b) (_smpy((a),(b))) /* short sat mpy => 32 */
  71. #define L_mac(a,b,c) (_sadd((a),L_mult(b, c))) /* saturated mpy & accum */
  72. #define L_macNs(a,b,c) L_add_c((a),L_mult(b,c)) /* mpy & accum w/o saturat*/
  73. #define L_msu(a,b,c) (_ssub((a),L_mult(b,c))) /* saturated mpy & sub */
  74. #define L_msuNs(a,b,c) L_sub_c(a,L_mult(b,c)) /* mpy & sub w/o saturate */
  75. #define L_shl(a,b) ((b) < 0 ? (a) >> -(b) : _sshl((a),(b)))
  76. #define L_shr(a,b) ((b) < 0 ? _sshl((a),-(b)) : (a) >> (b))
  77. #define L_shr_r(a,b) (L_shr((a),(b)) + ((b)>0 && (((a) & (1<<((b)-1))) != 0)))
  78. #define abs_s(a) (_abs((a)<<16)>>16) /* short absolute value */
  79. #define add(a,b) (_sadd((a)<<16,(b)<<16)>>16) /* short sat add */
  80. #define sub(a,b) (_ssub((a)<<16,(b)<<16)>>16) /* short sat subtract */
  81. #define extract_h(a) ((unsigned)(a)>>16) /* extract upper 16 bits */
  82. #define extract_l(a) ((a)&0xffff) /* extract lower 16 bits */
  83. #define round(a) extract_h(_sadd((a),0x8000)) /* round */
  84. #define mac_r(a,b,c) (round(L_mac(a,b,c))) /* mac w/ rounding */
  85. #define msu_r(a,b,c) (round(L_msu(a,b,c))) /* msu w/ rounding */
  86. #define mult_r(a,b) (round(L_mult(a,b))) /* sat mpy w/ round */
  87. #define mult(a,b) (L_mult(a,b)>>16) /* short sat mpy upper 16 */
  88. #define norm_l(a) ((a) == 0 ? 0 : _norm(a)) /* return NORM of int */
  89. #define norm_s(a) ((a) == 0 ? 0 : _norm(a)-16) /* return NORM of short */
  90. #define negate(a) (_ssub(0, ((a)<<16)) >> 16) /* short sat negate */
  91. #define shl(a,b) ((b) < 0 ? (a) >> -(b) : (_sshl((a),((b)+16))>>16))
  92. #define shr(a,b) ((b) < 0 ? (_sshl((a),(-(b)+16))>>16) : ((a) >> (b)))
  93. #define shr_r(a,b) ((b) < 0 ? (_sshl((a),(-(b)+16))>>16) : (b)==0 ? (a) : \
  94. ((a)+(1<<((b)-1))) >> (b))
  95. /******************************************************************************/
  96. /* For C++ code, place these function names in the C name space. */
  97. /******************************************************************************/
  98. #ifdef __cplusplus
  99. extern "C" {
  100. #endif
  101. _IDECL int L_add_c (int, int);
  102. _IDECL int L_sat (int);
  103. _IDECL short div_s (short, short);
  104. #ifdef _INLINE
  105. /******************************************************************************/
  106. /* Integer (32-bit) add with carry and overflow testing. */
  107. /******************************************************************************/
  108. static inline int L_add_c (int L_var1, int L_var2)
  109. {
  110. unsigned int uv1 = L_var1;
  111. unsigned int uv2 = L_var2;
  112. int cin = Carry;
  113. unsigned int result = uv1 + uv2 + cin;
  114. Carry = ((~result & (uv1 | uv2)) | (uv1 & uv2)) >> 31;
  115. Overflow = ((~(uv1 ^ uv2)) & (uv1 ^ result)) >> 31;
  116. if (cin && result == 0x80000000) Overflow = 1;
  117. return (int) result;
  118. }
  119. /******************************************************************************/
  120. /* Saturate any result after L_add_c or L_sub_c if overflow is set. */
  121. /******************************************************************************/
  122. static inline int L_sat (int L_var1)
  123. {
  124. int cin = Carry;
  125. return !Overflow ? L_var1 : (Carry = Overflow = 0, 0x7fffffff+cin);
  126. }
  127. /******************************************************************************/
  128. /* Short (16-bit) divide. */
  129. /******************************************************************************/
  130. static inline short div_s (short var1, short var2)
  131. {
  132. int iteration;
  133. unsigned int var1int;
  134. int var2int;
  135. if (var1 == 0) return 0;
  136. if (var1 == var2) return 0x7fff;
  137. var1int = var1 << 16;
  138. var2int = var2 << 16;
  139. for (iteration = 0; iteration < 16; iteration++)
  140. var1int = _subc(var1int,var2int);
  141. return var1int & 0xffff;
  142. }
  143. #endif /* _INLINE */
  144. #ifdef __cplusplus
  145. } /* extern "C" */
  146. #endif /* __cplusplus */
  147. #endif /* !_GSMHDR */