6#ifndef LMMC_TENSOR_ND_INTERNAL_H
7#define LMMC_TENSOR_ND_INTERNAL_H
15 size_t ndim,
const size_t* dims,
size_t* strides,
size_t* total) {
17 if (dims == NULL || total == NULL ||
21 for (
size_t i = ndim; i > 0; --i) {
22 const size_t axis = i - 1;
23 if (dims[axis] == 0) {
26 if (strides != NULL) {
27 strides[axis] = count;
29 if (!lmmc_safe_mul_size(count, dims[axis], &count)) {
37static inline int lmmc_tensor_nd_descriptor_is_valid(
const lmmc_tensor_nd_t* tensor) {
40 size_t max_offset = 0;
43 if (tensor == NULL || tensor->
data == NULL ||
45 !lmmc_safe_mul_size(total,
sizeof(
lmmc_real_t), &logical_bytes)) {
48 for (
size_t i = 0; i < tensor->
ndim; ++i) {
51 !lmmc_safe_mul_size(tensor->
dims[i] - 1, tensor->
strides[i], &axis_offset) ||
52 !lmmc_safe_add_size(max_offset, axis_offset, &max_offset)) {
56 return lmmc_safe_add_size(max_offset, 1, &elements) &&
57 lmmc_safe_mul_size(elements,
sizeof(
lmmc_real_t), &bytes) &&
58 (uintptr_t)bytes <= UINTPTR_MAX - (uintptr_t)tensor->
data;
69 !lmmc_safe_mul_size(total,
sizeof(
lmmc_real_t), &bytes)) {
72 for (
size_t i = 0; i < ndim; ++i) {
73 result.
dims[i] = dims[i];
79 memset(data, 0, bytes);
80 for (
size_t i = 0; i < total; ++i) {
91static inline size_t lmmc_tensor_nd_offset(
92 size_t ndim,
const size_t* strides,
const size_t* idx) {
94 for (
size_t i = 0; i < ndim; ++i) {
95 offset += idx[i] * strides[i];
101static inline int lmmc_tensor_nd_next_index(
102 size_t ndim,
const size_t* dims,
size_t* idx) {
106 if (idx[ndim] < dims[ndim]) {
114static inline void lmmc_tensor_nd_add_product(
#define LMMC_REAL_CLEAR(x)
释放一个 lmmc_real_t 变量(POD 类型为空操作)。
#define LMMC_REAL_INIT(x)
初始化一个 lmmc_real_t 变量(POD 类型为空操作)。
#define LMMC_REAL_ADD(res, a, b)
计算 *res = *a + *b 。
double lmmc_real_t
Public and internal LMMC real type.
#define LMMC_REAL_SET(dst, src)
将一个 lmmc_real_t 赋值到另一个:*dst = *src 。
#define LMMC_REAL_MUL(res, a, b)
计算 *res = *a * *b 。
void * lmmc_memory_alloc(size_t size)
Private recoverable allocator for persistent LMMC ownership.
lmmc_status_t
LMMC 库统一返回的状态码.
@ LMMC_STATUS_ALLOCATION_FAILED
@ LMMC_STATUS_INVALID_ARGUMENT
#define LMMC_TENSOR_MAX_NDIM
支持的最大张量维度.
size_t dims[LMMC_TENSOR_MAX_NDIM]
size_t strides[LMMC_TENSOR_MAX_NDIM]