feat(interp): Addition of math intrinsic functions#218
Conversation
WalkthroughThis change extends the intrinsic evaluator with additional math functions for Float32 and Float64 types, including inverse trigonometric functions (asin, acos, atan, atan2), hyperbolic functions (sinh, cosh, tanh), tan, floor, ceil, pow, min, and max, each with arity validation. ChangesMath Intrinsics Extension
Estimated code review effort: 2 (Simple) | ~10 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
sarek/interp/Sarek_ir_interp_intrinsics.ml (2)
361-398: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueMinor: inconsistent use of
Float.module qualifier in Float64 handlers.
floor,ceil,min,maxuse bare stdlib names whileFloat.abs(line 356),Float.pow(line 378), andFloat.of_int(line 401) use theFloat.prefix. Consider qualifying consistently (e.g.,Float.floor,Float.ceil,Float.min,Float.max) for clarity and to avoid subtle polymorphic-dispatch surprises withmin/max.♻️ Optional consistency fix
- | arg :: _ -> Some (VFloat64 (floor (to_float64 arg))) + | arg :: _ -> Some (VFloat64 (Float.floor (to_float64 arg)))- | arg :: _ -> Some (VFloat64 (ceil (to_float64 arg))) + | arg :: _ -> Some (VFloat64 (Float.ceil (to_float64 arg)))- | arg1 :: arg2 :: _ -> - Some (VFloat64 (min (to_float64 arg1) (to_float64 arg2))) + | arg1 :: arg2 :: _ -> + Some (VFloat64 (Float.min (to_float64 arg1) (to_float64 arg2)))- | arg1 :: arg2 :: _ -> - Some (VFloat64 (max (to_float64 arg1) (to_float64 arg2))) + | arg1 :: arg2 :: _ -> + Some (VFloat64 (Float.max (to_float64 arg1) (to_float64 arg2)))🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@sarek/interp/Sarek_ir_interp_intrinsics.ml` around lines 361 - 398, The Float64 intrinsic handlers in Sarek_ir_interp_intrinsics.ml use an inconsistent mix of bare stdlib functions and Float.qualified calls. Update the "floor", "ceil", "min", and "max" branches in the intrinsic dispatch to use the Float module consistently, matching the surrounding Float.abs, Float.pow, and Float.of_int usage in the same function, and keep the existing argument validation and error paths unchanged.
136-184: 📐 Maintainability & Code Quality | 🔵 Trivial | 🏗️ Heavy liftConsider extracting a helper to reduce repetitive arity-validation boilerplate.
Each handler follows the same pattern: match args, extract N arguments, apply function, wrap in
VFloatXX, or raiseUnsupported_operation. A pair of helpers would eliminate ~40 near-identical blocks:let unary_float32 name f args = match args with | arg :: _ -> Some (VFloat32 (f (to_float32 arg))) | [] -> Interp_error.raise_error (Unsupported_operation {operation = name; reason = "requires 1 argument"}) let binary_float32 name f args = match args with | arg1 :: arg2 :: _ -> Some (VFloat32 (f (to_float32 arg1) (to_float32 arg2))) | _ -> Interp_error.raise_error (Unsupported_operation {operation = name; reason = "requires 2 arguments"})Then each case becomes a one-liner:
| "asin" -> unary_float32 "asin" F32.asin args.Also applies to: 277-332, 361-398
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@sarek/interp/Sarek_ir_interp_intrinsics.ml` around lines 136 - 184, The intrinsic handlers in Sarek_ir_interp_intrinsics.ml repeat the same arity-check and wrapping logic across many cases, so refactor the repeated pattern into reusable helpers. Add unary and binary helpers near the dispatcher that accept the operation name and F32 function, perform the arg matching, and raise Unsupported_operation with the correct arity when needed; then update each matching branch such as "asin", "acos", "atan2", "sinh", "cosh", and "tanh" to call those helpers instead of duplicating the match logic.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@sarek/interp/Sarek_ir_interp_intrinsics.ml`:
- Around line 361-398: The Float64 intrinsic handlers in
Sarek_ir_interp_intrinsics.ml use an inconsistent mix of bare stdlib functions
and Float.qualified calls. Update the "floor", "ceil", "min", and "max" branches
in the intrinsic dispatch to use the Float module consistently, matching the
surrounding Float.abs, Float.pow, and Float.of_int usage in the same function,
and keep the existing argument validation and error paths unchanged.
- Around line 136-184: The intrinsic handlers in Sarek_ir_interp_intrinsics.ml
repeat the same arity-check and wrapping logic across many cases, so refactor
the repeated pattern into reusable helpers. Add unary and binary helpers near
the dispatcher that accept the operation name and F32 function, perform the arg
matching, and raise Unsupported_operation with the correct arity when needed;
then update each matching branch such as "asin", "acos", "atan2", "sinh",
"cosh", and "tanh" to call those helpers instead of duplicating the match logic.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: b5dc7b14-d1d4-4fed-82a4-31b7ed229933
📒 Files selected for processing (1)
sarek/interp/Sarek_ir_interp_intrinsics.ml
Overview
This PR introduces support for mathematical intrinsic operations within the interpreter. It adds mappings for mathematical trigonometric primitives (such as
atan,cosh,sinh,atan2...) as well as additional mathematical primitives infloat64(min,max,floor,ceil,pow).Such primitives can be used in signal processing or spatial simulations.
Summary by CodeRabbit
Float32andFloat64calculations, with support for both single- and two-argument forms where applicable.