1 // Copyright 2019 Google LLC
2 //
3 // This source code is licensed under the BSD-style license found in the
4 // LICENSE file in the root directory of this source tree.
5
6 #include <assert.h>
7
8 #include <xnnpack/math.h>
9 #include <xnnpack/vunary.h>
10
11
xnn_u8_vclamp_ukernel__scalar_x4(size_t n,const uint8_t * x,uint8_t * y,const union xnn_u8_minmax_params params[restrict XNN_MIN_ELEMENTS (1)])12 void xnn_u8_vclamp_ukernel__scalar_x4(
13 size_t n,
14 const uint8_t* x,
15 uint8_t* y,
16 const union xnn_u8_minmax_params params[restrict XNN_MIN_ELEMENTS(1)])
17 {
18 assert(n != 0);
19
20 const uint32_t voutput_max = params->scalar.max;
21 const uint32_t voutput_min = params->scalar.min;
22
23 for (; n >= 4 * sizeof(uint8_t); n -= 4 * sizeof(uint8_t)) {
24 uint32_t vt0 = (uint32_t) x[0];
25 uint32_t vt1 = (uint32_t) x[1];
26 uint32_t vt2 = (uint32_t) x[2];
27 uint32_t vt3 = (uint32_t) x[3];
28 x += 4;
29
30 vt0 = math_max_u32(vt0, voutput_min);
31 vt1 = math_max_u32(vt1, voutput_min);
32 vt2 = math_max_u32(vt2, voutput_min);
33 vt3 = math_max_u32(vt3, voutput_min);
34
35 vt0 = math_min_u32(vt0, voutput_max);
36 vt1 = math_min_u32(vt1, voutput_max);
37 vt2 = math_min_u32(vt2, voutput_max);
38 vt3 = math_min_u32(vt3, voutput_max);
39
40 y[0] = (uint8_t) vt0;
41 y[1] = (uint8_t) vt1;
42 y[2] = (uint8_t) vt2;
43 y[3] = (uint8_t) vt3;
44 y += 4;
45 }
46
47 if XNN_UNLIKELY(n != 0) {
48 do {
49 uint32_t vt = (uint32_t) *x++;
50 vt = math_max_u32(vt, voutput_min);
51 vt = math_min_u32(vt, voutput_max);
52 *y++ = (uint8_t) vt;
53
54 n -= sizeof(uint8_t);
55 } while (n != 0);
56 }
57 }
58