1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_float_to_q15.c
4  * Description:  Converts the elements of the floating-point vector to Q15 vector
5  *
6  * $Date:        23 April 2021
7  * $Revision:    V1.9.0
8  *
9  * Target Processor: Cortex-M and Cortex-A cores
10  * -------------------------------------------------------------------- */
11 /*
12  * Copyright (C) 2010-2021 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_f16.h"
30 
31 #if defined(ARM_FLOAT16_SUPPORTED)
32 
33 
34 /**
35   @ingroup groupSupport
36  */
37 
38 /**
39   @addtogroup float_to_x
40   @{
41  */
42 
43 /**
44   @brief         Converts the elements of the floating-point vector to f16 vector.
45   @param[in]     pSrc       points to the f32 input vector
46   @param[out]    pDst       points to the f16 output vector
47   @param[in]     blockSize  number of samples in each vector
48   @return        none
49 
50  */
51 
52 #if defined(ARM_MATH_MVE_FLOAT16) && !defined(ARM_MATH_AUTOVECTORIZE) && defined(__CMSIS_GCC_H)
53 #pragma GCC warning "Scalar version of arm_float_to_f16 built. Helium version has build issues with gcc."
54 #endif
55 
56 #if defined(ARM_MATH_MVE_FLOAT16) && !defined(ARM_MATH_AUTOVECTORIZE) &&  !defined(__CMSIS_GCC_H)
57 
arm_float_to_f16(const float32_t * pSrc,float16_t * pDst,uint32_t blockSize)58 void arm_float_to_f16(
59   const float32_t * pSrc,
60         float16_t * pDst,
61         uint32_t blockSize)
62 {
63     int32_t  blkCnt;           /* loop counters */
64     float32x4x2_t tmp;
65     float16x8_t vecDst;
66     float32_t const *pSrcVec;
67 
68 
69     pSrcVec = (float32_t const *) pSrc;
70     blkCnt = blockSize >> 3;
71     while (blkCnt > 0)
72     {
73         /* convert from float32 to float16 and then store the results in the destination buffer */
74         tmp = vld2q(pSrcVec);   pSrcVec += 8;
75         /* narrow / merge */
76         vecDst = vcvtbq_f16_f32(vecDst, tmp.val[0]);
77         vecDst = vcvttq_f16_f32(vecDst, tmp.val[1]);
78         vst1q(pDst, vecDst);    pDst += 8;
79         /*
80          * Decrement the blockSize loop counter
81          */
82         blkCnt--;
83     }
84 
85     /*
86      * tail
87      */
88     blkCnt = blockSize & 7;
89     if (blkCnt > 0)
90     {
91         mve_pred16_t p0 = vctp16q(blkCnt);
92         tmp = vld2q(pSrcVec);
93         vecDst = vcvtbq_f16_f32(vecDst, tmp.val[0]);
94         vecDst = vcvttq_f16_f32(vecDst, tmp.val[1]);
95         vstrhq_p(pDst, vecDst, p0);
96     }
97 }
98 
99 #else
100 
arm_float_to_f16(const float32_t * pSrc,float16_t * pDst,uint32_t blockSize)101 void arm_float_to_f16(
102   const float32_t * pSrc,
103         float16_t * pDst,
104         uint32_t blockSize)
105 {
106     const float32_t *pIn = pSrc;      /* Src pointer */
107     uint32_t  blkCnt;           /* loop counter */
108 
109     /*
110      * Loop over blockSize number of values
111      */
112     blkCnt = blockSize;
113 
114     while (blkCnt > 0U)
115     {
116 
117         *pDst++ = (float16_t) * pIn++;
118         /*
119          * Decrement the loop counter
120          */
121         blkCnt--;
122     }
123 }
124 #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
125 
126 /**
127   @} end of float_to_x group
128  */
129 
130 #endif /* #if defined(ARM_FLOAT16_SUPPORTED) */
131 
132