• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_roundd__scalar_cvt(size_t n,const float * input,float * output)15 void xnn_math_f32_roundd__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   // Unit constant to decrement results rounded "wrong way" (i.e. up) in the round-towards-zero operation.
26   const float vone = 1.0f;
27 
28   for (; n != 0; n -= sizeof(float)) {
29     const float vx = *input++;
30 
31     // Convert floating-point value x to integer, with rounding towards zero, and then back to floating-point.
32     // Note: the result is valid only for abs(x) < 2**31, but we further restrict its use to 2**23.
33     const float vprerndx = (float) (int32_t) vx;
34     // Compute abs(x) to check if the FP->INT->FP conversion result is valid.
35     const float vabsx = fabsf(vx);
36 
37     // Select between the x rounded via FP->INT->FP conversion and the original x value.
38     const float vrndx = XNN_UNPREDICTABLE(vabsx < vintegral_threshold) ? vprerndx : vx;
39 
40     // Restore the sign of -0.0f lost in the FP->INT->FP conversion.
41     const float vadjrndx = copysignf(vrndx, vx);
42 
43     // Adjust x rounded towards zero to get x rounded down.
44     // Note: addition implicitly converts SNaN inputs to QNaNs.
45     const float vy = XNN_UNPREDICTABLE(vrndx <= vx) ? vadjrndx : vrndx - vone;
46 
47     *output++ = vy;
48   }
49 }
50