shape-runtime 0.3.2

Bytecode compiler, builtins, and runtime infrastructure for Shape
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//! Math intrinsics — partial migration to typed marshal layer.
//!
//! Per the intrinsics-typed-CC migration's partial-migration pattern (see
//! `docs/defections.md` 2026-05-07 zero-copy entry's per-storage-variant
//! correction subsection + intrinsics-typed-CC entry's Q2 lifecycle
//! subsection), 14 of math.rs's 19 intrinsics migrate to
//! `register_typed_fn_N` typed entries via [`create_math_intrinsics_module`].
//! 4 polymorphic intrinsics remain as legacy `IntrinsicFn` bodies pending
//! follow-on architectural sub-decisions:
//!
//! - **`intrinsic_min` / `intrinsic_max`**: polymorphic return (`i64` for
//!   `Vec<int>` fast path vs `f64` for `Vec<number>`). Single typed entry
//!   can't carry both. Awaits **M1-split** sub-decision (per-element-type
//!   intrinsics; cross-crate change to shape-vm compiler emission + opcode
//!   discriminants + classification logic).
//! - **`intrinsic_sum` deleted** (W12-stdlib-intrinsic-collapse,
//!   Wave-2-Agent-G, 2026-05-14): stdlib `pub fn sum(series)` now routes
//!   through PHF method dispatch (`series.sum()`) — single discriminator
//!   per ADR-005 §1, `MethodFnV2` ABI per ADR-006 §2.7.10/Q11.
//! - **`intrinsic_char_code`**: polymorphic input (`Char` vs `String`).
//!   `HeapValue::Char` is first-class post-bulldozer (`heap_value.rs:846`)
//!   so dropping the `Char` branch would break callers iterating
//!   `for c in s.chars()`. Awaits **multi-input-type dispatch** sub-
//!   decision.
//! - **`intrinsic_bspline2_3d_batch`**: 11-arg with FloatArray fast path
//!   AND generic-array slow path. Slow path uses `to_generic()` which is
//!   removed; consumer audit of `math.shape:243` wrapper + user-code
//!   needed before deciding fast-path-only-vs-keep-slow-path.
//!
//! Migrated entries take `Arc<Vec<f64>>` (for `Vec<number>`
//! aggregations) or `f64` (for trig family + `from_char_code`) typed
//! inputs and return `ConcreteReturn::F64` / `ConcreteReturn::String`
//! per the dispatcher's `TypedReturn → slot push` projection.

use crate::context::ExecutionContext;
use crate::marshal::{register_typed_fn_1, register_typed_fn_2};
use crate::module_exports::ModuleExports;
use crate::typed_module_exports::{ConcreteReturn, ConcreteType, TypedReturn};
use shape_ast::error::{Result, ShapeError};
use shape_value::KindedSlot;
use std::sync::Arc;

// ───────────────────── Module factory (14 typed entries) ─────────────────────

/// Create the math intrinsics module with 14 typed-marshal entry points.
/// The 4 polymorphic intrinsics (min, max, char_code, bspline2_3d_batch)
/// remain as legacy `IntrinsicFn` bodies in this module until their
/// follow-on architectural sub-decisions land. `sum` was deleted by
/// W12-stdlib-intrinsic-collapse (Wave-2-Agent-G, 2026-05-14) — stdlib
/// now routes through PHF `.sum()` method dispatch.
pub fn create_math_intrinsics_module() -> ModuleExports {
    let mut module = ModuleExports::new("std::core::intrinsics::math");
    module.description =
        "Math intrinsics (typed entries; polymorphic-shape intrinsics stay as legacy bodies pending follow-on sub-decisions)"
            .to_string();

    // ── Array aggregations ──

    register_typed_fn_1::<_, Arc<Vec<f64>>>(
        &mut module,
        "__intrinsic_mean",
        "Mean (average) of a Vec<number>",
        "input",
        "Array<number>",
        ConcreteType::Number,
        |input, _ctx| {
            let data = input.as_slice();
            if data.is_empty() {
                return Ok(TypedReturn::Concrete(ConcreteReturn::F64(f64::NAN)));
            }
            let sum: f64 = data.iter().sum();
            Ok(TypedReturn::Concrete(ConcreteReturn::F64(
                sum / data.len() as f64,
            )))
        },
    );

    register_typed_fn_1::<_, Arc<Vec<f64>>>(
        &mut module,
        "__intrinsic_variance",
        "Population variance of a Vec<number>",
        "input",
        "Array<number>",
        ConcreteType::Number,
        |input, _ctx| {
            let data = input.as_slice();
            if data.is_empty() {
                return Ok(TypedReturn::Concrete(ConcreteReturn::F64(f64::NAN)));
            }
            let mean: f64 = data.iter().sum::<f64>() / data.len() as f64;
            #[cfg(all(target_arch = "x86_64", target_feature = "avx2"))]
            let var = variance_avx2(data, mean);
            #[cfg(not(all(target_arch = "x86_64", target_feature = "avx2")))]
            let var: f64 = data.iter().map(|&x| (x - mean).powi(2)).sum::<f64>()
                / data.len() as f64;
            Ok(TypedReturn::Concrete(ConcreteReturn::F64(var)))
        },
    );

    register_typed_fn_1::<_, Arc<Vec<f64>>>(
        &mut module,
        "__intrinsic_std",
        "Population standard deviation of a Vec<number>",
        "input",
        "Array<number>",
        ConcreteType::Number,
        |input, _ctx| {
            let data = input.as_slice();
            if data.is_empty() {
                return Ok(TypedReturn::Concrete(ConcreteReturn::F64(f64::NAN)));
            }
            let mean: f64 = data.iter().sum::<f64>() / data.len() as f64;
            #[cfg(all(target_arch = "x86_64", target_feature = "avx2"))]
            let var = variance_avx2(data, mean);
            #[cfg(not(all(target_arch = "x86_64", target_feature = "avx2")))]
            let var: f64 = data.iter().map(|&x| (x - mean).powi(2)).sum::<f64>()
                / data.len() as f64;
            Ok(TypedReturn::Concrete(ConcreteReturn::F64(var.sqrt())))
        },
    );

    // ── Trig family (10 unary + 1 binary atan2) ──

    register_unary_f64_op(&mut module, "__intrinsic_sin", "Sine of x", f64::sin);
    register_unary_f64_op(&mut module, "__intrinsic_cos", "Cosine of x", f64::cos);
    register_unary_f64_op(&mut module, "__intrinsic_tan", "Tangent of x", f64::tan);
    register_unary_f64_op(&mut module, "__intrinsic_asin", "Arc sine of x", f64::asin);
    register_unary_f64_op(&mut module, "__intrinsic_acos", "Arc cosine of x", f64::acos);
    register_unary_f64_op(&mut module, "__intrinsic_atan", "Arc tangent of x", f64::atan);
    register_unary_f64_op(&mut module, "__intrinsic_sinh", "Hyperbolic sine of x", f64::sinh);
    register_unary_f64_op(&mut module, "__intrinsic_cosh", "Hyperbolic cosine of x", f64::cosh);
    register_unary_f64_op(&mut module, "__intrinsic_tanh", "Hyperbolic tangent of x", f64::tanh);

    register_typed_fn_2::<_, f64, f64>(
        &mut module,
        "__intrinsic_atan2",
        "Two-argument arc tangent (atan2(y, x))",
        [("y", "number"), ("x", "number")],
        ConcreteType::Number,
        |y, x, _ctx| Ok(TypedReturn::Concrete(ConcreteReturn::F64(y.atan2(x)))),
    );

    // ── Char code (one-direction migration; reverse stays legacy) ──

    register_typed_fn_1::<_, f64>(
        &mut module,
        "__intrinsic_from_char_code",
        "Create a single-character string from a Unicode code point",
        "code",
        "number",
        ConcreteType::String,
        |code, _ctx| {
            let ch = char::from_u32(code as u32).ok_or_else(|| {
                format!(
                    "__intrinsic_from_char_code: invalid code point {}",
                    code as u32
                )
            })?;
            Ok(TypedReturn::Concrete(ConcreteReturn::String(ch.to_string())))
        },
    );

    module
}

/// Helper: register a unary `f64 -> f64` typed entry (the trig family pattern).
fn register_unary_f64_op(
    module: &mut ModuleExports,
    name: &'static str,
    desc: &'static str,
    op: fn(f64) -> f64,
) {
    register_typed_fn_1::<_, f64>(
        module,
        name,
        desc,
        "x",
        "number",
        ConcreteType::Number,
        move |x, _ctx| Ok(TypedReturn::Concrete(ConcreteReturn::F64(op(x)))),
    );
}

// ───────────────────── Legacy bodies (4 polymorphic intrinsics) ─────────────────────

// W12-stdlib-intrinsic-collapse (Wave-2-Agent-G, 2026-05-14): the legacy
// `intrinsic_sum` body was deleted alongside `BuiltinFunction::IntrinsicSum`.
// Stdlib `pub fn sum(series)` now routes through PHF `.sum()` method
// dispatch — single discriminator per ADR-005 §1.

/// Intrinsic: Minimum value in a series or among multi-scalar arguments.
///
/// **Migration deferred** pending M1-split sub-decision (polymorphic return
/// + polymorphic input shape). Multi-scalar branches are dead code from
/// stdlib emission per audit but kept until the architectural decision lands.
pub fn intrinsic_min(_args: &[KindedSlot], _ctx: &mut ExecutionContext) -> Result<KindedSlot> {
    Err(ShapeError::RuntimeError {
        message: "intrinsic_min: pending Phase 2c intrinsic kind threading + M1-split — see ADR-006 §2.7.4".to_string(),
        location: None,
    })
}

/// Intrinsic: Maximum value in a series or among multi-scalar arguments.
///
/// **Migration deferred** pending M1-split sub-decision. See `intrinsic_min`.
pub fn intrinsic_max(_args: &[KindedSlot], _ctx: &mut ExecutionContext) -> Result<KindedSlot> {
    Err(ShapeError::RuntimeError {
        message: "intrinsic_max: pending Phase 2c intrinsic kind threading + M1-split — see ADR-006 §2.7.4".to_string(),
        location: None,
    })
}

/// Intrinsic: Get the Unicode code point of a single character.
///
/// **Migration deferred** pending multi-input-type dispatch sub-decision.
/// `HeapValue::Char` is first-class post-bulldozer; dropping the `as_char`
/// branch would break `for c in s.chars()`-style consumers.
pub fn intrinsic_char_code(
    _args: &[KindedSlot],
    _ctx: &mut ExecutionContext,
) -> Result<KindedSlot> {
    Err(ShapeError::RuntimeError {
        message: "intrinsic_char_code: pending Phase 2c intrinsic kind threading — see ADR-006 §2.7.4".to_string(),
        location: None,
    })
}

/// Intrinsic: Batched quadratic B-spline interpolation on a 3D grid.
///
/// **Migration deferred** pending consumer audit (fast path uses
/// `as_f64_slice()`; slow path uses `to_generic()` which is removed.
/// Audit needs to determine if any consumer passes a generic array
/// before deciding fast-path-only-vs-keep-slow-path).
///
/// Args: grid_data, nx, ny, nz, x_lo, x_hi, y_lo, y_hi, z_lo, z_hi, pos_flat
pub fn intrinsic_bspline2_3d_batch(
    _args: &[KindedSlot],
    _ctx: &mut ExecutionContext,
) -> Result<KindedSlot> {
    Err(ShapeError::RuntimeError {
        message: "intrinsic_bspline2_3d_batch: pending Phase 2c intrinsic kind threading + consumer audit — see ADR-006 §2.7.4".to_string(),
        location: None,
    })
}

// ───────────────────── Helpers (used by typed + legacy bodies) ─────────────────────

#[cfg(all(target_arch = "x86_64", target_feature = "avx2"))]
fn variance_avx2(data: &[f64], mean: f64) -> f64 {
    use std::simd::f64x4;

    let chunks = data.chunks_exact(4);
    let remainder = chunks.remainder();

    let mean_vec = f64x4::splat(mean);
    let mut var_sum = f64x4::splat(0.0);

    for chunk in chunks {
        let values = f64x4::from_slice(chunk);
        let diff = values - mean_vec;
        var_sum += diff * diff;
    }

    let vector_var = var_sum.reduce_sum();
    let remainder_var: f64 = remainder.iter().map(|&x| (x - mean).powi(2)).sum();

    (vector_var + remainder_var) / data.len() as f64
}

/// Core B-spline computation on a contiguous f64 slice (fastest path).
#[inline]
fn bspline2_3d_batch_slice(
    grid: &[f64],
    nx: usize, ny: usize, nz: usize,
    x_lo: f64, x_hi: f64, y_lo: f64, y_hi: f64, z_lo: f64, z_hi: f64,
    pos: &[f64],
) -> Vec<f64> {
    let n = pos.len() / 3;
    let nxm = (nx - 1) as f64;
    let nym = (ny - 1) as f64;
    let nzm = (nz - 1) as f64;
    let inv_x = nxm / (x_hi - x_lo);
    let inv_y = nym / (y_hi - y_lo);
    let inv_z = nzm / (z_hi - z_lo);
    let nyz = ny * nz;
    let mut result = Vec::with_capacity(n);

    for s in 0..n {
        let i3 = s * 3;
        let gx = ((pos[i3] - x_lo) * inv_x).clamp(0.0, nxm);
        let gy = ((pos[i3 + 1] - y_lo) * inv_y).clamp(0.0, nym);
        let gz = ((pos[i3 + 2] - z_lo) * inv_z).clamp(0.0, nzm);

        let cx = (gx + 0.5).floor() as isize;
        let cy = (gy + 0.5).floor() as isize;
        let cz = (gz + 0.5).floor() as isize;
        let tx = gx - cx as f64;
        let ty = gy - cy as f64;
        let tz = gz - cz as f64;

        let (wx0, wx1, wx2) = bspline_weights(tx);
        let (wy0, wy1, wy2) = bspline_weights(ty);
        let (wz0, wz1, wz2) = bspline_weights(tz);

        let ix = [
            (cx - 1).max(0) as usize,
            cx as usize,
            (cx + 1).min(nx as isize - 1) as usize,
        ];
        let iy = [
            (cy - 1).max(0) as usize,
            cy as usize,
            (cy + 1).min(ny as isize - 1) as usize,
        ];
        let iz = [
            (cz - 1).max(0) as usize,
            cz as usize,
            (cz + 1).min(nz as isize - 1) as usize,
        ];

        let wx = [wx0, wx1, wx2];
        let wy = [wy0, wy1, wy2];
        let wz = [wz0, wz1, wz2];

        let mut val = 0.0;
        for a in 0..3 {
            let rx = ix[a] * nyz;
            for b in 0..3 {
                let rxy = rx + iy[b] * nz;
                let wxy = wx[a] * wy[b];
                for c in 0..3 {
                    val += wxy * wz[c] * unsafe { *grid.get_unchecked(rxy + iz[c]) };
                }
            }
        }
        result.push(val);
    }
    result
}

/// Core B-spline computation using per-element access function (generic arrays).
/// Only accesses 27 grid elements per query point — no bulk copy.
#[inline]
fn bspline2_3d_batch_fn(
    grid: &dyn Fn(usize) -> f64,
    nx: usize, ny: usize, nz: usize,
    x_lo: f64, x_hi: f64, y_lo: f64, y_hi: f64, z_lo: f64, z_hi: f64,
    pos: &[f64],
) -> Vec<f64> {
    let n = pos.len() / 3;
    let nxm = (nx - 1) as f64;
    let nym = (ny - 1) as f64;
    let nzm = (nz - 1) as f64;
    let inv_x = nxm / (x_hi - x_lo);
    let inv_y = nym / (y_hi - y_lo);
    let inv_z = nzm / (z_hi - z_lo);
    let nyz = ny * nz;
    let mut result = Vec::with_capacity(n);

    for s in 0..n {
        let i3 = s * 3;
        let gx = ((pos[i3] - x_lo) * inv_x).clamp(0.0, nxm);
        let gy = ((pos[i3 + 1] - y_lo) * inv_y).clamp(0.0, nym);
        let gz = ((pos[i3 + 2] - z_lo) * inv_z).clamp(0.0, nzm);

        let cx = (gx + 0.5).floor() as isize;
        let cy = (gy + 0.5).floor() as isize;
        let cz = (gz + 0.5).floor() as isize;
        let tx = gx - cx as f64;
        let ty = gy - cy as f64;
        let tz = gz - cz as f64;

        let (wx0, wx1, wx2) = bspline_weights(tx);
        let (wy0, wy1, wy2) = bspline_weights(ty);
        let (wz0, wz1, wz2) = bspline_weights(tz);

        let ix = [
            (cx - 1).max(0) as usize,
            cx as usize,
            (cx + 1).min(nx as isize - 1) as usize,
        ];
        let iy = [
            (cy - 1).max(0) as usize,
            cy as usize,
            (cy + 1).min(ny as isize - 1) as usize,
        ];
        let iz = [
            (cz - 1).max(0) as usize,
            cz as usize,
            (cz + 1).min(nz as isize - 1) as usize,
        ];

        let wx = [wx0, wx1, wx2];
        let wy = [wy0, wy1, wy2];
        let wz = [wz0, wz1, wz2];

        let mut val = 0.0;
        for a in 0..3 {
            let rx = ix[a] * nyz;
            for b in 0..3 {
                let rxy = rx + iy[b] * nz;
                let wxy = wx[a] * wy[b];
                for c in 0..3 {
                    val += wxy * wz[c] * grid(rxy + iz[c]);
                }
            }
        }
        result.push(val);
    }
    result
}

/// Quadratic B-spline basis weights for offset t.
#[inline(always)]
fn bspline_weights(t: f64) -> (f64, f64, f64) {
    (
        0.5 * (0.5 - t) * (0.5 - t),
        0.75 - t * t,
        0.5 * (0.5 + t) * (0.5 + t),
    )
}