mirror of
https://github.com/Ed94/Odin.git
synced 2026-06-23 14:14:59 -07:00
min, max, abs
This commit is contained in:
+1
-1
@@ -3,7 +3,7 @@
|
||||
#load "file.odin"
|
||||
|
||||
print_string :: proc(s: string) {
|
||||
file_write(file_get_standard(FileStandard.OUTPUT), s as []byte)
|
||||
file_write(file_get_standard(File_Standard.OUTPUT), s as []byte)
|
||||
}
|
||||
|
||||
byte_reverse :: proc(b: []byte) {
|
||||
|
||||
+8
-15
@@ -4,24 +4,17 @@
|
||||
|
||||
main :: proc() {
|
||||
|
||||
print_int(min(1, 2)); nl()
|
||||
print_int(max(1, 2)); nl()
|
||||
print_int(abs(-1337)); nl()
|
||||
|
||||
a, b, c := 1, 2, -1337
|
||||
print_int(min(a, b)); nl()
|
||||
print_int(max(a, b)); nl()
|
||||
print_int(abs(c) as int); nl()
|
||||
|
||||
match x := "1"; x {
|
||||
case "1":
|
||||
print_string("1!\n")
|
||||
case "2":
|
||||
print_string("2!\n")
|
||||
if true {
|
||||
break
|
||||
}
|
||||
case "3", "4":
|
||||
print_string("3 or 4!\n")
|
||||
fallthrough
|
||||
default:
|
||||
print_string("default!\n")
|
||||
}
|
||||
|
||||
nl()
|
||||
|
||||
/*
|
||||
Vec3 :: type struct { x, y, z: f32 }
|
||||
Entity :: type struct {
|
||||
|
||||
+5
-5
@@ -34,7 +34,7 @@ file_write :: proc(f: ^File, buf: []byte) -> bool {
|
||||
return WriteFile(f.handle, ^buf[0], len(buf) as i32, ^bytes_written, null) != 0
|
||||
}
|
||||
|
||||
FileStandard :: type enum {
|
||||
File_Standard :: type enum {
|
||||
INPUT,
|
||||
OUTPUT,
|
||||
ERROR,
|
||||
@@ -42,12 +42,12 @@ FileStandard :: type enum {
|
||||
}
|
||||
|
||||
__std_file_set := false;
|
||||
__std_files: [FileStandard.COUNT as int]File;
|
||||
__std_files: [File_Standard.COUNT as int]File;
|
||||
|
||||
file_get_standard :: proc(std: FileStandard) -> ^File {
|
||||
// using FileStandard;
|
||||
file_get_standard :: proc(std: File_Standard) -> ^File {
|
||||
// using File_Standard;
|
||||
if (!__std_file_set) {
|
||||
using FileStandard
|
||||
using File_Standard
|
||||
__std_files[INPUT] .handle = GetStdHandle(STD_INPUT_HANDLE)
|
||||
__std_files[OUTPUT].handle = GetStdHandle(STD_OUTPUT_HANDLE)
|
||||
__std_files[ERROR] .handle = GetStdHandle(STD_ERROR_HANDLE)
|
||||
|
||||
+3
-10
@@ -29,17 +29,10 @@ fsqrt :: proc(x: f32) -> f32 #foreign "llvm.sqrt.f32"
|
||||
fsin :: proc(x: f32) -> f32 #foreign "llvm.sin.f32"
|
||||
fcos :: proc(x: f32) -> f32 #foreign "llvm.cos.f32"
|
||||
flerp :: proc(a, b, t: f32) -> f32 { return a*(1-t) + b*t }
|
||||
fclamp :: proc(x, lower, upper: f32) -> f32 { return fmin(fmax(x, lower), upper) }
|
||||
fclamp :: proc(x, lower, upper: f32) -> f32 { return min(max(x, lower), upper) }
|
||||
fclamp01 :: proc(x: f32) -> f32 { return fclamp(x, 0, 1) }
|
||||
fabs :: proc(x: f32) -> f32 { if x < 0 { x = -x } return x }
|
||||
fsign :: proc(x: f32) -> f32 { if x >= 0 { return +1 } return -1 }
|
||||
|
||||
fmin :: proc(a, b: f32) -> f32 { if a < b { return a } return b }
|
||||
fmax :: proc(a, b: f32) -> f32 { if a > b { return a } return b }
|
||||
fmin3 :: proc(a, b, c: f32) -> f32 { return fmin(fmin(a, b), c) }
|
||||
fmax3 :: proc(a, b, c: f32) -> f32 { return fmax(fmax(a, b), c) }
|
||||
|
||||
|
||||
copy_sign :: proc(x, y: f32) -> f32 {
|
||||
ix := x transmute u32
|
||||
iy := y transmute u32
|
||||
@@ -76,8 +69,8 @@ remainder :: proc(x, y: f32) -> f32 {
|
||||
}
|
||||
|
||||
fmod :: proc(x, y: f32) -> f32 {
|
||||
y = fabs(y)
|
||||
result := remainder(fabs(x), y)
|
||||
y = abs(y)
|
||||
result := remainder(abs(x), y)
|
||||
if fsign(result) < 0 {
|
||||
result += y
|
||||
}
|
||||
|
||||
+29
-16
@@ -11,8 +11,15 @@ heap_free :: proc(ptr: rawptr) {
|
||||
}
|
||||
|
||||
|
||||
memory_zero :: proc(data: rawptr, len: int) {
|
||||
d := slice_ptr(data as ^byte, len)
|
||||
for i := 0; i < len; i++ {
|
||||
d[i] = 0
|
||||
}
|
||||
}
|
||||
|
||||
memory_compare :: proc(dst, src: rawptr, len: int) -> int {
|
||||
s1, s2: ^u8 = dst, src
|
||||
s1, s2: ^byte = dst, src
|
||||
for i := 0; i < len; i++ {
|
||||
a := ptr_offset(s1, i)^
|
||||
b := ptr_offset(s2, i)^
|
||||
@@ -31,7 +38,7 @@ memory_copy :: proc(dst, src: rawptr, n: int) #inline {
|
||||
v128b :: type {4}u32
|
||||
static_assert(align_of(v128b) == 16)
|
||||
|
||||
d, s: ^u8 = dst, src
|
||||
d, s: ^byte = dst, src
|
||||
|
||||
for ; s as uint % 16 != 0 && n != 0; n-- {
|
||||
d^ = s^
|
||||
@@ -158,7 +165,7 @@ memory_copy :: proc(dst, src: rawptr, n: int) #inline {
|
||||
}
|
||||
|
||||
memory_move :: proc(dst, src: rawptr, n: int) #inline {
|
||||
d, s: ^u8 = dst, src
|
||||
d, s: ^byte = dst, src
|
||||
if d == s {
|
||||
return
|
||||
}
|
||||
@@ -264,7 +271,7 @@ __string_ge :: proc(a, b : string) -> bool #inline { return __string_cmp(a, b) >
|
||||
|
||||
|
||||
|
||||
AllocationMode :: type enum {
|
||||
Allocation_Mode :: type enum {
|
||||
ALLOC,
|
||||
DEALLOC,
|
||||
DEALLOC_ALL,
|
||||
@@ -273,12 +280,12 @@ AllocationMode :: type enum {
|
||||
|
||||
|
||||
|
||||
AllocatorProc :: type proc(allocator_data: rawptr, mode: AllocationMode,
|
||||
size, alignment: int,
|
||||
old_memory: rawptr, old_size: int, flags: u64) -> rawptr
|
||||
Allocator_Proc :: type proc(allocator_data: rawptr, mode: Allocation_Mode,
|
||||
size, alignment: int,
|
||||
old_memory: rawptr, old_size: int, flags: u64) -> rawptr
|
||||
|
||||
Allocator :: type struct {
|
||||
procedure: AllocatorProc;
|
||||
procedure: Allocator_Proc;
|
||||
data: rawptr
|
||||
}
|
||||
|
||||
@@ -309,18 +316,18 @@ alloc :: proc(size: int) -> rawptr #inline { return alloc_align(size, DEFAULT_AL
|
||||
alloc_align :: proc(size, alignment: int) -> rawptr #inline {
|
||||
__check_context()
|
||||
a := context.allocator
|
||||
return a.procedure(a.data, AllocationMode.ALLOC, size, alignment, null, 0, 0)
|
||||
return a.procedure(a.data, Allocation_Mode.ALLOC, size, alignment, null, 0, 0)
|
||||
}
|
||||
|
||||
dealloc :: proc(ptr: rawptr) #inline {
|
||||
__check_context()
|
||||
a := context.allocator
|
||||
_ = a.procedure(a.data, AllocationMode.DEALLOC, 0, 0, ptr, 0, 0)
|
||||
_ = a.procedure(a.data, Allocation_Mode.DEALLOC, 0, 0, ptr, 0, 0)
|
||||
}
|
||||
dealloc_all :: proc(ptr: rawptr) #inline {
|
||||
__check_context()
|
||||
a := context.allocator
|
||||
_ = a.procedure(a.data, AllocationMode.DEALLOC_ALL, 0, 0, ptr, 0, 0)
|
||||
_ = a.procedure(a.data, Allocation_Mode.DEALLOC_ALL, 0, 0, ptr, 0, 0)
|
||||
}
|
||||
|
||||
|
||||
@@ -328,7 +335,7 @@ resize :: proc(ptr: rawptr, old_size, new_size: int) -> rawptr #inline { r
|
||||
resize_align :: proc(ptr: rawptr, old_size, new_size, alignment: int) -> rawptr #inline {
|
||||
__check_context()
|
||||
a := context.allocator
|
||||
return a.procedure(a.data, AllocationMode.RESIZE, new_size, alignment, ptr, old_size, 0)
|
||||
return a.procedure(a.data, Allocation_Mode.RESIZE, new_size, alignment, ptr, old_size, 0)
|
||||
}
|
||||
|
||||
|
||||
@@ -355,19 +362,25 @@ default_resize_align :: proc(old_memory: rawptr, old_size, new_size, alignment:
|
||||
if new_memory == null {
|
||||
return null
|
||||
}
|
||||
_ = copy(slice_ptr(new_memory as ^u8, new_size), slice_ptr(old_memory as ^u8, old_size))
|
||||
min_size := old_size;
|
||||
if min_size > new_size {
|
||||
min_size = new_size;
|
||||
}
|
||||
memory_copy(new_memory, old_memory, min_size);
|
||||
dealloc(old_memory)
|
||||
return new_memory
|
||||
}
|
||||
|
||||
|
||||
__default_allocator_proc :: proc(allocator_data: rawptr, mode: AllocationMode,
|
||||
__default_allocator_proc :: proc(allocator_data: rawptr, mode: Allocation_Mode,
|
||||
size, alignment: int,
|
||||
old_memory: rawptr, old_size: int, flags: u64) -> rawptr {
|
||||
using AllocationMode
|
||||
using Allocation_Mode
|
||||
match mode {
|
||||
case ALLOC:
|
||||
return heap_alloc(size)
|
||||
data := heap_alloc(size)
|
||||
memory_zero(data, size)
|
||||
return data
|
||||
case RESIZE:
|
||||
return default_resize_align(old_memory, old_size, size, alignment)
|
||||
case DEALLOC:
|
||||
|
||||
Reference in New Issue
Block a user