common.h 6.5 KB

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