diff options
| author | gingerBill <bill@gingerbill.org> | 2018-07-28 00:41:31 +0100 |
|---|---|---|
| committer | gingerBill <bill@gingerbill.org> | 2018-07-28 00:41:31 +0100 |
| commit | c3c783424604b14cdd86950bf4b0aaded1f97316 (patch) | |
| tree | cf8eb6d5f40545bdd63c2b8f81ffa645119e8473 /src/common.cpp | |
| parent | d0e04bf569465196e0679bf068126988dcab5122 (diff) | |
BigInt support in the constant system
Diffstat (limited to 'src/common.cpp')
| -rw-r--r-- | src/common.cpp | 46 |
1 files changed, 44 insertions, 2 deletions
diff --git a/src/common.cpp b/src/common.cpp index adeebc410..e7024ae43 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -3,6 +3,10 @@ #include <xmmintrin.h> #endif +#if defined(GB_COMPILER_MSVC) +#include <intrin.h> +#endif + #define GB_IMPLEMENTATION #include "gb/gb.h" @@ -200,7 +204,7 @@ u64 u64_from_string(String string) { } String u64_to_string(u64 v, char *out_buf, isize out_buf_len) { - char buf[200] = {0}; + char buf[32] = {0}; isize i = gb_size_of(buf); u64 b = 10; @@ -215,7 +219,7 @@ String u64_to_string(u64 v, char *out_buf, isize out_buf_len) { return make_string(cast(u8 *)out_buf, len); } String i64_to_string(i64 a, char *out_buf, isize out_buf_len) { - char buf[200] = {0}; + char buf[32] = {0}; isize i = gb_size_of(buf); bool negative = false; if (a < 0) { @@ -276,6 +280,44 @@ gb_global u64 const unsigned_integer_maxs[] = { }; +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) { + *result = x - y; + return *result > x; +} + +void mul_overflow_u64(u64 x, u64 y, u64 *lo, u64 *hi) { +#if defined(GB_COMPILER_MSVC) + *lo = _umul128(x, y, hi); +#else + // URL(bill): https://stackoverflow.com/questions/25095741/how-can-i-multiply-64-bit-operands-and-get-128-bit-result-portably#25096197 + u64 u1, v1, w1, t, w3, k; + + u1 = (x & 0xffffffff); + v1 = (y & 0xffffffff); + t = (u1 * v1); + w3 = (t & 0xffffffff); + k = (t >> 32); + + x >>= 32; + t = (x * v1) + k; + k = (t & 0xffffffff); + w1 = (t >> 32); + + y >>= 32; + t = (u1 * y) + k; + k = (t >> 32); + + *hi = (x * y) + w1 + k; + *lo = (t << 32) + w3; +#endif +} + + #include "map.cpp" #include "ptr_set.cpp" |