Commit 718c0ba9
718c0ba9a2ecMerged
Chris Hasiński <[email protected]> · 7 months ago · 2 files · +1 added · ~1 modified
Description
feat(codegen): add Math.tan, atan, asin, acos, log10, hypot
Math.sin, Math.cos, Math.sqrt, Math.log, Math.log2, Math.exp, and Math.atan2 are dispatched, but their close relatives are not. Calls to the missing methods returned 0 (the inferencer fell through to "int" and the codegen had no Math handler):
Math.sin(0.5) # 0.479 (works) Math.cos(0.5) # 0.878 (works) Math.tan(0.5) # was: 0 now: 0.546 Math.atan(1.0) # was: 0 now: 0.785 Math.asin(1.0) # was: 0 now: 1.5708 Math.acos(0.0) # was: 0 now: 1.5708 Math.log10(100) # was: 0 now: 2.0 Math.hypot(3, 4) # was: 0 now: 5.0
Both gaps fixed: infer_method_name_type returns "float" for the new names, and the Math constant handler in compile_call_expr forwards to libm (tan, atan, asin, acos, log10, hypot).
Also adds Math.log2 to the inference pass: it was already dispatched in compile_call_expr but missing from infer_method_name_type, so the result type was inferred as int. (Pre-existing inconsistency that this PR otherwise leaves untouched; included since we're already adjusting that table.)
Hypot remains 2-arg-only, matching CRuby (which raises ArgumentError on 1-arg hypot).
Test: bm_math.rb exercises every Math method this PR touches plus
the existing ones, with multiple inputs (0.0, 0.5, 1.0, 3.0). Compared
against CRuby via the standard make test harness.
Details
- Commit
- 718c0ba9a2ec
- Parent
- 287a6fb61aed
spinel_codegen.rbmodifiedView file
if mname == "sin" return "float" end if mname == "tan" return "float" end if mname == "acos" || mname == "asin" || mname == "atan" return "float" end if mname == "log" return "float" end if mname == "log2" return "float" end if mname == "log10" return "float" end if mname == "exp" return "float" end if mname == "atan2" return "float" end if mname == "hypot" return "float" end if mname == "freeze" if recv >= 0 return infer_type(recv) if mname == "sin" return "sin(" + compile_arg0(nid) + ")" end if mname == "tan" return "tan(" + compile_arg0(nid) + ")" end if mname == "acos" return "acos(" + compile_arg0(nid) + ")" end if mname == "asin" return "asin(" + compile_arg0(nid) + ")" end if mname == "atan" return "atan(" + compile_arg0(nid) + ")" end if mname == "log" return "log(" + compile_arg0(nid) + ")" end if mname == "log2" return "log2(" + compile_arg0(nid) + ")" end if mname == "log10" return "log10(" + compile_arg0(nid) + ")" end if mname == "exp" return "exp(" + compile_arg0(nid) + ")" end end end end if mname == "hypot" args_id = @nd_arguments[nid] if args_id >= 0 arg_ids = get_args(args_id) if arg_ids.length >= 2 return "hypot(" + compile_expr(arg_ids[0]) + ", " + compile_expr(arg_ids[1]) + ")" end end end end # File operations if rcname == "File"test/bm_math.rbaddedView file
Loading diff…