Skip to content

Commit 392caf9

Browse files
committed
Allow the apfloat_fallback macro to have a different fallback
`as` casts are only allowed for primitives, doing the same operations with `rustc_apfloat` requires using functions. Add a way to specify these separately.
1 parent 8da74d7 commit 392caf9

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

testcrate/src/lib.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ macro_rules! apfloat_fallback {
276276
// The expression to run. This expression may use `FloatTy` for its signature.
277277
// Optionally, the final conversion back to a float can be suppressed using
278278
// `=> no_convert` (for e.g. operations that return a bool).
279-
$op:expr $(=> $convert:ident)?,
279+
$op:expr $(=> $convert:ident)? $(; $apfloat_op:expr)?,
280280
// Arguments that get passed to `$op` after converting to a float
281281
$($arg:expr),+
282282
$(,)?
@@ -292,26 +292,40 @@ macro_rules! apfloat_fallback {
292292
use rustc_apfloat::Float;
293293
type FloatTy = rustc_apfloat::ieee::$apfloat_ty;
294294

295-
let op_res = $op( $(FloatTy::from_bits($arg.to_bits().into())),+ );
296-
297-
apfloat_fallback!(@convert $float_ty, op_res $(,$convert)?)
295+
apfloat_fallback!(@inner
296+
fty: $float_ty,
297+
// Apply a conversion to `FloatTy` to each arg, then pass all args to `$op`
298+
op_res: $op( $(FloatTy::from_bits($arg.to_bits().into())),+ ),
299+
$(apfloat_op: $apfloat_op, )?
300+
$(conv_opts: $convert,)?
301+
args: $($arg),+
302+
)
298303
};
299304

300305
ret
301306
}};
302307

303308
// Operations that do not need converting back to a float
304-
(@convert $float_ty:ty, $val:expr, no_convert) => {
309+
(@inner fty: $float_ty:ty, op_res: $val:expr, conv_opts: no_convert, args: $($_arg:expr),+) => {
305310
$val
306311
};
307312

308313
// Some apfloat operations return a `StatusAnd` that we need to extract the value from. This
309314
// is the default.
310-
(@convert $float_ty:ty, $val:expr) => {{
315+
(@inner fty: $float_ty:ty, op_res: $val:expr, args: $($_arg:expr),+) => {{
311316
// ignore the status, just get the value
312317
let unwrapped = $val.value;
313318

314319
<$float_ty>::from_bits(FloatTy::to_bits(unwrapped).try_into().unwrap())
315320
}};
316321

322+
// This is the case where we can't use the same expression for the default builtin and
323+
// nonstandard apfloat fallbac (e.g. `as` casts in std are normal functions in apfloat, so
324+
// two separate expressions must be specified.
325+
(@inner
326+
fty: $float_ty:ty, op_res: $_val:expr,
327+
apfloat_op: $apfloat_op:expr, args: $($arg:expr),+
328+
) => {{
329+
$apfloat_op($($arg),+)
330+
}};
317331
}

0 commit comments

Comments
 (0)