comparison libtwamr/log2.c @ 326:bfe74a9edd5a

libtwamr: integrate log2.c
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 18 Apr 2024 20:21:04 +0000
parents
children
comparison
equal deleted inserted replaced
325:67c31802392b 326:bfe74a9edd5a
1 /*
2 ********************************************************************************
3 *
4 * GSM AMR-NB speech codec R98 Version 7.6.0 December 12, 2001
5 * R99 Version 3.3.0
6 * REL-4 Version 4.1.0
7 *
8 ********************************************************************************
9 *
10 * File : log2.c
11 * Purpose : Computes log2(L_x)
12 *
13 ********************************************************************************
14 */
15 /*
16 ********************************************************************************
17 * MODULE INCLUDE FILE AND VERSION ID
18 ********************************************************************************
19 */
20 #include "namespace.h"
21 #include "log2.h"
22
23 /*
24 ********************************************************************************
25 * INCLUDE FILES
26 ********************************************************************************
27 */
28 #include "typedef.h"
29 #include "basic_op.h"
30 #include "no_count.h"
31
32 /*
33 ********************************************************************************
34 * LOCAL VARIABLES AND TABLES
35 ********************************************************************************
36 */
37 #include "log2.tab" /* Table for Log2() */
38
39 /*
40 ********************************************************************************
41 * PUBLIC PROGRAM CODE
42 ********************************************************************************
43 */
44
45 /*************************************************************************
46 *
47 * FUNCTION: Log2_norm()
48 *
49 * PURPOSE: Computes log2(L_x, exp), where L_x is positive and
50 * normalized, and exp is the normalisation exponent
51 * If L_x is negative or zero, the result is 0.
52 *
53 * DESCRIPTION:
54 * The function Log2(L_x) is approximated by a table and linear
55 * interpolation. The following steps are used to compute Log2(L_x)
56 *
57 * 1- exponent = 30-norm_exponent
58 * 2- i = bit25-b31 of L_x; 32<=i<=63 (because of normalization).
59 * 3- a = bit10-b24
60 * 4- i -=32
61 * 5- fraction = table[i]<<16 - (table[i] - table[i+1]) * a * 2
62 *
63 *************************************************************************/
64 void Log2_norm (
65 Word32 L_x, /* (i) : input value (normalized) */
66 Word16 exp, /* (i) : norm_l (L_x) */
67 Word16 *exponent, /* (o) : Integer part of Log2. (range: 0<=val<=30) */
68 Word16 *fraction /* (o) : Fractional part of Log2. (range: 0<=val<1) */
69 )
70 {
71 Word16 i, a, tmp;
72 Word32 L_y;
73
74 test ();
75 if (L_x <= (Word32) 0)
76 {
77 *exponent = 0; move16 ();
78 *fraction = 0; move16 ();
79 return;
80 }
81
82 *exponent = sub (30, exp); move16 ();
83
84 L_x = L_shr (L_x, 9);
85 i = extract_h (L_x); /* Extract b25-b31 */
86 L_x = L_shr (L_x, 1);
87 a = extract_l (L_x); /* Extract b10-b24 of fraction */
88 a = a & (Word16) 0x7fff; logic16 ();
89
90 i = sub (i, 32);
91
92 L_y = L_deposit_h (table[i]); /* table[i] << 16 */
93 tmp = sub (table[i], table[i + 1]); /* table[i] - table[i+1] */
94 L_y = L_msu (L_y, tmp, a); /* L_y -= tmp*a*2 */
95
96 *fraction = extract_h (L_y);move16 ();
97
98 return;
99 }
100
101 /*************************************************************************
102 *
103 * FUNCTION: Log2()
104 *
105 * PURPOSE: Computes log2(L_x), where L_x is positive.
106 * If L_x is negative or zero, the result is 0.
107 *
108 * DESCRIPTION:
109 * normalizes L_x and then calls Log2_norm().
110 *
111 *************************************************************************/
112 void Log2 (
113 Word32 L_x, /* (i) : input value */
114 Word16 *exponent, /* (o) : Integer part of Log2. (range: 0<=val<=30) */
115 Word16 *fraction /* (o) : Fractional part of Log2. (range: 0<=val<1) */
116 )
117 {
118 Word16 exp;
119
120 exp = norm_l (L_x);
121 Log2_norm (L_shl (L_x, exp), exp, exponent, fraction);
122 }