xref: /aosp_15_r20/external/XNNPACK/src/math/sqrt-u32-scalar-cvtu32-sqrtf-lrintf.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1 // Copyright 2022 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 <stddef.h>
8 #include <math.h>
9 
10 #include <xnnpack/common.h>
11 #include <xnnpack/math-stubs.h>
12 
13 
xnn_math_u32_sqrt__scalar_cvtu32_sqrtf_lrintf(size_t n,const uint32_t * input,uint32_t * output)14 void xnn_math_u32_sqrt__scalar_cvtu32_sqrtf_lrintf(
15     size_t n,
16     const uint32_t* input,
17     uint32_t* output)
18 {
19   assert(n % sizeof(uint32_t) == 0);
20 
21   for (; n != 0; n -= sizeof(uint32_t)) {
22     const uint32_t vx = *input++;
23 
24     uint32_t vy = vx;
25     if XNN_LIKELY(vx != 0) {
26       float vf = (float) vx;
27       vf = sqrtf(vf);
28       vy = (uint32_t) (int32_t) lrintf(vf);
29       const uint32_t vsquared_y_less_x = vy * vy - vx;
30       if XNN_UNPREDICTABLE((int32_t) (vsquared_y_less_x + vy) < 0) {
31         vy += 1;
32       } else if XNN_UNPREDICTABLE((int32_t) (vsquared_y_less_x - vy) >= 0) {
33         vy -= 1;
34       }
35     }
36 
37     *output++ = vy;
38   }
39 }
40