1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_bilinear_interp_f16.c
4  * Description:  Floating-point bilinear interpolation
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/interpolation_functions_f16.h"
30 
31 #if defined(ARM_FLOAT16_SUPPORTED)
32 
33 
34 /**
35   @ingroup groupInterpolation
36  */
37 
38 
39 
40   /**
41    * @addtogroup BilinearInterpolate
42    * @{
43    */
44 
45 
46   /**
47   * @brief  Floating-point bilinear interpolation.
48   * @param[in,out] S  points to an instance of the interpolation structure.
49   * @param[in]     X  interpolation coordinate.
50   * @param[in]     Y  interpolation coordinate.
51   * @return out interpolated value.
52   */
arm_bilinear_interp_f16(const arm_bilinear_interp_instance_f16 * S,float16_t X,float16_t Y)53   float16_t arm_bilinear_interp_f16(
54   const arm_bilinear_interp_instance_f16 * S,
55   float16_t X,
56   float16_t Y)
57   {
58     float16_t out;
59     float16_t f00, f01, f10, f11;
60     const float16_t *pData = S->pData;
61     int32_t xIndex, yIndex, index;
62     float16_t xdiff, ydiff;
63     float16_t b1, b2, b3, b4;
64 
65     xIndex = (int32_t) X;
66     yIndex = (int32_t) Y;
67 
68     /* Care taken for table outside boundary */
69     /* Returns zero output when values are outside table boundary */
70     if (xIndex < 0 || xIndex > (S->numCols - 2) || yIndex < 0 || yIndex > (S->numRows - 2))
71     {
72       return (0);
73     }
74 
75     /* Calculation of index for two nearest points in X-direction */
76     index = (xIndex ) + (yIndex ) * S->numCols;
77 
78 
79     /* Read two nearest points in X-direction */
80     f00 = pData[index];
81     f01 = pData[index + 1];
82 
83     /* Calculation of index for two nearest points in Y-direction */
84     index = (xIndex ) + (yIndex+1) * S->numCols;
85 
86 
87     /* Read two nearest points in Y-direction */
88     f10 = pData[index];
89     f11 = pData[index + 1];
90 
91     /* Calculation of intermediate values */
92     b1 = f00;
93     b2 = (_Float16)f01 - (_Float16)f00;
94     b3 = (_Float16)f10 - (_Float16)f00;
95     b4 = (_Float16)f00 - (_Float16)f01 - (_Float16)f10 + (_Float16)f11;
96 
97     /* Calculation of fractional part in X */
98     xdiff = (_Float16)X - (_Float16)xIndex;
99 
100     /* Calculation of fractional part in Y */
101     ydiff = (_Float16)Y - (_Float16)yIndex;
102 
103     /* Calculation of bi-linear interpolated output */
104     out = (_Float16)b1 + (_Float16)b2 * (_Float16)xdiff +
105     (_Float16)b3 * (_Float16)ydiff + (_Float16)b4 * (_Float16)xdiff * (_Float16)ydiff;
106 
107     /* return to application */
108     return (out);
109   }
110 
111   /**
112    * @} end of BilinearInterpolate group
113    */
114 
115 
116 #endif /* #if defined(ARM_FLOAT16_SUPPORTED) */
117 
118