1*412f47f9SXin Li /*
2*412f47f9SXin Li * Single-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 uint32x4_t tiny_bound, thres;
16*412f47f9SXin Li } data = {
17*412f47f9SXin Li .tiny_bound = V4 (0x20000000), /* asuint (0x1p-63). */
18*412f47f9SXin Li .thres = V4 (0x3f000000), /* asuint (0x1p63) - tiny_bound. */
19*412f47f9SXin Li };
20*412f47f9SXin Li #else
21*412f47f9SXin Li static const struct data
22*412f47f9SXin Li {
23*412f47f9SXin Li uint32x4_t tiny_bound;
24*412f47f9SXin Li uint16x8_t thres;
25*412f47f9SXin Li } data = {
26*412f47f9SXin Li .tiny_bound = V4 (0x0C800000), /* asuint (0x1p-102). */
27*412f47f9SXin Li .thres = V8 (0x7300), /* asuint (inf) - tiny_bound. */
28*412f47f9SXin Li };
29*412f47f9SXin Li #endif
30*412f47f9SXin Li
31*412f47f9SXin Li static float32x4_t VPCS_ATTR NOINLINE
special_case(float32x4_t x,float32x4_t y,float32x4_t sqsum,uint16x4_t special)32*412f47f9SXin Li special_case (float32x4_t x, float32x4_t y, float32x4_t sqsum,
33*412f47f9SXin Li uint16x4_t special)
34*412f47f9SXin Li {
35*412f47f9SXin Li return v_call2_f32 (hypotf, x, y, vsqrtq_f32 (sqsum), vmovl_u16 (special));
36*412f47f9SXin Li }
37*412f47f9SXin Li
38*412f47f9SXin Li /* Vector implementation of single-precision hypot.
39*412f47f9SXin Li Maximum error observed is 1.21 ULP:
40*412f47f9SXin Li _ZGVnN4vv_hypotf (0x1.6a419cp-13, 0x1.82a852p-22) got 0x1.6a41d2p-13
41*412f47f9SXin Li want 0x1.6a41dp-13. */
42*412f47f9SXin Li #if WANT_SIMD_EXCEPT
43*412f47f9SXin Li
V_NAME_F2(hypot)44*412f47f9SXin Li float32x4_t VPCS_ATTR V_NAME_F2 (hypot) (float32x4_t x, float32x4_t y)
45*412f47f9SXin Li {
46*412f47f9SXin Li const struct data *d = ptr_barrier (&data);
47*412f47f9SXin Li
48*412f47f9SXin Li float32x4_t ax = vabsq_f32 (x);
49*412f47f9SXin Li float32x4_t ay = vabsq_f32 (y);
50*412f47f9SXin Li
51*412f47f9SXin Li uint32x4_t ix = vreinterpretq_u32_f32 (ax);
52*412f47f9SXin Li uint32x4_t iy = vreinterpretq_u32_f32 (ay);
53*412f47f9SXin Li
54*412f47f9SXin Li /* Extreme values, NaNs, and infinities should be handled by the scalar
55*412f47f9SXin Li fallback for correct flag handling. */
56*412f47f9SXin Li uint32x4_t specialx = vcgeq_u32 (vsubq_u32 (ix, d->tiny_bound), d->thres);
57*412f47f9SXin Li uint32x4_t specialy = vcgeq_u32 (vsubq_u32 (iy, d->tiny_bound), d->thres);
58*412f47f9SXin Li ax = v_zerofy_f32 (ax, specialx);
59*412f47f9SXin Li ay = v_zerofy_f32 (ay, specialy);
60*412f47f9SXin Li uint16x4_t special = vaddhn_u32 (specialx, specialy);
61*412f47f9SXin Li
62*412f47f9SXin Li float32x4_t sqsum = vfmaq_f32 (vmulq_f32 (ax, ax), ay, ay);
63*412f47f9SXin Li
64*412f47f9SXin Li if (unlikely (v_any_u16h (special)))
65*412f47f9SXin Li return special_case (x, y, sqsum, special);
66*412f47f9SXin Li
67*412f47f9SXin Li return vsqrtq_f32 (sqsum);
68*412f47f9SXin Li }
69*412f47f9SXin Li #else
70*412f47f9SXin Li
V_NAME_F2(hypot)71*412f47f9SXin Li float32x4_t VPCS_ATTR V_NAME_F2 (hypot) (float32x4_t x, float32x4_t y)
72*412f47f9SXin Li {
73*412f47f9SXin Li const struct data *d = ptr_barrier (&data);
74*412f47f9SXin Li
75*412f47f9SXin Li float32x4_t sqsum = vfmaq_f32 (vmulq_f32 (x, x), y, y);
76*412f47f9SXin Li
77*412f47f9SXin Li uint16x4_t special = vcge_u16 (
78*412f47f9SXin Li vsubhn_u32 (vreinterpretq_u32_f32 (sqsum), d->tiny_bound),
79*412f47f9SXin Li vget_low_u16 (d->thres));
80*412f47f9SXin Li
81*412f47f9SXin Li if (unlikely (v_any_u16h (special)))
82*412f47f9SXin Li return special_case (x, y, sqsum, special);
83*412f47f9SXin Li
84*412f47f9SXin Li return vsqrtq_f32 (sqsum);
85*412f47f9SXin Li }
86*412f47f9SXin Li #endif
87*412f47f9SXin Li
88*412f47f9SXin Li PL_SIG (V, F, 2, hypot, -10.0, 10.0)
89*412f47f9SXin Li PL_TEST_ULP (V_NAME_F2 (hypot), 1.21)
90*412f47f9SXin Li PL_TEST_EXPECT_FENV (V_NAME_F2 (hypot), WANT_SIMD_EXCEPT)
91*412f47f9SXin Li PL_TEST_INTERVAL2 (V_NAME_F2 (hypot), 0, inf, 0, inf, 10000)
92*412f47f9SXin Li PL_TEST_INTERVAL2 (V_NAME_F2 (hypot), 0, inf, -0, -inf, 10000)
93*412f47f9SXin Li PL_TEST_INTERVAL2 (V_NAME_F2 (hypot), -0, -inf, 0, inf, 10000)
94*412f47f9SXin Li PL_TEST_INTERVAL2 (V_NAME_F2 (hypot), -0, -inf, -0, -inf, 10000)
95