From 900150def34299d3cea64cb4edbc17e9e6821899 Mon Sep 17 00:00:00 2001 From: David Mak Date: Mon, 11 Nov 2024 15:38:24 +0800 Subject: [PATCH] [core] irrt: Update some IRRT implementation - Change CSlice to use `void*` for better pointer compatibility - Remove __STDC_VERSION__ guard - Only include impl *.hpp files in irrt.cpp - Refactor typedef to using declaration - Add missing ``// namespace` --- nac3core/irrt/irrt.cpp | 1 - nac3core/irrt/irrt/cslice.hpp | 2 +- nac3core/irrt/irrt/exception.hpp | 16 ++++++++-------- nac3core/irrt/irrt/int_types.hpp | 9 --------- nac3core/irrt/irrt/list.hpp | 26 ++++++++++++++++---------- nac3core/irrt/irrt/math.hpp | 2 +- nac3core/irrt/irrt/ndarray.hpp | 2 +- nac3core/irrt/irrt/slice.hpp | 2 +- 8 files changed, 28 insertions(+), 32 deletions(-) diff --git a/nac3core/irrt/irrt.cpp b/nac3core/irrt/irrt.cpp index 404681cc..7966322a 100644 --- a/nac3core/irrt/irrt.cpp +++ b/nac3core/irrt/irrt.cpp @@ -1,5 +1,4 @@ #include "irrt/exception.hpp" -#include "irrt/int_types.hpp" #include "irrt/list.hpp" #include "irrt/math.hpp" #include "irrt/ndarray.hpp" diff --git a/nac3core/irrt/irrt/cslice.hpp b/nac3core/irrt/irrt/cslice.hpp index 6f1d0a27..41441d37 100644 --- a/nac3core/irrt/irrt/cslice.hpp +++ b/nac3core/irrt/irrt/cslice.hpp @@ -4,6 +4,6 @@ template struct CSlice { - uint8_t* base; + void* base; SizeT len; }; \ No newline at end of file diff --git a/nac3core/irrt/irrt/exception.hpp b/nac3core/irrt/irrt/exception.hpp index 4a4b093b..78accbc9 100644 --- a/nac3core/irrt/irrt/exception.hpp +++ b/nac3core/irrt/irrt/exception.hpp @@ -6,7 +6,7 @@ /** * @brief The int type of ARTIQ exception IDs. */ -typedef int32_t ExceptionId; +using ExceptionId = int32_t; /* * Set of exceptions C++ IRRT can use. @@ -55,14 +55,14 @@ void _raise_exception_helper(ExceptionId id, int64_t param2) { Exception e = { .id = id, - .filename = {.base = reinterpret_cast(const_cast(filename)), - .len = static_cast(__builtin_strlen(filename))}, + .filename = {.base = reinterpret_cast(const_cast(filename)), + .len = static_cast(__builtin_strlen(filename))}, .line = line, .column = 0, - .function = {.base = reinterpret_cast(const_cast(function)), - .len = static_cast(__builtin_strlen(function))}, - .msg = {.base = reinterpret_cast(const_cast(msg)), - .len = static_cast(__builtin_strlen(msg))}, + .function = {.base = reinterpret_cast(const_cast(function)), + .len = static_cast(__builtin_strlen(function))}, + .msg = {.base = reinterpret_cast(const_cast(msg)), + .len = static_cast(__builtin_strlen(msg))}, }; e.params[0] = param0; e.params[1] = param1; @@ -70,6 +70,7 @@ void _raise_exception_helper(ExceptionId id, __nac3_raise(reinterpret_cast(&e)); __builtin_unreachable(); } +} // namespace /** * @brief Raise an exception with location details (location in the IRRT source files). @@ -82,4 +83,3 @@ void _raise_exception_helper(ExceptionId id, */ #define raise_exception(SizeT, id, msg, param0, param1, param2) \ _raise_exception_helper(id, __FILE__, __LINE__, __FUNCTION__, msg, param0, param1, param2) -} // namespace \ No newline at end of file diff --git a/nac3core/irrt/irrt/int_types.hpp b/nac3core/irrt/irrt/int_types.hpp index 656a060e..e25e7326 100644 --- a/nac3core/irrt/irrt/int_types.hpp +++ b/nac3core/irrt/irrt/int_types.hpp @@ -1,20 +1,11 @@ #pragma once -#if __STDC_VERSION__ >= 202000 using int8_t = _BitInt(8); using uint8_t = unsigned _BitInt(8); using int32_t = _BitInt(32); using uint32_t = unsigned _BitInt(32); using int64_t = _BitInt(64); using uint64_t = unsigned _BitInt(64); -#else -using int8_t = _ExtInt(8); -using uint8_t = unsigned _ExtInt(8); -using int32_t = _ExtInt(32); -using uint32_t = unsigned _ExtInt(32); -using int64_t = _ExtInt(64); -using uint64_t = unsigned _ExtInt(64); -#endif // NDArray indices are always `uint32_t`. using NDIndex = uint32_t; diff --git a/nac3core/irrt/irrt/list.hpp b/nac3core/irrt/irrt/list.hpp index b389197f..28543945 100644 --- a/nac3core/irrt/irrt/list.hpp +++ b/nac3core/irrt/irrt/list.hpp @@ -13,12 +13,12 @@ extern "C" { SliceIndex __nac3_list_slice_assign_var_size(SliceIndex dest_start, SliceIndex dest_end, SliceIndex dest_step, - uint8_t* dest_arr, + void* dest_arr, SliceIndex dest_arr_len, SliceIndex src_start, SliceIndex src_end, SliceIndex src_step, - uint8_t* src_arr, + void* src_arr, SliceIndex src_arr_len, const SliceIndex size) { /* if dest_arr_len == 0, do nothing since we do not support extending list */ @@ -29,11 +29,13 @@ SliceIndex __nac3_list_slice_assign_var_size(SliceIndex dest_start, const SliceIndex src_len = (src_end >= src_start) ? (src_end - src_start + 1) : 0; const SliceIndex dest_len = (dest_end >= dest_start) ? (dest_end - dest_start + 1) : 0; if (src_len > 0) { - __builtin_memmove(dest_arr + dest_start * size, src_arr + src_start * size, src_len * size); + __builtin_memmove(static_cast(dest_arr) + dest_start * size, + static_cast(src_arr) + src_start * size, src_len * size); } if (dest_len > 0) { /* dropping */ - __builtin_memmove(dest_arr + (dest_start + src_len) * size, dest_arr + (dest_end + 1) * size, + __builtin_memmove(static_cast(dest_arr) + (dest_start + src_len) * size, + static_cast(dest_arr) + (dest_end + 1) * size, (dest_arr_len - dest_end - 1) * size); } /* shrink size */ @@ -44,7 +46,7 @@ SliceIndex __nac3_list_slice_assign_var_size(SliceIndex dest_start, && !(max(dest_start, dest_end) < min(src_start, src_end) || max(src_start, src_end) < min(dest_start, dest_end)); if (need_alloca) { - uint8_t* tmp = reinterpret_cast(__builtin_alloca(src_arr_len * size)); + void* tmp = __builtin_alloca(src_arr_len * size); __builtin_memcpy(tmp, src_arr, src_arr_len * size); src_arr = tmp; } @@ -53,20 +55,24 @@ SliceIndex __nac3_list_slice_assign_var_size(SliceIndex dest_start, for (; (src_step > 0) ? (src_ind <= src_end) : (src_ind >= src_end); src_ind += src_step, dest_ind += dest_step) { /* for constant optimization */ if (size == 1) { - __builtin_memcpy(dest_arr + dest_ind, src_arr + src_ind, 1); + __builtin_memcpy(static_cast(dest_arr) + dest_ind, static_cast(src_arr) + src_ind, 1); } else if (size == 4) { - __builtin_memcpy(dest_arr + dest_ind * 4, src_arr + src_ind * 4, 4); + __builtin_memcpy(static_cast(dest_arr) + dest_ind * 4, + static_cast(src_arr) + src_ind * 4, 4); } else if (size == 8) { - __builtin_memcpy(dest_arr + dest_ind * 8, src_arr + src_ind * 8, 8); + __builtin_memcpy(static_cast(dest_arr) + dest_ind * 8, + static_cast(src_arr) + src_ind * 8, 8); } else { /* memcpy for var size, cannot overlap after previous alloca */ - __builtin_memcpy(dest_arr + dest_ind * size, src_arr + src_ind * size, size); + __builtin_memcpy(static_cast(dest_arr) + dest_ind * size, + static_cast(src_arr) + src_ind * size, size); } } /* only dest_step == 1 can we shrink the dest list. */ /* size should be ensured prior to calling this function */ if (dest_step == 1 && dest_end >= dest_start) { - __builtin_memmove(dest_arr + dest_ind * size, dest_arr + (dest_end + 1) * size, + __builtin_memmove(static_cast(dest_arr) + dest_ind * size, + static_cast(dest_arr) + (dest_end + 1) * size, (dest_arr_len - dest_end - 1) * size); return dest_arr_len - (dest_end - dest_ind) - 1; } diff --git a/nac3core/irrt/irrt/math.hpp b/nac3core/irrt/irrt/math.hpp index ff10f3f3..1872f564 100644 --- a/nac3core/irrt/irrt/math.hpp +++ b/nac3core/irrt/irrt/math.hpp @@ -90,4 +90,4 @@ double __nac3_j0(double x) { return j0(x); } -} \ No newline at end of file +} // namespace \ No newline at end of file diff --git a/nac3core/irrt/irrt/ndarray.hpp b/nac3core/irrt/irrt/ndarray.hpp index 5dda1739..b239152a 100644 --- a/nac3core/irrt/irrt/ndarray.hpp +++ b/nac3core/irrt/irrt/ndarray.hpp @@ -141,4 +141,4 @@ void __nac3_ndarray_calc_broadcast_idx64(const uint64_t* src_dims, NDIndex* out_idx) { __nac3_ndarray_calc_broadcast_idx_impl(src_dims, src_ndims, in_idx, out_idx); } -} \ No newline at end of file +} // namespace \ No newline at end of file diff --git a/nac3core/irrt/irrt/slice.hpp b/nac3core/irrt/irrt/slice.hpp index a1523ddc..3f6d83a3 100644 --- a/nac3core/irrt/irrt/slice.hpp +++ b/nac3core/irrt/irrt/slice.hpp @@ -25,4 +25,4 @@ SliceIndex __nac3_range_slice_len(const SliceIndex start, const SliceIndex end, return 0; } } -} \ No newline at end of file +} // namespace \ No newline at end of file