add.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische
  3. * Universitaet Berlin. See the accompanying file "COPYRIGHT" for
  4. * details. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
  5. */
  6. /* $Header: /tmp_amd/presto/export/kbs/jutta/src/gsm/RCS/add.c,v 1.6 1996/07/02 09:57:33 jutta Exp $ */
  7. /*
  8. * See private.h for the more commonly used macro versions.
  9. */
  10. #include "config.h"
  11. #include <stdio.h>
  12. #include <assert.h>
  13. #include "private.h"
  14. #include "gsm.h"
  15. #include "proto.h"
  16. #define saturate(x) \
  17. ((x) < MIN_WORD ? MIN_WORD : (x) > MAX_WORD ? MAX_WORD: (x))
  18. word gsm_add P2((a,b), word a, word b)
  19. {
  20. longword sum = (longword)a + (longword)b;
  21. return saturate(sum);
  22. }
  23. word gsm_sub P2((a,b), word a, word b)
  24. {
  25. longword diff = (longword)a - (longword)b;
  26. return saturate(diff);
  27. }
  28. word gsm_mult P2((a,b), word a, word b)
  29. {
  30. if (a == MIN_WORD && b == MIN_WORD) return MAX_WORD;
  31. else return SASR( (longword)a * (longword)b, 15 );
  32. }
  33. word gsm_mult_r P2((a,b), word a, word b)
  34. {
  35. if (b == MIN_WORD && a == MIN_WORD) return MAX_WORD;
  36. else {
  37. longword prod = (longword)a * (longword)b + 16384;
  38. prod >>= 15;
  39. return prod & 0xFFFF;
  40. }
  41. }
  42. word gsm_abs P1((a), word a)
  43. {
  44. return a < 0 ? (a == MIN_WORD ? MAX_WORD : -a) : a;
  45. }
  46. longword gsm_L_mult P2((a,b),word a, word b)
  47. {
  48. assert( a != MIN_WORD || b != MIN_WORD );
  49. return ((longword)a * (longword)b) << 1;
  50. }
  51. longword gsm_L_add P2((a,b), longword a, longword b)
  52. {
  53. if (a < 0) {
  54. if (b >= 0) return a + b;
  55. else {
  56. ulongword A = (ulongword)-(a + 1) + (ulongword)-(b + 1);
  57. return A >= MAX_LONGWORD ? MIN_LONGWORD :-(longword)A-2;
  58. }
  59. }
  60. else if (b <= 0) return a + b;
  61. else {
  62. ulongword A = (ulongword)a + (ulongword)b;
  63. return A > MAX_LONGWORD ? MAX_LONGWORD : A;
  64. }
  65. }
  66. longword gsm_L_sub P2((a,b), longword a, longword b)
  67. {
  68. if (a >= 0) {
  69. if (b >= 0) return a - b;
  70. else {
  71. /* a>=0, b<0 */
  72. ulongword A = (ulongword)a + -(b + 1);
  73. return A >= MAX_LONGWORD ? MAX_LONGWORD : (A + 1);
  74. }
  75. }
  76. else if (b <= 0) return a - b;
  77. else {
  78. /* a<0, b>0 */
  79. ulongword A = (ulongword)-(a + 1) + b;
  80. return A >= MAX_LONGWORD ? MIN_LONGWORD : -(longword)A - 1;
  81. }
  82. }
  83. static unsigned char const bitoff[ 256 ] = {
  84. 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
  85. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  86. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  87. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  88. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  89. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  90. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  91. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  92. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  93. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  94. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  95. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  96. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  97. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  98. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  99. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  100. };
  101. word gsm_norm P1((a), longword a )
  102. /*
  103. * the number of left shifts needed to normalize the 32 bit
  104. * variable L_var1 for positive values on the interval
  105. *
  106. * with minimum of
  107. * minimum of 1073741824 (01000000000000000000000000000000) and
  108. * maximum of 2147483647 (01111111111111111111111111111111)
  109. *
  110. *
  111. * and for negative values on the interval with
  112. * minimum of -2147483648 (-10000000000000000000000000000000) and
  113. * maximum of -1073741824 ( -1000000000000000000000000000000).
  114. *
  115. * in order to normalize the result, the following
  116. * operation must be done: L_norm_var1 = L_var1 << norm( L_var1 );
  117. *
  118. * (That's 'ffs', only from the left, not the right..)
  119. */
  120. {
  121. assert(a != 0);
  122. if (a < 0) {
  123. if (a <= -1073741824) return 0;
  124. a = ~a;
  125. }
  126. return a & 0xffff0000
  127. ? ( a & 0xff000000
  128. ? -1 + bitoff[ 0xFF & (a >> 24) ]
  129. : 7 + bitoff[ 0xFF & (a >> 16) ] )
  130. : ( a & 0xff00
  131. ? 15 + bitoff[ 0xFF & (a >> 8) ]
  132. : 23 + bitoff[ 0xFF & a ] );
  133. }
  134. longword gsm_L_asl P2((a,n), longword a, int n)
  135. {
  136. if (n >= 32) return 0;
  137. if (n <= -32) return -(a < 0);
  138. if (n < 0) return gsm_L_asr(a, -n);
  139. return a << n;
  140. }
  141. word gsm_asl P2((a,n), word a, int n)
  142. {
  143. if (n >= 16) return 0;
  144. if (n <= -16) return -(a < 0);
  145. if (n < 0) return gsm_asr(a, -n);
  146. return a << n;
  147. }
  148. longword gsm_L_asr P2((a,n), longword a, int n)
  149. {
  150. if (n >= 32) return -(a < 0);
  151. if (n <= -32) return 0;
  152. if (n < 0) return a << -n;
  153. # ifdef SASR
  154. return a >> n;
  155. # else
  156. if (a >= 0) return a >> n;
  157. else return -(longword)( -(ulongword)a >> n );
  158. # endif
  159. }
  160. word gsm_asr P2((a,n), word a, int n)
  161. {
  162. if (n >= 16) return -(a < 0);
  163. if (n <= -16) return 0;
  164. if (n < 0) return a << -n;
  165. # ifdef SASR
  166. return a >> n;
  167. # else
  168. if (a >= 0) return a >> n;
  169. else return -(word)( -(uword)a >> n );
  170. # endif
  171. }
  172. /*
  173. * (From p. 46, end of section 4.2.5)
  174. *
  175. * NOTE: The following lines gives [sic] one correct implementation
  176. * of the div(num, denum) arithmetic operation. Compute div
  177. * which is the integer division of num by denum: with denum
  178. * >= num > 0
  179. */
  180. word gsm_div P2((num,denum), word num, word denum)
  181. {
  182. longword L_num = num;
  183. longword L_denum = denum;
  184. word div = 0;
  185. int k = 15;
  186. /* The parameter num sometimes becomes zero.
  187. * Although this is explicitly guarded against in 4.2.5,
  188. * we assume that the result should then be zero as well.
  189. */
  190. /* assert(num != 0); */
  191. assert(num >= 0 && denum >= num);
  192. if (num == 0)
  193. return 0;
  194. while (k--) {
  195. div <<= 1;
  196. L_num <<= 1;
  197. if (L_num >= L_denum) {
  198. L_num -= L_denum;
  199. div++;
  200. }
  201. }
  202. return div;
  203. }