1 /*
2  * Copyright (C) 2010-2020 Arm Limited or its affiliates. All rights reserved.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Licensed under the Apache License, Version 2.0 (the License); you may
7  * not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 /* ----------------------------------------------------------------------
20  * Project:      CMSIS NN Library
21  * Title:        arm_max_pool_s8.c
22  * Description:  Pooling function implementations
23  *
24  * $Date:        19. Februari 2021
25  * $Revision:    V.2.0.2
26  *
27  * Target Processor:  Cortex-M CPUs
28  *
29  * -------------------------------------------------------------------- */
30 
31 #include "arm_nnfunctions.h"
32 #include "arm_nnsupportfunctions.h"
33 
compare_and_replace_if_larger_q7(q7_t * base,const q7_t * target,int32_t length)34 static void compare_and_replace_if_larger_q7(q7_t *base, const q7_t *target, int32_t length)
35 {
36 #if defined(ARM_MATH_MVEI)
37     int32_t loop_count = (length + 15) / 16;
38     for (int i = 0; i < loop_count; i++)
39     {
40         mve_pred16_t p = vctp8q((uint32_t)length);
41         const int8x16_t op_1 = vldrbq_z_s8(base, p);
42         const int8x16_t op_2 = vldrbq_z_s8(target, p);
43         const int8x16_t max = vmaxq_m_s8(vuninitializedq_s8(), op_1, op_2, p);
44         vstrbq_p_s8(base, max, p);
45         base += 16;
46         target += 16;
47         length -= 16;
48     }
49 #else
50     q7_t *dst = base;
51     const q7_t *src = target;
52     union arm_nnword ref_max;
53     union arm_nnword comp_max;
54     int32_t cnt = length >> 2;
55 
56     while (cnt > 0l)
57     {
58         ref_max.word = arm_nn_read_q7x4(dst);
59         comp_max.word = arm_nn_read_q7x4_ia(&src);
60 
61         if (comp_max.bytes[0] > ref_max.bytes[0])
62         {
63             ref_max.bytes[0] = comp_max.bytes[0];
64         }
65         if (comp_max.bytes[1] > ref_max.bytes[1])
66         {
67             ref_max.bytes[1] = comp_max.bytes[1];
68         }
69         if (comp_max.bytes[2] > ref_max.bytes[2])
70         {
71             ref_max.bytes[2] = comp_max.bytes[2];
72         }
73         if (comp_max.bytes[3] > ref_max.bytes[3])
74         {
75             ref_max.bytes[3] = comp_max.bytes[3];
76         }
77 
78         write_q7x4_ia(&dst, ref_max.word);
79 
80         cnt--;
81     }
82 
83     cnt = length & 0x3;
84     while (cnt > 0l)
85     {
86         if (*src > *dst)
87         {
88             *dst = *src;
89         }
90         dst++;
91         src++;
92         cnt--;
93     }
94 #endif
95 }
96 
clamp_output(q7_t * source,int32_t length,const int32_t act_min,const int32_t act_max)97 static void clamp_output(q7_t *source, int32_t length, const int32_t act_min, const int32_t act_max)
98 {
99 #if defined(ARM_MATH_MVEI)
100     int32_t loop_count = (length + 15) / 16;
101     for (int i = 0; i < loop_count; i++)
102     {
103         mve_pred16_t p = vctp8q((uint32_t)length);
104         length -= 16;
105         const int8x16_t src = vldrbq_z_s8(source, p);
106         const int8x16_t predicated_min = vdupq_m_n_s8(vuninitializedq_s8(), (int8_t)act_min, p);
107         const int8x16_t predicated_max = vdupq_m_n_s8(vuninitializedq_s8(), (int8_t)act_max, p);
108         int8x16_t res = vmaxq_m_s8(vuninitializedq_s8(), src, predicated_min, p);
109         res = vminq_m_s8(vuninitializedq_s8(), res, predicated_max, p);
110         vstrbq_p_s8(source, res, p);
111         source += 16;
112     }
113 #else
114     union arm_nnword in;
115     int32_t cnt = length >> 2;
116 
117     while (cnt > 0l)
118     {
119         in.word = arm_nn_read_q7x4(source);
120 
121         in.bytes[0] = MAX(in.bytes[0], act_min);
122         in.bytes[0] = MIN(in.bytes[0], act_max);
123         in.bytes[1] = MAX(in.bytes[1], act_min);
124         in.bytes[1] = MIN(in.bytes[1], act_max);
125         in.bytes[2] = MAX(in.bytes[2], act_min);
126         in.bytes[2] = MIN(in.bytes[2], act_max);
127         in.bytes[3] = MAX(in.bytes[3], act_min);
128         in.bytes[3] = MIN(in.bytes[3], act_max);
129 
130         write_q7x4_ia(&source, in.word);
131         cnt--;
132     }
133 
134     cnt = length & 0x3;
135     while (cnt > 0l)
136     {
137         int32_t comp = *source;
138         comp = MAX(comp, act_min);
139         comp = MIN(comp, act_max);
140         *source++ = (int8_t)comp;
141         cnt--;
142     }
143 #endif
144 }
145 
146 /**
147  *  @ingroup groupNN
148  */
149 
150 /**
151  * @addtogroup Pooling
152  * @{
153  */
154 
155 /*
156  * Optimized s8 max pooling function
157  *
158  * Refer to header file for details.
159  *
160  */
161 
arm_max_pool_s8(const cmsis_nn_context * ctx,const cmsis_nn_pool_params * pool_params,const cmsis_nn_dims * input_dims,const q7_t * src,const cmsis_nn_dims * filter_dims,const cmsis_nn_dims * output_dims,q7_t * dst)162 arm_status arm_max_pool_s8(const cmsis_nn_context *ctx,
163                            const cmsis_nn_pool_params *pool_params,
164                            const cmsis_nn_dims *input_dims,
165                            const q7_t *src,
166                            const cmsis_nn_dims *filter_dims,
167                            const cmsis_nn_dims *output_dims,
168                            q7_t *dst)
169 {
170     const int32_t input_y = input_dims->h;
171     const int32_t input_x = input_dims->w;
172     const int32_t output_y = output_dims->h;
173     const int32_t output_x = output_dims->w;
174     const int32_t stride_y = pool_params->stride.h;
175     const int32_t stride_x = pool_params->stride.w;
176     const int32_t kernel_y = filter_dims->h;
177     const int32_t kernel_x = filter_dims->w;
178     const int32_t pad_y = pool_params->padding.h;
179     const int32_t pad_x = pool_params->padding.w;
180     const int32_t act_min = pool_params->activation.min;
181     const int32_t act_max = pool_params->activation.max;
182     const int32_t channel_in = input_dims->c;
183     (void)ctx;
184     q7_t *dst_base = dst;
185 
186     for (int i_y = 0, base_idx_y = -pad_y; i_y < output_y; base_idx_y += stride_y, i_y++)
187     {
188         for (int i_x = 0, base_idx_x = -pad_x; i_x < output_x; base_idx_x += stride_x, i_x++)
189         {
190             /* Condition for kernel start dimension: (base_idx_<x,y> + kernel_<x,y>_start) >= 0 */
191             const int32_t ker_y_start = MAX(0, -base_idx_y);
192             const int32_t ker_x_start = MAX(0, -base_idx_x);
193 
194             /* Condition for kernel end dimension: (base_idx_<x,y> + kernel_<x,y>_end) < dim_src_<width,height> */
195             const int32_t kernel_y_end = MIN(kernel_y, input_y - base_idx_y);
196             const int32_t kernel_x_end = MIN(kernel_x, input_x - base_idx_x);
197 
198             int count = 0;
199 
200             for (int k_y = ker_y_start; k_y < kernel_y_end; k_y++)
201             {
202                 for (int k_x = ker_x_start; k_x < kernel_x_end; k_x++)
203                 {
204                     const q7_t *start = src + channel_in * (k_x + base_idx_x + (k_y + base_idx_y) * input_x);
205 
206                     if (count == 0)
207                     {
208                         memcpy(dst, start, channel_in);
209                         count++;
210                     }
211                     else
212                     {
213                         compare_and_replace_if_larger_q7(dst, start, channel_in);
214                     }
215                 }
216             }
217             /* 'count' is expected to be non-zero here. */
218             dst += channel_in;
219         }
220     }
221 
222     clamp_output(dst_base, output_x * output_y * channel_in, act_min, act_max);
223 
224     return ARM_MATH_SUCCESS;
225 }
226 
227 /**
228  * @} end of Pooling group
229  */
230