common.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #define PK_VERSION "1.1.0"
  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. namespace pkpy{
  50. namespace std = ::std;
  51. template <size_t T>
  52. struct NumberTraits;
  53. template <>
  54. struct NumberTraits<4> {
  55. using int_t = int32_t;
  56. using float_t = float;
  57. template<typename... Args>
  58. static int_t stoi(Args&&... args) { return std::stoi(std::forward<Args>(args)...); }
  59. template<typename... Args>
  60. static float_t stof(Args&&... args) { return std::stof(std::forward<Args>(args)...); }
  61. static constexpr int_t c0 = 0b00000000011111111111111111111100;
  62. static constexpr int_t c1 = 0b11111111111111111111111111111100;
  63. static constexpr int_t c2 = 0b00000000000000000000000000000011;
  64. };
  65. template <>
  66. struct NumberTraits<8> {
  67. using int_t = int64_t;
  68. using float_t = double;
  69. template<typename... Args>
  70. static int_t stoi(Args&&... args) { return std::stoll(std::forward<Args>(args)...); }
  71. template<typename... Args>
  72. static float_t stof(Args&&... args) { return std::stod(std::forward<Args>(args)...); }
  73. static constexpr int_t c0 = 0b0000000000001111111111111111111111111111111111111111111111111100;
  74. static constexpr int_t c1 = 0b1111111111111111111111111111111111111111111111111111111111111100;
  75. static constexpr int_t c2 = 0b0000000000000000000000000000000000000000000000000000000000000011;
  76. };
  77. using Number = NumberTraits<sizeof(void*)>;
  78. using i64 = Number::int_t;
  79. using f64 = Number::float_t;
  80. static_assert(sizeof(i64) == sizeof(void*));
  81. static_assert(sizeof(f64) == sizeof(void*));
  82. static_assert(std::numeric_limits<f64>::is_iec559);
  83. struct Dummy { };
  84. struct DummyInstance { };
  85. struct DummyModule { };
  86. struct NoReturn { };
  87. struct Discarded { };
  88. struct Type {
  89. int index;
  90. Type(): index(-1) {}
  91. Type(int index): index(index) {}
  92. bool operator==(Type other) const noexcept { return this->index == other.index; }
  93. bool operator!=(Type other) const noexcept { return this->index != other.index; }
  94. operator int() const noexcept { return this->index; }
  95. };
  96. #define PK_LAMBDA(x) ([](VM* vm, ArgsView args) { return x; })
  97. #define PK_VAR_LAMBDA(x) ([](VM* vm, ArgsView args) { return VAR(x); })
  98. #define PK_ACTION(x) ([](VM* vm, ArgsView args) { x; return vm->None; })
  99. #ifdef POCKETPY_H
  100. #define FATAL_ERROR() throw std::runtime_error( "L" + std::to_string(__LINE__) + " FATAL_ERROR()!");
  101. #else
  102. #define FATAL_ERROR() throw std::runtime_error( __FILE__ + std::string(":") + std::to_string(__LINE__) + " FATAL_ERROR()!");
  103. #endif
  104. #define PK_ASSERT(x) if(!(x)) FATAL_ERROR();
  105. struct PyObject;
  106. #define PK_BITS(p) (reinterpret_cast<i64>(p))
  107. inline bool is_tagged(PyObject* p) noexcept { return (PK_BITS(p) & 0b11) != 0b00; }
  108. inline bool is_int(PyObject* p) noexcept { return (PK_BITS(p) & 0b11) == 0b01; }
  109. inline bool is_float(PyObject* p) noexcept { return (PK_BITS(p) & 0b11) == 0b10; }
  110. inline bool is_special(PyObject* p) noexcept { return (PK_BITS(p) & 0b11) == 0b11; }
  111. inline bool is_both_int_or_float(PyObject* a, PyObject* b) noexcept {
  112. return is_tagged(a) && is_tagged(b);
  113. }
  114. inline bool is_both_int(PyObject* a, PyObject* b) noexcept {
  115. return is_int(a) && is_int(b);
  116. }
  117. inline bool is_both_float(PyObject* a, PyObject* b) noexcept {
  118. return is_float(a) && is_float(b);
  119. }
  120. // special singals, is_tagged() for them is true
  121. inline PyObject* const PY_NULL = (PyObject*)0b000011; // tagged null
  122. inline PyObject* const PY_OP_CALL = (PyObject*)0b100011;
  123. inline PyObject* const PY_OP_YIELD = (PyObject*)0b110011;
  124. } // namespace pkpy