1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-28 19:19:53 +00:00

added (u)int64 typed arrays back

This commit is contained in:
J.-F. Cap 2019-03-14 13:50:15 +01:00 committed by Calvin Rose
parent 5a9f7c3a85
commit 34849ea7b3
3 changed files with 95 additions and 16 deletions

View File

@ -156,19 +156,7 @@ static bi_uint64 check_bi_uint64(Janet x) {
return 0; return 0;
} }
/* C API */
static Janet make_bi_int64(Janet x) {
bi_int64 *box = (bi_int64 *)janet_abstract(&bi_int64_type, sizeof(bi_int64));
*box = check_bi_int64(x);
return janet_wrap_abstract(box);
}
static Janet make_bi_uint64(Janet x) {
bi_uint64 *box = (bi_uint64 *)janet_abstract(&bi_uint64_type, sizeof(bi_uint64));
*box = check_bi_uint64(x);
return janet_wrap_abstract(box);
}
JanetBigintType janet_is_bigint(Janet x) { JanetBigintType janet_is_bigint(Janet x) {
if (!janet_checktype(x, JANET_ABSTRACT)) return JANET_BIGINT_TYPE_none; if (!janet_checktype(x, JANET_ABSTRACT)) return JANET_BIGINT_TYPE_none;
@ -176,17 +164,38 @@ JanetBigintType janet_is_bigint(Janet x) {
return (at == &bi_int64_type) ? JANET_BIGINT_TYPE_int64 : ((at == &bi_uint64_type) ? JANET_BIGINT_TYPE_uint64 : JANET_BIGINT_TYPE_none); return (at == &bi_int64_type) ? JANET_BIGINT_TYPE_int64 : ((at == &bi_uint64_type) ? JANET_BIGINT_TYPE_uint64 : JANET_BIGINT_TYPE_none);
} }
Janet janet_bigint_int64(int64_t x) {
bi_int64 *box = (bi_int64 *)janet_abstract(&bi_int64_type, sizeof(bi_int64));
*box = (bi_int64)x;
return janet_wrap_abstract(box);
}
Janet janet_bigint_uint64(uint64_t x) {
bi_uint64 *box = (bi_uint64 *)janet_abstract(&bi_uint64_type, sizeof(bi_uint64));
*box = (bi_uint64)x;
return janet_wrap_abstract(box);
}
int64_t janet_checkbigint_int64(Janet x) {
return (int64_t)check_bi_int64(x);
}
uint64_t janet_checkbigint_uint64(Janet x) {
return (uint64_t)check_bi_uint64(x);
}
static Janet cfun_bi_int64_new(int32_t argc, Janet *argv) { static Janet cfun_bi_int64_new(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1); janet_fixarity(argc, 1);
return make_bi_int64(argv[0]); return janet_bigint_int64(check_bi_int64(argv[0]));
} }
static Janet cfun_bi_uint64_new(int32_t argc, Janet *argv) { static Janet cfun_bi_uint64_new(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1); janet_fixarity(argc, 1);
return make_bi_uint64(argv[0]); return janet_bigint_uint64(check_bi_uint64(argv[0]));
} }
#define OPMETHOD(type,name,oper) \ #define OPMETHOD(type,name,oper) \
static Janet cfun_##type##_##name(int32_t argc, Janet *argv) { \ static Janet cfun_##type##_##name(int32_t argc, Janet *argv) { \
janet_arity(argc, 2, -1); \ janet_arity(argc, 2, -1); \

View File

@ -37,6 +37,10 @@ typedef uint32_t ta_uint32_t;
typedef int32_t ta_int32_t; typedef int32_t ta_int32_t;
typedef float ta_float32_t; typedef float ta_float32_t;
typedef double ta_float64_t; typedef double ta_float64_t;
#ifdef JANET_BIGINT
typedef uint64_t ta_uint64_t;
typedef int64_t ta_int64_t;
#endif
static char *ta_type_names[] = { static char *ta_type_names[] = {
"uint8", "uint8",
@ -45,6 +49,10 @@ static char *ta_type_names[] = {
"int16", "int16",
"uint32", "uint32",
"int32", "int32",
#ifdef JANET_BIGINT
"uint64",
"int64",
#endif
"float32", "float32",
"float64", "float64",
"any" "any"
@ -57,6 +65,10 @@ static size_t ta_type_sizes[] = {
sizeof(ta_int16_t), sizeof(ta_int16_t),
sizeof(ta_uint32_t), sizeof(ta_uint32_t),
sizeof(ta_int32_t), sizeof(ta_int32_t),
#ifdef JANET_BIGINT
sizeof(ta_uint64_t),
sizeof(ta_int64_t),
#endif
sizeof(ta_float32_t), sizeof(ta_float32_t),
sizeof(ta_float64_t), sizeof(ta_float64_t),
0 0
@ -191,6 +203,23 @@ static Janet ta_get_##type(void *p, Janet key) { \
return value; \ return value; \
} }
#define DEFINE_VIEW_GETTER_BIGINT(type) \
static Janet ta_get_##type(void *p, Janet key) { \
Janet value; \
size_t index; \
if (!janet_checksize(key)) \
janet_panic("expected size as key"); \
index = (size_t)janet_unwrap_number(key);\
TA_View_##type *array=(TA_View_##type *)p; \
if (index >= array->size) { \
value = janet_wrap_nil(); \
} else { \
value = janet_bigint_##type(array->data[index*array->stride]); \
} \
return value; \
}
#define DEFINE_VIEW_SETTER(type) \ #define DEFINE_VIEW_SETTER(type) \
void ta_put_##type(void *p, Janet key,Janet value) { \ void ta_put_##type(void *p, Janet key,Janet value) { \
size_t index;\ size_t index;\
@ -206,6 +235,20 @@ void ta_put_##type(void *p, Janet key,Janet value) { \
array->data[index*array->stride]=(ta_##type##_t)janet_unwrap_number(value); \ array->data[index*array->stride]=(ta_##type##_t)janet_unwrap_number(value); \
} }
#define DEFINE_VIEW_SETTER_BIGINT(type) \
void ta_put_##type(void *p, Janet key,Janet value) { \
size_t index;\
if (!janet_checksize(key))\
janet_panic("expected size as key"); \
index = (size_t)janet_unwrap_number(key); \
TA_View_##type *array=(TA_View_##type *)p; \
if (index >= array->size) { \
janet_panic("index out of bounds"); \
} \
array->data[index*array->stride]=(ta_##type##_t)janet_checkbigint_##type(value); \
}
#define DEFINE_VIEW_INITIALIZER(thetype) \ #define DEFINE_VIEW_INITIALIZER(thetype) \
static JanetTArrayView *ta_init_##thetype(JanetTArrayView *view, \ static JanetTArrayView *ta_init_##thetype(JanetTArrayView *view, \
JanetTArrayBuffer *buf, size_t size, \ JanetTArrayBuffer *buf, size_t size, \
@ -236,18 +279,30 @@ DEFINE_VIEW_GETTER(type) \
DEFINE_VIEW_SETTER(type) \ DEFINE_VIEW_SETTER(type) \
DEFINE_VIEW_INITIALIZER(type) DEFINE_VIEW_INITIALIZER(type)
#define BUILD_TYPE_BIGINT(type) \
DEFINE_VIEW_TYPE(type) \
DEFINE_VIEW_GETTER_BIGINT(type) \
DEFINE_VIEW_SETTER_BIGINT(type) \
DEFINE_VIEW_INITIALIZER(type)
BUILD_TYPE(uint8) BUILD_TYPE(uint8)
BUILD_TYPE(int8) BUILD_TYPE(int8)
BUILD_TYPE(uint16) BUILD_TYPE(uint16)
BUILD_TYPE(int16) BUILD_TYPE(int16)
BUILD_TYPE(uint32) BUILD_TYPE(uint32)
BUILD_TYPE(int32) BUILD_TYPE(int32)
#ifdef JANET_BIGINT
BUILD_TYPE_BIGINT(uint64)
BUILD_TYPE_BIGINT(int64)
#endif
BUILD_TYPE(float32) BUILD_TYPE(float32)
BUILD_TYPE(float64) BUILD_TYPE(float64)
#undef DEFINE_VIEW_TYPE #undef DEFINE_VIEW_TYPE
#undef DEFINE_VIEW_GETTER #undef DEFINE_VIEW_GETTER
#undef DEFINE_VIEW_SETTER #undef DEFINE_VIEW_SETTER
#undef DEFINE_VIEW_GETTER_BIGINT
#undef DEFINE_VIEW_SETTER_BIGINT
#undef DEFINE_VIEW_INITIALIZER #undef DEFINE_VIEW_INITIALIZER
#define DEFINE_VIEW_ABSTRACT_TYPE(type) \ #define DEFINE_VIEW_ABSTRACT_TYPE(type) \
@ -268,6 +323,10 @@ static const JanetAbstractType ta_array_types[] = {
DEFINE_VIEW_ABSTRACT_TYPE(int16), DEFINE_VIEW_ABSTRACT_TYPE(int16),
DEFINE_VIEW_ABSTRACT_TYPE(uint32), DEFINE_VIEW_ABSTRACT_TYPE(uint32),
DEFINE_VIEW_ABSTRACT_TYPE(int32), DEFINE_VIEW_ABSTRACT_TYPE(int32),
#ifdef JANET_BIGINT
DEFINE_VIEW_ABSTRACT_TYPE(uint64),
DEFINE_VIEW_ABSTRACT_TYPE(int64),
#endif
DEFINE_VIEW_ABSTRACT_TYPE(float32), DEFINE_VIEW_ABSTRACT_TYPE(float32),
DEFINE_VIEW_ABSTRACT_TYPE(float64) DEFINE_VIEW_ABSTRACT_TYPE(float64)
}; };
@ -308,6 +367,10 @@ JanetTArrayView *janet_tarray_view(JanetTArrayType type, size_t size, size_t str
CASE_TYPE_INITIALIZE(int16); CASE_TYPE_INITIALIZE(int16);
CASE_TYPE_INITIALIZE(uint32); CASE_TYPE_INITIALIZE(uint32);
CASE_TYPE_INITIALIZE(int32); CASE_TYPE_INITIALIZE(int32);
#ifdef JANET_BIGINT
CASE_TYPE_INITIALIZE(uint64);
CASE_TYPE_INITIALIZE(int64);
#endif
CASE_TYPE_INITIALIZE(float32); CASE_TYPE_INITIALIZE(float32);
CASE_TYPE_INITIALIZE(float64); CASE_TYPE_INITIALIZE(float64);
default : default :

View File

@ -1293,6 +1293,10 @@ typedef enum {
JANET_TARRAY_TYPE_int16, JANET_TARRAY_TYPE_int16,
JANET_TARRAY_TYPE_uint32, JANET_TARRAY_TYPE_uint32,
JANET_TARRAY_TYPE_int32, JANET_TARRAY_TYPE_int32,
#ifdef JANET_BIGINT
JANET_TARRAY_TYPE_uint64,
JANET_TARRAY_TYPE_int64,
#endif
JANET_TARRAY_TYPE_float32, JANET_TARRAY_TYPE_float32,
JANET_TARRAY_TYPE_float64, JANET_TARRAY_TYPE_float64,
JANET_TARRAY_TYPE_any, JANET_TARRAY_TYPE_any,
@ -1330,7 +1334,10 @@ typedef enum {
} JanetBigintType; } JanetBigintType;
JANET_API JanetBigintType janet_is_bigint(Janet x); JANET_API JanetBigintType janet_is_bigint(Janet x);
JANET_API Janet janet_bigint_int64(int64_t x);
JANET_API Janet janet_bigint_uint64(uint64_t x);
JANET_API int64_t janet_checkbigint_int64(Janet x);
JANET_API uint64_t janet_checkbigint_uint64(Janet x);
#endif #endif