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

@@ -2510,15 +2510,30 @@
25102510 if mname == "sin"
25112511 return "float"
25122512 end
2513+ if mname == "tan"
2514+ return "float"
2515+ end
2516+ if mname == "acos" || mname == "asin" || mname == "atan"
2517+ return "float"
2518+ end
25132519 if mname == "log"
25142520 return "float"
25152521 end
2522+ if mname == "log2"
2523+ return "float"
2524+ end
2525+ if mname == "log10"
2526+ return "float"
2527+ end
25162528 if mname == "exp"
25172529 return "float"
25182530 end
25192531 if mname == "atan2"
25202532 return "float"
25212533 end
2534+ if mname == "hypot"
2535+ return "float"
2536+ end
25222537 if mname == "freeze"
25232538 if recv >= 0
25242539 return infer_type(recv)
@@ -14783,12 +14798,27 @@
1478314798 if mname == "sin"
1478414799 return "sin(" + compile_arg0(nid) + ")"
1478514800 end
14801+ if mname == "tan"
14802+ return "tan(" + compile_arg0(nid) + ")"
14803+ end
14804+ if mname == "acos"
14805+ return "acos(" + compile_arg0(nid) + ")"
14806+ end
14807+ if mname == "asin"
14808+ return "asin(" + compile_arg0(nid) + ")"
14809+ end
14810+ if mname == "atan"
14811+ return "atan(" + compile_arg0(nid) + ")"
14812+ end
1478614813 if mname == "log"
1478714814 return "log(" + compile_arg0(nid) + ")"
1478814815 end
1478914816 if mname == "log2"
1479014817 return "log2(" + compile_arg0(nid) + ")"
1479114818 end
14819+ if mname == "log10"
14820+ return "log10(" + compile_arg0(nid) + ")"
14821+ end
1479214822 if mname == "exp"
1479314823 return "exp(" + compile_arg0(nid) + ")"
1479414824 end
@@ -14801,6 +14831,15 @@
1480114831 end
1480214832 end
1480314833 end
14834+ if mname == "hypot"
14835+ args_id = @nd_arguments[nid]
14836+ if args_id >= 0
14837+ arg_ids = get_args(args_id)
14838+ if arg_ids.length >= 2
14839+ return "hypot(" + compile_expr(arg_ids[0]) + ", " + compile_expr(arg_ids[1]) + ")"
14840+ end
14841+ end
14842+ end
1480414843 end
1480514844 # File operations
1480614845 if rcname == "File"