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 * @return none.
46 *
47 *\par Description:
48 * \par
49 * The equation used for the conversion process is:
50 * <pre>
51 * pDst[n] = (q7_t)(pSrc[n] * 128); 0 <= n < blockSize.
52 * </pre>
53 * \par Scaling and Overflow Behavior:
54 * \par
55 * The function uses saturating arithmetic.
56 * Results outside of the allowable Q7 range [0x80 0x7F] will be saturated.
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_q7(const float64_t * pSrc,q7_t * pDst,uint32_t blockSize)62 void arm_f64_to_q7(
63 const float64_t * pSrc,
64 q7_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 * 128 */
82
83 /* Convert from float to q7 and store result in destination buffer */
84 #ifdef ARM_MATH_ROUNDING
85
86 in = (*pIn++ * 128);
87 in += in > 0.0 ? 0.5 : -0.5;
88 *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
89
90 in = (*pIn++ * 128);
91 in += in > 0.0 ? 0.5 : -0.5;
92 *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
93
94 in = (*pIn++ * 128);
95 in += in > 0.0 ? 0.5 : -0.5;
96 *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
97
98 in = (*pIn++ * 128);
99 in += in > 0.0 ? 0.5 : -0.5;
100 *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
101
102 #else
103
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 *pDst++ = __SSAT((q31_t) (*pIn++ * 128.0), 8);
108
109 #endif /* #ifdef ARM_MATH_ROUNDING */
110
111 /* Decrement loop counter */
112 blkCnt--;
113 }
114
115 /* Loop unrolling: Compute remaining outputs */
116 blkCnt = blockSize % 0x4U;
117
118 #else
119
120 /* Initialize blkCnt with number of samples */
121 blkCnt = blockSize;
122
123 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
124
125 while (blkCnt > 0U)
126 {
127 /* C = A * 128 */
128
129 /* Convert from float to q7 and store result in destination buffer */
130 #ifdef ARM_MATH_ROUNDING
131
132 in = (*pIn++ * 128);
133 in += in > 0.0 ? 0.5 : -0.5;
134 *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
135
136 #else
137
138 *pDst++ = (q7_t) __SSAT((q31_t) (*pIn++ * 128.0), 8);
139
140 #endif /* #ifdef ARM_MATH_ROUNDING */
141
142 /* Decrement loop counter */
143 blkCnt--;
144 }
145
146 }
147
148
149 /**
150 @} end of f64_to_x group
151 */
152