xref: /aosp_15_r20/external/arm-optimized-routines/pl/math/v_hypot_1u5.c (revision 412f47f9e737e10ed5cc46ec6a8d7fa2264f8a14)
1*412f47f9SXin Li /*
2*412f47f9SXin Li  * Double-precision vector hypot(x) function.
3*412f47f9SXin Li  *
4*412f47f9SXin Li  * Copyright (c) 2023, Arm Limited.
5*412f47f9SXin Li  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6*412f47f9SXin Li  */
7*412f47f9SXin Li 
8*412f47f9SXin Li #include "v_math.h"
9*412f47f9SXin Li #include "pl_sig.h"
10*412f47f9SXin Li #include "pl_test.h"
11*412f47f9SXin Li 
12*412f47f9SXin Li #if WANT_SIMD_EXCEPT
13*412f47f9SXin Li static const struct data
14*412f47f9SXin Li {
15*412f47f9SXin Li   uint64x2_t tiny_bound, thres;
16*412f47f9SXin Li } data = {
17*412f47f9SXin Li   .tiny_bound = V2 (0x2000000000000000), /* asuint (0x1p-511).  */
18*412f47f9SXin Li   .thres = V2 (0x3fe0000000000000), /* asuint (0x1p511) - tiny_bound.  */
19*412f47f9SXin Li };
20*412f47f9SXin Li #else
21*412f47f9SXin Li static const struct data
22*412f47f9SXin Li {
23*412f47f9SXin Li   uint64x2_t tiny_bound;
24*412f47f9SXin Li   uint32x4_t thres;
25*412f47f9SXin Li } data = {
26*412f47f9SXin Li   .tiny_bound = V2 (0x0360000000000000), /* asuint (0x1p-969).  */
27*412f47f9SXin Li   .thres = V4 (0x7c900000),	 /* asuint (inf) - tiny_bound.  */
28*412f47f9SXin Li };
29*412f47f9SXin Li #endif
30*412f47f9SXin Li 
31*412f47f9SXin Li static float64x2_t VPCS_ATTR NOINLINE
special_case(float64x2_t x,float64x2_t y,float64x2_t sqsum,uint32x2_t special)32*412f47f9SXin Li special_case (float64x2_t x, float64x2_t y, float64x2_t sqsum,
33*412f47f9SXin Li 	      uint32x2_t special)
34*412f47f9SXin Li {
35*412f47f9SXin Li   return v_call2_f64 (hypot, x, y, vsqrtq_f64 (sqsum), vmovl_u32 (special));
36*412f47f9SXin Li }
37*412f47f9SXin Li 
38*412f47f9SXin Li /* Vector implementation of double-precision hypot.
39*412f47f9SXin Li    Maximum error observed is 1.21 ULP:
40*412f47f9SXin Li    _ZGVnN2vv_hypot (0x1.6a1b193ff85b5p-204, 0x1.bc50676c2a447p-222)
41*412f47f9SXin Li     got 0x1.6a1b19400964ep-204
42*412f47f9SXin Li    want 0x1.6a1b19400964dp-204.  */
43*412f47f9SXin Li #if WANT_SIMD_EXCEPT
44*412f47f9SXin Li 
V_NAME_D2(hypot)45*412f47f9SXin Li float64x2_t VPCS_ATTR V_NAME_D2 (hypot) (float64x2_t x, float64x2_t y)
46*412f47f9SXin Li {
47*412f47f9SXin Li   const struct data *d = ptr_barrier (&data);
48*412f47f9SXin Li 
49*412f47f9SXin Li   float64x2_t ax = vabsq_f64 (x);
50*412f47f9SXin Li   float64x2_t ay = vabsq_f64 (y);
51*412f47f9SXin Li 
52*412f47f9SXin Li   uint64x2_t ix = vreinterpretq_u64_f64 (ax);
53*412f47f9SXin Li   uint64x2_t iy = vreinterpretq_u64_f64 (ay);
54*412f47f9SXin Li 
55*412f47f9SXin Li   /* Extreme values, NaNs, and infinities should be handled by the scalar
56*412f47f9SXin Li      fallback for correct flag handling.  */
57*412f47f9SXin Li   uint64x2_t specialx = vcgeq_u64 (vsubq_u64 (ix, d->tiny_bound), d->thres);
58*412f47f9SXin Li   uint64x2_t specialy = vcgeq_u64 (vsubq_u64 (iy, d->tiny_bound), d->thres);
59*412f47f9SXin Li   ax = v_zerofy_f64 (ax, specialx);
60*412f47f9SXin Li   ay = v_zerofy_f64 (ay, specialy);
61*412f47f9SXin Li   uint32x2_t special = vaddhn_u64 (specialx, specialy);
62*412f47f9SXin Li 
63*412f47f9SXin Li   float64x2_t sqsum = vfmaq_f64 (vmulq_f64 (ax, ax), ay, ay);
64*412f47f9SXin Li 
65*412f47f9SXin Li   if (unlikely (v_any_u32h (special)))
66*412f47f9SXin Li     return special_case (x, y, sqsum, special);
67*412f47f9SXin Li 
68*412f47f9SXin Li   return vsqrtq_f64 (sqsum);
69*412f47f9SXin Li }
70*412f47f9SXin Li #else
71*412f47f9SXin Li 
V_NAME_D2(hypot)72*412f47f9SXin Li float64x2_t VPCS_ATTR V_NAME_D2 (hypot) (float64x2_t x, float64x2_t y)
73*412f47f9SXin Li {
74*412f47f9SXin Li   const struct data *d = ptr_barrier (&data);
75*412f47f9SXin Li 
76*412f47f9SXin Li   float64x2_t sqsum = vfmaq_f64 (vmulq_f64 (x, x), y, y);
77*412f47f9SXin Li 
78*412f47f9SXin Li   uint32x2_t special = vcge_u32 (
79*412f47f9SXin Li       vsubhn_u64 (vreinterpretq_u64_f64 (sqsum), d->tiny_bound),
80*412f47f9SXin Li       vget_low_u32 (d->thres));
81*412f47f9SXin Li 
82*412f47f9SXin Li   if (unlikely (v_any_u32h (special)))
83*412f47f9SXin Li     return special_case (x, y, sqsum, special);
84*412f47f9SXin Li 
85*412f47f9SXin Li   return vsqrtq_f64 (sqsum);
86*412f47f9SXin Li }
87*412f47f9SXin Li #endif
88*412f47f9SXin Li 
89*412f47f9SXin Li PL_SIG (V, D, 2, hypot, -10.0, 10.0)
90*412f47f9SXin Li PL_TEST_ULP (V_NAME_D2 (hypot), 1.21)
91*412f47f9SXin Li PL_TEST_EXPECT_FENV (V_NAME_D2 (hypot), WANT_SIMD_EXCEPT)
92*412f47f9SXin Li PL_TEST_INTERVAL2 (V_NAME_D2 (hypot), 0, inf, 0, inf, 10000)
93*412f47f9SXin Li PL_TEST_INTERVAL2 (V_NAME_D2 (hypot), 0, inf, -0, -inf, 10000)
94*412f47f9SXin Li PL_TEST_INTERVAL2 (V_NAME_D2 (hypot), -0, -inf, 0, inf, 10000)
95*412f47f9SXin Li PL_TEST_INTERVAL2 (V_NAME_D2 (hypot), -0, -inf, -0, -inf, 10000)
96