1 // clang-format off
2 /*
3  * Function wrappers for ulp.
4  *
5  * Copyright (c) 2022-2023, Arm Limited.
6  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
7  */
8 
9 #include <stdbool.h>
10 
11 #if USE_MPFR
12 static int sincos_mpfr_sin(mpfr_t y, const mpfr_t x, mpfr_rnd_t r) {
13   mpfr_cos(y, x, r);
14   return mpfr_sin(y, x, r);
15 }
16 static int sincos_mpfr_cos(mpfr_t y, const mpfr_t x, mpfr_rnd_t r) {
17   mpfr_sin(y, x, r);
18   return mpfr_cos(y, x, r);
19 }
20 static int wrap_mpfr_powi(mpfr_t ret, const mpfr_t x, const mpfr_t y, mpfr_rnd_t rnd) {
21   mpfr_t y2;
22   mpfr_init(y2);
23   mpfr_trunc(y2, y);
24   return mpfr_pow(ret, x, y2, rnd);
25 }
26 #endif
27 
28 /* Our implementations of powi/powk are too imprecise to verify
29    against any established pow implementation. Instead we have the
30    following simple implementation, against which it is enough to
31    maintain bitwise reproducibility. Note the test framework expects
32    the reference impl to be of higher precision than the function
33    under test. For instance this means that the reference for
34    double-precision powi will be passed a long double, so to check
35    bitwise reproducibility we have to cast it back down to
36    double. This is fine since a round-trip to higher precision and
37    back down is correctly rounded.  */
38 #define DECL_POW_INT_REF(NAME, DBL_T, FLT_T, INT_T)                            \
39   static DBL_T NAME (DBL_T in_val, DBL_T y)                                    \
40   {                                                                            \
41     INT_T n = (INT_T) round (y);                                               \
42     FLT_T acc = 1.0;                                                           \
43     bool want_recip = n < 0;                                                   \
44     n = n < 0 ? -n : n;                                                        \
45                                                                                \
46     for (FLT_T c = in_val; n; c *= c, n >>= 1)                                 \
47       {                                                                        \
48         if (n & 0x1)                                                           \
49           {                                                                    \
50             acc *= c;                                                          \
51           }                                                                    \
52       }                                                                        \
53     if (want_recip)                                                            \
54       {                                                                        \
55         acc = 1.0 / acc;                                                       \
56       }                                                                        \
57     return acc;                                                                \
58   }
59 
60 DECL_POW_INT_REF(ref_powif, double, float, int)
61 DECL_POW_INT_REF(ref_powi, long double, double, int)
62 
63 #define VF1_WRAP(func) static float v_##func##f(float x) { return __v_##func##f(argf(x))[0]; }
64 #define VF2_WRAP(func) static float v_##func##f(float x, float y) { return __v_##func##f(argf(x), argf(y))[0]; }
65 #define VD1_WRAP(func) static double v_##func(double x) { return __v_##func(argd(x))[0]; }
66 #define VD2_WRAP(func) static double v_##func(double x, double y) { return __v_##func(argd(x), argd(y))[0]; }
67 
68 #define VNF1_WRAP(func) static float vn_##func##f(float x) { return __vn_##func##f(argf(x))[0]; }
69 #define VNF2_WRAP(func) static float vn_##func##f(float x, float y) { return __vn_##func##f(argf(x), argf(y))[0]; }
70 #define VND1_WRAP(func) static double vn_##func(double x) { return __vn_##func(argd(x))[0]; }
71 #define VND2_WRAP(func) static double vn_##func(double x, double y) { return __vn_##func(argd(x), argd(y))[0]; }
72 
73 #define ZVF1_WRAP(func) static float Z_##func##f(float x) { return _ZGVnN4v_##func##f(argf(x))[0]; }
74 #define ZVF2_WRAP(func) static float Z_##func##f(float x, float y) { return _ZGVnN4vv_##func##f(argf(x), argf(y))[0]; }
75 #define ZVD1_WRAP(func) static double Z_##func(double x) { return _ZGVnN2v_##func(argd(x))[0]; }
76 #define ZVD2_WRAP(func) static double Z_##func(double x, double y) { return _ZGVnN2vv_##func(argd(x), argd(y))[0]; }
77 
78 #ifdef __vpcs
79 
80 #define ZVNF1_WRAP(func) VF1_WRAP(func) VNF1_WRAP(func) ZVF1_WRAP(func)
81 #define ZVNF2_WRAP(func) VF2_WRAP(func) VNF2_WRAP(func) ZVF2_WRAP(func)
82 #define ZVND1_WRAP(func) VD1_WRAP(func) VND1_WRAP(func) ZVD1_WRAP(func)
83 #define ZVND2_WRAP(func) VD2_WRAP(func) VND2_WRAP(func) ZVD2_WRAP(func)
84 
85 #elif __aarch64__
86 
87 #define ZVNF1_WRAP(func) VF1_WRAP(func) VNF1_WRAP(func)
88 #define ZVNF2_WRAP(func) VF2_WRAP(func) VNF2_WRAP(func)
89 #define ZVND1_WRAP(func) VD1_WRAP(func) VND1_WRAP(func)
90 #define ZVND2_WRAP(func) VD2_WRAP(func) VND2_WRAP(func)
91 
92 #elif WANT_VMATH
93 
94 #define ZVNF1_WRAP(func) VF1_WRAP(func)
95 #define ZVNF2_WRAP(func) VF2_WRAP(func)
96 #define ZVND1_WRAP(func) VD1_WRAP(func)
97 #define ZVND2_WRAP(func) VD2_WRAP(func)
98 
99 #else
100 
101 #define ZVNF1_WRAP(func)
102 #define ZVNF2_WRAP(func)
103 #define ZVND1_WRAP(func)
104 #define ZVND2_WRAP(func)
105 
106 #endif
107 
108 #define SVF1_WRAP(func) static float sv_##func##f(float x) { return svretf(__sv_##func##f_x(svargf(x), svptrue_b32())); }
109 #define SVF2_WRAP(func) static float sv_##func##f(float x, float y) { return svretf(__sv_##func##f_x(svargf(x), svargf(y), svptrue_b32())); }
110 #define SVD1_WRAP(func) static double sv_##func(double x) { return svretd(__sv_##func##_x(svargd(x), svptrue_b64())); }
111 #define SVD2_WRAP(func) static double sv_##func(double x, double y) { return svretd(__sv_##func##_x(svargd(x), svargd(y), svptrue_b64())); }
112 
113 #define ZSVF1_WRAP(func) static float Z_sv_##func##f(float x) { return svretf(_ZGVsMxv_##func##f(svargf(x), svptrue_b32())); }
114 #define ZSVF2_WRAP(func) static float Z_sv_##func##f(float x, float y) { return svretf(_ZGVsMxvv_##func##f(svargf(x), svargf(y), svptrue_b32())); }
115 #define ZSVD1_WRAP(func) static double Z_sv_##func(double x) { return svretd(_ZGVsMxv_##func(svargd(x), svptrue_b64())); }
116 #define ZSVD2_WRAP(func) static double Z_sv_##func(double x, double y) { return svretd(_ZGVsMxvv_##func(svargd(x), svargd(y), svptrue_b64())); }
117 
118 #if WANT_SVE_MATH
119 
120 #define ZSVNF1_WRAP(func) SVF1_WRAP(func) ZSVF1_WRAP(func)
121 #define ZSVNF2_WRAP(func) SVF2_WRAP(func) ZSVF2_WRAP(func)
122 #define ZSVND1_WRAP(func) SVD1_WRAP(func) ZSVD1_WRAP(func)
123 #define ZSVND2_WRAP(func) SVD2_WRAP(func) ZSVD2_WRAP(func)
124 
125 #else
126 
127 #define ZSVNF1_WRAP(func)
128 #define ZSVNF2_WRAP(func)
129 #define ZSVND1_WRAP(func)
130 #define ZSVND2_WRAP(func)
131 
132 #endif
133 
134 /* No wrappers for scalar routines, but PL_SIG will emit them.  */
135 #define ZSNF1_WRAP(func)
136 #define ZSNF2_WRAP(func)
137 #define ZSND1_WRAP(func)
138 #define ZSND2_WRAP(func)
139 
140 #include "ulp_wrappers_gen.h"
141 
142 #if WANT_SVE_MATH
143 static float Z_sv_powi(float x, float y) { return svretf(_ZGVsMxvv_powi(svargf(x), svdup_n_s32((int)round(y)), svptrue_b32())); }
144 static float sv_powif(float x, float y) { return svretf(__sv_powif_x(svargf(x), svdup_n_s32((int)round(y)), svptrue_b32())); }
145 static double Z_sv_powk(double x, double y) { return svretd(_ZGVsMxvv_powk(svargd(x), svdup_n_s64((long)round(y)), svptrue_b64())); }
146 static double sv_powi(double x, double y) { return svretd(__sv_powi_x(svargd(x), svdup_n_s64((long)round(y)), svptrue_b64())); }
147 #endif
148 // clang-format on
149