transmute(type)x; Minor code clean up

This commit is contained in:
Ginger Bill
2017-07-30 14:52:42 +01:00
parent 655931f0ea
commit 62a72f0163
16 changed files with 808 additions and 188 deletions
+36 -28
View File
@@ -160,11 +160,10 @@ Allocator :: struct #ordered {
Context :: struct #ordered {
allocator: Allocator;
thread_id: int;
allocator: Allocator;
user_data: rawptr;
user_data: any;
user_index: int;
derived: any; // May be used for derived data types
@@ -173,9 +172,9 @@ Context :: struct #ordered {
DEFAULT_ALIGNMENT :: align_of([vector 4]f32);
SourceCodeLocation :: struct #ordered {
fully_pathed_filename: string;
line, column: i64;
procedure: string;
file_path: string;
line, column: i64;
procedure: string;
}
@@ -371,7 +370,7 @@ pop :: proc(array: ^$T/[]$E) -> E #cc_contextless {
if array == nil do return E{};
assert(len(array) > 0);
res := array[len(array)-1];
(cast(^raw.Slice)array).len -= 1;
(^raw.Slice)(array).len -= 1;
return res;
}
@@ -379,7 +378,7 @@ pop :: proc(array: ^$T/[dynamic]$E) -> E #cc_contextless {
if array == nil do return E{};
assert(len(array) > 0);
res := array[len(array)-1];
(cast(^raw.DynamicArray)array).len -= 1;
(^raw.DynamicArray)(array).len -= 1;
return res;
}
@@ -445,25 +444,25 @@ __get_map_key :: proc(key: $K) -> __MapKey #cc_contextless {
match _ in ti {
case TypeInfo.Integer:
match 8*size_of(key) {
case 8: map_key.hash = u128((cast( ^u8)&key)^);
case 16: map_key.hash = u128((cast( ^u16)&key)^);
case 32: map_key.hash = u128((cast( ^u32)&key)^);
case 64: map_key.hash = u128((cast( ^u64)&key)^);
case 128: map_key.hash = u128((cast(^u128)&key)^);
case 8: map_key.hash = u128(( ^u8)(&key)^);
case 16: map_key.hash = u128(( ^u16)(&key)^);
case 32: map_key.hash = u128(( ^u32)(&key)^);
case 64: map_key.hash = u128(( ^u64)(&key)^);
case 128: map_key.hash = u128((^u128)(&key)^);
case: panic("Unhandled integer size");
}
case TypeInfo.Rune:
map_key.hash = u128((cast(^rune)&key)^);
case TypeInfo.Pointer:
map_key.hash = u128(uint((cast(^rawptr)&key)^));
map_key.hash = u128(uint((^rawptr)(&key)^));
case TypeInfo.Float:
match 8*size_of(key) {
case 32: map_key.hash = u128((cast(^u32)&key)^);
case 64: map_key.hash = u128((cast(^u64)&key)^);
case 32: map_key.hash = u128((^u32)(&key)^);
case 64: map_key.hash = u128((^u64)(&key)^);
case: panic("Unhandled float size");
}
case TypeInfo.String:
str := (cast(^string)&key)^;
str := (^string)(&key)^;
map_key.hash = __default_hash_string(str);
map_key.str = str;
case:
@@ -494,9 +493,9 @@ new_clone :: proc(data: $T) -> ^T #inline {
}
free :: proc(ptr: rawptr) do free_ptr(ptr);
free :: proc(str: $T/string) do free_ptr((cast(^raw.String)&str).data);
free :: proc(array: $T/[dynamic]$E) do free_ptr((cast(^raw.DynamicArray)&array).data);
free :: proc(slice: $T/[]$E) do free_ptr((cast(^raw.Slice)&slice).data);
free :: proc(str: $T/string) do free_ptr((^raw.String )(&str).data);
free :: proc(array: $T/[dynamic]$E) do free_ptr((^raw.DynamicArray)(&array).data);
free :: proc(slice: $T/[]$E) do free_ptr((^raw.Slice )(&slice).data);
free :: proc(m: $T/map[$K]$V) {
raw := cast(^raw.DynamicMap)&m;
free(raw.hashes);
@@ -508,14 +507,14 @@ free :: proc(m: $T/map[$K]$V) {
/*
make :: proc(T: type/[]$E, len: int, using location := #caller_location) -> T {
cap := len;
__slice_expr_error(fully_pathed_filename, int(line), int(column), 0, len, cap);
__slice_expr_error(file_path, int(line), int(column), 0, len, cap);
data := cast(^E)alloc(len * size_of(E), align_of(E));
for i in 0..len do (data+i)^ = E{};
s := raw.Slice{data = data, len = len, cap = len};
return (cast(^T)&s)^;
}
make :: proc(T: type/[]$E, len, cap: int, using location := #caller_location) -> T {
__slice_expr_error(fully_pathed_filename, int(line), int(column), 0, len, cap);
__slice_expr_error(file_path, int(line), int(column), 0, len, cap);
data := cast(^E)alloc(len * size_of(E), align_of(E));
for i in 0..len do (data+i)^ = E{};
s := raw.Slice{data = data, len = len, cap = len};
@@ -523,14 +522,14 @@ make :: proc(T: type/[]$E, len, cap: int, using location := #caller_location) ->
}
make :: proc(T: type/[dynamic]$E, len: int = 8, using location := #caller_location) -> T {
cap := len;
__slice_expr_error(fully_pathed_filename, int(line), int(column), 0, len, cap);
__slice_expr_error(file_path, int(line), int(column), 0, len, cap);
data := cast(^E)alloc(cap * size_of(E), align_of(E));
for i in 0..len do (data+i)^ = E{};
s := raw.DynamicArray{data = data, len = len, cap = cap, allocator = context.allocator};
return (cast(^T)&s)^;
}
make :: proc(T: type/[dynamic]$E, len, cap: int, using location := #caller_location) -> T {
__slice_expr_error(fully_pathed_filename, int(line), int(column), 0, len, cap);
__slice_expr_error(file_path, int(line), int(column), 0, len, cap);
data := cast(^E)alloc(cap * size_of(E), align_of(E));
for i in 0..len do (data+i)^ = E{};
s := raw.DynamicArray{data = data, len = len, cap = cap, allocator = context.allocator};
@@ -604,9 +603,9 @@ default_allocator :: proc() -> Allocator {
assert :: proc(condition: bool, message := "", using location := #caller_location) -> bool #cc_contextless {
if !condition {
if len(message) > 0 {
fmt.fprintf(os.stderr, "%s(%d:%d) Runtime assertion: %s\n", fully_pathed_filename, line, column, message);
fmt.fprintf(os.stderr, "%s(%d:%d) Runtime assertion: %s\n", file_path, line, column, message);
} else {
fmt.fprintf(os.stderr, "%s(%d:%d) Runtime assertion\n", fully_pathed_filename, line, column);
fmt.fprintf(os.stderr, "%s(%d:%d) Runtime assertion\n", file_path, line, column);
}
__debug_trap();
}
@@ -615,9 +614,9 @@ assert :: proc(condition: bool, message := "", using location := #caller_locatio
panic :: proc(message := "", using location := #caller_location) #cc_contextless {
if len(message) > 0 {
fmt.fprintf(os.stderr, "%s(%d:%d) Panic: %s\n", fully_pathed_filename, line, column, message);
fmt.fprintf(os.stderr, "%s(%d:%d) Panic: %s\n", file_path, line, column, message);
} else {
fmt.fprintf(os.stderr, "%s(%d:%d) Panic\n", fully_pathed_filename, line, column);
fmt.fprintf(os.stderr, "%s(%d:%d) Panic\n", file_path, line, column);
}
__debug_trap();
}
@@ -683,6 +682,15 @@ __string_decode_rune :: proc(s: string) -> (rune, int) #cc_contextless #inline {
return utf8.decode_rune(s);
}
__bounds_check_error_loc :: proc(using loc := #caller_location, index, count: int) #cc_contextless {
__bounds_check_error(file_path, int(line), int(column), index, count);
}
__slice_expr_error_loc :: proc(using loc := #caller_location, low, high, max: int) #cc_contextless {
__slice_expr_error(file_path, int(line), int(column), low, high, max);
}
__substring_expr_error_loc :: proc(using loc := #caller_location, low, high: int) #cc_contextless {
__substring_expr_error(file_path, int(line), int(column), low, high);
}
__mem_set :: proc(data: rawptr, value: i32, len: int) -> rawptr #cc_contextless {
if data == nil do return nil;
+14 -15
View File
@@ -5,17 +5,16 @@ __multi3 :: proc(a, b: u128) -> u128 #cc_c #link_name "__multi3" {
lower_mask :: u128(~u64(0) >> bits_in_dword_2);
when ODIN_ENDIAN == "bit" {
TWords :: struct #raw_union {
all: u128;
using _: struct {lo, hi: u64;};
TWords :: struct #raw_union {
all: u128;
using _: struct {
when ODIN_ENDIAN == "big" {
lo, hi: u64;
} else {
hi, lo: u64;
}
};
} else {
TWords :: struct #raw_union {
all: u128;
using _: struct {hi, lo: u64;};
};
}
};
r: TWords;
t: u64;
@@ -63,13 +62,13 @@ __i128_quo_mod :: proc(a, b: i128, rem: ^i128) -> (quo: i128) #cc_c #link_name "
b = (a~s) - s;
uquo: u128;
urem := __u128_quo_mod(transmute(u128, a), transmute(u128, b), &uquo);
iquo := transmute(i128, uquo);
irem := transmute(i128, urem);
urem := __u128_quo_mod(transmute(u128)a, transmute(u128)b, &uquo);
iquo := transmute(i128)uquo;
irem := transmute(i128)urem;
iquo = (iquo~s) - s;
irem = (irem~s) - s;
if rem != nil { rem^ = irem; }
if rem != nil do rem^ = irem;
return iquo;
}
@@ -78,7 +77,7 @@ __u128_quo_mod :: proc(a, b: u128, rem: ^u128) -> (quo: u128) #cc_c #link_name "
alo, ahi := u64(a), u64(a>>64);
blo, bhi := u64(b), u64(b>>64);
if b == 0 {
if rem != nil { rem^ = 0; }
if rem != nil do rem^ = 0;
return u128(alo/blo);
}
+1 -2
View File
@@ -149,8 +149,7 @@ aprintf :: proc(fmt: string, args: ...any) -> string {
}
// bprint* procedures return a string that was allocated with the current context
// They must be freed accordingly
// bprint* procedures return a string using a buffer from an array
bprint :: proc(buf: []u8, args: ...any) -> string {
sb := StringBuffer(buf[..0..len(buf)]);
return sbprint(&sb, ...args);
+6 -6
View File
@@ -60,19 +60,19 @@ sign :: proc(x: f64) -> f64 { if x >= 0 do return +1; return -1; }
copy_sign :: proc(x, y: f32) -> f32 {
ix := transmute(u32, x);
iy := transmute(u32, y);
ix := transmute(u32)x;
iy := transmute(u32)y;
ix &= 0x7fff_ffff;
ix |= iy & 0x8000_0000;
return transmute(f32, ix);
return transmute(f32)ix;
}
copy_sign :: proc(x, y: f64) -> f64 {
ix := transmute(u64, x);
iy := transmute(u64, y);
ix := transmute(u64)x;
iy := transmute(u64)y;
ix &= 0x7fff_ffff_ffff_ff;
ix |= iy & 0x8000_0000_0000_0000;
return transmute(f64, ix);
return transmute(f64)ix;
}
round :: proc(x: f32) -> f32 { if x >= 0 do return floor(x + 0.5); return ceil(x - 0.5); }
+2 -2
View File
@@ -226,10 +226,10 @@ generic_ftoa :: proc(buf: []u8, val: f64, fmt: u8, prec, bit_size: int) -> []u8
flt: ^FloatInfo;
match bit_size {
case 32:
bits = u64(transmute(u32, f32(val)));
bits = u64(transmute(u32)f32(val));
flt = &_f32_info;
case 64:
bits = transmute(u64, val);
bits = transmute(u64)val;
flt = &_f64_info;
case:
panic("strconv: invalid bit_size");
+1 -1
View File
@@ -5,7 +5,7 @@ import win32 "sys/windows.odin";
Thread :: struct {
using specific: OsSpecific;
procedure: Proc;
data: rawptr;
data: any;
user_index: int;
init_context: Context;