|
| 1 | +#ifndef LFORTRAN_BIGINT_H |
| 2 | +#define LFORTRAN_BIGINT_H |
| 3 | + |
| 4 | +#include <cstdint> |
| 5 | + |
| 6 | +#include <libasr/containers.h> |
| 7 | + |
| 8 | +namespace LFortran { |
| 9 | + |
| 10 | +namespace BigInt { |
| 11 | + |
| 12 | +/* |
| 13 | + * Arbitrary size integer implementation. |
| 14 | + * |
| 15 | + * We use tagged signed 64bit integers with no padding bits and using 2's |
| 16 | + * complement for negative values (int64_t) as the underlying data structure. |
| 17 | + * Little-endian is assumed. |
| 18 | + * |
| 19 | + * Bits (from the left): |
| 20 | + * 1 ..... sign: 0 positive, 1 negative |
| 21 | + * 2 ..... tag: bits 1-2 equal to 01: pointer; otherwise integer |
| 22 | + * 3-64 .. if the tag is - integer: rest of the signed integer bits in 2's |
| 23 | + * complement |
| 24 | + * - pointer: 64 bit pointer shifted by 2 |
| 25 | + * to the right (>> 2) |
| 26 | + * |
| 27 | + * The pointer must be aligned to 4 bytes (bits 63-64 must be 00). |
| 28 | + * Small signed integers are represented directly as integers in int64_t, large |
| 29 | + * integers are allocated on heap and a pointer to it is used as "tag pointer" |
| 30 | + * in int64_t. |
| 31 | + * |
| 32 | + * To check if the integer has a pointer tag, we check that the first two bits |
| 33 | + * (1-2) are equal to 01: |
| 34 | + */ |
| 35 | + |
| 36 | +// Returns true if "i" is a pointer and false if "i" is an integer |
| 37 | +inline static bool is_int_ptr(int64_t i) { |
| 38 | + return (((uint64_t)i) >> (64 - 2)) == 1; |
| 39 | +} |
| 40 | + |
| 41 | +/* |
| 42 | + * A pointer is converted to integer by shifting by 2 to the right and adding |
| 43 | + * 01 to the first two bits to tag it as a pointer: |
| 44 | + */ |
| 45 | + |
| 46 | +// Converts a pointer "p" (must be aligned to 4 bytes) to a tagged int64_t |
| 47 | +inline static int64_t ptr_to_int(void *p) { |
| 48 | + return (int64_t)( (((uint64_t)p) >> 2) | (1ULL << (64 - 2)) ); |
| 49 | +} |
| 50 | + |
| 51 | +/* An integer with the pointer tag is converted to a pointer by shifting by 2 |
| 52 | + * to the left, which erases the tag and puts 00 to bits 63-64: |
| 53 | + */ |
| 54 | + |
| 55 | +// Converts a tagged int64_t to a pointer (aligned to 4 bytes) |
| 56 | +inline static void* int_to_ptr(int64_t i) { |
| 57 | + return (void *)(((uint64_t)i) << 2); |
| 58 | +} |
| 59 | + |
| 60 | +/* The maximum small int is 2^62-1 |
| 61 | + */ |
| 62 | +const int64_t MAX_SMALL_INT = (int64_t)((1ULL << 62)-1); |
| 63 | + |
| 64 | +/* The minimum small int is -2^63 |
| 65 | + */ |
| 66 | +const int64_t MIN_SMALL_INT = (int64_t)(-(1ULL << 63)); |
| 67 | + |
| 68 | +// Returns true if "i" is a small int |
| 69 | +inline static bool is_small_int(int64_t i) { |
| 70 | + return (MIN_SMALL_INT <= i && i <= MAX_SMALL_INT); |
| 71 | +} |
| 72 | + |
| 73 | +/* Arbitrary integer implementation |
| 74 | + * For now large integers are implemented as strings with decimal digits. The |
| 75 | + * only supported operation on this is converting to and from a string. Later |
| 76 | + * we will replace with an actual large integer implementation and add other |
| 77 | + * operations. |
| 78 | + */ |
| 79 | + |
| 80 | +// Converts a string to a large int (allocated on heap, returns a pointer) |
| 81 | +inline static int64_t string_to_largeint(Allocator &al, const Str &s) { |
| 82 | + char *cs = s.c_str(al); |
| 83 | + return ptr_to_int(cs); |
| 84 | +} |
| 85 | + |
| 86 | +// Converts a large int to a string |
| 87 | +inline static char* largeint_to_string(int64_t i) { |
| 88 | + LFORTRAN_ASSERT(is_int_ptr(i)); |
| 89 | + void *p = int_to_ptr(i); |
| 90 | + char *cs = (char*)p; |
| 91 | + return cs; |
| 92 | +} |
| 93 | + |
| 94 | +inline static std::string int_to_str(int64_t i) { |
| 95 | + if (is_int_ptr(i)) { |
| 96 | + return std::string(largeint_to_string(i)); |
| 97 | + } else { |
| 98 | + return std::to_string(i); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +inline static bool is_int64(std::string str_repr) { |
| 103 | + std::string str_int64 = "9223372036854775807"; |
| 104 | + if( str_repr.size() > str_int64.size() ) { |
| 105 | + return false; |
| 106 | + } |
| 107 | + |
| 108 | + if( str_repr.size() < str_int64.size() ) { |
| 109 | + return true; |
| 110 | + } |
| 111 | + |
| 112 | + size_t i; |
| 113 | + for( i = 0; i < str_repr.size() - 1 && str_repr[i] == str_int64[i]; i++ ) { |
| 114 | + } |
| 115 | + return i == str_repr.size() - 1 || str_repr[i] < str_int64[i]; |
| 116 | +} |
| 117 | + |
| 118 | +/* BigInt is a thin wrapper over the functionality exposed in the functions |
| 119 | + * above. The idea is that one can use the int64_t type directly and just use |
| 120 | + * the function above to handle the large integer aspects, and if it is a small |
| 121 | + * integer, one can use it directly as int64 integer. |
| 122 | + * |
| 123 | + * Alternatively, one can use the BigInt class below that exposes the |
| 124 | + * functionality via methods. |
| 125 | + */ |
| 126 | + |
| 127 | +struct BigInt { |
| 128 | + int64_t n; |
| 129 | + |
| 130 | + BigInt() = default; |
| 131 | + BigInt(const BigInt &) = default; |
| 132 | + BigInt& operator=(const BigInt &) = default; |
| 133 | + |
| 134 | + void from_smallint(int64_t i) { |
| 135 | + LFORTRAN_ASSERT(is_small_int(i)); |
| 136 | + n = i; |
| 137 | + } |
| 138 | + |
| 139 | + void from_largeint(Allocator &al, const Str &s) { |
| 140 | + n = string_to_largeint(al, s); |
| 141 | + } |
| 142 | + |
| 143 | + bool is_large() const { |
| 144 | + return is_int_ptr(n); |
| 145 | + } |
| 146 | + |
| 147 | + int64_t as_smallint() const { |
| 148 | + LFORTRAN_ASSERT(!is_large()); |
| 149 | + return n; |
| 150 | + } |
| 151 | + |
| 152 | + std::string str() const { |
| 153 | + return int_to_str(n); |
| 154 | + } |
| 155 | + |
| 156 | +}; |
| 157 | + |
| 158 | +static_assert(std::is_standard_layout<BigInt>::value); |
| 159 | +static_assert(std::is_trivial<BigInt>::value); |
| 160 | +static_assert(sizeof(BigInt) == sizeof(int64_t)); |
| 161 | +static_assert(sizeof(BigInt) == 8); |
| 162 | + |
| 163 | + |
| 164 | +} // BigInt |
| 165 | + |
| 166 | +} // LFortran |
| 167 | + |
| 168 | +#endif // LFORTRAN_BIGINT_H |
0 commit comments