common.h 6.6 KB

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