Commit 9fe019d7

9fe019d77c9fMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

fix(codegen): bigintfloat / floatbigint coerces to float arith

bigint_lv * 0.5 was routed through sp_bigint_mul which wrapped 0.5 with sp_bigint_new_int (truncating to 0). Detect float + bigint mixed operands at the top of the bigint dispatch and emit mrb_float arith with the bigint unboxed via sp_bigint_to_int.

Promote-mode err: -1. Default 631/0/0.

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
9fe019d77c9f
Parent
20dcd7fdd82b

spinel_codegen.rbmodifiedView file

@@ -15703,6 +15703,41 @@
1570315703 if lt != "bigint" && expr_emit_is_bigint(recv) == 1
1570415704 lt = "bigint"
1570515705 end
15706+ # When ONE side is bigint and the OTHER side is float, Ruby
15707+ # promotes the result to float. Coerce the bigint operand to
15708+ # mrb_float via sp_bigint_to_int and dispatch as float arith.
15709+ if lt == "bigint"
15710+ args_fp = @nd_arguments[nid]
15711+ arg0_fp_t = ""
15712+ if args_fp >= 0
15713+ aargs_fp = get_args(args_fp)
15714+ if aargs_fp.length > 0
15715+ arg0_fp_t = infer_type(aargs_fp[0])
15716+ end
15717+ end
15718+ recv_fp_t = infer_type(recv)
15719+ if (recv_fp_t == "float" && arg0_fp_t == "bigint") || (recv_fp_t == "bigint" && arg0_fp_t == "float") || (recv_fp_t == "float" && arg0_fp_t == "float")
15720+ rc_fp = compile_expr(recv)
15721+ arg_fp = compile_expr(aargs_fp[0])
15722+ if recv_fp_t == "bigint"
15723+ @needs_bigint = 1
15724+ rc_fp = "(mrb_float)sp_bigint_to_int((sp_Bigint *)" + rc_fp + ")"
15725+ end
15726+ if arg0_fp_t == "bigint"
15727+ @needs_bigint = 1
15728+ arg_fp = "(mrb_float)sp_bigint_to_int((sp_Bigint *)" + arg_fp + ")"
15729+ end
15730+ if mname == "+" || mname == "-" || mname == "*"
15731+ return "(" + rc_fp + " " + mname + " " + arg_fp + ")"
15732+ end
15733+ if mname == "/"
15734+ return "(" + rc_fp + " / " + arg_fp + ")"
15735+ end
15736+ if mname == "<" || mname == ">" || mname == "<=" || mname == ">=" || mname == "==" || mname == "!="
15737+ return "(" + rc_fp + " " + mname + " " + arg_fp + ")"
15738+ end
15739+ end
15740+ end
1570615741 if lt == "bigint"
1570715742 rc_raw = compile_expr(recv)
1570815743 # Cast away volatile from bigint locals (see compile_bigint_arg).