common.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #pragma once
  2. #include <cmath>
  3. #include <cstring>
  4. #include <sstream>
  5. #include <regex>
  6. #include <stdexcept>
  7. #include <vector>
  8. #include <string>
  9. #include <chrono>
  10. #include <string_view>
  11. #include <iomanip>
  12. #include <memory>
  13. #include <iostream>
  14. #include <map>
  15. #include <set>
  16. #include <algorithm>
  17. #include <initializer_list>
  18. #include <variant>
  19. #include <type_traits>
  20. #include <random>
  21. #include <bitset>
  22. #define PK_VERSION "1.2.6"
  23. #include "config.h"
  24. #include "export.h"
  25. #ifdef min
  26. #undef min
  27. #endif
  28. #ifdef max
  29. #undef max
  30. #endif
  31. /*******************************************************************************/
  32. #if PK_ENABLE_STD_FUNCTION
  33. #include <functional>
  34. #endif
  35. /*******************************************************************************/
  36. #if PK_ENABLE_THREAD
  37. #define PK_THREAD_LOCAL thread_local
  38. #include <mutex>
  39. struct GIL {
  40. inline static std::mutex _mutex;
  41. explicit GIL() { _mutex.lock(); }
  42. ~GIL() { _mutex.unlock(); }
  43. };
  44. #define PK_GLOBAL_SCOPE_LOCK() GIL _lock;
  45. #else
  46. #define PK_THREAD_LOCAL
  47. #define PK_GLOBAL_SCOPE_LOCK()
  48. #endif
  49. /*******************************************************************************/
  50. #define PK_UNUSED(x) (void)(x)
  51. #define PK_LOCAL_STATIC static
  52. namespace pkpy{
  53. namespace std = ::std;
  54. template <size_t T>
  55. struct NumberTraits;
  56. template <>
  57. struct NumberTraits<4> {
  58. using int_t = int32_t;
  59. using float_t = float;
  60. static constexpr int_t kMaxSmallInt = (1 << 28) - 1;
  61. static constexpr int_t kMinSmallInt = - (1 << 28);
  62. };
  63. template <>
  64. struct NumberTraits<8> {
  65. using int_t = int64_t;
  66. using float_t = double;
  67. static constexpr int_t kMaxSmallInt = (1ll << 60) - 1;
  68. static constexpr int_t kMinSmallInt = - (1ll << 60);
  69. };
  70. using Number = NumberTraits<sizeof(void*)>;
  71. using i64 = int64_t; // always 64-bit
  72. using f64 = Number::float_t;
  73. template<size_t T>
  74. union BitsCvtImpl;
  75. template<>
  76. union BitsCvtImpl<4>{
  77. NumberTraits<4>::int_t _int;
  78. NumberTraits<4>::float_t _float;
  79. // 1 + 8 + 23
  80. int sign() const { return _int >> 31; }
  81. unsigned int exp() const { return (_int >> 23) & 0b1111'1111; }
  82. uint64_t mantissa() const { return _int & 0x7fffff; }
  83. void set_exp(int exp) { _int = (_int & 0x807f'ffff) | (exp << 23); }
  84. void set_sign(int sign) { _int = (_int & 0x7fff'ffff) | (sign << 31); }
  85. void zero_mantissa() { _int &= 0xff80'0000; }
  86. static constexpr int C0 = 127; // 2^7 - 1
  87. static constexpr int C1 = -62; // 2 - 2^6
  88. static constexpr int C2 = 63; // 2^6 - 1
  89. static constexpr NumberTraits<4>::int_t C3 = 0b1011'1111'1111'1111'1111'1111'1111'1111;
  90. static constexpr int C4 = 0b11111111;
  91. BitsCvtImpl(NumberTraits<4>::float_t val): _float(val) {}
  92. BitsCvtImpl(NumberTraits<4>::int_t val): _int(val) {}
  93. void print(){
  94. std::string s = std::bitset<32>(_int).to_string();
  95. std::cout << s.substr(0, 1) << '|';
  96. std::cout << s.substr(1, 8) << '|';
  97. std::cout << s.substr(9) << std::endl;
  98. }
  99. };
  100. template<>
  101. union BitsCvtImpl<8>{
  102. NumberTraits<8>::int_t _int;
  103. NumberTraits<8>::float_t _float;
  104. // 1 + 11 + 52
  105. int sign() const { return _int >> 63; }
  106. unsigned int exp() const { return (_int >> 52) & 0b0111'1111'1111; }
  107. uint64_t mantissa() const { return _int & 0xfffffffffffff; }
  108. void set_exp(uint64_t exp) { _int = (_int & 0x800f'ffff'ffff'ffff) | (exp << 52); }
  109. void set_sign(uint64_t sign) { _int = (_int & 0x7fff'ffff'ffff'ffff) | (sign << 63); }
  110. void zero_mantissa() { _int &= 0xfff0'0000'0000'0000; }
  111. static constexpr int C0 = 1023; // 2^10 - 1
  112. static constexpr int C1 = -510; // 2 - 2^9
  113. static constexpr int C2 = 511; // 2^9 - 1
  114. static constexpr NumberTraits<8>::int_t C3 = 0b1011'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111;
  115. static constexpr int C4 = 0b11111111111;
  116. BitsCvtImpl(NumberTraits<8>::float_t val): _float(val) {}
  117. BitsCvtImpl(NumberTraits<8>::int_t val): _int(val) {}
  118. void print(){
  119. std::string s = std::bitset<64>(_int).to_string();
  120. std::cout << s.substr(0, 1) << '|';
  121. std::cout << s.substr(1, 11) << '|';
  122. std::cout << s.substr(12) << std::endl;
  123. }
  124. };
  125. using BitsCvt = BitsCvtImpl<sizeof(void*)>;
  126. static_assert(sizeof(i64) == 8);
  127. static_assert(sizeof(Number::float_t) == sizeof(void*));
  128. static_assert(sizeof(Number::int_t) == sizeof(void*));
  129. static_assert(sizeof(BitsCvt) == sizeof(void*));
  130. static_assert(std::numeric_limits<f64>::is_iec559);
  131. struct Dummy { };
  132. struct DummyInstance { };
  133. struct DummyModule { };
  134. struct NoReturn { };
  135. struct Discarded { };
  136. struct Type {
  137. int index;
  138. Type(): index(-1) {}
  139. Type(int index): index(index) {}
  140. bool operator==(Type other) const { return this->index == other.index; }
  141. bool operator!=(Type other) const { return this->index != other.index; }
  142. operator int() const { return this->index; }
  143. };
  144. #define PK_LAMBDA(x) ([](VM* vm, ArgsView args) { return x; })
  145. #define PK_VAR_LAMBDA(x) ([](VM* vm, ArgsView args) { return VAR(x); })
  146. #define PK_ACTION(x) ([](VM* vm, ArgsView args) { x; return vm->None; })
  147. #ifdef POCKETPY_H
  148. #define FATAL_ERROR() throw std::runtime_error( "L" + std::to_string(__LINE__) + " FATAL_ERROR()!");
  149. #else
  150. #define FATAL_ERROR() throw std::runtime_error( __FILE__ + std::string(":") + std::to_string(__LINE__) + " FATAL_ERROR()!");
  151. #endif
  152. #define PK_ASSERT(x) if(!(x)) FATAL_ERROR();
  153. struct PyObject;
  154. #define PK_BITS(p) (reinterpret_cast<Number::int_t>(p))
  155. inline PyObject* tag_float(f64 val){
  156. BitsCvt decomposed(val);
  157. // std::cout << "tagging: " << val << std::endl;
  158. int sign = decomposed.sign();
  159. int exp_7b = decomposed.exp() - BitsCvt::C0;
  160. if(exp_7b < BitsCvt::C1){
  161. exp_7b = BitsCvt::C1 - 1; // -63 + 63 = 0
  162. decomposed.zero_mantissa();
  163. }else if(exp_7b > BitsCvt::C2){
  164. exp_7b = BitsCvt::C2 + 1; // 64 + 63 = 127
  165. if(!std::isnan(val)) decomposed.zero_mantissa();
  166. }
  167. decomposed.set_exp(exp_7b + BitsCvt::C2);
  168. decomposed._int = (decomposed._int << 1) | 0b01;
  169. decomposed.set_sign(sign);
  170. return reinterpret_cast<PyObject*>(decomposed._int);
  171. }
  172. inline f64 untag_float(PyObject* val){
  173. BitsCvt decomposed(reinterpret_cast<Number::int_t>(val));
  174. // std::cout << "untagging: " << val << std::endl;
  175. decomposed._int = (decomposed._int >> 1) & BitsCvt::C3;
  176. unsigned int exp_7b = decomposed.exp();
  177. if(exp_7b == 0) return 0.0f;
  178. if(exp_7b == BitsCvt::C0){
  179. decomposed.set_exp(BitsCvt::C4);
  180. return decomposed._float;
  181. }
  182. decomposed.set_exp(exp_7b - BitsCvt::C2 + BitsCvt::C0);
  183. return decomposed._float;
  184. }
  185. // is_pod<> for c++17 and c++20
  186. template<typename T>
  187. struct is_pod {
  188. static constexpr bool value = std::is_trivially_copyable_v<T> && std::is_standard_layout_v<T>;
  189. };
  190. #define PK_ALWAYS_PASS_BY_POINTER(T) \
  191. T(const T&) = delete; \
  192. T& operator=(const T&) = delete; \
  193. T(T&&) = delete; \
  194. T& operator=(T&&) = delete;
  195. } // namespace pkpy