aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorGinger Bill <bill@gingerbill.org>2016-09-02 14:14:12 +0100
committerGinger Bill <bill@gingerbill.org>2016-09-02 14:14:12 +0100
commit25e9b9bc87a5b4fa14fc7d47ca3077849ee5648d (patch)
tree115e165013c52edd77777f9bd64e212e0b29f7c8 /examples
parentfa09d805e23c59cb881573a7a1aee5fbc5752ea2 (diff)
min, max, abs
Diffstat (limited to 'examples')
-rw-r--r--examples/basic.odin2
-rw-r--r--examples/demo.odin23
-rw-r--r--examples/file.odin10
-rw-r--r--examples/math.odin13
-rw-r--r--examples/runtime.odin45
5 files changed, 46 insertions, 47 deletions
diff --git a/examples/basic.odin b/examples/basic.odin
index 96282ccd8..26c522d4c 100644
--- a/examples/basic.odin
+++ b/examples/basic.odin
@@ -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) {
diff --git a/examples/demo.odin b/examples/demo.odin
index 492974e3c..4d2344185 100644
--- a/examples/demo.odin
+++ b/examples/demo.odin
@@ -4,24 +4,17 @@
main :: proc() {
+ print_int(min(1, 2)); nl()
+ print_int(max(1, 2)); nl()
+ print_int(abs(-1337)); 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")
- }
+ 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()
- nl()
+ nl()
/*
Vec3 :: type struct { x, y, z: f32 }
Entity :: type struct {
diff --git a/examples/file.odin b/examples/file.odin
index 5b3aa9f8c..7ee8770e0 100644
--- a/examples/file.odin
+++ b/examples/file.odin
@@ -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)
diff --git a/examples/math.odin b/examples/math.odin
index ee0245d4b..d10bcd5d4 100644
--- a/examples/math.odin
+++ b/examples/math.odin
@@ -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
}
diff --git a/examples/runtime.odin b/examples/runtime.odin
index 6663819d4..a512a9b8e 100644
--- a/examples/runtime.odin
+++ b/examples/runtime.odin
@@ -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: