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.
7
/* $Header: /tmp_amd/presto/export/kbs/jutta/src/gsm/RCS/add.c,v 1.6 1996/07/02 09:57:33 jutta Exp $ */
10
* See private.h for the more commonly used macro versions.
22
((x) < MIN_WORD ? MIN_WORD : (x) > MAX_WORD ? MAX_WORD: (x))
24
word gsm_add P2((a,b), word a, word b)
26
longword sum = (longword)a + (longword)b;
30
word gsm_sub P2((a,b), word a, word b)
32
longword diff = (longword)a - (longword)b;
33
return saturate(diff);
36
word gsm_mult P2((a,b), word a, word b)
38
if (a == MIN_WORD && b == MIN_WORD) return MAX_WORD;
39
else return SASR( (longword)a * (longword)b, 15 );
42
word gsm_mult_r P2((a,b), word a, word b)
44
if (b == MIN_WORD && a == MIN_WORD) return MAX_WORD;
46
longword prod = (longword)a * (longword)b + 16384;
52
word gsm_abs P1((a), word a)
54
return a < 0 ? (a == MIN_WORD ? MAX_WORD : -a) : a;
57
longword gsm_L_mult P2((a,b),word a, word b)
59
assert( a != MIN_WORD || b != MIN_WORD );
60
return ((longword)a * (longword)b) << 1;
63
longword gsm_L_add P2((a,b), longword a, longword b)
66
if (b >= 0) return a + b;
68
ulongword A = (ulongword)-(a + 1) + (ulongword)-(b + 1);
69
return A >= MAX_LONGWORD ? MIN_LONGWORD :-(longword)A-2;
72
else if (b <= 0) return a + b;
74
ulongword A = (ulongword)a + (ulongword)b;
75
return A > MAX_LONGWORD ? MAX_LONGWORD : A;
79
longword gsm_L_sub P2((a,b), longword a, longword b)
82
if (b >= 0) return a - b;
86
ulongword A = (ulongword)a + -(b + 1);
87
return A >= MAX_LONGWORD ? MAX_LONGWORD : (A + 1);
90
else if (b <= 0) return a - b;
94
ulongword A = (ulongword)-(a + 1) + b;
95
return A >= MAX_LONGWORD ? MIN_LONGWORD : -(longword)A - 1;
99
static unsigned char const bitoff[ 256 ] = {
100
8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
101
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
102
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
103
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
104
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
105
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
106
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
107
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
108
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
109
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
110
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
111
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
112
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
113
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
114
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
115
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
118
word gsm_norm P1((a), longword a )
120
* the number of left shifts needed to normalize the 32 bit
121
* variable L_var1 for positive values on the interval
124
* minimum of 1073741824 (01000000000000000000000000000000) and
125
* maximum of 2147483647 (01111111111111111111111111111111)
128
* and for negative values on the interval with
129
* minimum of -2147483648 (-10000000000000000000000000000000) and
130
* maximum of -1073741824 ( -1000000000000000000000000000000).
132
* in order to normalize the result, the following
133
* operation must be done: L_norm_var1 = L_var1 << norm( L_var1 );
135
* (That's 'ffs', only from the left, not the right..)
141
if (a <= -1073741824) return 0;
145
return a & 0xffff0000
147
? -1 + bitoff[ 0xFF & (a >> 24) ]
148
: 7 + bitoff[ 0xFF & (a >> 16) ] )
150
? 15 + bitoff[ 0xFF & (a >> 8) ]
151
: 23 + bitoff[ 0xFF & a ] );
154
longword gsm_L_asl P2((a,n), longword a, int n)
156
if (n >= 32) return 0;
157
if (n <= -32) return -(a < 0);
158
if (n < 0) return gsm_L_asr(a, -n);
162
word gsm_asl P2((a,n), word a, int n)
164
if (n >= 16) return 0;
165
if (n <= -16) return -(a < 0);
166
if (n < 0) return gsm_asr(a, -n);
170
longword gsm_L_asr P2((a,n), longword a, int n)
172
if (n >= 32) return -(a < 0);
173
if (n <= -32) return 0;
174
if (n < 0) return a << -n;
179
if (a >= 0) return a >> n;
180
else return -(longword)( -(ulongword)a >> n );
184
word gsm_asr P2((a,n), word a, int n)
186
if (n >= 16) return -(a < 0);
187
if (n <= -16) return 0;
188
if (n < 0) return a << -n;
193
if (a >= 0) return a >> n;
194
else return -(word)( -(uword)a >> n );
199
* (From p. 46, end of section 4.2.5)
201
* NOTE: The following lines gives [sic] one correct implementation
202
* of the div(num, denum) arithmetic operation. Compute div
203
* which is the integer division of num by denum: with denum
207
word gsm_div P2((num,denum), word num, word denum)
209
longword L_num = num;
210
longword L_denum = denum;
214
/* The parameter num sometimes becomes zero.
215
* Although this is explicitly guarded against in 4.2.5,
216
* we assume that the result should then be zero as well.
219
/* assert(num != 0); */
221
assert(num >= 0 && denum >= num);
229
if (L_num >= L_denum) {