Commit 50065350
500653501c22Merged
Ori Pekelman <[email protected]> · 6 months ago · 3 files · ~3 modified
Description
feat(codegen): Math.{sinh,cosh,tanh,asinh,acosh,atanh}
Spinel already supports the full circular family
(sin/cos/tan/asin/acos/atan/atan2/hypot) plus exp/log{,2,10}/sqrt;
the hyperbolic and inverse-hyperbolic functions are the obvious
companions — same C99 libm wrappers, same one-arg shape. Surfaced
while writing a GeLU activation in a Spinel-compiled toy
transformer that had to inline tanh(u) = (e^{2u} − 1) / (e^{2u} + 1)
because Math.tanh fell back to the silent-zero path.
- infer_method_name_type: return "float" for the six new methods.
- compile (Math receiver): emit the matching libm call.
- test/math.rb: extend the existing battery; reference values from
CRuby (
(Math.tanh(0.5) * 1000).to_i→ 462, etc.).
Suite: 307 pass, 0 fail.
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 500653501c22
- Parent
- 4c3d2e30a19e
spinel_codegen.rbmodifiedView file
@@ -4031,6 +4031,17 @@
40314031
if mname == "acos" || mname == "asin" || mname == "atan"40324032
return "float"40334033
end4034+
# Hyperbolic + inverse hyperbolic — same C99 libm wrappers as the4035+
# circular ones above. Issue: returning "float" here lets call sites4036+
# that lift `Math.tanh(x)` into a non-Float context (e.g. into an4037+
# IntArray slot via `arr[i] = Math.tanh(x)`) get caught at type-4038+
# check time instead of silently emitting `0`.4039+
if mname == "sinh" || mname == "cosh" || mname == "tanh"4040+
return "float"4041+
end4042+
if mname == "asinh" || mname == "acosh" || mname == "atanh"4043+
return "float"4044+
end40344045
if mname == "log"40354046
return "float"40364047
end@@ -22945,6 +22956,24 @@
2294522956
if mname == "atan"2294622957
return "atan(" + compile_arg0(nid) + ")"2294722958
end22959+
if mname == "sinh"22960+
return "sinh(" + compile_arg0(nid) + ")"22961+
end22962+
if mname == "cosh"22963+
return "cosh(" + compile_arg0(nid) + ")"22964+
end22965+
if mname == "tanh"22966+
return "tanh(" + compile_arg0(nid) + ")"22967+
end22968+
if mname == "asinh"22969+
return "asinh(" + compile_arg0(nid) + ")"22970+
end22971+
if mname == "acosh"22972+
return "acosh(" + compile_arg0(nid) + ")"22973+
end22974+
if mname == "atanh"22975+
return "atanh(" + compile_arg0(nid) + ")"22976+
end2294822977
if mname == "log"2294922978
return "log(" + compile_arg0(nid) + ")"2295022979
endtest/math.rbmodifiedView file
Loading diff…
test/math.rb.expectedmodifiedView file
Loading diff…