~guilhem-fr/asterisk/asterisk-g72x

« back to all changes in this revision

Viewing changes to itu/g.723.1a/basop.c

  • Committer: Guilhem Lettron
  • Date: 2013-10-17 15:00:29 UTC
  • Revision ID: guilhem+ubuntu@lettron.fr-20131017150029-oxub7dt9hreocdej
Import from http://asterisk.hosting.lv/src/asterisk-g72x-1.1.tar.bz2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/*___________________________________________________________________________
 
3
 |                                                                           |
 
4
 | Basics operators.                                                         |
 
5
 |___________________________________________________________________________|
 
6
*/
 
7
 
 
8
/*___________________________________________________________________________
 
9
 |                                                                           |
 
10
 |   Include-Files                                                           |
 
11
 |___________________________________________________________________________|
 
12
*/
 
13
 
 
14
#include <stdio.h>
 
15
#include <stdlib.h>
 
16
#include "typedef.h"
 
17
#include "basop.h"
 
18
 
 
19
/*___________________________________________________________________________
 
20
 |                                                                           |
 
21
 |   Local Functions                                                         |
 
22
 |___________________________________________________________________________|
 
23
*/
 
24
Word16 sature(Word32 L_var1);
 
25
 
 
26
 
 
27
/*___________________________________________________________________________
 
28
 |                                                                           |
 
29
 |   Constants and Globals                                                   |
 
30
 |___________________________________________________________________________|
 
31
*/
 
32
__thread Flag Overflow =0;
 
33
__thread Flag Carry =0;
 
34
 
 
35
 
 
36
/*___________________________________________________________________________
 
37
 |                                                                           |
 
38
 |   Functions                                                               |
 
39
 |___________________________________________________________________________|
 
40
*/
 
41
 
 
42
 
 
43
/*___________________________________________________________________________
 
44
 |                                                                           |
 
45
 |   Function Name : sature                                                  |
 
46
 |                                                                           |
 
47
 |   Purpose :                                                               |
 
48
 |                                                                           |
 
49
 |    Limit the 32 bit input to the range of a 16 bit word.                  |
 
50
 |                                                                           |
 
51
 |   Inputs :                                                                |
 
52
 |                                                                           |
 
53
 |    L_var1                                                                 |
 
54
 |             32 bit long signed integer (Word32) whose value falls in the  |
 
55
 |             range : 0x8000 0000 <= L_var1 <= 0x7fff ffff.                 |
 
56
 |                                                                           |
 
57
 |   Outputs :                                                               |
 
58
 |                                                                           |
 
59
 |    none                                                                   |
 
60
 |                                                                           |
 
61
 |   Return Value :                                                          |
 
62
 |                                                                           |
 
63
 |    var_out                                                                |
 
64
 |             16 bit short signed integer (Word16) whose value falls in the |
 
65
 |             range : 0xffff 8000 <= var_out <= 0x0000 7fff.                |
 
66
 |___________________________________________________________________________|
 
67
*/
 
68
 
 
69
Word16 sature(Word32 L_var1)
 
70
{
 
71
    Word16 var_out;
 
72
 
 
73
    if (L_var1 > 0X00007fffL) {
 
74
        Overflow = 1;
 
75
        var_out = MAX_16;
 
76
    }
 
77
    else {
 
78
        if (L_var1 < (Word32)0xffff8000L) {
 
79
            Overflow = 1;
 
80
            var_out = MIN_16;
 
81
        }
 
82
        else {
 
83
            Overflow = 0;
 
84
            var_out = extract_l(L_var1);
 
85
        }
 
86
    }
 
87
    return(var_out);
 
88
}
 
89
 
 
90
/*___________________________________________________________________________
 
91
 |                                                                           |
 
92
 |   Function Name : add                                                     |
 
93
 |                                                                           |
 
94
 |   Purpose :                                                               |
 
95
 |                                                                           |
 
96
 |    Performs the addition (var1+var2) with overflow control and saturation;|
 
97
 |    the 16 bit result is set at +32767 when overflow occurs or at -32768   |
 
98
 |    when underflow occurs.                                                 |
 
99
 |                                                                           |
 
100
 |   Complexity weight : 1                                                   |
 
101
 |                                                                           |
 
102
 |   Inputs :                                                                |
 
103
 |                                                                           |
 
104
 |    var1                                                                   |
 
105
 |             16 bit short signed integer (Word16) whose value falls in the |
 
106
 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
 
107
 |                                                                           |
 
108
 |    var2                                                                   |
 
109
 |             16 bit short signed integer (Word16) whose value falls in the |
 
110
 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
 
111
 |                                                                           |
 
112
 |   Outputs :                                                               |
 
113
 |                                                                           |
 
114
 |    none                                                                   |
 
115
 |                                                                           |
 
116
 |   Return Value :                                                          |
 
117
 |                                                                           |
 
118
 |    var_out                                                                |
 
119
 |             16 bit short signed integer (Word16) whose value falls in the |
 
120
 |             range : 0xffff 8000 <= var_out <= 0x0000 7fff.                |
 
121
 |___________________________________________________________________________|
 
122
*/
 
123
 
 
124
Word16 add(Word16 var1,Word16 var2)
 
125
{
 
126
    Word16 var_out;
 
127
    Word32 L_somme;
 
128
 
 
129
    L_somme = (Word32) var1 + (Word32) var2;
 
130
    var_out = sature(L_somme);
 
131
    return(var_out);
 
132
}
 
133
 
 
134
/*___________________________________________________________________________
 
135
 |                                                                           |
 
136
 |   Function Name : sub                                                     |
 
137
 |                                                                           |
 
138
 |   Purpose :                                                               |
 
139
 |                                                                           |
 
140
 |    Performs the subtraction (var1+var2) with overflow control and satu-   |
 
141
 |    ration; the 16 bit result is set at +32767 when overflow occurs or at  |
 
142
 |    -32768 when underflow occurs.                                          |
 
143
 |                                                                           |
 
144
 |   Complexity weight : 1                                                   |
 
145
 |                                                                           |
 
146
 |   Inputs :                                                                |
 
147
 |                                                                           |
 
148
 |    var1                                                                   |
 
149
 |             16 bit short signed integer (Word16) whose value falls in the |
 
150
 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
 
151
 |                                                                           |
 
152
 |    var2                                                                   |
 
153
 |             16 bit short signed integer (Word16) whose value falls in the |
 
154
 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
 
155
 |                                                                           |
 
156
 |   Outputs :                                                               |
 
157
 |                                                                           |
 
158
 |    none                                                                   |
 
159
 |                                                                           |
 
160
 |   Return Value :                                                          |
 
161
 |                                                                           |
 
162
 |    var_out                                                                |
 
163
 |             16 bit short signed integer (Word16) whose value falls in the |
 
164
 |             range : 0xffff 8000 <= var_out <= 0x0000 7fff.                |
 
165
 |___________________________________________________________________________|
 
166
*/
 
167
 
 
168
Word16 sub(Word16 var1,Word16 var2)
 
169
{
 
170
    Word16 var_out;
 
171
    Word32 L_diff;
 
172
 
 
173
    L_diff = (Word32) var1 -  (Word32) var2;
 
174
    var_out = sature(L_diff);
 
175
    return(var_out);
 
176
}
 
177
 
 
178
/*___________________________________________________________________________
 
179
 |                                                                           |
 
180
 |   Function Name : abs_s                                                   |
 
181
 |                                                                           |
 
182
 |   Purpose :                                                               |
 
183
 |                                                                           |
 
184
 |    Absolute value of var1; abs_s(-32768) = 32767.                         |
 
185
 |                                                                           |
 
186
 |   Complexity weight : 1                                                   |
 
187
 |                                                                           |
 
188
 |   Inputs :                                                                |
 
189
 |                                                                           |
 
190
 |    var1                                                                   |
 
191
 |             16 bit short signed integer (Word16) whose value falls in the |
 
192
 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
 
193
 |                                                                           |
 
194
 |   Outputs :                                                               |
 
195
 |                                                                           |
 
196
 |    none                                                                   |
 
197
 |                                                                           |
 
198
 |   Return Value :                                                          |
 
199
 |                                                                           |
 
200
 |    var_out                                                                |
 
201
 |             16 bit short signed integer (Word16) whose value falls in the |
 
202
 |             range : 0x0000 0000 <= var_out <= 0x0000 7fff.                |
 
203
 |___________________________________________________________________________|
 
204
*/
 
205
 
 
206
Word16 abs_s(Word16 var1)
 
207
{
 
208
    Word16 var_out;
 
209
 
 
210
    if (var1 == (Word16)0X8000 ) {
 
211
        var_out = MAX_16;
 
212
    }
 
213
    else {
 
214
        if (var1 < 0) {
 
215
            var_out = -var1;
 
216
        }
 
217
        else {
 
218
            var_out = var1;
 
219
        }
 
220
    }
 
221
 
 
222
    return(var_out);
 
223
}
 
224
 
 
225
/*___________________________________________________________________________
 
226
 |                                                                           |
 
227
 |   Function Name : shl                                                     |
 
228
 |                                                                           |
 
229
 |   Purpose :                                                               |
 
230
 |                                                                           |
 
231
 |   Arithmetically shift the 16 bit input var1 left var2 positions.Zero fill|
 
232
 |   the var2 LSB of the result. If var2 is negative, arithmetically shift   |
 
233
 |   var1 right by -var2 with sign extension. Saturate the result in case of |
 
234
 |   underflows or overflows.                                                |
 
235
 |                                                                           |
 
236
 |   Complexity weight : 1                                                   |
 
237
 |                                                                           |
 
238
 |   Inputs :                                                                |
 
239
 |                                                                           |
 
240
 |    var1                                                                   |
 
241
 |             16 bit short signed integer (Word16) whose value falls in the |
 
242
 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
 
243
 |                                                                           |
 
244
 |    var2                                                                   |
 
245
 |             16 bit short signed integer (Word16) whose value falls in the |
 
246
 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
 
247
 |                                                                           |
 
248
 |   Outputs :                                                               |
 
249
 |                                                                           |
 
250
 |    none                                                                   |
 
251
 |                                                                           |
 
252
 |   Return Value :                                                          |
 
253
 |                                                                           |
 
254
 |    var_out                                                                |
 
255
 |             16 bit short signed integer (Word16) whose value falls in the |
 
256
 |             range : 0xffff 8000 <= var_out <= 0x0000 7fff.                |
 
257
 |___________________________________________________________________________|
 
258
*/
 
259
 
 
260
Word16 shl(Word16 var1,Word16 var2)
 
261
{
 
262
    Word16 var_out;
 
263
    Word32 L_result;
 
264
 
 
265
    if (var2 < 0) {
 
266
        var_out = shr(var1, (Word16)-var2);
 
267
    }
 
268
    else {
 
269
        L_result = (Word32) var1 * ((Word32) 1 << var2);
 
270
        if ( ((var2 > 15) && (var1 != 0)) ||
 
271
             (L_result != (Word32)((Word16) L_result)) ) {
 
272
            Overflow = 1;
 
273
            var_out = (var1 > 0) ? MAX_16 : MIN_16;
 
274
        }
 
275
        else {
 
276
            var_out = extract_l(L_result);
 
277
        }
 
278
    }
 
279
 
 
280
    return(var_out);
 
281
}
 
282
 
 
283
/*___________________________________________________________________________
 
284
 |                                                                           |
 
285
 |   Function Name : shr                                                     |
 
286
 |                                                                           |
 
287
 |   Purpose :                                                               |
 
288
 |                                                                           |
 
289
 |   Arithmetically shift the 16 bit input var1 right var2 positions with    |
 
290
 |   sign extension. If var2 is negative, arithmetically shift var1 left by  |
 
291
 |   -var2 with sign extension. Saturate the result in case of underflows or |
 
292
 |   overflows.                                                              |
 
293
 |                                                                           |
 
294
 |   Complexity weight : 1                                                   |
 
295
 |                                                                           |
 
296
 |   Inputs :                                                                |
 
297
 |                                                                           |
 
298
 |    var1                                                                   |
 
299
 |             16 bit short signed integer (Word16) whose value falls in the |
 
300
 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
 
301
 |                                                                           |
 
302
 |    var2                                                                   |
 
303
 |             16 bit short signed integer (Word16) whose value falls in the |
 
304
 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
 
305
 |                                                                           |
 
306
 |   Outputs :                                                               |
 
307
 |                                                                           |
 
308
 |    none                                                                   |
 
309
 |                                                                           |
 
310
 |   Return Value :                                                          |
 
311
 |                                                                           |
 
312
 |    var_out                                                                |
 
313
 |             16 bit short signed integer (Word16) whose value falls in the |
 
314
 |             range : 0xffff 8000 <= var_out <= 0x0000 7fff.                |
 
315
 |___________________________________________________________________________|
 
316
*/
 
317
 
 
318
Word16 shr(Word16 var1,Word16 var2)
 
319
{
 
320
    Word16 var_out;
 
321
 
 
322
    if (var2 < 0) {
 
323
        var_out = shl(var1, (Word16)-var2);
 
324
    }
 
325
    else {
 
326
        if (var2 >= 15) {
 
327
            var_out = (var1 < (Word16)0) ? (Word16)-1 : (Word16)0;
 
328
        }
 
329
        else {
 
330
            if (var1 < 0) {
 
331
                var_out = ~(( ~var1) >> var2 );
 
332
            }
 
333
            else {
 
334
                var_out = var1 >> var2;
 
335
            }
 
336
        }
 
337
    }
 
338
 
 
339
    return(var_out);
 
340
}
 
341
 
 
342
/*___________________________________________________________________________
 
343
 |                                                                           |
 
344
 |   Function Name : mult                                                    |
 
345
 |                                                                           |
 
346
 |   Purpose :                                                               |
 
347
 |                                                                           |
 
348
 |    Performs the multiplication of var1 by var2 and gives a 16 bit result  |
 
349
 |    which is scaled i.e.:                                                  |
 
350
 |             mult(var1,var2) = shr((var1 times var2),15) and               |
 
351
 |             mult(-32768,-32768) = 32767.                                  |
 
352
 |                                                                           |
 
353
 |   Complexity weight : 1                                                   |
 
354
 |                                                                           |
 
355
 |   Inputs :                                                                |
 
356
 |                                                                           |
 
357
 |    var1                                                                   |
 
358
 |             16 bit short signed integer (Word16) whose value falls in the |
 
359
 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
 
360
 |                                                                           |
 
361
 |    var2                                                                   |
 
362
 |             16 bit short signed integer (Word16) whose value falls in the |
 
363
 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
 
364
 |                                                                           |
 
365
 |   Outputs :                                                               |
 
366
 |                                                                           |
 
367
 |    none                                                                   |
 
368
 |                                                                           |
 
369
 |   Return Value :                                                          |
 
370
 |                                                                           |
 
371
 |    var_out                                                                |
 
372
 |             16 bit short signed integer (Word16) whose value falls in the |
 
373
 |             range : 0xffff 8000 <= var_out <= 0x0000 7fff.                |
 
374
 |___________________________________________________________________________|
 
375
*/
 
376
 
 
377
Word16 mult(Word16 var1, Word16 var2)
 
378
{
 
379
    Word16 var_out;
 
380
    Word32 L_produit;
 
381
 
 
382
    L_produit = (Word32)var1 * (Word32)var2;
 
383
 
 
384
    L_produit = (L_produit & (Word32) 0xffff8000L) >> 15;
 
385
 
 
386
    if (L_produit & (Word32) 0x00010000L) {
 
387
        L_produit |= (Word32) 0xffff0000L;
 
388
    }
 
389
    var_out = sature(L_produit);
 
390
    return(var_out);
 
391
}
 
392
 
 
393
 
 
394
/*___________________________________________________________________________
 
395
 |                                                                           |
 
396
 |   Function Name : L_mult                                                  |
 
397
 |                                                                           |
 
398
 |   Purpose :                                                               |
 
399
 |                                                                           |
 
400
 |   L_mult is the 32 bit result of the multiplication of var1 times var2    |
 
401
 |   with one shift left i.e.:                                               |
 
402
 |        L_mult(var1,var2) = shl((var1 times var2),1) and                   |
 
403
 |        L_mult(-32768,-32768) = 2147483647.                                |
 
404
 |                                                                           |
 
405
 |   Complexity weight : 1                                                   |
 
406
 |                                                                           |
 
407
 |   Inputs :                                                                |
 
408
 |                                                                           |
 
409
 |    var1                                                                   |
 
410
 |             16 bit short signed integer (Word16) whose value falls in the |
 
411
 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
 
412
 |                                                                           |
 
413
 |    var2                                                                   |
 
414
 |             16 bit short signed integer (Word16) whose value falls in the |
 
415
 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
 
416
 |                                                                           |
 
417
 |   Outputs :                                                               |
 
418
 |                                                                           |
 
419
 |    none                                                                   |
 
420
 |                                                                           |
 
421
 |   Return Value :                                                          |
 
422
 |                                                                           |
 
423
 |    L_var_out                                                              |
 
424
 |             32 bit long signed integer (Word32) whose value falls in the  |
 
425
 |             range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.              |
 
426
 |___________________________________________________________________________|
 
427
*/
 
428
 
 
429
Word32 L_mult(Word16 var1,Word16 var2)
 
430
{
 
431
    Word32 L_var_out;
 
432
 
 
433
    L_var_out = (Word32)var1 * (Word32)var2;
 
434
    if (L_var_out != (Word32)0x40000000L) {
 
435
        L_var_out *= 2L;
 
436
    }
 
437
    else {
 
438
        Overflow = 1;
 
439
        L_var_out = MAX_32;
 
440
    }
 
441
 
 
442
    return(L_var_out);
 
443
}
 
444
 
 
445
 
 
446
/*___________________________________________________________________________
 
447
 |                                                                           |
 
448
 |   Function Name : negate                                                  |
 
449
 |                                                                           |
 
450
 |   Purpose :                                                               |
 
451
 |                                                                           |
 
452
 |   Negate var1 with saturation, saturate in the case where input is -32768:|
 
453
 |                negate(var1) = sub(0,var1).                                |
 
454
 |                                                                           |
 
455
 |   Complexity weight : 1                                                   |
 
456
 |                                                                           |
 
457
 |   Inputs :                                                                |
 
458
 |                                                                           |
 
459
 |    var1                                                                   |
 
460
 |             16 bit short signed integer (Word16) whose value falls in the |
 
461
 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
 
462
 |                                                                           |
 
463
 |   Outputs :                                                               |
 
464
 |                                                                           |
 
465
 |    none                                                                   |
 
466
 |                                                                           |
 
467
 |   Return Value :                                                          |
 
468
 |                                                                           |
 
469
 |    var_out                                                                |
 
470
 |             16 bit short signed integer (Word16) whose value falls in the |
 
471
 |             range : 0xffff 8000 <= var_out <= 0x0000 7fff.                |
 
472
 |___________________________________________________________________________|
 
473
*/
 
474
 
 
475
Word16 negate(Word16 var1)
 
476
{
 
477
    Word16 var_out;
 
478
 
 
479
    var_out = (var1 == MIN_16) ? MAX_16 : -var1;
 
480
    return(var_out);
 
481
}
 
482
 
 
483
 
 
484
/*___________________________________________________________________________
 
485
 |                                                                           |
 
486
 |   Function Name : extract_h                                               |
 
487
 |                                                                           |
 
488
 |   Purpose :                                                               |
 
489
 |                                                                           |
 
490
 |   Return the 16 MSB of L_var1.                                            |
 
491
 |                                                                           |
 
492
 |   Complexity weight : 1                                                   |
 
493
 |                                                                           |
 
494
 |   Inputs :                                                                |
 
495
 |                                                                           |
 
496
 |    L_var1                                                                 |
 
497
 |             32 bit long signed integer (Word32 ) whose value falls in the |
 
498
 |             range : 0x8000 0000 <= L_var1 <= 0x7fff ffff.                 |
 
499
 |                                                                           |
 
500
 |   Outputs :                                                               |
 
501
 |                                                                           |
 
502
 |    none                                                                   |
 
503
 |                                                                           |
 
504
 |   Return Value :                                                          |
 
505
 |                                                                           |
 
506
 |    var_out                                                                |
 
507
 |             16 bit short signed integer (Word16) whose value falls in the |
 
508
 |             range : 0xffff 8000 <= var_out <= 0x0000 7fff.                |
 
509
 |___________________________________________________________________________|
 
510
*/
 
511
 
 
512
Word16 extract_h(Word32 L_var1)
 
513
{
 
514
    Word16 var_out;
 
515
 
 
516
    var_out = (Word16) (L_var1 >> 16);
 
517
    return(var_out);
 
518
}
 
519
 
 
520
/*___________________________________________________________________________
 
521
 |                                                                           |
 
522
 |   Function Name : extract_l                                               |
 
523
 |                                                                           |
 
524
 |   Purpose :                                                               |
 
525
 |                                                                           |
 
526
 |   Return the 16 LSB of L_var1.                                            |
 
527
 |                                                                           |
 
528
 |   Complexity weight : 1                                                   |
 
529
 |                                                                           |
 
530
 |   Inputs :                                                                |
 
531
 |                                                                           |
 
532
 |    L_var1                                                                 |
 
533
 |             32 bit long signed integer (Word32 ) whose value falls in the |
 
534
 |             range : 0x8000 0000 <= L_var1 <= 0x7fff ffff.                 |
 
535
 |                                                                           |
 
536
 |   Outputs :                                                               |
 
537
 |                                                                           |
 
538
 |    none                                                                   |
 
539
 |                                                                           |
 
540
 |   Return Value :                                                          |
 
541
 |                                                                           |
 
542
 |    var_out                                                                |
 
543
 |             16 bit short signed integer (Word16) whose value falls in the |
 
544
 |             range : 0xffff 8000 <= var_out <= 0x0000 7fff.                |
 
545
 |___________________________________________________________________________|
 
546
*/
 
547
 
 
548
Word16 extract_l(Word32 L_var1)
 
549
{
 
550
    Word16 var_out;
 
551
 
 
552
    var_out = (Word16) L_var1;
 
553
    return(var_out);
 
554
}
 
555
 
 
556
 
 
557
/*___________________________________________________________________________
 
558
 |                                                                           |
 
559
 |   Function Name : round                                                   |
 
560
 |                                                                           |
 
561
 |   Purpose :                                                               |
 
562
 |                                                                           |
 
563
 |   Round the lower 16 bits of the 32 bit input number into its MS 16 bits  |
 
564
 |   with saturation. Shift the resulting bits right by 16 and return the 16 |
 
565
 |   bit number:                                                             |
 
566
 |               g723_round(L_var1) = extract_h(L_add(L_var1,32768))              |
 
567
 |                                                                           |
 
568
 |   Complexity weight : 1                                                   |
 
569
 |                                                                           |
 
570
 |   Inputs :                                                                |
 
571
 |                                                                           |
 
572
 |    L_var1                                                                 |
 
573
 |             32 bit long signed integer (Word32 ) whose value falls in the |
 
574
 |             range : 0x8000 0000 <= L_var1 <= 0x7fff ffff.                 |
 
575
 |                                                                           |
 
576
 |   Outputs :                                                               |
 
577
 |                                                                           |
 
578
 |    none                                                                   |
 
579
 |                                                                           |
 
580
 |   Return Value :                                                          |
 
581
 |                                                                           |
 
582
 |    var_out                                                                |
 
583
 |             16 bit short signed integer (Word16) whose value falls in the |
 
584
 |             range : 0xffff 8000 <= var_out <= 0x0000 7fff.                |
 
585
 |___________________________________________________________________________|
 
586
*/
 
587
 
 
588
Word16 g723_round(Word32 L_var1)
 
589
{
 
590
    Word16 var_out;
 
591
    Word32 L_arrondi;
 
592
 
 
593
    L_arrondi = L_add(L_var1, (Word32)0x00008000L);
 
594
    var_out = extract_h(L_arrondi);
 
595
    return(var_out);
 
596
}
 
597
 
 
598
 
 
599
/*___________________________________________________________________________
 
600
 |                                                                           |
 
601
 |   Function Name : L_mac                                                   |
 
602
 |                                                                           |
 
603
 |   Purpose :                                                               |
 
604
 |                                                                           |
 
605
 |   Multiply var1 by var2 and shift the result left by 1. Add the 32 bit    |
 
606
 |   result to L_var3 with saturation, return a 32 bit result:               |
 
607
 |        L_mac(L_var3,var1,var2) = L_add(L_var3,(L_mult(var1,var2)).        |
 
608
 |                                                                           |
 
609
 |   Complexity weight : 1                                                   |
 
610
 |                                                                           |
 
611
 |   Inputs :                                                                |
 
612
 |                                                                           |
 
613
 |    L_var3   32 bit long signed integer (Word32) whose value falls in the  |
 
614
 |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
 
615
 |                                                                           |
 
616
 |    var1                                                                   |
 
617
 |             16 bit short signed integer (Word16) whose value falls in the |
 
618
 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
 
619
 |                                                                           |
 
620
 |    var2                                                                   |
 
621
 |             16 bit short signed integer (Word16) whose value falls in the |
 
622
 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
 
623
 |                                                                           |
 
624
 |   Outputs :                                                               |
 
625
 |                                                                           |
 
626
 |    none                                                                   |
 
627
 |                                                                           |
 
628
 |   Return Value :                                                          |
 
629
 |                                                                           |
 
630
 |    L_var_out                                                              |
 
631
 |             32 bit long signed integer (Word32) whose value falls in the  |
 
632
 |             range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.              |
 
633
 |___________________________________________________________________________|
 
634
*/
 
635
 
 
636
Word32 L_mac(Word32 L_var3, Word16 var1, Word16 var2)
 
637
{
 
638
    Word32 L_var_out;
 
639
    Word32 L_produit;
 
640
 
 
641
    L_produit = L_mult(var1, var2);
 
642
    L_var_out = L_add(L_var3, L_produit);
 
643
    return(L_var_out);
 
644
}
 
645
 
 
646
 
 
647
/*___________________________________________________________________________
 
648
 |                                                                           |
 
649
 |   Function Name : L_msu                                                   |
 
650
 |                                                                           |
 
651
 |   Purpose :                                                               |
 
652
 |                                                                           |
 
653
 |   Multiply var1 by var2 and shift the result left by 1. Subtract the 32   |
 
654
 |   bit result to L_var3 with saturation, return a 32 bit result:           |
 
655
 |        L_msu(L_var3,var1,var2) = L_sub(L_var3,(L_mult(var1,var2)).        |
 
656
 |                                                                           |
 
657
 |   Complexity weight : 1                                                   |
 
658
 |                                                                           |
 
659
 |   Inputs :                                                                |
 
660
 |                                                                           |
 
661
 |    L_var3   32 bit long signed integer (Word32) whose value falls in the  |
 
662
 |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
 
663
 |                                                                           |
 
664
 |    var1                                                                   |
 
665
 |             16 bit short signed integer (Word16) whose value falls in the |
 
666
 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
 
667
 |                                                                           |
 
668
 |    var2                                                                   |
 
669
 |             16 bit short signed integer (Word16) whose value falls in the |
 
670
 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
 
671
 |                                                                           |
 
672
 |   Outputs :                                                               |
 
673
 |                                                                           |
 
674
 |    none                                                                   |
 
675
 |                                                                           |
 
676
 |   Return Value :                                                          |
 
677
 |                                                                           |
 
678
 |    L_var_out                                                              |
 
679
 |             32 bit long signed integer (Word32) whose value falls in the  |
 
680
 |             range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.              |
 
681
 |___________________________________________________________________________|
 
682
*/
 
683
 
 
684
Word32 L_msu(Word32 L_var3, Word16 var1, Word16 var2)
 
685
{
 
686
    Word32 L_var_out;
 
687
    Word32 L_produit;
 
688
 
 
689
    L_produit = L_mult(var1, var2);
 
690
    L_var_out = L_sub(L_var3, L_produit);
 
691
    return(L_var_out);
 
692
}
 
693
 
 
694
 
 
695
/*___________________________________________________________________________
 
696
 |                                                                           |
 
697
 |   Function Name : L_macNs                                                 |
 
698
 |                                                                           |
 
699
 |   Purpose :                                                               |
 
700
 |                                                                           |
 
701
 |   Multiply var1 by var2 and shift the result left by 1. Add the 32 bit    |
 
702
 |   result to L_var3 without saturation, return a 32 bit result. Generate   |
 
703
 |   carry and overflow values :                                             |
 
704
 |        L_macNs(L_var3,var1,var2) = L_add_c(L_var3,(L_mult(var1,var2)).    |
 
705
 |                                                                           |
 
706
 |   Complexity weight : 1                                                   |
 
707
 |                                                                           |
 
708
 |   Inputs :                                                                |
 
709
 |                                                                           |
 
710
 |    L_var3   32 bit long signed integer (Word32) whose value falls in the  |
 
711
 |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
 
712
 |                                                                           |
 
713
 |    var1                                                                   |
 
714
 |             16 bit short signed integer (Word16) whose value falls in the |
 
715
 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
 
716
 |                                                                           |
 
717
 |    var2                                                                   |
 
718
 |             16 bit short signed integer (Word16) whose value falls in the |
 
719
 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
 
720
 |                                                                           |
 
721
 |   Outputs :                                                               |
 
722
 |                                                                           |
 
723
 |    none                                                                   |
 
724
 |                                                                           |
 
725
 |   Return Value :                                                          |
 
726
 |                                                                           |
 
727
 |    L_var_out                                                              |
 
728
 |             32 bit long signed integer (Word32) whose value falls in the  |
 
729
 |             range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.              |
 
730
 |                                                                           |
 
731
 |   Caution :                                                               |
 
732
 |                                                                           |
 
733
 |    In some cases the Carry flag has to be cleared or set before using op- |
 
734
 |    rators which take into account its value.                              |
 
735
 |___________________________________________________________________________|
 
736
*/
 
737
 
 
738
Word32 L_macNs(Word32 L_var3, Word16 var1, Word16 var2)
 
739
{
 
740
    Word32 L_var_out;
 
741
 
 
742
    L_var_out = L_mult(var1, var2);
 
743
    L_var_out = L_add_c(L_var3, L_var_out);
 
744
    return(L_var_out);
 
745
}
 
746
 
 
747
/*___________________________________________________________________________
 
748
 |                                                                           |
 
749
 |   Function Name : L_msuNs                                                 |
 
750
 |                                                                           |
 
751
 |   Purpose :                                                               |
 
752
 |                                                                           |
 
753
 |   Multiply var1 by var2 and shift the result left by 1. Subtract the 32   |
 
754
 |   bit result from L_var3 without saturation, return a 32 bit result. Ge-  |
 
755
 |   nerate carry and overflow values :                                      |
 
756
 |        L_msuNs(L_var3,var1,var2) = L_sub_c(L_var3,(L_mult(var1,var2)).    |
 
757
 |                                                                           |
 
758
 |   Complexity weight : 1                                                   |
 
759
 |                                                                           |
 
760
 |   Inputs :                                                                |
 
761
 |                                                                           |
 
762
 |    L_var3   32 bit long signed integer (Word32) whose value falls in the  |
 
763
 |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
 
764
 |                                                                           |
 
765
 |    var1                                                                   |
 
766
 |             16 bit short signed integer (Word16) whose value falls in the |
 
767
 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
 
768
 |                                                                           |
 
769
 |    var2                                                                   |
 
770
 |             16 bit short signed integer (Word16) whose value falls in the |
 
771
 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
 
772
 |                                                                           |
 
773
 |   Outputs :                                                               |
 
774
 |                                                                           |
 
775
 |    none                                                                   |
 
776
 |                                                                           |
 
777
 |   Return Value :                                                          |
 
778
 |                                                                           |
 
779
 |    L_var_out                                                              |
 
780
 |             32 bit long signed integer (Word32) whose value falls in the  |
 
781
 |             range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.              |
 
782
 |                                                                           |
 
783
 |   Caution :                                                               |
 
784
 |                                                                           |
 
785
 |    In some cases the Carry flag has to be cleared or set before using op- |
 
786
 |    rators which take into account its value.                              |
 
787
 |___________________________________________________________________________|
 
788
*/
 
789
 
 
790
Word32 L_msuNs(Word32 L_var3, Word16 var1, Word16 var2)
 
791
{
 
792
    Word32 L_var_out;
 
793
 
 
794
    L_var_out = L_mult(var1,var2);
 
795
    L_var_out = L_sub_c(L_var3,L_var_out);
 
796
    return(L_var_out);
 
797
}
 
798
 
 
799
/*___________________________________________________________________________
 
800
 |                                                                           |
 
801
 |   Function Name : L_add                                                   |
 
802
 |                                                                           |
 
803
 |   Purpose :                                                               |
 
804
 |                                                                           |
 
805
 |   32 bits addition of the two 32 bits variables (L_var1+L_var2) with      |
 
806
 |   overflow control and saturation; the result is set at +214783647 when   |
 
807
 |   overflow occurs or at -214783648 when underflow occurs.                 |
 
808
 |                                                                           |
 
809
 |   Complexity weight : 2                                                   |
 
810
 |                                                                           |
 
811
 |   Inputs :                                                                |
 
812
 |                                                                           |
 
813
 |    L_var1   32 bit long signed integer (Word32) whose value falls in the  |
 
814
 |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
 
815
 |                                                                           |
 
816
 |    L_var2   32 bit long signed integer (Word32) whose value falls in the  |
 
817
 |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
 
818
 |                                                                           |
 
819
 |   Outputs :                                                               |
 
820
 |                                                                           |
 
821
 |    none                                                                   |
 
822
 |                                                                           |
 
823
 |   Return Value :                                                          |
 
824
 |                                                                           |
 
825
 |    L_var_out                                                              |
 
826
 |             32 bit long signed integer (Word32) whose value falls in the  |
 
827
 |             range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.              |
 
828
 |___________________________________________________________________________|
 
829
*/
 
830
 
 
831
Word32 L_add(Word32 L_var1, Word32 L_var2)
 
832
{
 
833
    Word32 L_var_out;
 
834
 
 
835
    L_var_out = L_var1 + L_var2;
 
836
 
 
837
    if (((L_var1 ^ L_var2) & MIN_32) == 0L) {
 
838
        if ((L_var_out ^ L_var1) & MIN_32) {
 
839
            L_var_out = (L_var1 < 0L) ? MIN_32 : MAX_32;
 
840
            Overflow = 1;
 
841
        }
 
842
    }
 
843
    return(L_var_out);
 
844
}
 
845
 
 
846
/*___________________________________________________________________________
 
847
 |                                                                           |
 
848
 |   Function Name : L_sub                                                   |
 
849
 |                                                                           |
 
850
 |   Purpose :                                                               |
 
851
 |                                                                           |
 
852
 |   32 bits subtraction of the two 32 bits variables (L_var1-L_var2) with   |
 
853
 |   overflow control and saturation; the result is set at +214783647 when   |
 
854
 |   overflow occurs or at -214783648 when underflow occurs.                 |
 
855
 |                                                                           |
 
856
 |   Complexity weight : 2                                                   |
 
857
 |                                                                           |
 
858
 |   Inputs :                                                                |
 
859
 |                                                                           |
 
860
 |    L_var1   32 bit long signed integer (Word32) whose value falls in the  |
 
861
 |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
 
862
 |                                                                           |
 
863
 |    L_var2   32 bit long signed integer (Word32) whose value falls in the  |
 
864
 |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
 
865
 |                                                                           |
 
866
 |   Outputs :                                                               |
 
867
 |                                                                           |
 
868
 |    none                                                                   |
 
869
 |                                                                           |
 
870
 |   Return Value :                                                          |
 
871
 |                                                                           |
 
872
 |    L_var_out                                                              |
 
873
 |             32 bit long signed integer (Word32) whose value falls in the  |
 
874
 |             range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.              |
 
875
 |___________________________________________________________________________|
 
876
*/
 
877
 
 
878
Word32 L_sub(Word32 L_var1, Word32 L_var2)
 
879
{
 
880
    Word32 L_var_out;
 
881
 
 
882
    L_var_out = L_var1 - L_var2;
 
883
 
 
884
    if (((L_var1 ^ L_var2) & MIN_32) != 0L) {
 
885
        if ((L_var_out ^ L_var1) & MIN_32) {
 
886
            L_var_out = (L_var1 < 0L) ? MIN_32 : MAX_32;
 
887
            Overflow = 1;
 
888
        }
 
889
    }
 
890
    return(L_var_out);
 
891
}
 
892
 
 
893
/*___________________________________________________________________________
 
894
 |                                                                           |
 
895
 |   Function Name : L_add_c                                                 |
 
896
 |                                                                           |
 
897
 |   Purpose :                                                               |
 
898
 |                                                                           |
 
899
 |   Performs 32 bits addition of the two 32 bits variables (L_var1+L_var2+C)|
 
900
 |   with carry. No saturation. Generate carry and Overflow values. The car- |
 
901
 |   ry and overflow values are binary variables which can be tested and as- |
 
902
 |   signed values.                                                          |
 
903
 |                                                                           |
 
904
 |   Complexity weight : 2                                                   |
 
905
 |                                                                           |
 
906
 |   Inputs :                                                                |
 
907
 |                                                                           |
 
908
 |    L_var1   32 bit long signed integer (Word32) whose value falls in the  |
 
909
 |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
 
910
 |                                                                           |
 
911
 |    L_var2   32 bit long signed integer (Word32) whose value falls in the  |
 
912
 |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
 
913
 |                                                                           |
 
914
 |   Outputs :                                                               |
 
915
 |                                                                           |
 
916
 |    none                                                                   |
 
917
 |                                                                           |
 
918
 |   Return Value :                                                          |
 
919
 |                                                                           |
 
920
 |    L_var_out                                                              |
 
921
 |             32 bit long signed integer (Word32) whose value falls in the  |
 
922
 |             range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.              |
 
923
 |                                                                           |
 
924
 |   Caution :                                                               |
 
925
 |                                                                           |
 
926
 |    In some cases the Carry flag has to be cleared or set before using op- |
 
927
 |    rators which take into account its value.                              |
 
928
 |___________________________________________________________________________|
 
929
*/
 
930
Word32 L_add_c(Word32 L_var1, Word32 L_var2)
 
931
{
 
932
    Word32 L_var_out;
 
933
    Word32 L_test;
 
934
    Flag carry_int = 0;
 
935
 
 
936
    L_var_out = L_var1 + L_var2 + (Word32)Carry;
 
937
 
 
938
    L_test = L_var1 + L_var2;
 
939
 
 
940
    if ((L_var1 > 0L) && (L_var2 > 0L) && (L_test < 0L)) {
 
941
        Overflow = 1;
 
942
        carry_int = 0;
 
943
    }
 
944
    else {
 
945
        if ((L_var1 < 0L) && (L_var2 < 0L) && (L_test > 0L)) {
 
946
            Overflow = 1;
 
947
            carry_int = 1;
 
948
        }
 
949
        else {
 
950
            if (((L_var1 ^ L_var2) < 0L) && (L_test > 0L)) {
 
951
                Overflow = 0;
 
952
                carry_int = 1;
 
953
            }
 
954
            else {
 
955
                Overflow = 0;
 
956
                carry_int = 0;
 
957
            }
 
958
        }
 
959
    }
 
960
 
 
961
    if (Carry) {
 
962
        if (L_test == MAX_32) {
 
963
            Overflow = 1;
 
964
            Carry = carry_int;
 
965
        }
 
966
        else {
 
967
            if (L_test == (Word32) 0xFFFFFFFFL) {
 
968
                Carry = 1;
 
969
            }
 
970
            else {
 
971
                Carry = carry_int;
 
972
            }
 
973
        }
 
974
    }
 
975
    else {
 
976
        Carry = carry_int;
 
977
    }
 
978
 
 
979
    return(L_var_out);
 
980
}
 
981
 
 
982
/*___________________________________________________________________________
 
983
 |                                                                           |
 
984
 |   Function Name : L_sub_c                                                 |
 
985
 |                                                                           |
 
986
 |   Purpose :                                                               |
 
987
 |                                                                           |
 
988
 |   Performs 32 bits subtraction of the two 32 bits variables with carry    |
 
989
 |   (borrow) : L_var1-L_var2-C. No saturation. Generate carry and Overflow  |
 
990
 |   values. The carry and overflow values are binary variables which can    |
 
991
 |   be tested and assigned values.                                          |
 
992
 |                                                                           |
 
993
 |   Complexity weight : 2                                                   |
 
994
 |                                                                           |
 
995
 |   Inputs :                                                                |
 
996
 |                                                                           |
 
997
 |    L_var1   32 bit long signed integer (Word32) whose value falls in the  |
 
998
 |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
 
999
 |                                                                           |
 
1000
 |    L_var2   32 bit long signed integer (Word32) whose value falls in the  |
 
1001
 |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
 
1002
 |                                                                           |
 
1003
 |   Outputs :                                                               |
 
1004
 |                                                                           |
 
1005
 |    none                                                                   |
 
1006
 |                                                                           |
 
1007
 |   Return Value :                                                          |
 
1008
 |                                                                           |
 
1009
 |    L_var_out                                                              |
 
1010
 |             32 bit long signed integer (Word32) whose value falls in the  |
 
1011
 |             range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.              |
 
1012
 |                                                                           |
 
1013
 |   Caution :                                                               |
 
1014
 |                                                                           |
 
1015
 |    In some cases the Carry flag has to be cleared or set before using op- |
 
1016
 |    rators which take into account its value.                              |
 
1017
 |___________________________________________________________________________|
 
1018
*/
 
1019
 
 
1020
Word32 L_sub_c(Word32 L_var1, Word32 L_var2)
 
1021
{
 
1022
    Word32 L_var_out;
 
1023
    Word32 L_test;
 
1024
    Flag carry_int = 0;
 
1025
 
 
1026
    if (Carry) {
 
1027
        Carry = 0;
 
1028
        if (L_var2 != MIN_32) {
 
1029
            L_var_out = L_add_c(L_var1,-L_var2);
 
1030
        }
 
1031
        else {
 
1032
            L_var_out = L_var1 - L_var2;
 
1033
            if (L_var1 > 0L) {
 
1034
                Overflow = 1;
 
1035
                Carry = 0;
 
1036
            }
 
1037
        }
 
1038
    }
 
1039
    else {
 
1040
        L_var_out = L_var1 - L_var2 - (Word32)0X00000001L;
 
1041
        L_test = L_var1 - L_var2;
 
1042
 
 
1043
        if ((L_test < 0L) && (L_var1 > 0L) && (L_var2 < 0L)) {
 
1044
            Overflow = 1;
 
1045
            carry_int = 0;
 
1046
        }
 
1047
        else if ((L_test > 0L) && (L_var1 < 0L) && (L_var2 > 0L)) {
 
1048
            Overflow = 1;
 
1049
            carry_int = 1;
 
1050
        }
 
1051
        else if ((L_test > 0L) && ((L_var1 ^ L_var2) > 0L)) {
 
1052
            Overflow = 0;
 
1053
            carry_int = 1;
 
1054
        }
 
1055
 
 
1056
 
 
1057
        if (L_test == MIN_32) {
 
1058
            Overflow = 1;
 
1059
            Carry = carry_int;
 
1060
        }
 
1061
        else {
 
1062
            Carry = carry_int;
 
1063
        }
 
1064
    }
 
1065
 
 
1066
    return(L_var_out);
 
1067
}
 
1068
 
 
1069
/*___________________________________________________________________________
 
1070
 |                                                                           |
 
1071
 |   Function Name : L_negate                                                |
 
1072
 |                                                                           |
 
1073
 |   Purpose :                                                               |
 
1074
 |                                                                           |
 
1075
 |   Negate the 32 bit variable L_var1 with saturation; saturate in the case |
 
1076
 |   where input is -2147483648 (0x8000 0000).                               |
 
1077
 |                                                                           |
 
1078
 |   Complexity weight : 2                                                   |
 
1079
 |                                                                           |
 
1080
 |   Inputs :                                                                |
 
1081
 |                                                                           |
 
1082
 |    L_var1   32 bit long signed integer (Word32) whose value falls in the  |
 
1083
 |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
 
1084
 |                                                                           |
 
1085
 |   Outputs :                                                               |
 
1086
 |                                                                           |
 
1087
 |    none                                                                   |
 
1088
 |                                                                           |
 
1089
 |   Return Value :                                                          |
 
1090
 |                                                                           |
 
1091
 |    L_var_out                                                              |
 
1092
 |             32 bit long signed integer (Word32) whose value falls in the  |
 
1093
 |             range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.              |
 
1094
 |___________________________________________________________________________|
 
1095
*/
 
1096
 
 
1097
Word32 L_negate(Word32 L_var1)
 
1098
{
 
1099
    Word32 L_var_out;
 
1100
 
 
1101
    L_var_out = (L_var1 == MIN_32) ? MAX_32 : -L_var1;
 
1102
    return(L_var_out);
 
1103
}
 
1104
 
 
1105
/*___________________________________________________________________________
 
1106
 |                                                                           |
 
1107
 |   Function Name : mult_r                                                  |
 
1108
 |                                                                           |
 
1109
 |   Purpose :                                                               |
 
1110
 |                                                                           |
 
1111
 |   Same as mult with rounding, i.e.:                                       |
 
1112
 |     mult_r(var1,var2) = shr(((var1*var2) + 16384),15) and                 |
 
1113
 |     mult_r(-32768,-32768) = 32767.                                        |
 
1114
 |                                                                           |
 
1115
 |   Complexity weight : 2                                                   |
 
1116
 |                                                                           |
 
1117
 |   Inputs :                                                                |
 
1118
 |                                                                           |
 
1119
 |    var1                                                                   |
 
1120
 |             16 bit short signed integer (Word16) whose value falls in the |
 
1121
 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
 
1122
 |                                                                           |
 
1123
 |    var2                                                                   |
 
1124
 |             16 bit short signed integer (Word16) whose value falls in the |
 
1125
 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
 
1126
 |                                                                           |
 
1127
 |   Outputs :                                                               |
 
1128
 |                                                                           |
 
1129
 |    none                                                                   |
 
1130
 |                                                                           |
 
1131
 |   Return Value :                                                          |
 
1132
 |                                                                           |
 
1133
 |    var_out                                                                |
 
1134
 |             16 bit short signed integer (Word16) whose value falls in the |
 
1135
 |             range : 0xffff 8000 <= var_out <= 0x0000 7fff.                |
 
1136
 |___________________________________________________________________________|
 
1137
*/
 
1138
 
 
1139
Word16 mult_r(Word16 var1, Word16 var2)
 
1140
{
 
1141
    Word16 var_out;
 
1142
    Word32 L_produit_arr;
 
1143
 
 
1144
    L_produit_arr = (Word32)var1 * (Word32)var2; /* product */
 
1145
    L_produit_arr += (Word32) 0x00004000L;        /* round */
 
1146
    L_produit_arr &= (Word32) 0xffff8000L;
 
1147
    L_produit_arr >>= 15;                        /* shift */
 
1148
 
 
1149
    if (L_produit_arr & (Word32) 0x00010000L)  {
 
1150
                                     /* sign extend when necessary */
 
1151
        L_produit_arr |= (Word32) 0xffff0000L;
 
1152
    }
 
1153
 
 
1154
    var_out = sature(L_produit_arr);
 
1155
    return(var_out);
 
1156
}
 
1157
 
 
1158
/*___________________________________________________________________________
 
1159
 |                                                                           |
 
1160
 |   Function Name : L_shl                                                   |
 
1161
 |                                                                           |
 
1162
 |   Purpose :                                                               |
 
1163
 |                                                                           |
 
1164
 |   Arithmetically shift the 32 bit input L_var1 left var2 positions. Zero  |
 
1165
 |   fill the var2 LSB of the result. If var2 is negative, L_var1 right by   |
 
1166
 |   -var2 arithmetically shift with sign extension. Saturate the result in  |
 
1167
 |   case of underflows or overflows.                                        |
 
1168
 |                                                                           |
 
1169
 |   Complexity weight : 2                                                   |
 
1170
 |                                                                           |
 
1171
 |   Inputs :                                                                |
 
1172
 |                                                                           |
 
1173
 |    L_var1   32 bit long signed integer (Word32) whose value falls in the  |
 
1174
 |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
 
1175
 |                                                                           |
 
1176
 |    var2                                                                   |
 
1177
 |             16 bit short signed integer (Word16) whose value falls in the |
 
1178
 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
 
1179
 |                                                                           |
 
1180
 |   Outputs :                                                               |
 
1181
 |                                                                           |
 
1182
 |    none                                                                   |
 
1183
 |                                                                           |
 
1184
 |   Return Value :                                                          |
 
1185
 |                                                                           |
 
1186
 |    L_var_out                                                              |
 
1187
 |             32 bit long signed integer (Word32) whose value falls in the  |
 
1188
 |             range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.              |
 
1189
 |___________________________________________________________________________|
 
1190
*/
 
1191
 
 
1192
Word32 L_shl(Word32 L_var1, Word16 var2)
 
1193
{
 
1194
    Word32 L_var_out = 0L;
 
1195
 
 
1196
    if (var2 <= (Word16)0) {
 
1197
        L_var_out = L_shr(L_var1, (Word16)-var2);
 
1198
    }
 
1199
    else {
 
1200
        for(; var2 > (Word16)0; var2--) {
 
1201
            if (L_var1 > (Word32) 0X3fffffffL) {
 
1202
                Overflow = 1;
 
1203
                L_var_out = MAX_32;
 
1204
                break;
 
1205
            }
 
1206
            else {
 
1207
                if (L_var1 < (Word32) 0xc0000000L) {
 
1208
                    Overflow = 1;
 
1209
                    L_var_out = MIN_32;
 
1210
                    break;
 
1211
                }
 
1212
            }
 
1213
            L_var1 *= 2L;
 
1214
            L_var_out = L_var1;
 
1215
        }
 
1216
    }
 
1217
    return(L_var_out);
 
1218
}
 
1219
 
 
1220
 
 
1221
/*___________________________________________________________________________
 
1222
 |                                                                           |
 
1223
 |   Function Name : L_shr                                                   |
 
1224
 |                                                                           |
 
1225
 |   Purpose :                                                               |
 
1226
 |                                                                           |
 
1227
 |   Arithmetically shift the 32 bit input L_var1 right var2 positions with  |
 
1228
 |   sign extension. If var2 is negative, arithmetically shift L_var1 left   |
 
1229
 |   by -var2 and zero fill the var2 LSB of the result. Saturate the result  |
 
1230
 |   in case of underflows or overflows.                                     |
 
1231
 |                                                                           |
 
1232
 |   Complexity weight : 2                                                   |
 
1233
 |                                                                           |
 
1234
 |   Inputs :                                                                |
 
1235
 |                                                                           |
 
1236
 |    L_var1   32 bit long signed integer (Word32) whose value falls in the  |
 
1237
 |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
 
1238
 |                                                                           |
 
1239
 |    var2                                                                   |
 
1240
 |             16 bit short signed integer (Word16) whose value falls in the |
 
1241
 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
 
1242
 |                                                                           |
 
1243
 |   Outputs :                                                               |
 
1244
 |                                                                           |
 
1245
 |    none                                                                   |
 
1246
 |                                                                           |
 
1247
 |   Return Value :                                                          |
 
1248
 |                                                                           |
 
1249
 |    L_var_out                                                              |
 
1250
 |             32 bit long signed integer (Word32) whose value falls in the  |
 
1251
 |             range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.              |
 
1252
 |___________________________________________________________________________|
 
1253
*/
 
1254
 
 
1255
Word32 L_shr(Word32 L_var1, Word16 var2)
 
1256
{
 
1257
    Word32 L_var_out;
 
1258
 
 
1259
    if (var2 < (Word16)0) {
 
1260
        L_var_out = L_shl(L_var1, (Word16)-var2);
 
1261
    }
 
1262
    else {
 
1263
        if (var2 >= (Word16)31) {
 
1264
            L_var_out = (L_var1 < 0L) ? -1L : 0L;
 
1265
        }
 
1266
        else {
 
1267
            if (L_var1< 0L)  {
 
1268
                L_var_out = ~((~L_var1) >> var2);
 
1269
            }
 
1270
            else {
 
1271
                L_var_out = L_var1 >> var2;
 
1272
            }
 
1273
        }
 
1274
    }
 
1275
    return(L_var_out);
 
1276
}
 
1277
 
 
1278
/*___________________________________________________________________________
 
1279
 |                                                                           |
 
1280
 |   Function Name : shr_r                                                   |
 
1281
 |                                                                           |
 
1282
 |   Purpose :                                                               |
 
1283
 |                                                                           |
 
1284
 |   Same as shr(var1,var2) but with rounding. Saturate the result in case of|
 
1285
 |   underflows or overflows :                                               |
 
1286
 |    If var2 is greater than zero :                                         |
 
1287
 |       shr_r(var1,var2) = shr(add(var1,2**(var2-1)),var2)                  |
 
1288
 |    If var2 is less than zero :                                            |
 
1289
 |       shr_r(var1,var2) = shr(var1,var2).                                  |
 
1290
 |                                                                           |
 
1291
 |   Complexity weight : 2                                                   |
 
1292
 |                                                                           |
 
1293
 |   Inputs :                                                                |
 
1294
 |                                                                           |
 
1295
 |    var1                                                                   |
 
1296
 |             16 bit short signed integer (Word16) whose value falls in the |
 
1297
 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
 
1298
 |                                                                           |
 
1299
 |    var2                                                                   |
 
1300
 |             16 bit short signed integer (Word16) whose value falls in the |
 
1301
 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
 
1302
 |                                                                           |
 
1303
 |   Outputs :                                                               |
 
1304
 |                                                                           |
 
1305
 |    none                                                                   |
 
1306
 |                                                                           |
 
1307
 |   Return Value :                                                          |
 
1308
 |                                                                           |
 
1309
 |    var_out                                                                |
 
1310
 |             16 bit short signed integer (Word16) whose value falls in the |
 
1311
 |             range : 0xffff 8000 <= var_out <= 0x0000 7fff.                |
 
1312
 |___________________________________________________________________________|
 
1313
*/
 
1314
 
 
1315
Word16 shr_r(Word16 var1, Word16 var2)
 
1316
{
 
1317
    Word16 var_out;
 
1318
 
 
1319
    if (var2 > (Word16)15) {
 
1320
        var_out = (Word16)0;
 
1321
    }
 
1322
    else {
 
1323
        var_out = shr(var1,var2);
 
1324
 
 
1325
        if (var2 > (Word16)0) {
 
1326
            if ((var1 & ((Word16)1 << (var2-(Word16)1))) != (Word16)0) {
 
1327
                var_out++;
 
1328
            }
 
1329
        }
 
1330
    }
 
1331
    return(var_out);
 
1332
}
 
1333
 
 
1334
/*___________________________________________________________________________
 
1335
 |                                                                           |
 
1336
 |   Function Name : mac_r                                                   |
 
1337
 |                                                                           |
 
1338
 |   Purpose :                                                               |
 
1339
 |                                                                           |
 
1340
 |   Multiply var1 by var2 and shift the result left by 1. Add the 32 bit    |
 
1341
 |   result to L_var3 with saturation. Round the LS 16 bits of the result    |
 
1342
 |   into the MS 16 bits with saturation and shift the result right by 16.   |
 
1343
 |   Return a 16 bit result.                                                 |
 
1344
 |            mac_r(L_var3,var1,var2) = g723_round(L_mac(Lvar3,var1,var2))        |
 
1345
 |                                                                           |
 
1346
 |   Complexity weight : 2                                                   |
 
1347
 |                                                                           |
 
1348
 |   Inputs :                                                                |
 
1349
 |                                                                           |
 
1350
 |    L_var3   32 bit long signed integer (Word32) whose value falls in the  |
 
1351
 |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
 
1352
 |                                                                           |
 
1353
 |    var1                                                                   |
 
1354
 |             16 bit short signed integer (Word16) whose value falls in the |
 
1355
 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
 
1356
 |                                                                           |
 
1357
 |    var2                                                                   |
 
1358
 |             16 bit short signed integer (Word16) whose value falls in the |
 
1359
 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
 
1360
 |                                                                           |
 
1361
 |   Outputs :                                                               |
 
1362
 |                                                                           |
 
1363
 |    none                                                                   |
 
1364
 |                                                                           |
 
1365
 |   Return Value :                                                          |
 
1366
 |                                                                           |
 
1367
 |    var_out                                                                |
 
1368
 |             16 bit short signed integer (Word16) whose value falls in the |
 
1369
 |             range : 0x0000 8000 <= L_var_out <= 0x0000 7fff.              |
 
1370
 |___________________________________________________________________________|
 
1371
*/
 
1372
 
 
1373
Word16 mac_r(Word32 L_var3, Word16 var1, Word16 var2)
 
1374
{
 
1375
    Word16 var_out;
 
1376
 
 
1377
    L_var3 = L_mac(L_var3, var1, var2);
 
1378
    L_var3 = L_add(L_var3, (Word32) 0x00008000L);
 
1379
    var_out = extract_h(L_var3);
 
1380
    return(var_out);
 
1381
}
 
1382
 
 
1383
 
 
1384
/*___________________________________________________________________________
 
1385
 |                                                                           |
 
1386
 |   Function Name : msu_r                                                   |
 
1387
 |                                                                           |
 
1388
 |   Purpose :                                                               |
 
1389
 |                                                                           |
 
1390
 |   Multiply var1 by var2 and shift the result left by 1. Subtract the 32   |
 
1391
 |   bit result to L_var3 with saturation. Round the LS 16 bits of the res-  |
 
1392
 |   ult into the MS 16 bits with saturation and shift the result right by   |
 
1393
 |   16. Return a 16 bit result.                                             |
 
1394
 |            msu_r(L_var3,var1,var2) = g723_round(L_msu(Lvar3,var1,var2))        |
 
1395
 |                                                                           |
 
1396
 |   Complexity weight : 2                                                   |
 
1397
 |                                                                           |
 
1398
 |   Inputs :                                                                |
 
1399
 |                                                                           |
 
1400
 |    L_var3   32 bit long signed integer (Word32) whose value falls in the  |
 
1401
 |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
 
1402
 |                                                                           |
 
1403
 |    var1                                                                   |
 
1404
 |             16 bit short signed integer (Word16) whose value falls in the |
 
1405
 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
 
1406
 |                                                                           |
 
1407
 |    var2                                                                   |
 
1408
 |             16 bit short signed integer (Word16) whose value falls in the |
 
1409
 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
 
1410
 |                                                                           |
 
1411
 |   Outputs :                                                               |
 
1412
 |                                                                           |
 
1413
 |    none                                                                   |
 
1414
 |                                                                           |
 
1415
 |   Return Value :                                                          |
 
1416
 |                                                                           |
 
1417
 |    var_out                                                                |
 
1418
 |             16 bit short signed integer (Word16) whose value falls in the |
 
1419
 |             range : 0x0000 8000 <= L_var_out <= 0x0000 7fff.              |
 
1420
 |___________________________________________________________________________|
 
1421
*/
 
1422
 
 
1423
Word16 msu_r(Word32 L_var3, Word16 var1, Word16 var2)
 
1424
{
 
1425
    Word16 var_out;
 
1426
 
 
1427
    L_var3 = L_msu(L_var3,var1,var2);
 
1428
    L_var3 = L_add(L_var3, (Word32) 0x00008000L);
 
1429
    var_out = extract_h(L_var3);
 
1430
    return(var_out);
 
1431
}
 
1432
 
 
1433
 
 
1434
 
 
1435
/*___________________________________________________________________________
 
1436
 |                                                                           |
 
1437
 |   Function Name : L_deposit_h                                             |
 
1438
 |                                                                           |
 
1439
 |   Purpose :                                                               |
 
1440
 |                                                                           |
 
1441
 |   Deposit the 16 bit var1 into the 16 MS bits of the 32 bit output. The   |
 
1442
 |   16 LS bits of the output are zeroed.                                    |
 
1443
 |                                                                           |
 
1444
 |   Complexity weight : 2                                                   |
 
1445
 |                                                                           |
 
1446
 |   Inputs :                                                                |
 
1447
 |                                                                           |
 
1448
 |    var1                                                                   |
 
1449
 |             16 bit short signed integer (Word16) whose value falls in the |
 
1450
 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
 
1451
 |                                                                           |
 
1452
 |   Outputs :                                                               |
 
1453
 |                                                                           |
 
1454
 |    none                                                                   |
 
1455
 |                                                                           |
 
1456
 |   Return Value :                                                          |
 
1457
 |                                                                           |
 
1458
 |    L_var_out                                                              |
 
1459
 |             32 bit long signed integer (Word32) whose value falls in the  |
 
1460
 |             range : 0x8000 0000 <= var_out <= 0x7fff 0000.                |
 
1461
 |___________________________________________________________________________|
 
1462
*/
 
1463
 
 
1464
Word32 L_deposit_h(Word16 var1)
 
1465
{
 
1466
    Word32 L_var_out;
 
1467
 
 
1468
    L_var_out = (Word32) var1 << 16;
 
1469
    return(L_var_out);
 
1470
}
 
1471
 
 
1472
/*___________________________________________________________________________
 
1473
 |                                                                           |
 
1474
 |   Function Name : L_deposit_l                                             |
 
1475
 |                                                                           |
 
1476
 |   Purpose :                                                               |
 
1477
 |                                                                           |
 
1478
 |   Deposit the 16 bit var1 into the 16 LS bits of the 32 bit output. The   |
 
1479
 |   16 MS bits of the output are sign extended.                             |
 
1480
 |                                                                           |
 
1481
 |   Complexity weight : 2                                                   |
 
1482
 |                                                                           |
 
1483
 |   Inputs :                                                                |
 
1484
 |                                                                           |
 
1485
 |    var1                                                                   |
 
1486
 |             16 bit short signed integer (Word16) whose value falls in the |
 
1487
 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
 
1488
 |                                                                           |
 
1489
 |   Outputs :                                                               |
 
1490
 |                                                                           |
 
1491
 |    none                                                                   |
 
1492
 |                                                                           |
 
1493
 |   Return Value :                                                          |
 
1494
 |                                                                           |
 
1495
 |    L_var_out                                                              |
 
1496
 |             32 bit long signed integer (Word32) whose value falls in the  |
 
1497
 |             range : 0xFFFF 8000 <= var_out <= 0x0000 7fff.                |
 
1498
 |___________________________________________________________________________|
 
1499
*/
 
1500
 
 
1501
Word32 L_deposit_l(Word16 var1)
 
1502
{
 
1503
    Word32 L_var_out;
 
1504
 
 
1505
    L_var_out = (Word32) var1;
 
1506
    return(L_var_out);
 
1507
}
 
1508
 
 
1509
/*___________________________________________________________________________
 
1510
 |                                                                           |
 
1511
 |   Function Name : L_shr_r                                                 |
 
1512
 |                                                                           |
 
1513
 |   Purpose :                                                               |
 
1514
 |                                                                           |
 
1515
 |   Same as L_shr(L_var1,var2)but with rounding. Saturate the result in case|
 
1516
 |   of underflows or overflows :                                            |
 
1517
 |    If var2 is greater than zero :                                         |
 
1518
 |       L_shr_r(var1,var2) = L_shr(L_add(L_var1,2**(var2-1)),var2)          |
 
1519
 |    If var2 is less than zero :                                            |
 
1520
 |       L_shr_r(var1,var2) = L_shr(L_var1,var2).                            |
 
1521
 |                                                                           |
 
1522
 |   Complexity weight : 3                                                   |
 
1523
 |                                                                           |
 
1524
 |   Inputs :                                                                |
 
1525
 |                                                                           |
 
1526
 |    L_var1                                                                 |
 
1527
 |             32 bit long signed integer (Word32) whose value falls in the  |
 
1528
 |             range : 0x8000 0000 <= var1 <= 0x7fff ffff.                   |
 
1529
 |                                                                           |
 
1530
 |    var2                                                                   |
 
1531
 |             16 bit short signed integer (Word16) whose value falls in the |
 
1532
 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
 
1533
 |                                                                           |
 
1534
 |   Outputs :                                                               |
 
1535
 |                                                                           |
 
1536
 |    none                                                                   |
 
1537
 |                                                                           |
 
1538
 |   Return Value :                                                          |
 
1539
 |                                                                           |
 
1540
 |    L_var_out                                                              |
 
1541
 |             32 bit long signed integer (Word32) whose value falls in the  |
 
1542
 |             range : 0x8000 0000 <= var_out <= 0x7fff ffff.                |
 
1543
 |___________________________________________________________________________|
 
1544
*/
 
1545
 
 
1546
Word32 L_shr_r(Word32 L_var1,Word16 var2)
 
1547
{
 
1548
    Word32 L_var_out;
 
1549
 
 
1550
    if (var2 > (Word16)31) {
 
1551
        L_var_out = 0L;
 
1552
    }
 
1553
    else {
 
1554
        L_var_out = L_shr(L_var1,var2);
 
1555
        if (var2 > (Word16)0) {
 
1556
            if ( (L_var1 & ( 1L << (Word16)(var2-(Word16)1) )) != 0L)  {
 
1557
                L_var_out++;
 
1558
            }
 
1559
        }
 
1560
    }
 
1561
    return(L_var_out);
 
1562
}
 
1563
 
 
1564
/*___________________________________________________________________________
 
1565
 |                                                                           |
 
1566
 |   Function Name : L_abs                                                   |
 
1567
 |                                                                           |
 
1568
 |   Purpose :                                                               |
 
1569
 |                                                                           |
 
1570
 |    Absolute value of L_var1; Saturate in case where the input is          |
 
1571
 |                                                               -214783648  |
 
1572
 |                                                                           |
 
1573
 |   Complexity weight : 3                                                   |
 
1574
 |                                                                           |
 
1575
 |   Inputs :                                                                |
 
1576
 |                                                                           |
 
1577
 |    L_var1                                                                 |
 
1578
 |             32 bit long signed integer (Word32) whose value falls in the  |
 
1579
 |             range : 0x8000 0000 <= var1 <= 0x7fff ffff.                   |
 
1580
 |                                                                           |
 
1581
 |   Outputs :                                                               |
 
1582
 |                                                                           |
 
1583
 |    none                                                                   |
 
1584
 |                                                                           |
 
1585
 |   Return Value :                                                          |
 
1586
 |                                                                           |
 
1587
 |    L_var_out                                                              |
 
1588
 |             32 bit long signed integer (Word32) whose value falls in the  |
 
1589
 |             range : 0x0000 0000 <= var_out <= 0x7fff ffff.                |
 
1590
 |___________________________________________________________________________|
 
1591
*/
 
1592
 
 
1593
Word32 L_abs(Word32 L_var1)
 
1594
{
 
1595
    Word32 L_var_out;
 
1596
 
 
1597
    if (L_var1 == MIN_32) {
 
1598
        L_var_out = MAX_32;
 
1599
    }
 
1600
    else {
 
1601
        if (L_var1 < 0L) {
 
1602
            L_var_out = -L_var1;
 
1603
        }
 
1604
        else {
 
1605
            L_var_out = L_var1;
 
1606
        }
 
1607
    }
 
1608
 
 
1609
    return(L_var_out);
 
1610
}
 
1611
 
 
1612
/*___________________________________________________________________________
 
1613
 |                                                                           |
 
1614
 |   Function Name : L_sat                                                   |
 
1615
 |                                                                           |
 
1616
 |   Purpose :                                                               |
 
1617
 |                                                                           |
 
1618
 |    32 bit L_var1 is set to 2147833647 if an overflow occured or to        |
 
1619
 |    -214783648 if an underflow occured on the most recent L_add_c, L_sub_c,|
 
1620
 |    L_macNs or LmsuNs operations. The carry and overflow values are binary |
 
1621
 |    values which can be tested and assigned values.                        |
 
1622
 |                                                                           |
 
1623
 |   Complexity weight : 4                                                   |
 
1624
 |                                                                           |
 
1625
 |   Inputs :                                                                |
 
1626
 |                                                                           |
 
1627
 |    L_var1                                                                 |
 
1628
 |             32 bit long signed integer (Word32) whose value falls in the  |
 
1629
 |             range : 0x8000 0000 <= var1 <= 0x7fff ffff.                   |
 
1630
 |                                                                           |
 
1631
 |   Outputs :                                                               |
 
1632
 |                                                                           |
 
1633
 |    none                                                                   |
 
1634
 |                                                                           |
 
1635
 |   Return Value :                                                          |
 
1636
 |                                                                           |
 
1637
 |    L_var_out                                                              |
 
1638
 |             32 bit long signed integer (Word32) whose value falls in the  |
 
1639
 |             range : 0x8000 0000 <= var_out <= 0x7fff ffff.                |
 
1640
 |___________________________________________________________________________|
 
1641
*/
 
1642
 
 
1643
Word32 L_sat (Word32 L_var1)
 
1644
{
 
1645
    Word32 L_var_out;
 
1646
 
 
1647
 
 
1648
    L_var_out = L_var1;
 
1649
 
 
1650
    if (Overflow) {
 
1651
 
 
1652
        if (Carry) {
 
1653
            L_var_out = MIN_32;
 
1654
        }
 
1655
        else {
 
1656
            L_var_out = MAX_32;
 
1657
        }
 
1658
 
 
1659
        Carry = 0;
 
1660
        Overflow = 0;
 
1661
    }
 
1662
 
 
1663
    return(L_var_out);
 
1664
}
 
1665
 
 
1666
 
 
1667
/*___________________________________________________________________________
 
1668
 |                                                                           |
 
1669
 |   Function Name : norm_s                                                  |
 
1670
 |                                                                           |
 
1671
 |   Purpose :                                                               |
 
1672
 |                                                                           |
 
1673
 |   Produces the number of left shift needed to normalize the 16 bit varia- |
 
1674
 |   ble var1 for positive values on the interval with minimum of 16384 and  |
 
1675
 |   maximum of 32767, and for negative values on the interval with minimum  |
 
1676
 |   of -32768 and maximum of -16384; in order to normalize the result, the  |
 
1677
 |   following operation must be done :                                      |
 
1678
 |                    norm_var1 = shl(var1,norm_s(var1)).                    |
 
1679
 |                                                                           |
 
1680
 |   Complexity weight : 15                                                  |
 
1681
 |                                                                           |
 
1682
 |   Inputs :                                                                |
 
1683
 |                                                                           |
 
1684
 |    var1                                                                   |
 
1685
 |             16 bit short signed integer (Word16) whose value falls in the |
 
1686
 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
 
1687
 |                                                                           |
 
1688
 |   Outputs :                                                               |
 
1689
 |                                                                           |
 
1690
 |    none                                                                   |
 
1691
 |                                                                           |
 
1692
 |   Return Value :                                                          |
 
1693
 |                                                                           |
 
1694
 |    var_out                                                                |
 
1695
 |             16 bit short signed integer (Word16) whose value falls in the |
 
1696
 |             range : 0x0000 0000 <= var_out <= 0x0000 000f.                |
 
1697
 |___________________________________________________________________________|
 
1698
*/
 
1699
 
 
1700
Word16 norm_s(Word16 var1)
 
1701
{
 
1702
    Word16 var_out;
 
1703
 
 
1704
    if (var1 == (Word16)0) {
 
1705
        var_out = (Word16)0;
 
1706
    }
 
1707
    else {
 
1708
        if (var1 == (Word16) 0xffff) {
 
1709
            var_out = (Word16)15;
 
1710
        }
 
1711
        else {
 
1712
            if (var1 < (Word16)0) {
 
1713
                var1 = ~var1;
 
1714
            }
 
1715
 
 
1716
            for(var_out = (Word16)0; var1 < (Word16)0x4000; var_out++) {
 
1717
                var1 <<= 1;
 
1718
            }
 
1719
        }
 
1720
    }
 
1721
 
 
1722
    return(var_out);
 
1723
}
 
1724
 
 
1725
 
 
1726
/*___________________________________________________________________________
 
1727
 |                                                                           |
 
1728
 |   Function Name : div_s                                                   |
 
1729
 |                                                                           |
 
1730
 |   Purpose :                                                               |
 
1731
 |                                                                           |
 
1732
 |   Produces a result which is the fractional integer division of var1 by  |
 
1733
 |   var2; var1 and var2 must be positive and var2 must be greater or equal  |
 
1734
 |   to var1; the result is positive (leading bit equal to 0) and truncated  |
 
1735
 |   to 16 bits.                                                             |
 
1736
 |   If var1 = var2 then div(var1,var2) = 32767.                             |
 
1737
 |                                                                           |
 
1738
 |   Complexity weight : 18                                                  |
 
1739
 |                                                                           |
 
1740
 |   Inputs :                                                                |
 
1741
 |                                                                           |
 
1742
 |    var1                                                                   |
 
1743
 |             16 bit short signed integer (Word16) whose value falls in the |
 
1744
 |             range : 0x0000 0000 <= var1 <= var2 and var2 != 0.            |
 
1745
 |                                                                           |
 
1746
 |    var2                                                                   |
 
1747
 |             16 bit short signed integer (Word16) whose value falls in the |
 
1748
 |             range : var1 <= var2 <= 0x0000 7fff and var2 != 0.            |
 
1749
 |                                                                           |
 
1750
 |   Outputs :                                                               |
 
1751
 |                                                                           |
 
1752
 |    none                                                                   |
 
1753
 |                                                                           |
 
1754
 |   Return Value :                                                          |
 
1755
 |                                                                           |
 
1756
 |    var_out                                                                |
 
1757
 |             16 bit short signed integer (Word16) whose value falls in the |
 
1758
 |             range : 0x0000 0000 <= var_out <= 0x0000 7fff.                |
 
1759
 |             It's a Q15 value (point between b15 and b14).                 |
 
1760
 |___________________________________________________________________________|
 
1761
*/
 
1762
 
 
1763
Word16 div_s(Word16 var1, Word16 var2)
 
1764
{
 
1765
    Word16 var_out = (Word16)0;
 
1766
    Word16 iteration;
 
1767
    Word32 L_num;
 
1768
    Word32 L_denom;
 
1769
 
 
1770
    if ((var1 > var2) || (var1 < (Word16)0) || (var2 < (Word16)0)) {
 
1771
        printf("Division Error\n");
 
1772
        exit(0);
 
1773
    }
 
1774
 
 
1775
    if (var2 == (Word16)0) {
 
1776
        printf("Division by 0, Fatal error \n");
 
1777
        exit(0);
 
1778
    }
 
1779
 
 
1780
    if (var1 == (Word16)0) {
 
1781
        var_out = (Word16)0;
 
1782
    }
 
1783
    else {
 
1784
        if (var1 == var2) {
 
1785
            var_out = MAX_16;
 
1786
        }
 
1787
        else {
 
1788
            L_num = L_deposit_l(var1);
 
1789
            L_denom = L_deposit_l(var2);
 
1790
 
 
1791
            for(iteration=(Word16)0; iteration< (Word16)15;iteration++) {
 
1792
                var_out <<=1;
 
1793
                L_num <<= 1;
 
1794
 
 
1795
                if (L_num >= L_denom) {
 
1796
                    L_num = L_sub(L_num,L_denom);
 
1797
                    var_out = add(var_out, (Word16)1);
 
1798
                }
 
1799
            }
 
1800
        }
 
1801
    }
 
1802
 
 
1803
    return(var_out);
 
1804
}
 
1805
 
 
1806
 
 
1807
/*___________________________________________________________________________
 
1808
 |                                                                           |
 
1809
 |   Function Name : norm_l                                                  |
 
1810
 |                                                                           |
 
1811
 |   Purpose :                                                               |
 
1812
 |                                                                           |
 
1813
 |   Produces the number of left shift needed to normalize the 32 bit varia- |
 
1814
 |   ble l_var1 for positive values on the interval with minimum of          |
 
1815
 |   1073741824 and maximum of 2147483647, and for negative values on the in-|
 
1816
 |   terval with minimum of -2147483648 and maximum of -1073741824; in order |
 
1817
 |   to normalize the result, the following operation must be done :         |
 
1818
 |                   norm_L_var1 = L_shl(L_var1,norm_l(L_var1)).             |
 
1819
 |                                                                           |
 
1820
 |   Complexity weight : 30                                                  |
 
1821
 |                                                                           |
 
1822
 |   Inputs :                                                                |
 
1823
 |                                                                           |
 
1824
 |    L_var1                                                                 |
 
1825
 |             32 bit long signed integer (Word32) whose value falls in the  |
 
1826
 |             range : 0x8000 0000 <= var1 <= 0x7fff ffff.                   |
 
1827
 |                                                                           |
 
1828
 |   Outputs :                                                               |
 
1829
 |                                                                           |
 
1830
 |    none                                                                   |
 
1831
 |                                                                           |
 
1832
 |   Return Value :                                                          |
 
1833
 |                                                                           |
 
1834
 |    var_out                                                                |
 
1835
 |             16 bit short signed integer (Word16) whose value falls in the |
 
1836
 |             range : 0x0000 0000 <= var_out <= 0x0000 001f.                |
 
1837
 |___________________________________________________________________________|
 
1838
*/
 
1839
 
 
1840
Word16 norm_l(Word32 L_var1)
 
1841
{
 
1842
    Word16 var_out;
 
1843
 
 
1844
    if (L_var1 == 0L) {
 
1845
        var_out = (Word16)0;
 
1846
    }
 
1847
    else {
 
1848
        if (L_var1 == (Word32)0xffffffffL) {
 
1849
            var_out = (Word16)31;
 
1850
        }
 
1851
        else {
 
1852
            if (L_var1 < 0L) {
 
1853
                L_var1 = ~L_var1;
 
1854
            }
 
1855
 
 
1856
            for(var_out = (Word16)0;L_var1 < (Word32)0x40000000L;var_out++) {
 
1857
                L_var1 <<= 1L;
 
1858
            }
 
1859
        }
 
1860
    }
 
1861
 
 
1862
    return(var_out);
 
1863
}
 
1864
/*
 
1865
   Additional operators
 
1866
*/
 
1867
Word32   L_mls( Word32 Lv, Word16 v )
 
1868
{
 
1869
   Word32   Temp  ;
 
1870
 
 
1871
   Temp = Lv & (Word32) 0x0000ffff ;
 
1872
   Temp = Temp * (Word32) v ;
 
1873
   Temp = L_shr( Temp, (Word16) 15 ) ;
 
1874
   Temp = L_mac( Temp, v, extract_h(Lv) ) ;
 
1875
 
 
1876
   return Temp ;
 
1877
}
 
1878
/*__________________________________________________________________________
 
1879
|                                                                           |
 
1880
|   Function Name : div_l                                                   |
 
1881
|                                                                           |
 
1882
|   Purpose :                                                               |
 
1883
|                                                                           |
 
1884
|   Produces a result which is the fractional integer division of L_var1 by|
 
1885
|   var2; L_var1 and var2 must be positive and var2 << 16 must be greater or|
 
1886
|   equal to L_var1; the result is positive (leading bit equal to 0) and    |
 
1887
|   truncated to 16 bits.                                                   |
 
1888
|   If L_var1 == var2 << 16 then div_l(L_var1,var2) = 32767.                |
 
1889
|                                                                           |
 
1890
|   Complexity weight : 20                                                  |
 
1891
|                                                                           |
 
1892
|   Inputs :                                                                |
 
1893
|                                                                           |
 
1894
|    L_var1                                                                 |
 
1895
|             32 bit long signed integer (Word32) whose value falls in the  |
 
1896
|             range : 0x0000 0000 <= var1 <= (var2 << 16)  and var2 != 0.   |
 
1897
|             L_var1 must be considered as a Q.31 value                     |
 
1898
|                                                                           |
 
1899
|    var2                                                                   |
 
1900
|             16 bit short signed integer (Word16) whose value falls in the |
 
1901
|             range : var1 <= (var2<< 16) <= 0x7fff0000 and var2 != 0.      |
 
1902
|             var2 must be considered as a Q.15 value                       |
 
1903
|                                                                           |
 
1904
|   Outputs :                                                               |
 
1905
|                                                                           |
 
1906
|    none                                                                   |
 
1907
|                                                                           |
 
1908
|   Return Value :                                                          |
 
1909
|                                                                           |
 
1910
|    var_out                                                                |
 
1911
|             16 bit short signed integer (Word16) whose value falls in the |
 
1912
|             range : 0x0000 0000 <= var_out <= 0x0000 7fff.                |
 
1913
|             It's a Q15 value (point between b15 and b14).                 |
 
1914
|___________________________________________________________________________|
 
1915
*/
 
1916
 
 
1917
 
 
1918
Word16 div_l( Word32  L_num, Word16 den ){
 
1919
 
 
1920
    Word16   var_out = (Word16)0;
 
1921
    Word32   L_den;
 
1922
    Word16   iteration;
 
1923
 
 
1924
    if ( den == (Word16) 0 ) {
 
1925
        printf("Division by 0 in div_l, Fatal error \n");
 
1926
        exit(0);
 
1927
    }
 
1928
 
 
1929
    if ( (L_num < (Word32) 0) || (den < (Word16) 0) ) {
 
1930
        printf("Division Error in div_l, Fatal error \n");
 
1931
        exit(0);
 
1932
    }
 
1933
 
 
1934
    L_den = L_deposit_h( den ) ;
 
1935
 
 
1936
    if ( L_num >= L_den ){
 
1937
        return MAX_16 ;
 
1938
    }
 
1939
    else {
 
1940
        L_num = L_shr(L_num, (Word16)1) ;
 
1941
        L_den = L_shr(L_den, (Word16)1);
 
1942
        for(iteration=(Word16)0; iteration< (Word16)15;iteration++) {
 
1943
            var_out = shl( var_out, (Word16)1);
 
1944
            L_num   = L_shl( L_num, (Word16)1);
 
1945
            if (L_num >= L_den) {
 
1946
                L_num = L_sub(L_num,L_den);
 
1947
                var_out = add(var_out, (Word16)1);
 
1948
            }
 
1949
        }
 
1950
 
 
1951
        return var_out;
 
1952
    }
 
1953
}
 
1954
 
 
1955
/*---------------------------------------------------------------------------*
 
1956
 *  Function  i_mult()                                                        *
 
1957
 *  ~~~~~~~~~~~~~~~~~                                                        *
 
1958
 * Integer multiplication.                                                   *
 
1959
 *--------------------------------------------------------------------------*/
 
1960
Word16 i_mult(Word16 a, Word16 b)
 
1961
{
 
1962
   return  a*b;
 
1963
}
 
1964
 
 
1965