1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_f64_to_q15.c
4 * Description: Converts the elements of the 64 bit floating-point vector to Q15 vector
5 *
6 * $Date: 18 August 2022
7 * $Revision: V1.0.0
8 *
9 * Target Processor: Cortex-M and Cortex-A cores
10 * -------------------------------------------------------------------- */
11 /*
12 * Copyright (C) 2010-2022 ARM Limited or its affiliates. All rights reserved.
13 *
14 * SPDX-License-Identifier: Apache-2.0
15 *
16 * Licensed under the Apache License, Version 2.0 (the License); you may
17 * not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
19 *
20 * www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
27 */
28
29 #include "dsp/support_functions.h"
30
31 /**
32 @ingroup groupSupport
33 */
34
35 /**
36 @addtogroup f64_to_x
37 @{
38 */
39
40 /**
41 @brief Converts the elements of the 64 bit floating-point vector to Q15 vector.
42 @param[in] pSrc points to the 64 bit floating-point input vector
43 @param[out] pDst points to the Q15 output vector
44 @param[in] blockSize number of samples in each vector
45 @return none
46
47 @par Details
48 The equation used for the conversion process is:
49 <pre>
50 pDst[n] = (q15_t)(pSrc[n] * 32768); 0 <= n < blockSize.
51 </pre>
52
53 @par Scaling and Overflow Behavior
54 The function uses saturating arithmetic.
55 Results outside of the allowable Q15 range [0x8000 0x7FFF] are saturated.
56
57 @note
58 In order to apply rounding, the library should be rebuilt with the ROUNDING macro
59 defined in the preprocessor section of project options.
60 */
61
arm_f64_to_q15(const float64_t * pSrc,q15_t * pDst,uint32_t blockSize)62 void arm_f64_to_q15(
63 const float64_t * pSrc,
64 q15_t * pDst,
65 uint32_t blockSize)
66 {
67 uint32_t blkCnt; /* Loop counter */
68 const float64_t *pIn = pSrc; /* Source pointer */
69
70 #ifdef ARM_MATH_ROUNDING
71 float64_t in;
72 #endif /* #ifdef ARM_MATH_ROUNDING */
73
74 #if defined (ARM_MATH_LOOPUNROLL)
75
76 /* Loop unrolling: Compute 4 outputs at a time */
77 blkCnt = blockSize >> 2U;
78
79 while (blkCnt > 0U)
80 {
81 /* C = A * 32768 */
82
83 /* convert from float to Q15 and store result in destination buffer */
84 #ifdef ARM_MATH_ROUNDING
85 in = (*pIn++ * 32768.0);
86 in += in > 0.0 ? 0.5 : -0.5;
87 *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16));
88
89 in = (*pIn++ * 32768.0);
90 in += in > 0.0 ? 0.5 : -0.5;
91 *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16));
92
93 in = (*pIn++ * 32768.0);
94 in += in > 0.0 ? 0.5 : -0.5;
95 *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16));
96
97 in = (*pIn++ * 32768.0);
98 in += in > 0.0 ? 0.5 : -0.5;
99 *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16));
100
101 #else
102
103 *pDst++ = (q15_t) __SSAT((q31_t) (*pIn++ * 32768.0), 16);
104 *pDst++ = (q15_t) __SSAT((q31_t) (*pIn++ * 32768.0), 16);
105 *pDst++ = (q15_t) __SSAT((q31_t) (*pIn++ * 32768.0), 16);
106 *pDst++ = (q15_t) __SSAT((q31_t) (*pIn++ * 32768.0), 16);
107
108 #endif /* #ifdef ARM_MATH_ROUNDING */
109
110 /* Decrement loop counter */
111 blkCnt--;
112 }
113
114 /* Loop unrolling: Compute remaining outputs */
115 blkCnt = blockSize % 0x4U;
116
117 #else
118
119 /* Initialize blkCnt with number of samples */
120 blkCnt = blockSize;
121
122 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
123
124 while (blkCnt > 0U)
125 {
126 /* C = A * 32768 */
127
128 /* convert from float to Q15 and store result in destination buffer */
129 #ifdef ARM_MATH_ROUNDING
130
131 in = (*pIn++ * 32768.0);
132 in += in > 0.0 ? 0.5 : -0.5;
133 *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16));
134
135 #else
136
137 /* C = A * 32768 */
138 /* Convert from float to q15 and then store the results in the destination buffer */
139 *pDst++ = (q15_t) __SSAT((q31_t) (*pIn++ * 32768.0), 16);
140
141 #endif /* #ifdef ARM_MATH_ROUNDING */
142
143 /* Decrement loop counter */
144 blkCnt--;
145 }
146
147 }
148
149
150 /**
151 @} end of f64_to_x group
152 */
153
154