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/clamp.h> 9 10 xnn_u8_clamp_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)])11void xnn_u8_clamp_ukernel__scalar_x4( 12 size_t n, 13 const uint8_t* x, 14 uint8_t* y, 15 const union xnn_u8_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) 16 { 17 assert(n != 0); 18 19 const uint8_t voutput_max = params->scalar.max; 20 const uint8_t voutput_min = params->scalar.min; 21 22 for (; n >= 4 * sizeof(uint8_t); n -= 4 * sizeof(uint8_t)) { 23 uint8_t vt0 = x[0]; 24 uint8_t vt1 = x[1]; 25 uint8_t vt2 = x[2]; 26 uint8_t vt3 = x[3]; 27 x += 4; 28 29 vt0 = XNN_UNPREDICTABLE(vt0 < voutput_min) ? voutput_min : vt0; 30 vt1 = XNN_UNPREDICTABLE(vt1 < voutput_min) ? voutput_min : vt1; 31 vt2 = XNN_UNPREDICTABLE(vt2 < voutput_min) ? voutput_min : vt2; 32 vt3 = XNN_UNPREDICTABLE(vt3 < voutput_min) ? voutput_min : vt3; 33 34 vt0 = XNN_UNPREDICTABLE(vt0 > voutput_max) ? voutput_max : vt0; 35 vt1 = XNN_UNPREDICTABLE(vt1 > voutput_max) ? voutput_max : vt1; 36 vt2 = XNN_UNPREDICTABLE(vt2 > voutput_max) ? voutput_max : vt2; 37 vt3 = XNN_UNPREDICTABLE(vt3 > voutput_max) ? voutput_max : vt3; 38 39 y[0] = vt0; 40 y[1] = vt1; 41 y[2] = vt2; 42 y[3] = vt3; 43 y += 4; 44 } 45 46 if XNN_UNLIKELY(n != 0) { 47 do { 48 uint8_t vt = *x++; 49 vt = XNN_UNPREDICTABLE(vt < voutput_min) ? voutput_min : vt; 50 vt = XNN_UNPREDICTABLE(vt > voutput_max) ? voutput_max : vt; 51 *y++ = vt; 52 53 n -= sizeof(uint8_t); 54 } while (n != 0); 55 } 56 } 57