1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_f64_to_q7.c
4 * Description: Converts the elements of the 64 bit floating-point vector to Q7 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 Q7 vector.
42 * @param[in] *pSrc points to the 64 bit floating-point input vector
43 * @param[out] *pDst points to the Q7 output vector
44 * @param[in] blockSize length of the input vector
45 *
46 *\par Description:
47 * \par
48 * The equation used for the conversion process is:
49 * <pre>
50 * pDst[n] = (q7_t)(pSrc[n] * 128); 0 <= n < blockSize.
51 * </pre>
52 * \par Scaling and Overflow Behavior:
53 * \par
54 * The function uses saturating arithmetic.
55 * Results outside of the allowable Q7 range [0x80 0x7F] will be saturated.
56 * \note
57 * In order to apply rounding, the library should be rebuilt with the ROUNDING macro
58 * defined in the preprocessor section of project options.
59 */
60
arm_f64_to_q7(const float64_t * pSrc,q7_t * pDst,uint32_t blockSize)61 ARM_DSP_ATTRIBUTE void arm_f64_to_q7(
62 const float64_t * pSrc,
63 q7_t * pDst,
64 uint32_t blockSize)
65 {
66 uint32_t blkCnt; /* Loop counter */
67 const float64_t *pIn = pSrc; /* Source pointer */
68
69 #ifdef ARM_MATH_ROUNDING
70 float64_t in;
71 #endif /* #ifdef ARM_MATH_ROUNDING */
72
73 #if defined (ARM_MATH_LOOPUNROLL)
74
75 /* Loop unrolling: Compute 4 outputs at a time */
76 blkCnt = blockSize >> 2U;
77
78 while (blkCnt > 0U)
79 {
80 /* C = A * 128 */
81
82 /* Convert from float to q7 and store result in destination buffer */
83 #ifdef ARM_MATH_ROUNDING
84
85 in = (*pIn++ * 128);
86 in += in > 0.0 ? 0.5 : -0.5;
87 *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
88
89 in = (*pIn++ * 128);
90 in += in > 0.0 ? 0.5 : -0.5;
91 *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
92
93 in = (*pIn++ * 128);
94 in += in > 0.0 ? 0.5 : -0.5;
95 *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
96
97 in = (*pIn++ * 128);
98 in += in > 0.0 ? 0.5 : -0.5;
99 *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
100
101 #else
102
103 *pDst++ = __SSAT((q31_t) (*pIn++ * 128.0), 8);
104 *pDst++ = __SSAT((q31_t) (*pIn++ * 128.0), 8);
105 *pDst++ = __SSAT((q31_t) (*pIn++ * 128.0), 8);
106 *pDst++ = __SSAT((q31_t) (*pIn++ * 128.0), 8);
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 * 128 */
127
128 /* Convert from float to q7 and store result in destination buffer */
129 #ifdef ARM_MATH_ROUNDING
130
131 in = (*pIn++ * 128);
132 in += in > 0.0 ? 0.5 : -0.5;
133 *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
134
135 #else
136
137 *pDst++ = (q7_t) __SSAT((q31_t) (*pIn++ * 128.0), 8);
138
139 #endif /* #ifdef ARM_MATH_ROUNDING */
140
141 /* Decrement loop counter */
142 blkCnt--;
143 }
144
145 }
146
147
148 /**
149 @} end of f64_to_x group
150 */
151