added 'new' and 'math.clamp'
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 17 Feb 2021 16:20:51 +0000 (10:20 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 17 Feb 2021 16:20:51 +0000 (10:20 -0600)
core/builtin.onyx
core/math.onyx

index 73f5fb1cb33f43e0561151f3382a543f9b4e1c61..e2ca1eb6ff3f747677f6981ec2d8a1532726636b 100644 (file)
@@ -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;
+}
index ff7ab779ea002148fc96a100fc73cd69c37bed6b..366b9fd1d14ecefcf71d9bf0d930b3b85b4c5afd 100644 (file)
@@ -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);