xref: /aosp_15_r20/external/arm-optimized-routines/math/test/rtest/wrappers.c (revision 412f47f9e737e10ed5cc46ec6a8d7fa2264f8a14)
1*412f47f9SXin Li /*
2*412f47f9SXin Li  * wrappers.c - wrappers to modify output of MPFR/MPC test functions
3*412f47f9SXin Li  *
4*412f47f9SXin Li  * Copyright (c) 2014-2019, 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 <assert.h>
9*412f47f9SXin Li #include <stddef.h>
10*412f47f9SXin Li #include <stdint.h>
11*412f47f9SXin Li 
12*412f47f9SXin Li #include "intern.h"
13*412f47f9SXin Li 
wrapper_init(wrapperctx * ctx)14*412f47f9SXin Li void wrapper_init(wrapperctx *ctx)
15*412f47f9SXin Li {
16*412f47f9SXin Li     int i;
17*412f47f9SXin Li     ctx->nops = ctx->nresults = 0;
18*412f47f9SXin Li     for (i = 0; i < 2; i++) {
19*412f47f9SXin Li         ctx->mpfr_ops[i] = NULL;
20*412f47f9SXin Li         ctx->mpc_ops[i] = NULL;
21*412f47f9SXin Li         ctx->ieee_ops[i] = NULL;
22*412f47f9SXin Li     }
23*412f47f9SXin Li     ctx->mpfr_result = NULL;
24*412f47f9SXin Li     ctx->mpc_result = NULL;
25*412f47f9SXin Li     ctx->ieee_result = NULL;
26*412f47f9SXin Li     ctx->need_regen = 0;
27*412f47f9SXin Li }
28*412f47f9SXin Li 
wrapper_op_real(wrapperctx * ctx,const mpfr_t r,int size,const uint32 * ieee)29*412f47f9SXin Li void wrapper_op_real(wrapperctx *ctx, const mpfr_t r,
30*412f47f9SXin Li                      int size, const uint32 *ieee)
31*412f47f9SXin Li {
32*412f47f9SXin Li     assert(ctx->nops < 2);
33*412f47f9SXin Li     ctx->mpfr_ops[ctx->nops] = r;
34*412f47f9SXin Li     ctx->ieee_ops[ctx->nops] = ieee;
35*412f47f9SXin Li     ctx->size_ops[ctx->nops] = size;
36*412f47f9SXin Li     ctx->nops++;
37*412f47f9SXin Li }
38*412f47f9SXin Li 
wrapper_op_complex(wrapperctx * ctx,const mpc_t c,int size,const uint32 * ieee)39*412f47f9SXin Li void wrapper_op_complex(wrapperctx *ctx, const mpc_t c,
40*412f47f9SXin Li                         int size, const uint32 *ieee)
41*412f47f9SXin Li {
42*412f47f9SXin Li     assert(ctx->nops < 2);
43*412f47f9SXin Li     ctx->mpc_ops[ctx->nops] = c;
44*412f47f9SXin Li     ctx->ieee_ops[ctx->nops] = ieee;
45*412f47f9SXin Li     ctx->size_ops[ctx->nops] = size;
46*412f47f9SXin Li     ctx->nops++;
47*412f47f9SXin Li }
48*412f47f9SXin Li 
wrapper_result_real(wrapperctx * ctx,mpfr_t r,int size,uint32 * ieee)49*412f47f9SXin Li void wrapper_result_real(wrapperctx *ctx, mpfr_t r,
50*412f47f9SXin Li                          int size, uint32 *ieee)
51*412f47f9SXin Li {
52*412f47f9SXin Li     assert(ctx->nresults < 1);
53*412f47f9SXin Li     ctx->mpfr_result = r;
54*412f47f9SXin Li     ctx->ieee_result = ieee;
55*412f47f9SXin Li     ctx->size_result = size;
56*412f47f9SXin Li     ctx->nresults++;
57*412f47f9SXin Li }
58*412f47f9SXin Li 
wrapper_result_complex(wrapperctx * ctx,mpc_t c,int size,uint32 * ieee)59*412f47f9SXin Li void wrapper_result_complex(wrapperctx *ctx, mpc_t c,
60*412f47f9SXin Li                             int size, uint32 *ieee)
61*412f47f9SXin Li {
62*412f47f9SXin Li     assert(ctx->nresults < 1);
63*412f47f9SXin Li     ctx->mpc_result = c;
64*412f47f9SXin Li     ctx->ieee_result = ieee;
65*412f47f9SXin Li     ctx->size_result = size;
66*412f47f9SXin Li     ctx->nresults++;
67*412f47f9SXin Li }
68*412f47f9SXin Li 
wrapper_run(wrapperctx * ctx,wrapperfunc wrappers[MAXWRAPPERS])69*412f47f9SXin Li int wrapper_run(wrapperctx *ctx, wrapperfunc wrappers[MAXWRAPPERS])
70*412f47f9SXin Li {
71*412f47f9SXin Li     int i;
72*412f47f9SXin Li     for (i = 0; i < MAXWRAPPERS && wrappers[i]; i++)
73*412f47f9SXin Li         wrappers[i](ctx);
74*412f47f9SXin Li     universal_wrapper(ctx);
75*412f47f9SXin Li     return ctx->need_regen;
76*412f47f9SXin Li }
77*412f47f9SXin Li 
wrapper_get_mpfr(wrapperctx * ctx,int op)78*412f47f9SXin Li mpfr_srcptr wrapper_get_mpfr(wrapperctx *ctx, int op)
79*412f47f9SXin Li {
80*412f47f9SXin Li     if (op < 0) {
81*412f47f9SXin Li         assert(ctx->mpfr_result);
82*412f47f9SXin Li         return ctx->mpfr_result;
83*412f47f9SXin Li     } else {
84*412f47f9SXin Li         assert(ctx->mpfr_ops[op]);
85*412f47f9SXin Li         return ctx->mpfr_ops[op];
86*412f47f9SXin Li     }
87*412f47f9SXin Li }
88*412f47f9SXin Li 
wrapper_get_ieee(wrapperctx * ctx,int op)89*412f47f9SXin Li const uint32 *wrapper_get_ieee(wrapperctx *ctx, int op)
90*412f47f9SXin Li {
91*412f47f9SXin Li     if (op < 0) {
92*412f47f9SXin Li         assert(ctx->mpfr_result);
93*412f47f9SXin Li         return ctx->ieee_result;
94*412f47f9SXin Li     } else {
95*412f47f9SXin Li         assert(ctx->mpfr_ops[op]);
96*412f47f9SXin Li         return ctx->ieee_ops[op];
97*412f47f9SXin Li     }
98*412f47f9SXin Li }
99*412f47f9SXin Li 
wrapper_get_nops(wrapperctx * ctx)100*412f47f9SXin Li int wrapper_get_nops(wrapperctx *ctx)
101*412f47f9SXin Li {
102*412f47f9SXin Li     return ctx->nops;
103*412f47f9SXin Li }
104*412f47f9SXin Li 
wrapper_get_size(wrapperctx * ctx,int op)105*412f47f9SXin Li int wrapper_get_size(wrapperctx *ctx, int op)
106*412f47f9SXin Li {
107*412f47f9SXin Li     if (op < 0) {
108*412f47f9SXin Li         assert(ctx->mpfr_result || ctx->mpc_result);
109*412f47f9SXin Li         return ctx->size_result;
110*412f47f9SXin Li     } else {
111*412f47f9SXin Li         assert(ctx->mpfr_ops[op] || ctx->mpc_ops[op]);
112*412f47f9SXin Li         return ctx->size_ops[op];
113*412f47f9SXin Li     }
114*412f47f9SXin Li }
115*412f47f9SXin Li 
wrapper_is_complex(wrapperctx * ctx,int op)116*412f47f9SXin Li int wrapper_is_complex(wrapperctx *ctx, int op)
117*412f47f9SXin Li {
118*412f47f9SXin Li     if (op < 0) {
119*412f47f9SXin Li         assert(ctx->mpfr_result || ctx->mpc_result);
120*412f47f9SXin Li         return ctx->mpc_result != NULL;
121*412f47f9SXin Li     } else {
122*412f47f9SXin Li         assert(ctx->mpfr_ops[op] || ctx->mpc_ops[op]);
123*412f47f9SXin Li         return ctx->mpc_ops[op] != NULL;
124*412f47f9SXin Li     }
125*412f47f9SXin Li }
126*412f47f9SXin Li 
wrapper_get_mpc(wrapperctx * ctx,int op)127*412f47f9SXin Li mpc_srcptr wrapper_get_mpc(wrapperctx *ctx, int op)
128*412f47f9SXin Li {
129*412f47f9SXin Li     if (op < 0) {
130*412f47f9SXin Li         assert(ctx->mpc_result);
131*412f47f9SXin Li         return ctx->mpc_result;
132*412f47f9SXin Li     } else {
133*412f47f9SXin Li         assert(ctx->mpc_ops[op]);
134*412f47f9SXin Li         return ctx->mpc_ops[op];
135*412f47f9SXin Li     }
136*412f47f9SXin Li }
137*412f47f9SXin Li 
wrapper_get_mpfr_r(wrapperctx * ctx,int op)138*412f47f9SXin Li mpfr_srcptr wrapper_get_mpfr_r(wrapperctx *ctx, int op)
139*412f47f9SXin Li {
140*412f47f9SXin Li     if (op < 0) {
141*412f47f9SXin Li         assert(ctx->mpc_result);
142*412f47f9SXin Li         return mpc_realref(ctx->mpc_result);
143*412f47f9SXin Li     } else {
144*412f47f9SXin Li         assert(ctx->mpc_ops[op]);
145*412f47f9SXin Li         return mpc_realref(ctx->mpc_ops[op]);
146*412f47f9SXin Li     }
147*412f47f9SXin Li }
148*412f47f9SXin Li 
wrapper_get_mpfr_i(wrapperctx * ctx,int op)149*412f47f9SXin Li mpfr_srcptr wrapper_get_mpfr_i(wrapperctx *ctx, int op)
150*412f47f9SXin Li {
151*412f47f9SXin Li     if (op < 0) {
152*412f47f9SXin Li         assert(ctx->mpc_result);
153*412f47f9SXin Li         return mpc_imagref(ctx->mpc_result);
154*412f47f9SXin Li     } else {
155*412f47f9SXin Li         assert(ctx->mpc_ops[op]);
156*412f47f9SXin Li         return mpc_imagref(ctx->mpc_ops[op]);
157*412f47f9SXin Li     }
158*412f47f9SXin Li }
159*412f47f9SXin Li 
wrapper_get_ieee_r(wrapperctx * ctx,int op)160*412f47f9SXin Li const uint32 *wrapper_get_ieee_r(wrapperctx *ctx, int op)
161*412f47f9SXin Li {
162*412f47f9SXin Li     if (op < 0) {
163*412f47f9SXin Li         assert(ctx->mpc_result);
164*412f47f9SXin Li         return ctx->ieee_result;
165*412f47f9SXin Li     } else {
166*412f47f9SXin Li         assert(ctx->mpc_ops[op]);
167*412f47f9SXin Li         return ctx->ieee_ops[op];
168*412f47f9SXin Li     }
169*412f47f9SXin Li }
170*412f47f9SXin Li 
wrapper_get_ieee_i(wrapperctx * ctx,int op)171*412f47f9SXin Li const uint32 *wrapper_get_ieee_i(wrapperctx *ctx, int op)
172*412f47f9SXin Li {
173*412f47f9SXin Li     if (op < 0) {
174*412f47f9SXin Li         assert(ctx->mpc_result);
175*412f47f9SXin Li         return ctx->ieee_result + 4;
176*412f47f9SXin Li     } else {
177*412f47f9SXin Li         assert(ctx->mpc_ops[op]);
178*412f47f9SXin Li         return ctx->ieee_ops[op] + 2;
179*412f47f9SXin Li     }
180*412f47f9SXin Li }
181*412f47f9SXin Li 
wrapper_set_sign(wrapperctx * ctx,uint32 sign)182*412f47f9SXin Li void wrapper_set_sign(wrapperctx *ctx, uint32 sign)
183*412f47f9SXin Li {
184*412f47f9SXin Li     assert(ctx->mpfr_result);
185*412f47f9SXin Li     ctx->ieee_result[0] |= (sign & 0x80000000U);
186*412f47f9SXin Li }
187*412f47f9SXin Li 
wrapper_set_sign_r(wrapperctx * ctx,uint32 sign)188*412f47f9SXin Li void wrapper_set_sign_r(wrapperctx *ctx, uint32 sign)
189*412f47f9SXin Li {
190*412f47f9SXin Li     assert(ctx->mpc_result);
191*412f47f9SXin Li     ctx->ieee_result[0] |= (sign & 0x80000000U);
192*412f47f9SXin Li }
193*412f47f9SXin Li 
wrapper_set_sign_i(wrapperctx * ctx,uint32 sign)194*412f47f9SXin Li void wrapper_set_sign_i(wrapperctx *ctx, uint32 sign)
195*412f47f9SXin Li {
196*412f47f9SXin Li     assert(ctx->mpc_result);
197*412f47f9SXin Li     ctx->ieee_result[4] |= (sign & 0x80000000U);
198*412f47f9SXin Li }
199*412f47f9SXin Li 
wrapper_set_nan(wrapperctx * ctx)200*412f47f9SXin Li void wrapper_set_nan(wrapperctx *ctx)
201*412f47f9SXin Li {
202*412f47f9SXin Li     assert(ctx->mpfr_result);
203*412f47f9SXin Li     mpfr_set_nan(ctx->mpfr_result);
204*412f47f9SXin Li     ctx->need_regen = 1;
205*412f47f9SXin Li }
206*412f47f9SXin Li 
wrapper_set_nan_r(wrapperctx * ctx)207*412f47f9SXin Li void wrapper_set_nan_r(wrapperctx *ctx)
208*412f47f9SXin Li {
209*412f47f9SXin Li     assert(ctx->mpc_result);
210*412f47f9SXin Li     mpfr_set_nan(mpc_realref(ctx->mpc_result)); /* FIXME: better way? */
211*412f47f9SXin Li     ctx->need_regen = 1;
212*412f47f9SXin Li }
213*412f47f9SXin Li 
wrapper_set_nan_i(wrapperctx * ctx)214*412f47f9SXin Li void wrapper_set_nan_i(wrapperctx *ctx)
215*412f47f9SXin Li {
216*412f47f9SXin Li     assert(ctx->mpc_result);
217*412f47f9SXin Li     mpfr_set_nan(mpc_imagref(ctx->mpc_result)); /* FIXME: better way? */
218*412f47f9SXin Li     ctx->need_regen = 1;
219*412f47f9SXin Li }
220*412f47f9SXin Li 
wrapper_set_int(wrapperctx * ctx,int val)221*412f47f9SXin Li void wrapper_set_int(wrapperctx *ctx, int val)
222*412f47f9SXin Li {
223*412f47f9SXin Li     assert(ctx->mpfr_result);
224*412f47f9SXin Li     mpfr_set_si(ctx->mpfr_result, val, GMP_RNDN);
225*412f47f9SXin Li     ctx->need_regen = 1;
226*412f47f9SXin Li }
227*412f47f9SXin Li 
wrapper_set_int_r(wrapperctx * ctx,int val)228*412f47f9SXin Li void wrapper_set_int_r(wrapperctx *ctx, int val)
229*412f47f9SXin Li {
230*412f47f9SXin Li     assert(ctx->mpc_result);
231*412f47f9SXin Li     mpfr_set_si(mpc_realref(ctx->mpc_result), val, GMP_RNDN);
232*412f47f9SXin Li     ctx->need_regen = 1;
233*412f47f9SXin Li }
234*412f47f9SXin Li 
wrapper_set_int_i(wrapperctx * ctx,int val)235*412f47f9SXin Li void wrapper_set_int_i(wrapperctx *ctx, int val)
236*412f47f9SXin Li {
237*412f47f9SXin Li     assert(ctx->mpc_result);
238*412f47f9SXin Li     mpfr_set_si(mpc_realref(ctx->mpc_result), val, GMP_RNDN);
239*412f47f9SXin Li     ctx->need_regen = 1;
240*412f47f9SXin Li }
241*412f47f9SXin Li 
wrapper_set_mpfr(wrapperctx * ctx,const mpfr_t val)242*412f47f9SXin Li void wrapper_set_mpfr(wrapperctx *ctx, const mpfr_t val)
243*412f47f9SXin Li {
244*412f47f9SXin Li     assert(ctx->mpfr_result);
245*412f47f9SXin Li     mpfr_set(ctx->mpfr_result, val, GMP_RNDN);
246*412f47f9SXin Li     ctx->need_regen = 1;
247*412f47f9SXin Li }
248*412f47f9SXin Li 
wrapper_set_mpfr_r(wrapperctx * ctx,const mpfr_t val)249*412f47f9SXin Li void wrapper_set_mpfr_r(wrapperctx *ctx, const mpfr_t val)
250*412f47f9SXin Li {
251*412f47f9SXin Li     assert(ctx->mpc_result);
252*412f47f9SXin Li     mpfr_set(mpc_realref(ctx->mpc_result), val, GMP_RNDN);
253*412f47f9SXin Li     ctx->need_regen = 1;
254*412f47f9SXin Li }
255*412f47f9SXin Li 
wrapper_set_mpfr_i(wrapperctx * ctx,const mpfr_t val)256*412f47f9SXin Li void wrapper_set_mpfr_i(wrapperctx *ctx, const mpfr_t val)
257*412f47f9SXin Li {
258*412f47f9SXin Li     assert(ctx->mpc_result);
259*412f47f9SXin Li     mpfr_set(mpc_realref(ctx->mpc_result), val, GMP_RNDN);
260*412f47f9SXin Li     ctx->need_regen = 1;
261*412f47f9SXin Li }
262