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