carl9170: Update to latest upstream
[linux-libre-firmware.git] / a56 / frac2int.c
1 #if 0
2 +----------------------------------------------------------------------+
3 |   Question: My filter design package gives me decimal fractional     |
4 |             numbers  as output  for my  FIR filter coefficients.     |
5 |             How  do I convert  these decimal fractions  into the     |
6 |             DSP56200's format?                                       |
7 +----------------------------------------------------------------------+
8
9       Answer:
10            It's fairly easy to convert decimal fractions into  the
11       data  format required by the DSP56200.  The DSP56200 coeffi-
12       cients are represented as  24-bit  signed,  fractional,  2's
13       complement numbers as shown in Table 1.
14
15          TABLE 1 - Representations for FIR Filter Coefficients
16          -----------------------------------------------------
17
18           Decimal                                  Hexadecimal
19           Fraction       24-Bit Binary Value       Coefficient
20           --------       -------------------       ----------
21
22             0.75     0.1100000 00000000 00000000    60 00 00
23             0.5      0.1000000 00000000 00000000    40 00 00
24             0.25     0.0100000 00000000 00000000    20 00 00
25             0.125    0.0010000 00000000 00000000    10 00 00
26            -0.125    1.1110000 00000000 00000000    F0 00 00
27            -0.75     1.0100000 00000000 00000000    A0 00 00
28
29       Each  24-bit  coefficient  is  separated  into  three  bytes
30       corresponding  to the high, middle, and low bytes.  The high
31       byte is written to the DSP56200 at  address  0A  (hex),  the
32       middle byte to address 0B (hex), and the low byte to address
33       0C (hex).
34
35       Conversion Procedure:
36
37          Step 1:  Multiply the decimal fraction by 8388608.0 (decimal).
38                   Note that 8388608 = 2 raised to the 23rd power.
39          Step 2:  Truncate or round the result into an integer.
40          Step 3:  Logically AND this integer with 00FFFFFF (hex).
41          Step 4:  Write the result to an output file as a hex integer.
42
43       It is easy to write a program to perform this conversion  on
44       a  set  of  coefficients.  If done by computer program, make
45       sure that all integer  variables  are  represented  with  at
46       least  24-bits.   An  example  of  a  short  "C"  program is
47       included at the end of this answer.
48
49       Things to watch for:
50
51          (1) Avoid letting a coefficient value be exactly 800000
52              (-1.0 in a fractional system).  If this coefficient
53              is multiplied by a data sample with value -1.0, the
54              result is -1.0 instead of +1.0 as expected. This is
55              because +1.0 cannot be represented in a signed, 2's
56              complement, fractional system.
57
58          (2) The filter coefficients must  be carefully selected
59              to prevent overflow.   If there is a possibility of
60              overflow with a set of filter coefficients, then all
61              coefficients must be scaled by a constant value. The
62              DSP56200's 24-bit coefficients  allow plenty of room
63              for scaling.   If 12-bit input data samples are used
64              in a system,  the potential  for overflow is greatly
65              reduced if the samples are right justified and sign-
66              extended four bits before they are sent to the 
67              DSP56200.
68
69 "C" Program:
70 #endif
71            /****************************************\
72            **  DECIMAL FRACTION TO HEX CONVERSION  **
73            \****************************************/
74
75 /******************************************************************\
76 *  This program converts one decimal, fractional number to a hex   *
77 *  number which can be written to the DSP56200's Coefficient RAM   *
78 *  Access registers.  It prompts the user for a decimal fraction   *
79 *  and returns the corresponding hex number.                       *
80 *                                                                  *
81 *   Examples of Program Results (useful for checking programs)     *
82 *        Inputs                    Outputs                         *
83 *        ------                    -------                         *
84 *         0.750                =>  600000                          *
85 *         0.500                =>  400000                          *
86 *         0.250                =>  200000                          *
87 *         0.125                =>  100000                          *
88 *        -0.125                =>  f00000                          *
89 *        -0.750                =>  a00000                          *
90 *         0.00784313678741455  =>  010101                          *
91 *        -0.00784313678741455  =>  fefeff                          *
92 *                                                                  *
93 *   Note: The program assumes that the variable type "long" is an  *
94 *         integer which is at least 24-bits wide.                  *
95 *                                                                  *
96 *   Also: The DSP56200 cannot  use any coefficient value with a    *
97 *         magnitude of 1.0 or larger.  All coefficients must be    *
98 *         signed, fractional quantities.                           *
99 \******************************************************************/
100
101 main()
102 {
103            double fractnum;              /* double precision floating pt */
104            long   hexval;                /* long integer */
105
106     for(;;) {
107         /* Read 1 Decimal Floating Point Number from the Keyboard */
108            printf("Enter the decimal fraction: ");
109            scanf("%lf", &fractnum);
110
111         /* Convert to a Hex Integer which can be used by the DSP56200 */
112            hexval =  (long) (8388608.0 * fractnum);
113            hexval =  0x00ffffffL & hexval;
114
115         /* Write the Hex number out to the Terminal */
116            printf("DSP56200 Coefficient = %06lx\n", hexval);
117     }
118 }