diff options
| author | gingerBill <gingerBill@users.noreply.github.com> | 2023-01-01 13:26:43 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-01-01 13:26:43 +0000 |
| commit | 28fb35f2f7a6ffd75e76dd95352f4194d79b3166 (patch) | |
| tree | eac021b897fe6525a076264d5545aac6c96cfbb5 /src/common.cpp | |
| parent | 547c7bce1b28757415c553830a18d94636cedbf8 (diff) | |
| parent | c1384afe2fd705ce075277aa8dc6bc259dc94cdc (diff) | |
Merge pull request #2263 from odin-lang/compiler-improvements-2022-12
Compiler Improvements for 2022-12
Diffstat (limited to 'src/common.cpp')
| -rw-r--r-- | src/common.cpp | 99 |
1 files changed, 54 insertions, 45 deletions
diff --git a/src/common.cpp b/src/common.cpp index bc421b5fd..3624446f1 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -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 @@ -50,15 +50,19 @@ void debugf(char const *fmt, ...); #include "string.cpp" #include "range_cache.cpp" +#if defined(GB_SYSTEM_WINDOWS) + #pragma warning(push) + #pragma warning(disable: 4505) +#endif -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 +70,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 +78,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 +86,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 +95,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 +116,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 +137,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 +166,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 +209,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 +224,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 +286,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 +346,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 +367,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 +391,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 +405,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 +421,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 +436,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 +446,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 +461,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 +472,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 +484,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 +497,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 +508,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 +519,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 +532,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 +583,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 +599,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 +627,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 +710,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 +815,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 +874,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 +892,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++) { @@ -900,3 +904,8 @@ Slice<DistanceAndTarget> did_you_mean_results(DidYouMeanAnswers *d) { } return slice_array(d->distances, 0, count); } + + +#if defined(GB_SYSTEM_WINDOWS) + #pragma warning(pop) +#endif
\ No newline at end of file |