1 /* ======================================================================= */
2 /* TEXAS INSTRUMENTS, INC. */
3 /* */
4 /* DSPLIB DSP Signal Processing Library */
5 /* */
6 /* This library contains proprietary intellectual property of Texas */
7 /* Instruments, Inc. The library and its source code are protected by */
8 /* various copyrights, and portions may also be protected by patents or */
9 /* other legal protections. */
10 /* */
11 /* This software is licensed for use with Texas Instruments TMS320 */
12 /* family DSPs. This license was provided to you prior to installing */
13 /* the software. You may review this license by consulting the file */
14 /* TI_license.PDF which accompanies the files in this library. */
15 /* */
16 /* ----------------------------------------------------------------------- */
17 /* */
18 /* DSPF_fltoq15.c -- Float to Q15 conversion */
19 /* Optimized C Implementation (w/ Intrinsics) */
20 /* */
21 /* Rev 0.0.1 */
22 /* */
23 /* Usage */
24 /* This routine is C-callable and can be called as: */
25 /* */
26 /* void DSPF_fltoq15 ( */
27 /* const float *restrict flt, // Input float array */
28 /* short *restrict q15, // Output Q15 array */
29 /* short nx // Number of elements */
30 /* ); */
31 /* */
32 /* Convert the IEEE floating point numbers stored in vector x[] into */
33 /* Q.15 format numbers stored in vector r[]. Results are truncated */
34 /* towards zero. Values that exceed the size limit will be saturated */
35 /* to 0x7fff if value is positive and 0x8000 if value is negative. */
36 /* All values too small to be correctly represented will be truncated */
37 /* to 0. */
38 /* */
39 /* The 16-bit Q.15 format is defined as follows: */
40 /* */
41 /* 1 11111 */
42 /* 5 432109876543210 */
43 /* S.XXXXXXXXXXXXXXX */
44 /* */
45 /* range: 1.000000000000000 = -1.0 <-> -32768 */
46 /* 0.111111111111111 = 0.999969482421875 <-> 32767 */
47 /* */
48 /* IEEE floating point format is defined as follows: */
49 /* */
50 /* 31 | 30....23 | 22.....0 */
51 /* S | EXPONENT | MANTISSA */
52 /* */
53 /* The value is obtained as: (-1)^S * 1.MANTISSA * 2^(EXPONENT-127) */
54 /* */
55 /* Assumptions */
56 /* Arrays flt and q15 do not overlap */
57 /* nx >= 2 nx % 2 == 0 */
58 /* */
59 /* Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ */
60 /* */
61 /* */
62 /* Redistribution and use in source and binary forms, with or without */
63 /* modification, are permitted provided that the following conditions */
64 /* are met: */
65 /* */
66 /* Redistributions of source code must retain the above copyright */
67 /* notice, this list of conditions and the following disclaimer. */
68 /* */
69 /* Redistributions in binary form must reproduce the above copyright */
70 /* notice, this list of conditions and the following disclaimer in the */
71 /* documentation and/or other materials provided with the */
72 /* distribution. */
73 /* */
74 /* Neither the name of Texas Instruments Incorporated nor the names of */
75 /* its contributors may be used to endorse or promote products derived */
76 /* from this software without specific prior written permission. */
77 /* */
78 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
79 /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
80 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR */
81 /* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
82 /* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
83 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */
84 /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, */
85 /* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY */
86 /* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
87 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE */
88 /* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
89 /* */
90 /* ======================================================================= */
92 #pragma CODE_SECTION(DSPF_fltoq15, ".text:optimized");
94 #include "DSPF_fltoq15.h"
96 extern cregister volatile unsigned int FADCR;
98 void DSPF_fltoq15(const float* restrict x, short* restrict y, const int n)
99 {
100 int i, a;
101 int fadcr_store;
102 float t, t1;
104 _nassert(n > 0);
105 _nassert(n % 2 == 0);
107 /* preserve the FADCR value */
108 fadcr_store = FADCR;
110 /* set FADCR bits to switch to ROUND TOWARD NEGATIVE INF */
111 FADCR = _set(FADCR,9,10);
113 /* set mode for both L units */
114 FADCR = _set(FADCR,25,26);
116 #pragma MUST_ITERATE(1,,)
117 for(i = 0; i < n; i++)
118 {
119 /* load the float */
120 t = x[i];
122 /* multiply by 2^15 for converting to Q15 format */
123 /* this brings the number in the range -32768 to 32767 */
124 t1 = t * 32768;
126 /* use intrinsic to convert the number to integer */
127 a = _spint(t1);
129 /* saturate the number if required */
130 a = _sshl(a, 16);
132 /* fetch the lower 16 bits of the result */
133 y[i] = _mpyhlu(a,1);
134 }
136 /* restore the FADCR value */
137 FADCR = fadcr_store;
138 }
140 /* ======================================================================= */
141 /* End of file: DSPF_fltoq15.c */
142 /* ----------------------------------------------------------------------- */
143 /* Copyright (c) 2011 Texas Instruments, Incorporated. */
144 /* All Rights Reserved. */
145 /* ======================================================================= */