feat(vm): add deterministic gameplay math and domain coverage gates
Intrepid/Prometeu/Runtime/pipeline/pr-master There was a failure building this commit
Intrepid/Prometeu/Runtime/pipeline/pr-master There was a failure building this commit
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
//! Exact scalar identities. Public math overloads are a consumer projection.
|
||||
use crate::builtins::{AbiType, BuiltinScalarType, IntrinsicExecutionError as E, IntrinsicMeta};
|
||||
use crate::numeric::{self, NumericError};
|
||||
use prometeu_bytecode::{TRAP_NUMERIC_DOMAIN, TRAP_NUMERIC_RANGE, Value};
|
||||
use prometeu_hal::HostContext;
|
||||
const I32: AbiType = AbiType::Scalar(BuiltinScalarType::Int32);
|
||||
const I64: AbiType = AbiType::Scalar(BuiltinScalarType::Int64);
|
||||
const FLOAT: AbiType = AbiType::Scalar(BuiltinScalarType::Float);
|
||||
const fn entry<const ID: u32>(
|
||||
owner: &'static str,
|
||||
name: &'static str,
|
||||
arg_layout: &'static [AbiType],
|
||||
ret_layout: &'static [AbiType],
|
||||
parameter_names: &'static [&'static str],
|
||||
cost_cycles: u64,
|
||||
) -> IntrinsicMeta {
|
||||
IntrinsicMeta {
|
||||
id: ID,
|
||||
owner,
|
||||
name,
|
||||
version: 1,
|
||||
arg_layout,
|
||||
ret_layout,
|
||||
parameter_names,
|
||||
cost_cycles,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: execute::<ID>,
|
||||
}
|
||||
}
|
||||
pub(crate) const MATH_INTRINSICS: [IntrinsicMeta; 26] = [
|
||||
entry::<0x3000>("math.i32", "min", &[I32, I32], &[I32], &["a", "b"], 2),
|
||||
entry::<0x3001>("math.i32", "max", &[I32, I32], &[I32], &["a", "b"], 2),
|
||||
entry::<0x3002>("math.i32", "clamp", &[I32, I32, I32], &[I32], &["value", "min", "max"], 4),
|
||||
entry::<0x3003>("math.i32", "abs", &[I32], &[I32], &["value"], 2),
|
||||
entry::<0x3004>("math.i64", "min", &[I64, I64], &[I64], &["a", "b"], 2),
|
||||
entry::<0x3005>("math.i64", "max", &[I64, I64], &[I64], &["a", "b"], 2),
|
||||
entry::<0x3006>("math.i64", "clamp", &[I64, I64, I64], &[I64], &["value", "min", "max"], 4),
|
||||
entry::<0x3007>("math.i64", "abs", &[I64], &[I64], &["value"], 2),
|
||||
entry::<0x3008>("mathf", "min", &[FLOAT, FLOAT], &[FLOAT], &["a", "b"], 2),
|
||||
entry::<0x3009>("mathf", "max", &[FLOAT, FLOAT], &[FLOAT], &["a", "b"], 2),
|
||||
entry::<0x300a>(
|
||||
"mathf",
|
||||
"clamp",
|
||||
&[FLOAT, FLOAT, FLOAT],
|
||||
&[FLOAT],
|
||||
&["value", "min", "max"],
|
||||
4,
|
||||
),
|
||||
entry::<0x300b>("mathf", "abs", &[FLOAT], &[FLOAT], &["value"], 2),
|
||||
entry::<0x300c>("mathf", "floor", &[FLOAT], &[FLOAT], &["value"], 4),
|
||||
entry::<0x300d>("mathf", "ceil", &[FLOAT], &[FLOAT], &["value"], 4),
|
||||
entry::<0x300e>("mathf", "trunc", &[FLOAT], &[FLOAT], &["value"], 4),
|
||||
entry::<0x300f>("mathf", "round", &[FLOAT], &[FLOAT], &["value"], 4),
|
||||
entry::<0x3010>("mathf", "sqrt", &[FLOAT], &[FLOAT], &["value"], 16),
|
||||
entry::<0x3011>("mathf", "sin", &[FLOAT], &[FLOAT], &["angle"], 32),
|
||||
entry::<0x3012>("mathf", "cos", &[FLOAT], &[FLOAT], &["angle"], 32),
|
||||
entry::<0x3013>("mathf", "atan2", &[FLOAT, FLOAT], &[FLOAT], &["y", "x"], 48),
|
||||
entry::<0x3014>("math.i32", "to_i64", &[I32], &[I64], &["value"], 2),
|
||||
entry::<0x3015>("math.i64", "to_i32", &[I64], &[I32], &["value"], 2),
|
||||
entry::<0x3016>("math.i32", "to_float", &[I32], &[FLOAT], &["value"], 2),
|
||||
entry::<0x3017>("math.i64", "to_float", &[I64], &[FLOAT], &["value"], 2),
|
||||
entry::<0x3018>("mathf", "to_i32", &[FLOAT], &[I32], &["value"], 2),
|
||||
entry::<0x3019>("mathf", "to_i64", &[FLOAT], &[I64], &["value"], 2),
|
||||
];
|
||||
impl From<NumericError> for E {
|
||||
fn from(error: NumericError) -> Self {
|
||||
Self::Numeric(error)
|
||||
}
|
||||
}
|
||||
fn integer(args: &[Value], offset: u32, narrow: bool) -> Result<Value, E> {
|
||||
let mut a = [0i64; 3];
|
||||
for (i, value) in args.iter().enumerate() {
|
||||
a[i] = match value {
|
||||
Value::Int32(v) => i64::from(*v),
|
||||
Value::Int64(v) => *v,
|
||||
_ => unreachable!("validated exact integer carrier"),
|
||||
};
|
||||
}
|
||||
let result = match offset {
|
||||
0 => a[0].min(a[1]),
|
||||
1 => a[0].max(a[1]),
|
||||
2 => {
|
||||
if a[1] > a[2] {
|
||||
return Err(NumericError::new(TRAP_NUMERIC_DOMAIN).into());
|
||||
}
|
||||
a[0].clamp(a[1], a[2])
|
||||
}
|
||||
3 => {
|
||||
if (narrow && a[0] == i64::from(i32::MIN)) || a[0] == i64::MIN {
|
||||
return Err(NumericError::new(TRAP_NUMERIC_RANGE).into());
|
||||
}
|
||||
a[0].abs()
|
||||
}
|
||||
_ => unreachable!("private integer operation"),
|
||||
};
|
||||
Ok(if narrow { Value::Int32(result as i32) } else { Value::Int64(result) })
|
||||
}
|
||||
fn execute<const ID: u32>(args: &[Value], _: &mut HostContext<'_>) -> Result<Vec<Value>, E> {
|
||||
let meta = crate::lookup_intrinsic_by_id(ID).expect("static scalar identity");
|
||||
meta.validate_argument_values(args)?;
|
||||
if ID < 0x3008 {
|
||||
return Ok(vec![integer(args, (ID - 0x3000) % 4, ID < 0x3004)?]);
|
||||
}
|
||||
let mut floats = [0.0; 3];
|
||||
for (i, value) in args.iter().enumerate() {
|
||||
if let Value::Float(v) = value {
|
||||
floats[i] = *v;
|
||||
}
|
||||
}
|
||||
if (0x3008..=0x3013).contains(&ID) || ID >= 0x3018 {
|
||||
numeric::check_inputs(&floats[..args.len()])?;
|
||||
}
|
||||
let x = floats[0];
|
||||
let value = match ID {
|
||||
0x3008 => Value::Float(numeric::min(x, floats[1])),
|
||||
0x3009 => Value::Float(numeric::max(x, floats[1])),
|
||||
0x300a => Value::Float(numeric::clamp(x, floats[1], floats[2])?),
|
||||
0x300b => Value::Float(numeric::abs(x)),
|
||||
0x300c => Value::Float(numeric::floor(x)?),
|
||||
0x300d => Value::Float(numeric::ceil(x)?),
|
||||
0x300e => Value::Float(numeric::trunc(x)?),
|
||||
0x300f => Value::Float(numeric::round(x)?),
|
||||
0x3010 => Value::Float(numeric::sqrt(x)?),
|
||||
0x3011 => Value::Float(numeric::sin(x)?),
|
||||
0x3012 => Value::Float(numeric::cos(x)?),
|
||||
0x3013 => Value::Float(numeric::atan2(x, floats[1])?),
|
||||
0x3014 => {
|
||||
let Value::Int32(v) = args[0] else { unreachable!() };
|
||||
Value::Int64(i64::from(v))
|
||||
}
|
||||
0x3015 => {
|
||||
let Value::Int64(v) = args[0] else { unreachable!() };
|
||||
Value::Int32(i32::try_from(v).map_err(|_| NumericError::new(TRAP_NUMERIC_RANGE))?)
|
||||
}
|
||||
0x3016 => {
|
||||
let Value::Int32(v) = args[0] else { unreachable!() };
|
||||
Value::Float(numeric::from_i64(i64::from(v)))
|
||||
}
|
||||
0x3017 => {
|
||||
let Value::Int64(v) = args[0] else { unreachable!() };
|
||||
Value::Float(numeric::from_i64(v))
|
||||
}
|
||||
0x3018 => Value::Int32(numeric::to_i64(x, 32)? as i32),
|
||||
0x3019 => Value::Int64(numeric::to_i64(x, 64)?),
|
||||
_ => unreachable!("static scalar identity"),
|
||||
};
|
||||
Ok(vec![value])
|
||||
}
|
||||
Reference in New Issue
Block a user