From 5fde4980e17243ba1f993e7290be0c785c2be444 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Sun, 8 Jun 2025 02:39:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8C++=2020/23=E7=9A=84bit?= =?UTF-8?q?=E7=B3=BB=E5=88=97=E5=87=BD=E6=95=B0=E5=8F=96=E4=BB=A3=E5=8E=9F?= =?UTF-8?q?=E5=85=88=E6=89=8B=E5=86=99=E7=9A=84=E4=B8=80=E4=BA=9B=E4=BD=8D?= =?UTF-8?q?=E8=AE=A1=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/hgl/TypeFunc.h | 47 ++++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/inc/hgl/TypeFunc.h b/inc/hgl/TypeFunc.h index 852b657..db358d5 100644 --- a/inc/hgl/TypeFunc.h +++ b/inc/hgl/TypeFunc.h @@ -1,8 +1,14 @@ #pragma once #include +#include #include #include +#include + +#if __cplusplus >= 202302L //C++23 +#include +#endif// namespace hgl { @@ -33,8 +39,6 @@ namespace hgl #define RANGE_CHECK_RETURN_FALSE(value) if(!RangeCheck(value))return(false); #define RANGE_CHECK_RETURN_NULLPTR(value) if(!RangeCheck(value))return(nullptr); - #define HGL_CONVER_TO_MEM_ALIGN(x) ((((x)+HGL_MEM_ALIGN-1)/HGL_MEM_ALIGN)*HGL_MEM_ALIGN) //内存对齐转换宏 - #ifndef NULL #define NULL 0 #endif// @@ -196,8 +200,7 @@ namespace hgl /** * 求数值对齐后的值 */ - template - inline T hgl_align(const T value,const T alignment) + template constexpr const inline T hgl_align(const T value,const T alignment) { return ((value+alignment-1)/alignment)*alignment; } @@ -205,19 +208,21 @@ namespace hgl /** * 求数值对齐后的值(仅支持2次幂) */ - template - inline T hgl_align_pow2(const T value,const T alignment) - { + template constexpr const inline T hgl_align_pow2(const T value,const T alignment) + { + #if defined(__cpp_lib_align) && __cpp_lib_align >= 202306L + return std::align_up(value,alignment); + #else const T align_size=alignment-1; return (value+align_size)&(~align_size); + #endif } /** * 取适合正巧大于当前数的2次幂值 */ - template - inline T power_to_2(T value) + template constexpr const inline T power_to_2(T value) { T result=1; @@ -226,11 +231,16 @@ namespace hgl return(result); } - + + template constexpr const inline T power_to_2_down(T value) + { + return std::bit_floor(value); + } + /** * 向上对齐 */ - template inline T align_up(T val, T alignment) // align val to the next multiple of alignment + template constexpr const inline T align_up(T val, T alignment) // align val to the next multiple of alignment { return (val + alignment - (T)1) & ~(alignment - (T)1); } @@ -238,15 +248,15 @@ namespace hgl /** * 向下对齐 */ - template inline T align_down(T val, T alignment) // align val to the previous multiple of alignment + template constexpr const inline T align_down(T val, T alignment) // align val to the previous multiple of alignment { return val & ~(alignment - (T)1); } - + /** * 求对齐数量 */ - template inline T divide_rounding_up(T x, T y) + template constexpr const inline T divide_rounding_up(T x, T y) { return (x + y - (T)1) / y; } @@ -315,17 +325,18 @@ namespace hgl return(value); } - inline const uint GetMipLevel(const uint size) + inline constexpr const uint GetMipLevel(const uint size) { - return static_cast(floor(log2(size)))+1; + //return static_cast(floor(log2(size)))+1; + return std::bit_width(size); } - inline const uint GetMipLevel(const uint width,const uint height) + inline constexpr const uint GetMipLevel(const uint width,const uint height) { return GetMipLevel(hgl_max(width,height)); } - inline const uint GetMipLevel(const uint width,const uint height,const uint depth) + inline constexpr const uint GetMipLevel(const uint width,const uint height,const uint depth) { return GetMipLevel(hgl_max(hgl_max(width,height),depth)); }