1 // Copyright 2020 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 #include <math.h> 8 #include <stddef.h> 9 #include <stdint.h> 10 11 #include <xnnpack/common.h> 12 #include <xnnpack/math-stubs.h> 13 14 xnn_math_f32_roundz__scalar_cvt(size_t n,const float * input,float * output)15void xnn_math_f32_roundz__scalar_cvt( 16 size_t n, 17 const float* input, 18 float* output) 19 { 20 assert(n % sizeof(float) == 0); 21 22 // Threshold of non-integral values in single-precision floating-point representation. 23 // All inputs above this threshold (by absolute value) are integer numbers. 24 const float vintegral_threshold = 0x1.000000p+23f; 25 26 for (; n != 0; n -= sizeof(float)) { 27 const float vx = *input++; 28 29 // Convert floating-point value x to integer, with rounding towards zero, and then back to floating-point. 30 // Note: the result is valid only for abs(x) < 2**31, but we further restrict its use to 2**23. 31 const float vrndx = (float) (int32_t) vx; 32 // Compute abs(x) to check if the FP->INT->FP conversion result is valid. 33 const float vabsx = fabsf(vx); 34 35 // Select between the x rounded via FP->INT->FP conversion and the original x value. 36 const float vprey = XNN_UNPREDICTABLE(vabsx < vintegral_threshold) ? vrndx : vx; 37 // Restore the sign of -0.0f lost in the FP->INT->FP conversion. 38 const float vy = copysignf(vprey, vx); 39 40 *output++ = vy; 41 } 42 } 43