This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cinttypes> | |
#include <atomic> | |
#include <memory> | |
#include <type_traits> | |
//start 00:20 | |
// genius idea https://en.wikipedia.org/wiki/Tagged_pointer | |
template <class T> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Installation. | |
// Abseil, compile with -labseil | |
// Libdivide as header. | |
// GMP as header, compile with -lgmp | |
// Change the private constructor of absl::uin128 to public in order to use public inheritance. | |
// GMOCK, GUNIT, compile it. | |
#include "absl/numeric/int128.h" | |
#include <random> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct GmpDiv { | |
uint64_t operator()(uint64_t u1, uint64_t u0, uint64_t v, du_int* r) const { | |
mp_limb_t q[2] = {u0, u1}; | |
mp_limb_t result[2] = {0, 0}; | |
*r = mpn_divrem_1(result, 0, q, 2, v); | |
return result[0]; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// dividend / divisor, remainder is stored in rem. | |
uint128 __udivmodti4(uint128 dividend, uint128 divisor, uint128* rem) { | |
if (divisor > dividend) { | |
if (rem) | |
*rem = dividend; | |
return 0; | |
} | |
// 64 bit divisor implementation | |
... | |
// end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if defined(__x86_64__) | |
inline uint64_t Divide128Div64To64(uint64_t high, uint64_t low, | |
uint64_t divisor, uint64_t* remainder) { | |
uint64_t result; | |
__asm__("divq %[v]" | |
: "=a"(result), "=d"(*remainder) // Ouput parametrs, =a for rax, =d for rdx, [v] is an | |
// alias for divisor, input paramters "a" and "d" for low and high. | |
: [v] "r"(divisor), "a"(low), "d"(high)); | |
return result; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if defined(__x86_64__) | |
inline uint64_t Divide128Div64To64(uint64_t high, uint64_t low, | |
uint64_t divisor, uint64_t* remainder) { | |
uint64_t result; | |
__asm__("divq %[v]" | |
: "=a"(result), "=d"(*remainder) // Output parametrs, =a for rax, =d for rdx, [v] is an | |
// alias for divisor, input paramters "a" and "d" for low and high. | |
: [v] "r"(divisor), "a"(low), "d"(high)); | |
return result; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cinttypes> | |
uint64_t div(uint64_t u1, uint64_t u0, uint64_t v) { | |
uint64_t result; | |
uint64_t remainder; | |
__asm__("divq %[v]" : "=a"(result), "=d"(remainder) : [v] "r"(v), "a"(u0), "d"(u1)); | |
return result; | |
} | |
int main() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// dividend / divisor, remainder is stored in rem. | |
uint128 __udivmodti4(uint128 dividend, uint128 divisor, uint128* rem) { | |
if (divisor > dividend) { | |
if (rem) | |
*rem = dividend; | |
return 0; | |
} | |
// Calculate the distance between most significant bits, 128 > shift >= 0. | |
int shift = Distance(dividend, divisor); | |
divisor <<= shift; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "int_lib.h" | |
/* | |
typedef int si_int; | |
typedef unsigned su_int; | |
typedef long long di_int; | |
typedef unsigned long long du_int; | |
typedef int ti_int __attribute__((mode(TI))); // 128 signed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int main() { | |
long a = 3; | |
long b = 2; | |
long c = a + b; | |
a = 4; | |
} |