Compare commits

...

3 Commits

Author SHA1 Message Date
lyken ff1088b429
tmp: reformat nac3standalone/demo/demo.c 2024-08-26 19:20:42 +08:00
lyken 6a35203101
tmp: reformat nac3core/irrt 2024-08-26 19:20:39 +08:00
lyken ff6fed6d45
tmp: use .clang-format 2024-08-26 19:19:21 +08:00
14 changed files with 188 additions and 159 deletions

View File

@ -1,3 +1,32 @@
BasedOnStyle: LLVM BasedOnStyle: LLVM
Language: Cpp
Standard: Cpp11
AccessModifierOffset: -1
AlignEscapedNewlines: Left
AlwaysBreakAfterReturnType: None
AlwaysBreakTemplateDeclarations: Yes
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortFunctionsOnASingleLine: Inline
BinPackParameters: false
BreakBeforeBinaryOperators: NonAssignment
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: AfterColon
BreakInheritanceList: AfterColon
ColumnLimit: 120
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ContinuationIndentWidth: 4
DerivePointerAlignment: false
IndentCaseLabels: true
IndentPPDirectives: None
IndentWidth: 4 IndentWidth: 4
ReflowComments: false MaxEmptyLinesToKeep: 1
PointerAlignment: Left
ReflowComments: true
SortIncludes: false
SortUsingDeclarations: true
SpaceAfterTemplateKeyword: false
SpacesBeforeTrailingComments: 2
TabWidth: 4
UseTab: Never

View File

@ -2,7 +2,8 @@
#include <irrt/int_types.hpp> #include <irrt/int_types.hpp>
template <typename SizeT> struct CSlice { template<typename SizeT>
uint8_t *base; struct CSlice {
uint8_t* base;
SizeT len; SizeT len;
}; };

View File

@ -7,9 +7,8 @@
#define IRRT_DEBUG_ASSERT_BOOL false #define IRRT_DEBUG_ASSERT_BOOL false
#endif #endif
#define raise_debug_assert(SizeT, msg, param1, param2, param3) \ #define raise_debug_assert(SizeT, msg, param1, param2, param3) \
raise_exception(SizeT, EXN_ASSERTION_ERROR, \ raise_exception(SizeT, EXN_ASSERTION_ERROR, "IRRT debug assert failed: " msg, param1, param2, param3);
"IRRT debug assert failed: " msg, param1, param2, param3);
#define debug_assert_eq(SizeT, lhs, rhs) if constexpr (IRRT_DEBUG_ASSERT_BOOL) #define debug_assert_eq(SizeT, lhs, rhs) if constexpr (IRRT_DEBUG_ASSERT_BOOL)
{ {

View File

@ -25,13 +25,14 @@ ExceptionId EXN_TYPE_ERROR;
* The parameter `err` could be `Exception<int32_t>` or `Exception<int64_t>`. The caller * The parameter `err` could be `Exception<int32_t>` or `Exception<int64_t>`. The caller
* must make sure to pass `Exception`s with the correct `SizeT` depending on the `size_t` of the runtime. * must make sure to pass `Exception`s with the correct `SizeT` depending on the `size_t` of the runtime.
*/ */
extern "C" void __nac3_raise(void *err); extern "C" void __nac3_raise(void* err);
namespace { namespace {
/** /**
* @brief NAC3's Exception struct * @brief NAC3's Exception struct
*/ */
template <typename SizeT> struct Exception { template<typename SizeT>
struct Exception {
ExceptionId id; ExceptionId id;
CSlice<SizeT> filename; CSlice<SizeT> filename;
int32_t line; int32_t line;
@ -43,25 +44,27 @@ template <typename SizeT> struct Exception {
constexpr int64_t NO_PARAM = 0; constexpr int64_t NO_PARAM = 0;
template <typename SizeT> template<typename SizeT>
void _raise_exception_helper(ExceptionId id, const char *filename, int32_t line, void _raise_exception_helper(ExceptionId id,
const char *function, const char *msg, const char* filename,
int64_t param0, int64_t param1, int64_t param2) { int32_t line,
const char* function,
const char* msg,
int64_t param0,
int64_t param1,
int64_t param2) {
Exception<SizeT> e = { Exception<SizeT> e = {
.id = id, .id = id,
.filename = {.base = reinterpret_cast<const uint8_t *>(filename), .filename = {.base = reinterpret_cast<const uint8_t*>(filename), .len = __builtin_strlen(filename)},
.len = __builtin_strlen(filename)},
.line = line, .line = line,
.column = 0, .column = 0,
.function = {.base = reinterpret_cast<const uint8_t *>(function), .function = {.base = reinterpret_cast<const uint8_t*>(function), .len = __builtin_strlen(function)},
.len = __builtin_strlen(function)}, .msg = {.base = reinterpret_cast<const uint8_t*>(msg), .len = __builtin_strlen(msg)},
.msg = {.base = reinterpret_cast<const uint8_t *>(msg),
.len = __builtin_strlen(msg)},
}; };
e.params[0] = param0; e.params[0] = param0;
e.params[1] = param1; e.params[1] = param1;
e.params[2] = param2; e.params[2] = param2;
__nac3_raise(reinterpret_cast<void *>(&e)); __nac3_raise(reinterpret_cast<void*>(&e));
__builtin_unreachable(); __builtin_unreachable();
} }
@ -74,7 +77,6 @@ void _raise_exception_helper(ExceptionId id, const char *filename, int32_t line,
* `param0` to `param2` are optional format arguments of `msg`. They should be set to * `param0` to `param2` are optional format arguments of `msg`. They should be set to
* `NO_PARAM` to indicate they are unused. * `NO_PARAM` to indicate they are unused.
*/ */
#define raise_exception(SizeT, id, msg, param0, param1, param2) \ #define raise_exception(SizeT, id, msg, param0, param1, param2) \
_raise_exception_helper<SizeT>(id, __FILE__, __LINE__, __FUNCTION__, msg, \ _raise_exception_helper<SizeT>(id, __FILE__, __LINE__, __FUNCTION__, msg, param0, param1, param2)
param0, param1, param2) } // namespace
} // namespace

View File

@ -10,48 +10,47 @@ extern "C" {
// - The end index is *inclusive*, // - The end index is *inclusive*,
// - The length of src and dest slice size should already // - The length of src and dest slice size should already
// be checked: if dest.step == 1 then len(src) <= len(dest) else len(src) == len(dest) // be checked: if dest.step == 1 then len(src) <= len(dest) else len(src) == len(dest)
SliceIndex __nac3_list_slice_assign_var_size( SliceIndex __nac3_list_slice_assign_var_size(SliceIndex dest_start,
SliceIndex dest_start, SliceIndex dest_end, SliceIndex dest_step, SliceIndex dest_end,
uint8_t *dest_arr, SliceIndex dest_arr_len, SliceIndex src_start, SliceIndex dest_step,
SliceIndex src_end, SliceIndex src_step, uint8_t *src_arr, uint8_t* dest_arr,
SliceIndex src_arr_len, const SliceIndex size) { SliceIndex dest_arr_len,
SliceIndex src_start,
SliceIndex src_end,
SliceIndex src_step,
uint8_t* src_arr,
SliceIndex src_arr_len,
const SliceIndex size) {
/* if dest_arr_len == 0, do nothing since we do not support extending list */ /* if dest_arr_len == 0, do nothing since we do not support extending list */
if (dest_arr_len == 0) if (dest_arr_len == 0)
return dest_arr_len; return dest_arr_len;
/* if both step is 1, memmove directly, handle the dropping of the list, and shrink size */ /* if both step is 1, memmove directly, handle the dropping of the list, and shrink size */
if (src_step == dest_step && dest_step == 1) { if (src_step == dest_step && dest_step == 1) {
const SliceIndex src_len = const SliceIndex src_len = (src_end >= src_start) ? (src_end - src_start + 1) : 0;
(src_end >= src_start) ? (src_end - src_start + 1) : 0; const SliceIndex dest_len = (dest_end >= dest_start) ? (dest_end - dest_start + 1) : 0;
const SliceIndex dest_len =
(dest_end >= dest_start) ? (dest_end - dest_start + 1) : 0;
if (src_len > 0) { if (src_len > 0) {
__builtin_memmove(dest_arr + dest_start * size, __builtin_memmove(dest_arr + dest_start * size, src_arr + src_start * size, src_len * size);
src_arr + src_start * size, src_len * size);
} }
if (dest_len > 0) { if (dest_len > 0) {
/* dropping */ /* dropping */
__builtin_memmove(dest_arr + (dest_start + src_len) * size, __builtin_memmove(dest_arr + (dest_start + src_len) * size, dest_arr + (dest_end + 1) * size,
dest_arr + (dest_end + 1) * size,
(dest_arr_len - dest_end - 1) * size); (dest_arr_len - dest_end - 1) * size);
} }
/* shrink size */ /* shrink size */
return dest_arr_len - (dest_len - src_len); return dest_arr_len - (dest_len - src_len);
} }
/* if two range overlaps, need alloca */ /* if two range overlaps, need alloca */
uint8_t need_alloca = uint8_t need_alloca = (dest_arr == src_arr)
(dest_arr == src_arr) && && !(max(dest_start, dest_end) < min(src_start, src_end)
!(max(dest_start, dest_end) < min(src_start, src_end) || || max(src_start, src_end) < min(dest_start, dest_end));
max(src_start, src_end) < min(dest_start, dest_end));
if (need_alloca) { if (need_alloca) {
uint8_t *tmp = uint8_t* tmp = reinterpret_cast<uint8_t*>(__builtin_alloca(src_arr_len * size));
reinterpret_cast<uint8_t *>(__builtin_alloca(src_arr_len * size));
__builtin_memcpy(tmp, src_arr, src_arr_len * size); __builtin_memcpy(tmp, src_arr, src_arr_len * size);
src_arr = tmp; src_arr = tmp;
} }
SliceIndex src_ind = src_start; SliceIndex src_ind = src_start;
SliceIndex dest_ind = dest_start; SliceIndex dest_ind = dest_start;
for (; (src_step > 0) ? (src_ind <= src_end) : (src_ind >= src_end); for (; (src_step > 0) ? (src_ind <= src_end) : (src_ind >= src_end); src_ind += src_step, dest_ind += dest_step) {
src_ind += src_step, dest_ind += dest_step) {
/* for constant optimization */ /* for constant optimization */
if (size == 1) { if (size == 1) {
__builtin_memcpy(dest_arr + dest_ind, src_arr + src_ind, 1); __builtin_memcpy(dest_arr + dest_ind, src_arr + src_ind, 1);
@ -61,18 +60,16 @@ SliceIndex __nac3_list_slice_assign_var_size(
__builtin_memcpy(dest_arr + dest_ind * 8, src_arr + src_ind * 8, 8); __builtin_memcpy(dest_arr + dest_ind * 8, src_arr + src_ind * 8, 8);
} else { } else {
/* memcpy for var size, cannot overlap after previous alloca */ /* memcpy for var size, cannot overlap after previous alloca */
__builtin_memcpy(dest_arr + dest_ind * size, __builtin_memcpy(dest_arr + dest_ind * size, src_arr + src_ind * size, size);
src_arr + src_ind * size, size);
} }
} }
/* only dest_step == 1 can we shrink the dest list. */ /* only dest_step == 1 can we shrink the dest list. */
/* size should be ensured prior to calling this function */ /* size should be ensured prior to calling this function */
if (dest_step == 1 && dest_end >= dest_start) { if (dest_step == 1 && dest_end >= dest_start) {
__builtin_memmove(dest_arr + dest_ind * size, __builtin_memmove(dest_arr + dest_ind * size, dest_arr + (dest_end + 1) * size,
dest_arr + (dest_end + 1) * size,
(dest_arr_len - dest_end - 1) * size); (dest_arr_len - dest_end - 1) * size);
return dest_arr_len - (dest_end - dest_ind) - 1; return dest_arr_len - (dest_end - dest_ind) - 1;
} }
return dest_arr_len; return dest_arr_len;
} }
} // extern "C" } // extern "C"

View File

@ -3,7 +3,8 @@
namespace { namespace {
// 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> T __nac3_int_exp_impl(T base, T exp) { template<typename T>
T __nac3_int_exp_impl(T base, T exp) {
T res = 1; T res = 1;
/* repeated squaring method */ /* repeated squaring method */
do { do {
@ -15,11 +16,11 @@ template <typename T> T __nac3_int_exp_impl(T base, T exp) {
} while (exp); } while (exp);
return res; return res;
} }
} // namespace } // namespace
#define DEF_nac3_int_exp_(T) \ #define DEF_nac3_int_exp_(T) \
T __nac3_int_exp_##T(T base, T exp) { \ T __nac3_int_exp_##T(T base, T exp) { \
return __nac3_int_exp_impl(base, exp); \ return __nac3_int_exp_impl(base, exp); \
} }
extern "C" { extern "C" {
@ -31,9 +32,13 @@ DEF_nac3_int_exp_(int64_t);
DEF_nac3_int_exp_(uint32_t); DEF_nac3_int_exp_(uint32_t);
DEF_nac3_int_exp_(uint64_t); DEF_nac3_int_exp_(uint64_t);
int32_t __nac3_isinf(double x) { return __builtin_isinf(x); } int32_t __nac3_isinf(double x) {
return __builtin_isinf(x);
}
int32_t __nac3_isnan(double x) { return __builtin_isnan(x); } int32_t __nac3_isnan(double x) {
return __builtin_isnan(x);
}
double tgamma(double arg); double tgamma(double arg);

View File

@ -1,11 +1,13 @@
#pragma once #pragma once
namespace { namespace {
template <typename T> const T &max(const T &a, const T &b) { template<typename T>
const T& max(const T& a, const T& b) {
return a > b ? a : b; return a > b ? a : b;
} }
template <typename T> const T &min(const T &a, const T &b) { template<typename T>
const T& min(const T& a, const T& b) {
return a > b ? b : a; return a > b ? b : a;
} }
} // namespace } // namespace

View File

@ -3,9 +3,8 @@
#include <irrt/int_types.hpp> #include <irrt/int_types.hpp>
namespace { namespace {
template <typename SizeT> template<typename SizeT>
SizeT __nac3_ndarray_calc_size_impl(const SizeT *list_data, SizeT list_len, SizeT __nac3_ndarray_calc_size_impl(const SizeT* list_data, SizeT list_len, SizeT begin_idx, SizeT end_idx) {
SizeT begin_idx, SizeT end_idx) {
__builtin_assume(end_idx <= list_len); __builtin_assume(end_idx <= list_len);
SizeT num_elems = 1; SizeT num_elems = 1;
@ -17,9 +16,8 @@ SizeT __nac3_ndarray_calc_size_impl(const SizeT *list_data, SizeT list_len,
return num_elems; return num_elems;
} }
template <typename SizeT> template<typename SizeT>
void __nac3_ndarray_calc_nd_indices_impl(SizeT index, const SizeT *dims, void __nac3_ndarray_calc_nd_indices_impl(SizeT index, const SizeT* dims, SizeT num_dims, NDIndex* idxs) {
SizeT num_dims, NDIndex *idxs) {
SizeT stride = 1; SizeT stride = 1;
for (SizeT dim = 0; dim < num_dims; dim++) { for (SizeT dim = 0; dim < num_dims; dim++) {
SizeT i = num_dims - dim - 1; SizeT i = num_dims - dim - 1;
@ -29,10 +27,8 @@ void __nac3_ndarray_calc_nd_indices_impl(SizeT index, const SizeT *dims,
} }
} }
template <typename SizeT> template<typename SizeT>
SizeT __nac3_ndarray_flatten_index_impl(const SizeT *dims, SizeT num_dims, SizeT __nac3_ndarray_flatten_index_impl(const SizeT* dims, SizeT num_dims, const NDIndex* indices, SizeT num_indices) {
const NDIndex *indices,
SizeT num_indices) {
SizeT idx = 0; SizeT idx = 0;
SizeT stride = 1; SizeT stride = 1;
for (SizeT i = 0; i < num_dims; ++i) { for (SizeT i = 0; i < num_dims; ++i) {
@ -47,18 +43,18 @@ SizeT __nac3_ndarray_flatten_index_impl(const SizeT *dims, SizeT num_dims,
return idx; return idx;
} }
template <typename SizeT> template<typename SizeT>
void __nac3_ndarray_calc_broadcast_impl(const SizeT *lhs_dims, SizeT lhs_ndims, void __nac3_ndarray_calc_broadcast_impl(const SizeT* lhs_dims,
const SizeT *rhs_dims, SizeT rhs_ndims, SizeT lhs_ndims,
SizeT *out_dims) { const SizeT* rhs_dims,
SizeT rhs_ndims,
SizeT* out_dims) {
SizeT max_ndims = lhs_ndims > rhs_ndims ? lhs_ndims : rhs_ndims; SizeT max_ndims = lhs_ndims > rhs_ndims ? lhs_ndims : rhs_ndims;
for (SizeT i = 0; i < max_ndims; ++i) { for (SizeT i = 0; i < max_ndims; ++i) {
const SizeT *lhs_dim_sz = const SizeT* lhs_dim_sz = i < lhs_ndims ? &lhs_dims[lhs_ndims - i - 1] : nullptr;
i < lhs_ndims ? &lhs_dims[lhs_ndims - i - 1] : nullptr; const SizeT* rhs_dim_sz = i < rhs_ndims ? &rhs_dims[rhs_ndims - i - 1] : nullptr;
const SizeT *rhs_dim_sz = SizeT* out_dim = &out_dims[max_ndims - i - 1];
i < rhs_ndims ? &rhs_dims[rhs_ndims - i - 1] : nullptr;
SizeT *out_dim = &out_dims[max_ndims - i - 1];
if (lhs_dim_sz == nullptr) { if (lhs_dim_sz == nullptr) {
*out_dim = *rhs_dim_sz; *out_dim = *rhs_dim_sz;
@ -76,84 +72,73 @@ void __nac3_ndarray_calc_broadcast_impl(const SizeT *lhs_dims, SizeT lhs_ndims,
} }
} }
template <typename SizeT> template<typename SizeT>
void __nac3_ndarray_calc_broadcast_idx_impl(const SizeT *src_dims, void __nac3_ndarray_calc_broadcast_idx_impl(const SizeT* src_dims,
SizeT src_ndims, SizeT src_ndims,
const NDIndex *in_idx, const NDIndex* in_idx,
NDIndex *out_idx) { NDIndex* out_idx) {
for (SizeT i = 0; i < src_ndims; ++i) { for (SizeT i = 0; i < src_ndims; ++i) {
SizeT src_i = src_ndims - i - 1; SizeT src_i = src_ndims - i - 1;
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 } // namespace
extern "C" { extern "C" {
uint32_t __nac3_ndarray_calc_size(const uint32_t *list_data, uint32_t list_len, uint32_t __nac3_ndarray_calc_size(const uint32_t* list_data, uint32_t list_len, uint32_t begin_idx, uint32_t end_idx) {
uint32_t begin_idx, uint32_t end_idx) { return __nac3_ndarray_calc_size_impl(list_data, list_len, begin_idx, end_idx);
return __nac3_ndarray_calc_size_impl(list_data, list_len, begin_idx,
end_idx);
} }
uint64_t __nac3_ndarray_calc_size64(const uint64_t *list_data, uint64_t
uint64_t list_len, uint64_t begin_idx, __nac3_ndarray_calc_size64(const uint64_t* list_data, uint64_t list_len, uint64_t begin_idx, uint64_t end_idx) {
uint64_t end_idx) { return __nac3_ndarray_calc_size_impl(list_data, list_len, begin_idx, end_idx);
return __nac3_ndarray_calc_size_impl(list_data, list_len, begin_idx,
end_idx);
} }
void __nac3_ndarray_calc_nd_indices(uint32_t index, const uint32_t *dims, void __nac3_ndarray_calc_nd_indices(uint32_t index, const uint32_t* dims, uint32_t num_dims, NDIndex* idxs) {
uint32_t num_dims, NDIndex *idxs) {
__nac3_ndarray_calc_nd_indices_impl(index, dims, num_dims, idxs); __nac3_ndarray_calc_nd_indices_impl(index, dims, num_dims, idxs);
} }
void __nac3_ndarray_calc_nd_indices64(uint64_t index, const uint64_t *dims, void __nac3_ndarray_calc_nd_indices64(uint64_t index, const uint64_t* dims, uint64_t num_dims, NDIndex* idxs) {
uint64_t num_dims, NDIndex *idxs) {
__nac3_ndarray_calc_nd_indices_impl(index, dims, num_dims, idxs); __nac3_ndarray_calc_nd_indices_impl(index, dims, num_dims, idxs);
} }
uint32_t __nac3_ndarray_flatten_index(const uint32_t *dims, uint32_t num_dims, uint32_t
const NDIndex *indices, __nac3_ndarray_flatten_index(const uint32_t* dims, uint32_t num_dims, const NDIndex* indices, uint32_t num_indices) {
uint32_t num_indices) { return __nac3_ndarray_flatten_index_impl(dims, num_dims, indices, num_indices);
return __nac3_ndarray_flatten_index_impl(dims, num_dims, indices,
num_indices);
} }
uint64_t __nac3_ndarray_flatten_index64(const uint64_t *dims, uint64_t num_dims, uint64_t
const NDIndex *indices, __nac3_ndarray_flatten_index64(const uint64_t* dims, uint64_t num_dims, const NDIndex* indices, uint64_t num_indices) {
uint64_t num_indices) { return __nac3_ndarray_flatten_index_impl(dims, num_dims, indices, num_indices);
return __nac3_ndarray_flatten_index_impl(dims, num_dims, indices,
num_indices);
} }
void __nac3_ndarray_calc_broadcast(const uint32_t *lhs_dims, uint32_t lhs_ndims, void __nac3_ndarray_calc_broadcast(const uint32_t* lhs_dims,
const uint32_t *rhs_dims, uint32_t rhs_ndims, uint32_t lhs_ndims,
uint32_t *out_dims) { const uint32_t* rhs_dims,
return __nac3_ndarray_calc_broadcast_impl(lhs_dims, lhs_ndims, rhs_dims, uint32_t rhs_ndims,
rhs_ndims, out_dims); uint32_t* out_dims) {
return __nac3_ndarray_calc_broadcast_impl(lhs_dims, lhs_ndims, rhs_dims, rhs_ndims, out_dims);
} }
void __nac3_ndarray_calc_broadcast64(const uint64_t *lhs_dims, void __nac3_ndarray_calc_broadcast64(const uint64_t* lhs_dims,
uint64_t lhs_ndims, uint64_t lhs_ndims,
const uint64_t *rhs_dims, const uint64_t* rhs_dims,
uint64_t rhs_ndims, uint64_t *out_dims) { uint64_t rhs_ndims,
return __nac3_ndarray_calc_broadcast_impl(lhs_dims, lhs_ndims, rhs_dims, uint64_t* out_dims) {
rhs_ndims, out_dims); return __nac3_ndarray_calc_broadcast_impl(lhs_dims, lhs_ndims, rhs_dims, rhs_ndims, out_dims);
} }
void __nac3_ndarray_calc_broadcast_idx(const uint32_t *src_dims, void __nac3_ndarray_calc_broadcast_idx(const uint32_t* src_dims,
uint32_t src_ndims, uint32_t src_ndims,
const NDIndex *in_idx, const NDIndex* in_idx,
NDIndex *out_idx) { NDIndex* out_idx) {
__nac3_ndarray_calc_broadcast_idx_impl(src_dims, src_ndims, in_idx, __nac3_ndarray_calc_broadcast_idx_impl(src_dims, src_ndims, in_idx, out_idx);
out_idx);
} }
void __nac3_ndarray_calc_broadcast_idx64(const uint64_t *src_dims, void __nac3_ndarray_calc_broadcast_idx64(const uint64_t* src_dims,
uint64_t src_ndims, uint64_t src_ndims,
const NDIndex *in_idx, const NDIndex* in_idx,
NDIndex *out_idx) { NDIndex* out_idx) {
__nac3_ndarray_calc_broadcast_idx_impl(src_dims, src_ndims, in_idx, __nac3_ndarray_calc_broadcast_idx_impl(src_dims, src_ndims, in_idx, out_idx);
out_idx);
} }
} }

View File

@ -15,8 +15,7 @@ SliceIndex __nac3_slice_index_bound(SliceIndex i, const SliceIndex len) {
return i; return i;
} }
SliceIndex __nac3_range_slice_len(const SliceIndex start, const SliceIndex end, SliceIndex __nac3_range_slice_len(const SliceIndex start, const SliceIndex end, const SliceIndex step) {
const SliceIndex step) {
SliceIndex diff = end - start; SliceIndex diff = end - start;
if (diff > 0 && step > 0) { if (diff > 0 && step > 0) {
return ((diff - 1) / step) + 1; return ((diff - 1) / step) + 1;

View File

@ -6,19 +6,33 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
double dbl_nan(void) { return NAN; } double dbl_nan(void) {
return NAN;
}
double dbl_inf(void) { return INFINITY; } double dbl_inf(void) {
return INFINITY;
}
void output_bool(bool x) { puts(x ? "True" : "False"); } void output_bool(bool x) {
puts(x ? "True" : "False");
}
void output_int32(int32_t x) { printf("%" PRId32 "\n", x); } void output_int32(int32_t x) {
printf("%" PRId32 "\n", x);
}
void output_int64(int64_t x) { printf("%" PRId64 "\n", x); } void output_int64(int64_t x) {
printf("%" PRId64 "\n", x);
}
void output_uint32(uint32_t x) { printf("%" PRIu32 "\n", x); } void output_uint32(uint32_t x) {
printf("%" PRIu32 "\n", x);
}
void output_uint64(uint64_t x) { printf("%" PRIu64 "\n", x); } void output_uint64(uint64_t x) {
printf("%" PRIu64 "\n", x);
}
void output_float64(double x) { void output_float64(double x) {
if (isnan(x)) { if (isnan(x)) {
@ -38,7 +52,7 @@ void output_range(int32_t range[3]) {
} }
void output_asciiart(int32_t x) { void output_asciiart(int32_t x) {
static const char *chars = " .,-:;i+hHM$*#@ "; static const char* chars = " .,-:;i+hHM$*#@ ";
if (x < 0) { if (x < 0) {
putchar('\n'); putchar('\n');
} else { } else {
@ -47,12 +61,12 @@ void output_asciiart(int32_t x) {
} }
struct cslice { struct cslice {
void *data; void* data;
size_t len; size_t len;
}; };
void output_int32_list(struct cslice *slice) { void output_int32_list(struct cslice* slice) {
const int32_t *data = reinterpret_cast<int32_t *>(slice->data); const int32_t* data = reinterpret_cast<int32_t*>(slice->data);
putchar('['); putchar('[');
for (size_t i = 0; i < slice->len; ++i) { for (size_t i = 0; i < slice->len; ++i) {
@ -66,29 +80,27 @@ void output_int32_list(struct cslice *slice) {
putchar('\n'); putchar('\n');
} }
void output_str(struct cslice *slice) { void output_str(struct cslice* slice) {
const char *data = reinterpret_cast<const char *>(slice->data); const char* data = reinterpret_cast<const char*>(slice->data);
for (size_t i = 0; i < slice->len; ++i) { for (size_t i = 0; i < slice->len; ++i) {
putchar(data[i]); putchar(data[i]);
} }
} }
void output_strln(struct cslice *slice) { void output_strln(struct cslice* slice) {
output_str(slice); output_str(slice);
putchar('\n'); putchar('\n');
} }
uint64_t dbg_stack_address(__attribute__((unused)) struct cslice *slice) { uint64_t dbg_stack_address(__attribute__((unused)) struct cslice* slice) {
int i; int i;
void *ptr = static_cast<void *>(&i); void* ptr = static_cast<void*>(&i);
return (uintptr_t)ptr; return (uintptr_t)ptr;
} }
uint32_t __nac3_personality(uint32_t state, uint32_t exception_object, uint32_t __nac3_personality(uint32_t state, uint32_t exception_object, uint32_t context) {
uint32_t context) { printf("__nac3_personality(state: %u, exception_object: %u, context: %u)\n", state, exception_object, context);
printf("__nac3_personality(state: %u, exception_object: %u, context: %u)\n",
state, exception_object, context);
exit(101); exit(101);
__builtin_unreachable(); __builtin_unreachable();
} }
@ -105,18 +117,14 @@ struct Exception {
int64_t param[3]; int64_t param[3];
}; };
uint32_t __nac3_raise(struct Exception *e) { uint32_t __nac3_raise(struct Exception* e) {
printf("__nac3_raise called. Exception details:\n"); printf("__nac3_raise called. Exception details:\n");
printf(" ID: %" PRIu32 "\n", e->id); printf(" ID: %" PRIu32 "\n", e->id);
printf(" Location: %*s:%" PRIu32 ":%" PRIu32 "\n", printf(" Location: %*s:%" PRIu32 ":%" PRIu32 "\n", static_cast<int>(e->file.len),
static_cast<int>(e->file.len), reinterpret_cast<const char*>(e->file.data), e->line, e->column);
reinterpret_cast<const char *>(e->file.data), e->line, e->column); printf(" Function: %*s\n", static_cast<int>(e->function.len), reinterpret_cast<const char*>(e->function.data));
printf(" Function: %*s\n", static_cast<int>(e->function.len), printf(" Message: \"%*s\"\n", static_cast<int>(e->message.len), reinterpret_cast<const char*>(e->message.data));
reinterpret_cast<const char *>(e->function.data)); printf(" Params: {0}=%" PRId64 ", {1}=%" PRId64 ", {2}=%" PRId64 "\n", e->param[0], e->param[1], e->param[2]);
printf(" Message: \"%*s\"\n", static_cast<int>(e->message.len),
reinterpret_cast<const char *>(e->message.data));
printf(" Params: {0}=%" PRId64 ", {1}=%" PRId64 ", {2}=%" PRId64 "\n",
e->param[0], e->param[1], e->param[2]);
exit(101); exit(101);
__builtin_unreachable(); __builtin_unreachable();
} }
@ -125,4 +133,6 @@ void __nac3_end_catch(void) {}
extern int32_t run(void); extern int32_t run(void);
int main(void) { run(); } int main(void) {
run();
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.