From f03f4ebefdf318a5982f04d7d4aaa5e9f8094209 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Mon, 13 May 2024 19:46:35 +0800 Subject: [PATCH] some fix --- include/pocketpy/common.h | 25 ++++++++++++++----------- include/pocketpy/tuplelist.h | 2 +- tests/80_linalg.py | 4 ++-- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/include/pocketpy/common.h b/include/pocketpy/common.h index 1262251e..3770fbd5 100644 --- a/include/pocketpy/common.h +++ b/include/pocketpy/common.h @@ -172,27 +172,30 @@ struct PyVar final{ Type type; bool is_sso; uint8_t flags; - char _bytes[12]; + // 12 bytes SSO + int _0; i64 _1; // uninitialized PyVar() = default; // zero initialized - PyVar(std::nullptr_t): type(), is_sso(false), flags(0), _bytes{0} { } + constexpr PyVar(std::nullptr_t): type(0), is_sso(false), flags(0), _0(0), _1(0) {} // PyObject* initialized (is_sso = false) - PyVar(Type type, PyObject* p): type(type), is_sso(false), flags(0) { - as() = p; - } + PyVar(Type type, PyObject* p): type(type), is_sso(false), flags(0), _1(reinterpret_cast(p)) {} // SSO initialized (is_sso = true) template PyVar(Type type, T value): type(type), is_sso(true), flags(0) { - static_assert(sizeof(T) <= sizeof(_bytes), "SSO size exceeded"); + static_assert(sizeof(T) <= 12, "SSO size exceeded"); as() = value; } template - T& as() const { + T& as(){ static_assert(!std::is_reference_v); - return *(T*)_bytes; + if constexpr(sizeof(T) <= 8){ + return reinterpret_cast(_1); + }else{ + return reinterpret_cast(_0); + } } explicit operator bool() const { return (bool)type; } @@ -210,15 +213,15 @@ struct PyVar final{ PyObject* get() const { PK_DEBUG_ASSERT(!is_sso) - return as(); + return reinterpret_cast(_1); } PyObject* operator->() const { PK_DEBUG_ASSERT(!is_sso) - return as(); + return reinterpret_cast(_1); } - i64 hash() const { return as(); } + i64 hash() const { return _1; } template T& obj_get(); diff --git a/include/pocketpy/tuplelist.h b/include/pocketpy/tuplelist.h index e8fd5779..b3b5afda 100644 --- a/include/pocketpy/tuplelist.h +++ b/include/pocketpy/tuplelist.h @@ -49,7 +49,7 @@ struct ArgsView{ PyVar* end() const { return _end; } int size() const { return _end - _begin; } bool empty() const { return _begin == _end; } - PyVar operator[](int i) const { return _begin[i]; } + PyVar& operator[](int i) const { return _begin[i]; } List to_list() const; Tuple to_tuple() const; diff --git a/tests/80_linalg.py b/tests/80_linalg.py index 5076039a..b321cfff 100644 --- a/tests/80_linalg.py +++ b/tests/80_linalg.py @@ -3,8 +3,8 @@ import random import sys import math -a = vec2(1, 2) -assert a.x == 1 +a = vec2(1.5, 2) +assert a.x == 1.5 assert a.y == 2 assert repr(math) == ""