Skip to main content

CodegenOptions

Struct CodegenOptions 

Source
pub struct CodegenOptions {
    pub math_backend: MathBackend,
    pub precision: Precision,
    pub inline: bool,
    pub must_use: bool,
    pub cse: bool,
    pub unit_annotation: UnitAnnotation,
    pub param_units: Vec<(String, String)>,
    pub return_unit: Option<String>,
    pub use_mul_add: bool,
    pub checked_domain: bool,
    pub emit_runtime: bool,
}
Expand description

Configuration for Rust code generation.

Fields§

§math_backend: MathBackend

Math function dispatch strategy.

§precision: Precision

Floating-point precision.

§inline: bool

Add #[inline] annotation to generated functions.

§must_use: bool

Add #[must_use] annotation.

§cse: bool

Run CSE before code generation.

§unit_annotation: UnitAnnotation

Unit annotations for generated function signatures.

§param_units: Vec<(String, String)>

Map from parameter names to uom type names (e.g., “theta1” → “Angle”). Only used when unit_annotation is UnitAnnotation::Uom.

§return_unit: Option<String>

The uom type name for the return value (e.g., “Length”). Only used when unit_annotation is UnitAnnotation::Uom.

§use_mul_add: bool

Fuse a*b + c patterns into mul_add / fma calls (default true).

Fused multiply-add rounds once instead of twice, so results can differ from a plain a*b + c in the last bit; disable for bit-exact reproduction of unfused arithmetic.

§checked_domain: bool

Emit debug_assert! domain checks (e.g. ln(x) requires x > 0, sqrt(x) requires x >= 0, lambertw(x) requires x >= -1/e). Default false. Checks compile away in release builds.

§emit_runtime: bool

Emit the mod symplex_rt { … } special-function runtime preamble when the generated function needs it (default true).

Set to false when concatenating many generated functions into one file and emit the runtime once via CodegenOptions::runtime_module.

Implementations§

Source§

impl CodegenOptions

Source

pub fn runtime_module(&self) -> String

The complete special-function runtime module (mod symplex_rt { … }) for this configuration’s math backend, containing every helper.

Generated functions only embed the helpers they use. When many functions are concatenated into one file, set emit_runtime to false and place the output of this method once at the top of the file instead.

use symplex::matrix::CodegenOptions;

let rt = CodegenOptions::default().runtime_module();
assert!(rt.starts_with("#[allow(dead_code, clippy::all)]\nmod symplex_rt {"));
assert!(rt.contains("pub fn gamma("));
assert!(rt.contains("pub fn bessel_k("));
Source

pub fn runtime_module_for(&self, generated_code: &str) -> Option<String>

The mod symplex_rt { … } block containing exactly the helpers that generated_code references as symplex_rt::name(…) (plus their transitive dependencies), or None when the code uses none.

This is what a single Ex::to_rust_fn embeds when emit_runtime is set; use it to emit one shared runtime for a file assembled from several functions generated with emit_runtime: false.

use symplex::matrix::CodegenOptions;
use symplex::prelude::*;

let ctx = Context::new();
let x = ctx.symbol("x");
let opts = CodegenOptions { emit_runtime: false, ..Default::default() };
let g = x.gamma().to_rust_fn_with_options("g", &["x"], &opts).unwrap();
let e = x.erf().to_rust_fn_with_options("e", &["x"], &opts).unwrap();
let body = format!("{g}\n{e}");
let rt = opts.runtime_module_for(&body).expect("gamma/erf need the runtime");
assert!(rt.contains("pub fn gamma(") && rt.contains("pub fn erf("));
assert!(!rt.contains("pub fn bessel_k("));
assert!(opts.runtime_module_for("fn f(x: f64) -> f64 { x }").is_none());
Source

pub fn c_runtime(&self) -> String

The complete C99 helper library (static inline symplex_* functions) used by Ex::to_c_fn, with its #include <math.h>.

Generated C functions only embed the helpers they use; when several functions share one translation unit, set emit_runtime to false and paste this once.

use symplex::matrix::CodegenOptions;

let rt = CodegenOptions::default().c_runtime();
assert!(rt.contains("#include <math.h>"));
assert!(rt.contains("static inline double symplex_lambert_w0(double x)"));
Source

pub fn no_std() -> Self

Configuration for no_std embedded targets. Uses cfg-gated math backend and adds #[inline].

Source

pub fn embedded_f32() -> Self

Configuration for embedded f32 targets (e.g., Cortex-M4F). Uses cfg-gated math backend, f32 precision, and #[inline].

Source

pub fn with_uom(self) -> Self

Enable uom type annotations on the generated function.

Source

pub fn param_unit(self, name: &str, uom_type: &str) -> Self

Set the uom type for a parameter.

Source

pub fn return_unit_type(self, uom_type: &str) -> Self

Set the uom type for the return value.

Trait Implementations§

Source§

impl Clone for CodegenOptions

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CodegenOptions

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for CodegenOptions

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more