// but can get the speed increase of a simple allocation strategy.
FixedAllocatorData :: struct {
- ptr : rawptr;
- size : u32;
+ ptr : rawptr;
+ size : u32;
}
#private_file
fixed_allocator_proc :: proc (data: rawptr, aa: AllocationAction, size: u32, align: u32, oldptr: rawptr) -> rawptr {
- fa_data := cast(^FixedAllocatorData) data;
+ fa_data := cast(^FixedAllocatorData) data;
- if aa != AllocationAction.Alloc do return null;
- if size > fa_data.size do return null;
-
- return fa_data.ptr;
+ if aa != AllocationAction.Alloc do return null;
+ if size > fa_data.size do return null;
+
+ return fa_data.ptr;
}
make :: proc (ptr: rawptr, size: u32) -> FixedAllocatorData {
- return FixedAllocatorData.{
- ptr = ptr,
- size = size,
- };
+ return FixedAllocatorData.{
+ ptr = ptr,
+ size = size,
+ };
}
make_allocator :: proc (fa_data: ^FixedAllocatorData) -> Allocator {
- return Allocator.{
- func = fixed_allocator_proc,
- data = fa_data,
- };
+ return Allocator.{
+ func = fixed_allocator_proc,
+ data = fa_data,
+ };
}
}
make :: proc (buffer: rawptr, length: u32) -> RingState {
- return RingState.{
- base_ptr = buffer,
- curr_ptr = buffer,
- size = length,
- };
+ return RingState.{
+ base_ptr = buffer,
+ curr_ptr = buffer,
+ size = length,
+ };
}
make_allocator :: proc (rs: ^RingState) -> Allocator {
- return Allocator.{
- func = ring_alloc_proc,
- data = rs,
- };
+ return Allocator.{
+ func = ring_alloc_proc,
+ data = rs,
+ };
}
}
hash_function :: proc {
- proc (key: rawptr) -> u32 { return 0xcbf29ce7 ^ cast(u32) key; },
- proc (key: i32) -> u32 { return 0xcbf29ce7 ^ cast(u32) key; },
- proc (key: i64) -> u32 { return cast(u32) (cast(u64) 0xcbf29ce7 ^ cast(u64) key); },
-
- proc (key: str) -> u32 {
- hash: u32 = 5381;
- for ch: key do hash += (hash << 5) + ~~ch;
- return hash;
- },
+ proc (key: rawptr) -> u32 { return 0xcbf29ce7 ^ cast(u32) key; },
+ proc (key: i32) -> u32 { return 0xcbf29ce7 ^ cast(u32) key; },
+ proc (key: i64) -> u32 { return cast(u32) (cast(u64) 0xcbf29ce7 ^ cast(u64) key); },
+
+ proc (key: str) -> u32 {
+ hash: u32 = 5381;
+ for ch: key do hash += (hash << 5) + ~~ch;
+ return hash;
+ },
}
cmp_function :: proc {
- proc (a: rawptr, b: rawptr) -> bool { return a == b; },
- proc (a: i32, b: i32) -> bool { return a == b; },
- proc (a: i64, b: i64) -> bool { return a == b; },
+ proc (a: rawptr, b: rawptr) -> bool { return a == b; },
+ proc (a: i32, b: i32) -> bool { return a == b; },
+ proc (a: i64, b: i64) -> bool { return a == b; },
- string.equal,
+ string.equal,
}
//
// Simple taylor series approximation of sin(t)
sin :: proc (t_: f32) -> f32 {
- t := t_;
- while t >= PI do t -= TAU;
- while t <= -PI do t += TAU;
+ t := t_;
+ while t >= PI do t -= TAU;
+ while t <= -PI do t += TAU;
- res := 0.0f;
+ res := 0.0f;
- plus_minus := 1.0f;
- n := 13;
- while n > 1 {
- res += plus_minus;
- res *= t * t / cast(f32) (n * n - n);
+ plus_minus := 1.0f;
+ n := 13;
+ while n > 1 {
+ res += plus_minus;
+ res *= t * t / cast(f32) (n * n - n);
- plus_minus = -plus_minus;
- n -= 2;
- }
+ plus_minus = -plus_minus;
+ n -= 2;
+ }
- res += 1.0f;
- res *= t;
- return res;
+ res += 1.0f;
+ res *= t;
+ return res;
}
// Simple taylor series approximation of cos(t)
cos :: proc (t_: f32) -> f32 {
- t := t_;
- while t >= PI do t -= TAU;
- while t <= -PI do t += TAU;
+ t := t_;
+ while t >= PI do t -= TAU;
+ while t <= -PI do t += TAU;
- res := 0.0f;
+ res := 0.0f;
- plus_minus := 1.0f;
- n := 12;
- while n > 1 {
- res += plus_minus;
- res *= t * t / cast(f32) (n * n - n);
+ plus_minus := 1.0f;
+ n := 12;
+ while n > 1 {
+ res += plus_minus;
+ res *= t * t / cast(f32) (n * n - n);
- plus_minus = -plus_minus;
- n -= 2;
- }
+ plus_minus = -plus_minus;
+ n -= 2;
+ }
- res += 1.0f;
- return res;
+ res += 1.0f;
+ return res;
}
max :: proc (a: $T, b: T) -> T {
package core.memory
copy :: proc (dst_: rawptr, src_: rawptr, len: u32) {
- dst := cast(^u8) dst_;
- src := cast(^u8) src_;
- for i: 0 .. len do dst[i] = src[i];
+ dst := cast(^u8) dst_;
+ src := cast(^u8) src_;
+ for i: 0 .. len do dst[i] = src[i];
}
set :: proc (start: rawptr, length: u32, value: u8) {
int :: proc (s := ^seed) -> u32 {
*s = *s * RANDOM_MULTIPLIER + RANDOM_INCREMENT;
- return *s;
+ return *s;
}
between :: proc (lo: i32, hi: i32) -> i32 do return int () % (hi + 1 - lo) + lo;
specific_compose_2 :: #solidify specific_compose_1 { C = f64 };
main :: proc (args: [] cstr) {
- printf("max(1, 2) = %i\n", math.max(1, 2));
- printf("max_f32(1.0, 2.0) = %f\n", max_f32(1, 2));
+ printf("max(1, 2) = %i\n", math.max(1, 2));
+ printf("max_f32(1.0, 2.0) = %f\n", max_f32(1, 2));
- // printf("max_f32(1, 2) = %i\n", max_f32(cast(u32) 1, cast(u32) 2));
+ // printf("max_f32(1, 2) = %i\n", max_f32(cast(u32) 1, cast(u32) 2));
- println(specific_compose_2(
- 2,
- proc (a: f32) -> f32 { return ~~(a * 2); },
- proc (b: f32) -> f64 { return ~~(b + 6); }));
+ println(specific_compose_2(
+ 2,
+ proc (a: f32) -> f32 { return ~~(a * 2); },
+ proc (b: f32) -> f64 { return ~~(b + 6); }));
arr1 : [..] f32;
use package core
main :: proc (args: [] cstr) {
- println("Hello, World!");
+ println("Hello, World!");
}