common.h 6.4 KB

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