1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_mat_cholesky_f16.c
4  * Description:  Floating-point Cholesky decomposition
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/matrix_functions_f16.h"
30 
31 #if defined(ARM_FLOAT16_SUPPORTED)
32 
33 /**
34   @ingroup groupMatrix
35  */
36 
37 /**
38   @addtogroup MatrixChol
39   @{
40  */
41 
42 /**
43    * @brief Floating-point Cholesky decomposition of positive-definite matrix.
44    * @param[in]  pSrc   points to the instance of the input floating-point matrix structure.
45    * @param[out] pDst   points to the instance of the output floating-point matrix structure.
46    * @return The function returns ARM_MATH_SIZE_MISMATCH, if the dimensions do not match.
47    * @return        execution status
48                    - \ref ARM_MATH_SUCCESS       : Operation successful
49                    - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed
50                    - \ref ARM_MATH_DECOMPOSITION_FAILURE      : Input matrix cannot be decomposed
51    * @par
52    * If the matrix is ill conditioned or only semi-definite, then it is better using the LDL^t decomposition.
53    * The decomposition of A is returning a lower triangular matrix U such that A = U U^t
54    */
55 
56 #if defined(ARM_MATH_MVE_FLOAT16) && !defined(ARM_MATH_AUTOVECTORIZE)
57 
58 #include "arm_helium_utils.h"
59 
arm_mat_cholesky_f16(const arm_matrix_instance_f16 * pSrc,arm_matrix_instance_f16 * pDst)60 arm_status arm_mat_cholesky_f16(
61   const arm_matrix_instance_f16 * pSrc,
62         arm_matrix_instance_f16 * pDst)
63 {
64 
65   arm_status status;                             /* status of matrix inverse */
66 
67 
68 #ifdef ARM_MATH_MATRIX_CHECK
69 
70   /* Check for matrix mismatch condition */
71   if ((pSrc->numRows != pSrc->numCols) ||
72       (pDst->numRows != pDst->numCols) ||
73       (pSrc->numRows != pDst->numRows)   )
74   {
75     /* Set status as ARM_MATH_SIZE_MISMATCH */
76     status = ARM_MATH_SIZE_MISMATCH;
77   }
78   else
79 
80 #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
81 
82   {
83     int i,j,k;
84     int n = pSrc->numRows;
85     _Float16 invSqrtVj;
86     float16_t *pA,*pG;
87     int kCnt;
88 
89     mve_pred16_t p0;
90 
91     f16x8_t acc, acc0, acc1, acc2, acc3;
92     f16x8_t vecGi;
93     f16x8_t vecGj,vecGj0,vecGj1,vecGj2,vecGj3;
94 
95 
96     pA = pSrc->pData;
97     pG = pDst->pData;
98 
99     for(i=0 ;i < n ; i++)
100     {
101        for(j=i ; j+3 < n ; j+=4)
102        {
103           acc0 = vdupq_n_f16(0.0f16);
104           acc0[0]=pA[(j + 0) * n + i];
105 
106           acc1 = vdupq_n_f16(0.0f16);
107           acc1[0]=pA[(j + 1) * n + i];
108 
109           acc2 = vdupq_n_f16(0.0f16);
110           acc2[0]=pA[(j + 2) * n + i];
111 
112           acc3 = vdupq_n_f16(0.0f16);
113           acc3[0]=pA[(j + 3) * n + i];
114 
115           kCnt = i;
116           for(k=0; k < i ; k+=8)
117           {
118              p0 = vctp16q(kCnt);
119 
120              vecGi=vldrhq_z_f16(&pG[i * n + k],p0);
121 
122              vecGj0=vldrhq_z_f16(&pG[(j + 0) * n + k],p0);
123              vecGj1=vldrhq_z_f16(&pG[(j + 1) * n + k],p0);
124              vecGj2=vldrhq_z_f16(&pG[(j + 2) * n + k],p0);
125              vecGj3=vldrhq_z_f16(&pG[(j + 3) * n + k],p0);
126 
127              acc0 = vfmsq_m(acc0, vecGi, vecGj0, p0);
128              acc1 = vfmsq_m(acc1, vecGi, vecGj1, p0);
129              acc2 = vfmsq_m(acc2, vecGi, vecGj2, p0);
130              acc3 = vfmsq_m(acc3, vecGi, vecGj3, p0);
131 
132              kCnt -= 8;
133           }
134           pG[(j + 0) * n + i] = vecAddAcrossF16Mve(acc0);
135           pG[(j + 1) * n + i] = vecAddAcrossF16Mve(acc1);
136           pG[(j + 2) * n + i] = vecAddAcrossF16Mve(acc2);
137           pG[(j + 3) * n + i] = vecAddAcrossF16Mve(acc3);
138        }
139 
140        for(; j < n ; j++)
141        {
142 
143           kCnt = i;
144           acc = vdupq_n_f16(0.0f16);
145           acc[0] = pA[j * n + i];
146 
147           for(k=0; k < i ; k+=8)
148           {
149              p0 = vctp16q(kCnt);
150 
151              vecGi=vldrhq_z_f16(&pG[i * n + k],p0);
152              vecGj=vldrhq_z_f16(&pG[j * n + k],p0);
153 
154              acc = vfmsq_m(acc, vecGi, vecGj,p0);
155 
156              kCnt -= 8;
157           }
158           pG[j * n + i] = vecAddAcrossF16Mve(acc);
159        }
160 
161        if (pG[i * n + i] <= 0.0f16)
162        {
163          return(ARM_MATH_DECOMPOSITION_FAILURE);
164        }
165 
166        invSqrtVj = (_Float16)1.0f/sqrtf(pG[i * n + i]);
167        for(j=i; j < n ; j++)
168        {
169          pG[j * n + i] = (_Float16)pG[j * n + i] * invSqrtVj ;
170        }
171     }
172 
173     status = ARM_MATH_SUCCESS;
174 
175   }
176 
177 
178   /* Return to application */
179   return (status);
180 }
181 
182 #else
arm_mat_cholesky_f16(const arm_matrix_instance_f16 * pSrc,arm_matrix_instance_f16 * pDst)183 arm_status arm_mat_cholesky_f16(
184   const arm_matrix_instance_f16 * pSrc,
185         arm_matrix_instance_f16 * pDst)
186 {
187 
188   arm_status status;                             /* status of matrix inverse */
189 
190 
191 #ifdef ARM_MATH_MATRIX_CHECK
192 
193   /* Check for matrix mismatch condition */
194   if ((pSrc->numRows != pSrc->numCols) ||
195       (pDst->numRows != pDst->numCols) ||
196       (pSrc->numRows != pDst->numRows)   )
197   {
198     /* Set status as ARM_MATH_SIZE_MISMATCH */
199     status = ARM_MATH_SIZE_MISMATCH;
200   }
201   else
202 
203 #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
204 
205   {
206     int i,j,k;
207     int n = pSrc->numRows;
208     float16_t invSqrtVj;
209     float16_t *pA,*pG;
210 
211     pA = pSrc->pData;
212     pG = pDst->pData;
213 
214 
215     for(i=0 ; i < n ; i++)
216     {
217        for(j=i ; j < n ; j++)
218        {
219           pG[j * n + i] = pA[j * n + i];
220 
221           for(k=0; k < i ; k++)
222           {
223              pG[j * n + i] = pG[j * n + i] - pG[i * n + k] * pG[j * n + k];
224           }
225        }
226 
227        if (pG[i * n + i] <= 0.0f)
228        {
229          return(ARM_MATH_DECOMPOSITION_FAILURE);
230        }
231 
232        invSqrtVj = 1.0f/sqrtf(pG[i * n + i]);
233        for(j=i ; j < n ; j++)
234        {
235          pG[j * n + i] = pG[j * n + i] * invSqrtVj ;
236        }
237     }
238 
239     status = ARM_MATH_SUCCESS;
240 
241   }
242 
243 
244   /* Return to application */
245   return (status);
246 }
247 
248 #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
249 
250 /**
251   @} end of MatrixChol group
252  */
253 #endif /* #if defined(ARM_FLOAT16_SUPPORTED) */
254