core: Reformat and modernize irrt.cpp

- Use anon namespace instead of static
- Use using declaration instead of typedef
- Align pointers to the type instead of the identifier
This commit is contained in:
David Mak 2024-07-09 13:52:18 +08:00
parent bc40a32524
commit 1c56005a01
1 changed files with 272 additions and 270 deletions

View File

@ -1,29 +1,30 @@
typedef _BitInt(8) int8_t; using int8_t = _BitInt(8);
typedef unsigned _BitInt(8) uint8_t; using uint8_t = unsigned _BitInt(8);
typedef _BitInt(32) int32_t; using int32_t = _BitInt(32);
typedef unsigned _BitInt(32) uint32_t; using uint32_t = unsigned _BitInt(32);
typedef _BitInt(64) int64_t; using int64_t = _BitInt(64);
typedef unsigned _BitInt(64) uint64_t; using uint64_t = unsigned _BitInt(64);
// NDArray indices are always `uint32_t`. // NDArray indices are always `uint32_t`.
typedef uint32_t NDIndex; using NDIndex = uint32_t;
// The type of an index or a value describing the length of a range/slice is always `int32_t`. // The type of an index or a value describing the length of a range/slice is always `int32_t`.
typedef int32_t SliceIndex; using SliceIndex = int32_t;
namespace {
template <typename T> template <typename T>
static T max(T a, T b) { const T& max(const T& a, const T& b) {
return a > b ? a : b; return a > b ? a : b;
} }
template <typename T> template <typename T>
static T min(T a, T b) { const T& min(const T& a, const T& b) {
return a > b ? b : a; return a > b ? b : a;
} }
// adapted from GNU Scientific Library: https://git.savannah.gnu.org/cgit/gsl.git/tree/sys/pow_int.c // adapted from GNU Scientific Library: https://git.savannah.gnu.org/cgit/gsl.git/tree/sys/pow_int.c
// need to make sure `exp >= 0` before calling this function // need to make sure `exp >= 0` before calling this function
template <typename T> template <typename T>
static T __nac3_int_exp_impl(T base, T exp) { T __nac3_int_exp_impl(T base, T exp) {
T res = 1; T res = 1;
/* repeated squaring method */ /* repeated squaring method */
do { do {
@ -37,7 +38,7 @@ static T __nac3_int_exp_impl(T base, T exp) {
} }
template <typename SizeT> template <typename SizeT>
static SizeT __nac3_ndarray_calc_size_impl( SizeT __nac3_ndarray_calc_size_impl(
const SizeT* list_data, const SizeT* list_data,
SizeT list_len, SizeT list_len,
SizeT begin_idx, SizeT begin_idx,
@ -55,7 +56,7 @@ static SizeT __nac3_ndarray_calc_size_impl(
} }
template <typename SizeT> template <typename SizeT>
static void __nac3_ndarray_calc_nd_indices_impl( void __nac3_ndarray_calc_nd_indices_impl(
SizeT index, SizeT index,
const SizeT* dims, const SizeT* dims,
SizeT num_dims, SizeT num_dims,
@ -71,7 +72,7 @@ static void __nac3_ndarray_calc_nd_indices_impl(
} }
template <typename SizeT> template <typename SizeT>
static SizeT __nac3_ndarray_flatten_index_impl( SizeT __nac3_ndarray_flatten_index_impl(
const SizeT* dims, const SizeT* dims,
SizeT num_dims, SizeT num_dims,
const NDIndex* indices, const NDIndex* indices,
@ -92,7 +93,7 @@ static SizeT __nac3_ndarray_flatten_index_impl(
} }
template <typename SizeT> template <typename SizeT>
static void __nac3_ndarray_calc_broadcast_impl( void __nac3_ndarray_calc_broadcast_impl(
const SizeT* lhs_dims, const SizeT* lhs_dims,
SizeT lhs_ndims, SizeT lhs_ndims,
const SizeT* rhs_dims, const SizeT* rhs_dims,
@ -123,7 +124,7 @@ static void __nac3_ndarray_calc_broadcast_impl(
} }
template <typename SizeT> template <typename SizeT>
static void __nac3_ndarray_calc_broadcast_idx_impl( void __nac3_ndarray_calc_broadcast_idx_impl(
const SizeT* src_dims, const SizeT* src_dims,
SizeT src_ndims, SizeT src_ndims,
const NDIndex* in_idx, const NDIndex* in_idx,
@ -134,6 +135,7 @@ static void __nac3_ndarray_calc_broadcast_idx_impl(
out_idx[src_i] = src_dims[src_i] == 1 ? 0 : in_idx[src_i]; out_idx[src_i] = src_dims[src_i] == 1 ? 0 : in_idx[src_i];
} }
} }
} // namespace
extern "C" { extern "C" {
#define DEF_nac3_int_exp_(T) \ #define DEF_nac3_int_exp_(T) \
@ -409,4 +411,4 @@ extern "C" {
) { ) {
__nac3_ndarray_calc_broadcast_idx_impl(src_dims, src_ndims, in_idx, out_idx); __nac3_ndarray_calc_broadcast_idx_impl(src_dims, src_ndims, in_idx, out_idx);
} }
} } // extern "C"