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 
46   @par           Details
47                    The equation used for the conversion process is:
48   <pre>
49       pDst[n] = (q15_t)(pSrc[n] * 32768);   0 <= n < blockSize.
50   </pre>
51 
52   @par           Scaling and Overflow Behavior
53                    The function uses saturating arithmetic.
54                    Results outside of the allowable Q15 range [0x8000 0x7FFF] are saturated.
55 
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_q15(const float64_t * pSrc,q15_t * pDst,uint32_t blockSize)61 void arm_f64_to_q15(
62   const float64_t * pSrc,
63         q15_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 * 32768 */
81 
82     /* convert from float to Q15 and store result in destination buffer */
83 #ifdef ARM_MATH_ROUNDING
84     in = (*pIn++ * 32768.0);
85     in += in > 0.0 ? 0.5 : -0.5;
86     *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16));
87 
88     in = (*pIn++ * 32768.0);
89     in += in > 0.0 ? 0.5 : -0.5;
90     *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16));
91 
92     in = (*pIn++ * 32768.0);
93     in += in > 0.0 ? 0.5 : -0.5;
94     *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16));
95 
96     in = (*pIn++ * 32768.0);
97     in += in > 0.0 ? 0.5 : -0.5;
98     *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16));
99 
100 #else
101 
102     *pDst++ = (q15_t) __SSAT((q31_t) (*pIn++ * 32768.0), 16);
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 
107 #endif /* #ifdef ARM_MATH_ROUNDING */
108 
109     /* Decrement loop counter */
110     blkCnt--;
111   }
112 
113   /* Loop unrolling: Compute remaining outputs */
114   blkCnt = blockSize % 0x4U;
115 
116 #else
117 
118   /* Initialize blkCnt with number of samples */
119   blkCnt = blockSize;
120 
121 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
122 
123   while (blkCnt > 0U)
124   {
125     /* C = A * 32768 */
126 
127     /* convert from float to Q15 and store result in destination buffer */
128 #ifdef ARM_MATH_ROUNDING
129 
130     in = (*pIn++ * 32768.0);
131     in += in > 0.0 ? 0.5 : -0.5;
132     *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16));
133 
134 #else
135 
136     /* C = A * 32768 */
137     /* Convert from float to q15 and then store the results in the destination buffer */
138     *pDst++ = (q15_t) __SSAT((q31_t) (*pIn++ * 32768.0), 16);
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 
153