mirror of
https://github.com/odin-lang/Odin.git
synced 2026-04-18 20:40:28 +00:00
gb_internal a lot
This commit is contained in:
@@ -29,14 +29,14 @@
|
||||
#include <string.h>
|
||||
#include <atomic> // Because I wanted the C++11 memory order semantics, of which gb.h does not offer (because it was a C89 library)
|
||||
|
||||
gbAllocator heap_allocator(void);
|
||||
gb_internal gbAllocator heap_allocator(void);
|
||||
|
||||
#define for_array(index_, array_) for (isize index_ = 0; index_ < (array_).count; index_++)
|
||||
|
||||
i32 next_pow2(i32 n);
|
||||
i64 next_pow2(i64 n);
|
||||
isize next_pow2_isize(isize n);
|
||||
void debugf(char const *fmt, ...);
|
||||
gb_internal i32 next_pow2(i32 n);
|
||||
gb_internal i64 next_pow2(i64 n);
|
||||
gb_internal isize next_pow2_isize(isize n);
|
||||
gb_internal void debugf(char const *fmt, ...);
|
||||
|
||||
#if defined(GB_SYSTEM_WINDOWS) && defined(GB_ARCH_32_BIT)
|
||||
#error Odin on Windows requires a 64-bit build-system. The 'Developer Command Prompt' for VS still defaults to 32-bit shell. The 64-bit shell can be found under the name 'x64 Native Tools Command Prompt' for VS. For more information, please see https://odin-lang.org/docs/install/#for-windows
|
||||
@@ -51,14 +51,14 @@ void debugf(char const *fmt, ...);
|
||||
#include "range_cache.cpp"
|
||||
|
||||
|
||||
bool is_power_of_two(i64 x) {
|
||||
gb_internal gb_inline bool is_power_of_two(i64 x) {
|
||||
if (x <= 0) {
|
||||
return false;
|
||||
}
|
||||
return !(x & (x-1));
|
||||
}
|
||||
|
||||
int isize_cmp(isize x, isize y) {
|
||||
gb_internal int isize_cmp(isize x, isize y) {
|
||||
if (x < y) {
|
||||
return -1;
|
||||
} else if (x > y) {
|
||||
@@ -66,7 +66,7 @@ int isize_cmp(isize x, isize y) {
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int u64_cmp(u64 x, u64 y) {
|
||||
gb_internal int u64_cmp(u64 x, u64 y) {
|
||||
if (x < y) {
|
||||
return -1;
|
||||
} else if (x > y) {
|
||||
@@ -74,7 +74,7 @@ int u64_cmp(u64 x, u64 y) {
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int i64_cmp(i64 x, i64 y) {
|
||||
gb_internal int i64_cmp(i64 x, i64 y) {
|
||||
if (x < y) {
|
||||
return -1;
|
||||
} else if (x > y) {
|
||||
@@ -82,7 +82,7 @@ int i64_cmp(i64 x, i64 y) {
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int i32_cmp(i32 x, i32 y) {
|
||||
gb_internal int i32_cmp(i32 x, i32 y) {
|
||||
if (x < y) {
|
||||
return -1;
|
||||
} else if (x > y) {
|
||||
@@ -91,7 +91,7 @@ int i32_cmp(i32 x, i32 y) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32 fnv32a(void const *data, isize len) {
|
||||
gb_internal u32 fnv32a(void const *data, isize len) {
|
||||
u8 const *bytes = cast(u8 const *)data;
|
||||
u32 h = 0x811c9dc5;
|
||||
|
||||
@@ -112,7 +112,7 @@ u32 fnv32a(void const *data, isize len) {
|
||||
return h;
|
||||
}
|
||||
|
||||
u64 fnv64a(void const *data, isize len) {
|
||||
gb_internal u64 fnv64a(void const *data, isize len) {
|
||||
u8 const *bytes = cast(u8 const *)data;
|
||||
u64 h = 0xcbf29ce484222325ull;
|
||||
|
||||
@@ -133,7 +133,7 @@ u64 fnv64a(void const *data, isize len) {
|
||||
return h;
|
||||
}
|
||||
|
||||
u64 u64_digit_value(Rune r) {
|
||||
gb_internal u64 u64_digit_value(Rune r) {
|
||||
switch (r) {
|
||||
case '0': return 0;
|
||||
case '1': return 1;
|
||||
@@ -162,7 +162,7 @@ u64 u64_digit_value(Rune r) {
|
||||
}
|
||||
|
||||
|
||||
u64 u64_from_string(String string) {
|
||||
gb_internal u64 u64_from_string(String string) {
|
||||
u64 base = 10;
|
||||
bool has_prefix = false;
|
||||
if (string.len > 2 && string[0] == '0') {
|
||||
@@ -205,7 +205,7 @@ gb_global char const global_num_to_char_table[] =
|
||||
"abcdefghijklmnopqrstuvwxyz"
|
||||
"@$";
|
||||
|
||||
String u64_to_string(u64 v, char *out_buf, isize out_buf_len) {
|
||||
gb_internal String u64_to_string(u64 v, char *out_buf, isize out_buf_len) {
|
||||
char buf[32] = {0};
|
||||
isize i = gb_size_of(buf);
|
||||
|
||||
@@ -220,7 +220,7 @@ String u64_to_string(u64 v, char *out_buf, isize out_buf_len) {
|
||||
gb_memmove(out_buf, &buf[i], len);
|
||||
return make_string(cast(u8 *)out_buf, len);
|
||||
}
|
||||
String i64_to_string(i64 a, char *out_buf, isize out_buf_len) {
|
||||
gb_internal String i64_to_string(i64 a, char *out_buf, isize out_buf_len) {
|
||||
char buf[32] = {0};
|
||||
isize i = gb_size_of(buf);
|
||||
bool negative = false;
|
||||
@@ -282,17 +282,17 @@ gb_global u64 const unsigned_integer_maxs[] = {
|
||||
};
|
||||
|
||||
|
||||
bool add_overflow_u64(u64 x, u64 y, u64 *result) {
|
||||
gb_internal bool add_overflow_u64(u64 x, u64 y, u64 *result) {
|
||||
*result = x + y;
|
||||
return *result < x || *result < y;
|
||||
}
|
||||
|
||||
bool sub_overflow_u64(u64 x, u64 y, u64 *result) {
|
||||
gb_internal bool sub_overflow_u64(u64 x, u64 y, u64 *result) {
|
||||
*result = x - y;
|
||||
return *result > x;
|
||||
}
|
||||
|
||||
void mul_overflow_u64(u64 x, u64 y, u64 *lo, u64 *hi) {
|
||||
gb_internal void mul_overflow_u64(u64 x, u64 y, u64 *lo, u64 *hi) {
|
||||
#if defined(GB_COMPILER_MSVC) && defined(GB_ARCH_64_BIT)
|
||||
*lo = _umul128(x, y, hi);
|
||||
#else
|
||||
@@ -342,7 +342,7 @@ struct StringIntern {
|
||||
PtrMap<uintptr, StringIntern *> string_intern_map = {}; // Key: u64
|
||||
gb_global Arena string_intern_arena = {};
|
||||
|
||||
char const *string_intern(char const *text, isize len) {
|
||||
gb_internal char const *string_intern(char const *text, isize len) {
|
||||
u64 hash = gb_fnv64a(text, len);
|
||||
uintptr key = cast(uintptr)(hash ? hash : 1);
|
||||
StringIntern **found = map_get(&string_intern_map, key);
|
||||
@@ -363,18 +363,18 @@ char const *string_intern(char const *text, isize len) {
|
||||
return new_intern->str;
|
||||
}
|
||||
|
||||
char const *string_intern(String const &string) {
|
||||
gb_internal char const *string_intern(String const &string) {
|
||||
return string_intern(cast(char const *)string.text, string.len);
|
||||
}
|
||||
|
||||
void init_string_interner(void) {
|
||||
gb_internal void init_string_interner(void) {
|
||||
map_init(&string_intern_map, heap_allocator());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
i32 next_pow2(i32 n) {
|
||||
gb_internal i32 next_pow2(i32 n) {
|
||||
if (n <= 0) {
|
||||
return 0;
|
||||
}
|
||||
@@ -387,7 +387,7 @@ i32 next_pow2(i32 n) {
|
||||
n++;
|
||||
return n;
|
||||
}
|
||||
i64 next_pow2(i64 n) {
|
||||
gb_internal i64 next_pow2(i64 n) {
|
||||
if (n <= 0) {
|
||||
return 0;
|
||||
}
|
||||
@@ -401,7 +401,7 @@ i64 next_pow2(i64 n) {
|
||||
n++;
|
||||
return n;
|
||||
}
|
||||
isize next_pow2_isize(isize n) {
|
||||
gb_internal isize next_pow2_isize(isize n) {
|
||||
if (n <= 0) {
|
||||
return 0;
|
||||
}
|
||||
@@ -417,7 +417,7 @@ isize next_pow2_isize(isize n) {
|
||||
n++;
|
||||
return n;
|
||||
}
|
||||
u32 next_pow2_u32(u32 n) {
|
||||
gb_internal u32 next_pow2_u32(u32 n) {
|
||||
if (n == 0) {
|
||||
return 0;
|
||||
}
|
||||
@@ -432,7 +432,7 @@ u32 next_pow2_u32(u32 n) {
|
||||
}
|
||||
|
||||
|
||||
i32 bit_set_count(u32 x) {
|
||||
gb_internal i32 bit_set_count(u32 x) {
|
||||
x -= ((x >> 1) & 0x55555555);
|
||||
x = (((x >> 2) & 0x33333333) + (x & 0x33333333));
|
||||
x = (((x >> 4) + x) & 0x0f0f0f0f);
|
||||
@@ -442,13 +442,13 @@ i32 bit_set_count(u32 x) {
|
||||
return cast(i32)(x & 0x0000003f);
|
||||
}
|
||||
|
||||
i64 bit_set_count(u64 x) {
|
||||
gb_internal i64 bit_set_count(u64 x) {
|
||||
u32 a = *(cast(u32 *)&x);
|
||||
u32 b = *(cast(u32 *)&x + 1);
|
||||
return bit_set_count(a) + bit_set_count(b);
|
||||
}
|
||||
|
||||
u32 floor_log2(u32 x) {
|
||||
gb_internal u32 floor_log2(u32 x) {
|
||||
x |= x >> 1;
|
||||
x |= x >> 2;
|
||||
x |= x >> 4;
|
||||
@@ -457,7 +457,7 @@ u32 floor_log2(u32 x) {
|
||||
return cast(u32)(bit_set_count(x) - 1);
|
||||
}
|
||||
|
||||
u64 floor_log2(u64 x) {
|
||||
gb_internal u64 floor_log2(u64 x) {
|
||||
x |= x >> 1;
|
||||
x |= x >> 2;
|
||||
x |= x >> 4;
|
||||
@@ -468,7 +468,7 @@ u64 floor_log2(u64 x) {
|
||||
}
|
||||
|
||||
|
||||
u32 ceil_log2(u32 x) {
|
||||
gb_internal u32 ceil_log2(u32 x) {
|
||||
i32 y = cast(i32)(x & (x-1));
|
||||
y |= -y;
|
||||
y >>= 32-1;
|
||||
@@ -480,7 +480,7 @@ u32 ceil_log2(u32 x) {
|
||||
return cast(u32)(bit_set_count(x) - 1 - y);
|
||||
}
|
||||
|
||||
u64 ceil_log2(u64 x) {
|
||||
gb_internal u64 ceil_log2(u64 x) {
|
||||
i64 y = cast(i64)(x & (x-1));
|
||||
y |= -y;
|
||||
y >>= 64-1;
|
||||
@@ -493,7 +493,7 @@ u64 ceil_log2(u64 x) {
|
||||
return cast(u64)(bit_set_count(x) - 1 - y);
|
||||
}
|
||||
|
||||
u32 prev_pow2(u32 n) {
|
||||
gb_internal u32 prev_pow2(u32 n) {
|
||||
if (n == 0) {
|
||||
return 0;
|
||||
}
|
||||
@@ -504,7 +504,7 @@ u32 prev_pow2(u32 n) {
|
||||
n |= n >> 16;
|
||||
return n - (n >> 1);
|
||||
}
|
||||
i32 prev_pow2(i32 n) {
|
||||
gb_internal i32 prev_pow2(i32 n) {
|
||||
if (n <= 0) {
|
||||
return 0;
|
||||
}
|
||||
@@ -515,7 +515,7 @@ i32 prev_pow2(i32 n) {
|
||||
n |= n >> 16;
|
||||
return n - (n >> 1);
|
||||
}
|
||||
i64 prev_pow2(i64 n) {
|
||||
gb_internal i64 prev_pow2(i64 n) {
|
||||
if (n <= 0) {
|
||||
return 0;
|
||||
}
|
||||
@@ -528,7 +528,7 @@ i64 prev_pow2(i64 n) {
|
||||
return n - (n >> 1);
|
||||
}
|
||||
|
||||
u16 f32_to_f16(f32 value) {
|
||||
gb_internal u16 f32_to_f16(f32 value) {
|
||||
union { u32 i; f32 f; } v;
|
||||
i32 i, s, e, m;
|
||||
|
||||
@@ -579,7 +579,7 @@ u16 f32_to_f16(f32 value) {
|
||||
}
|
||||
}
|
||||
|
||||
f32 f16_to_f32(u16 value) {
|
||||
gb_internal f32 f16_to_f32(u16 value) {
|
||||
typedef union { u32 u; f32 f; } fp32;
|
||||
fp32 v;
|
||||
|
||||
@@ -595,7 +595,7 @@ f32 f16_to_f32(u16 value) {
|
||||
return v.f;
|
||||
}
|
||||
|
||||
f64 gb_sqrt(f64 x) {
|
||||
gb_internal gb_inline f64 gb_sqrt(f64 x) {
|
||||
return sqrt(x);
|
||||
}
|
||||
|
||||
@@ -623,7 +623,7 @@ f64 gb_sqrt(f64 x) {
|
||||
|
||||
#if defined(GB_SYSTEM_WINDOWS)
|
||||
|
||||
wchar_t **command_line_to_wargv(wchar_t *cmd_line, int *_argc) {
|
||||
gb_internal wchar_t **command_line_to_wargv(wchar_t *cmd_line, int *_argc) {
|
||||
u32 i, j;
|
||||
|
||||
u32 len = cast(u32)string16_len(cmd_line);
|
||||
@@ -706,7 +706,7 @@ enum LoadedFileError {
|
||||
LoadedFile_COUNT,
|
||||
};
|
||||
|
||||
LoadedFileError load_file_32(char const *fullpath, LoadedFile *memory_mapped_file, bool copy_file_contents) {
|
||||
gb_internal LoadedFileError load_file_32(char const *fullpath, LoadedFile *memory_mapped_file, bool copy_file_contents) {
|
||||
LoadedFileError err = LoadedFile_None;
|
||||
|
||||
if (!copy_file_contents) {
|
||||
@@ -811,7 +811,7 @@ LoadedFileError load_file_32(char const *fullpath, LoadedFile *memory_mapped_fil
|
||||
|
||||
#define USE_DAMERAU_LEVENSHTEIN 1
|
||||
|
||||
isize levenstein_distance_case_insensitive(String const &a, String const &b) {
|
||||
gb_internal isize levenstein_distance_case_insensitive(String const &a, String const &b) {
|
||||
isize w = b.len+1;
|
||||
isize h = a.len+1;
|
||||
isize *matrix = gb_alloc_array(temporary_allocator(), isize, w*h);
|
||||
@@ -870,16 +870,16 @@ struct DidYouMeanAnswers {
|
||||
|
||||
enum {MAX_SMALLEST_DID_YOU_MEAN_DISTANCE = 3-USE_DAMERAU_LEVENSHTEIN};
|
||||
|
||||
DidYouMeanAnswers did_you_mean_make(gbAllocator allocator, isize cap, String const &key) {
|
||||
gb_internal DidYouMeanAnswers did_you_mean_make(gbAllocator allocator, isize cap, String const &key) {
|
||||
DidYouMeanAnswers d = {};
|
||||
array_init(&d.distances, allocator, 0, cap);
|
||||
d.key = key;
|
||||
return d;
|
||||
}
|
||||
void did_you_mean_destroy(DidYouMeanAnswers *d) {
|
||||
gb_internal void did_you_mean_destroy(DidYouMeanAnswers *d) {
|
||||
array_free(&d->distances);
|
||||
}
|
||||
void did_you_mean_append(DidYouMeanAnswers *d, String const &target) {
|
||||
gb_internal void did_you_mean_append(DidYouMeanAnswers *d, String const &target) {
|
||||
if (target.len == 0 || target == "_") {
|
||||
return;
|
||||
}
|
||||
@@ -888,7 +888,7 @@ void did_you_mean_append(DidYouMeanAnswers *d, String const &target) {
|
||||
dat.distance = levenstein_distance_case_insensitive(d->key, target);
|
||||
array_add(&d->distances, dat);
|
||||
}
|
||||
Slice<DistanceAndTarget> did_you_mean_results(DidYouMeanAnswers *d) {
|
||||
gb_internal Slice<DistanceAndTarget> did_you_mean_results(DidYouMeanAnswers *d) {
|
||||
gb_sort_array(d->distances.data, d->distances.count, gb_isize_cmp(gb_offset_of(DistanceAndTarget, distance)));
|
||||
isize count = 0;
|
||||
for (isize i = 0; i < d->distances.count; i++) {
|
||||
|
||||
Reference in New Issue
Block a user