Subversion Repositories DashDisplay

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 mjames 1
/* ----------------------------------------------------------------------------    
2
* Copyright (C) 2010-2014 ARM Limited. All rights reserved.    
3
*    
4
* $Date:        19. March 2015
5
* $Revision:    V.1.4.5  
6
*    
7
* Project:          CMSIS DSP Library    
8
* Title:                arm_float_to_q31.c    
9
*    
10
* Description:  Converts the elements of the floating-point vector to Q31 vector.    
11
*    
12
* Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
13
*  
14
* Redistribution and use in source and binary forms, with or without
15
* modification, are permitted provided that the following conditions
16
* are met:
17
*   - Redistributions of source code must retain the above copyright
18
*     notice, this list of conditions and the following disclaimer.
19
*   - Redistributions in binary form must reproduce the above copyright
20
*     notice, this list of conditions and the following disclaimer in
21
*     the documentation and/or other materials provided with the
22
*     distribution.
23
*   - Neither the name of ARM LIMITED nor the names of its contributors
24
*     may be used to endorse or promote products derived from this
25
*     software without specific prior written permission.
26
*
27
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
* POSSIBILITY OF SUCH DAMAGE.  
39
* ---------------------------------------------------------------------------- */
40
 
41
#include "arm_math.h"
42
 
43
/**    
44
 * @ingroup groupSupport    
45
 */
46
 
47
/**    
48
 * @defgroup float_to_x  Convert 32-bit floating point value    
49
 */
50
 
51
/**    
52
 * @addtogroup float_to_x    
53
 * @{    
54
 */
55
 
56
/**    
57
 * @brief Converts the elements of the floating-point vector to Q31 vector.    
58
 * @param[in]       *pSrc points to the floating-point input vector    
59
 * @param[out]      *pDst points to the Q31 output vector  
60
 * @param[in]       blockSize length of the input vector    
61
 * @return none.    
62
 *    
63
 *\par Description:    
64
 * \par  
65
 * The equation used for the conversion process is:    
66
 *  
67
 * <pre>    
68
 *      pDst[n] = (q31_t)(pSrc[n] * 2147483648);   0 <= n < blockSize.    
69
 * </pre>    
70
 * <b>Scaling and Overflow Behavior:</b>    
71
 * \par    
72
 * The function uses saturating arithmetic.    
73
 * Results outside of the allowable Q31 range[0x80000000 0x7FFFFFFF] will be saturated.    
74
 *  
75
 * \note In order to apply rounding, the library should be rebuilt with the ROUNDING macro    
76
 * defined in the preprocessor section of project options.    
77
 */
78
 
79
 
80
void arm_float_to_q31(
81
  float32_t * pSrc,
82
  q31_t * pDst,
83
  uint32_t blockSize)
84
{
85
  float32_t *pIn = pSrc;                         /* Src pointer */
86
  uint32_t blkCnt;                               /* loop counter */
87
 
88
#ifdef ARM_MATH_ROUNDING
89
 
90
  float32_t in;
91
 
92
#endif /*      #ifdef ARM_MATH_ROUNDING        */
93
 
94
#ifndef ARM_MATH_CM0_FAMILY
95
 
96
  /* Run the below code for Cortex-M4 and Cortex-M3 */
97
 
98
  /*loop Unrolling */
99
  blkCnt = blockSize >> 2u;
100
 
101
  /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.    
102
   ** a second loop below computes the remaining 1 to 3 samples. */
103
  while(blkCnt > 0u)
104
  {
105
 
106
#ifdef ARM_MATH_ROUNDING
107
 
108
    /* C = A * 32768 */
109
    /* convert from float to Q31 and then store the results in the destination buffer */
110
    in = *pIn++;
111
    in = (in * 2147483648.0f);
112
    in += in > 0.0f ? 0.5f : -0.5f;
113
    *pDst++ = clip_q63_to_q31((q63_t) (in));
114
 
115
    in = *pIn++;
116
    in = (in * 2147483648.0f);
117
    in += in > 0.0f ? 0.5f : -0.5f;
118
    *pDst++ = clip_q63_to_q31((q63_t) (in));
119
 
120
    in = *pIn++;
121
    in = (in * 2147483648.0f);
122
    in += in > 0.0f ? 0.5f : -0.5f;
123
    *pDst++ = clip_q63_to_q31((q63_t) (in));
124
 
125
    in = *pIn++;
126
    in = (in * 2147483648.0f);
127
    in += in > 0.0f ? 0.5f : -0.5f;
128
    *pDst++ = clip_q63_to_q31((q63_t) (in));
129
 
130
#else
131
 
132
    /* C = A * 2147483648 */
133
    /* convert from float to Q31 and then store the results in the destination buffer */
134
    *pDst++ = clip_q63_to_q31((q63_t) (*pIn++ * 2147483648.0f));
135
    *pDst++ = clip_q63_to_q31((q63_t) (*pIn++ * 2147483648.0f));
136
    *pDst++ = clip_q63_to_q31((q63_t) (*pIn++ * 2147483648.0f));
137
    *pDst++ = clip_q63_to_q31((q63_t) (*pIn++ * 2147483648.0f));
138
 
139
#endif /*      #ifdef ARM_MATH_ROUNDING        */
140
 
141
    /* Decrement the loop counter */
142
    blkCnt--;
143
  }
144
 
145
  /* If the blockSize is not a multiple of 4, compute any remaining output samples here.    
146
   ** No loop unrolling is used. */
147
  blkCnt = blockSize % 0x4u;
148
 
149
  while(blkCnt > 0u)
150
  {
151
 
152
#ifdef ARM_MATH_ROUNDING
153
 
154
    /* C = A * 2147483648 */
155
    /* convert from float to Q31 and then store the results in the destination buffer */
156
    in = *pIn++;
157
    in = (in * 2147483648.0f);
158
    in += in > 0.0f ? 0.5f : -0.5f;
159
    *pDst++ = clip_q63_to_q31((q63_t) (in));
160
 
161
#else
162
 
163
    /* C = A * 2147483648 */
164
    /* convert from float to Q31 and then store the results in the destination buffer */
165
    *pDst++ = clip_q63_to_q31((q63_t) (*pIn++ * 2147483648.0f));
166
 
167
#endif /*      #ifdef ARM_MATH_ROUNDING        */
168
 
169
    /* Decrement the loop counter */
170
    blkCnt--;
171
  }
172
 
173
 
174
#else
175
 
176
  /* Run the below code for Cortex-M0 */
177
 
178
  /* Loop over blockSize number of values */
179
  blkCnt = blockSize;
180
 
181
  while(blkCnt > 0u)
182
  {
183
 
184
#ifdef ARM_MATH_ROUNDING
185
 
186
    /* C = A * 2147483648 */
187
    /* convert from float to Q31 and then store the results in the destination buffer */
188
    in = *pIn++;
189
    in = (in * 2147483648.0f);
190
    in += in > 0 ? 0.5f : -0.5f;
191
    *pDst++ = clip_q63_to_q31((q63_t) (in));
192
 
193
#else
194
 
195
    /* C = A * 2147483648 */
196
    /* convert from float to Q31 and then store the results in the destination buffer */
197
    *pDst++ = clip_q63_to_q31((q63_t) (*pIn++ * 2147483648.0f));
198
 
199
#endif /*      #ifdef ARM_MATH_ROUNDING        */
200
 
201
    /* Decrement the loop counter */
202
    blkCnt--;
203
  }
204
 
205
#endif /* #ifndef ARM_MATH_CM0_FAMILY */
206
 
207
}
208
 
209
/**    
210
 * @} end of float_to_x group    
211
 */