From: Brendan Hansen Date: Wed, 17 Feb 2021 16:20:51 +0000 (-0600) Subject: added 'new' and 'math.clamp' X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=a67739a8b4b13c9184fc516fd3caf210494fb075;p=onyx.git added 'new' and 'math.clamp' --- diff --git a/core/builtin.onyx b/core/builtin.onyx index 73f5fb1c..e2ca1eb6 100644 --- a/core/builtin.onyx +++ b/core/builtin.onyx @@ -92,3 +92,11 @@ calloc :: (size: u32) -> rawptr do return raw_alloc(context.allocator, size); cresize :: (ptr: rawptr, size: u32) -> rawptr do return raw_resize(context.allocator, ptr, size); cfree :: (ptr: rawptr) do raw_free(context.allocator, ptr); +new :: ($T: type_expr, allocator := context.allocator) -> ^T { + res := cast(^T) calloc(sizeof T); + + // At some point, it would be nice to initialize the resulting memory, i.e. + // *res = T.{}; + + return res; +} diff --git a/core/math.onyx b/core/math.onyx index ff7ab779..366b9fd1 100644 --- a/core/math.onyx +++ b/core/math.onyx @@ -242,6 +242,12 @@ min_poly :: (a: $T, b: T) -> T { return b; } +clamp :: (v: $T, lo: T, hi: T) -> T { + if v < lo do return lo; + if v > hi do return hi; + return v; +} + sqrt :: proc { wasm.sqrt_f32, wasm.sqrt_f64, sqrt_poly } sqrt_poly :: proc (x: $T) -> T { return ~~ sqrt_f64(~~ x);