diff --git a/src/core/typedarray.c b/src/core/typedarray.c index c7e557bd..7ba04859 100644 --- a/src/core/typedarray.c +++ b/src/core/typedarray.c @@ -1,5 +1,5 @@ /* -* Copyright (c) 2019 Calvin Rose +* Copyright (c) 2019 Calvin Rose & contributors * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to @@ -55,7 +55,7 @@ typedef enum TA_Type { #define TA_COUNT_TYPES (TA_TYPE_float64 + 1) -static char * ta_type_names[]= { +static char *ta_type_names[] = { "uint8", "int8", "uint16", @@ -68,7 +68,7 @@ static char * ta_type_names[]= { "float64", }; -static size_t ta_type_sizes[]= { +static size_t ta_type_sizes[] = { sizeof(ta_uint8_t), sizeof(ta_int8_t), sizeof(ta_uint16_t), @@ -82,9 +82,9 @@ static size_t ta_type_sizes[]= { }; -static TA_Type get_ta_type_by_name(const uint8_t * name) { - size_t nt=sizeof(ta_type_names)/sizeof(char *); - for (size_t i=0; idata=(uint8_t *)calloc(size,sizeof(uint8_t)); - buf->size=size; +static TA_Buffer *ta_buffer_init(TA_Buffer *buf, size_t size) { + buf->data = (uint8_t *)calloc(size, sizeof(uint8_t)); + buf->size = size; return buf; } static int ta_buffer_gc(void *p, size_t s) { (void) s; - TA_Buffer * buf=(TA_Buffer *)p; + TA_Buffer *buf = (TA_Buffer *)p; free(buf->data); return 0; } -static const JanetAbstractType ta_buffer_type= {"ta/buffer",ta_buffer_gc,NULL,NULL,NULL}; +static const JanetAbstractType ta_buffer_type = {"ta/buffer", ta_buffer_gc, NULL, NULL, NULL}; typedef struct { TA_Buffer *buffer; - void * data; /* pointer inside buffer->data */ + void *data; /* pointer inside buffer->data */ size_t size; size_t stride; TA_Type type; @@ -124,26 +124,26 @@ typedef struct { static int ta_mark(void *p, size_t s) { (void) s; - TA_View * view=(TA_View *)p; + TA_View *view = (TA_View *)p; janet_mark(janet_wrap_abstract(view->buffer)); return 0; } #define DEFINE_VIEW_TYPE(type) \ - typedef struct { \ - TA_Buffer * buffer; \ - ta_##type##_t * data; \ - size_t size; \ - size_t stride; \ - TA_Type type; \ + typedef struct { \ + TA_Buffer * buffer; \ + ta_##type##_t * data; \ + size_t size; \ + size_t stride; \ + TA_Type type; \ } TA_View_##type ; #define DEFINE_VIEW_GETTER(type) \ static Janet ta_get_##type(void *p, Janet key) { \ - Janet value; \ + Janet value; \ size_t index; \ - if (!janet_checkint(key)) \ + if (!janet_checkint(key)) \ janet_panic("expected integer key"); \ index = (size_t)janet_unwrap_integer(key);\ TA_View_##type * array=(TA_View_##type *)p; \ @@ -173,7 +173,7 @@ void ta_put_##type(void *p, Janet key,Janet value) { \ #define DEFINE_VIEW_INITIALIZER(type) \ static TA_View * ta_init_##type(TA_View * view,TA_Buffer * buf,size_t size,size_t offset,size_t stride) { \ TA_View_##type * tview=(TA_View_##type *) view; \ - size_t buf_size=offset+(size-1)*(sizeof(ta_##type##_t))*stride+1; \ + size_t buf_size=offset+(size-1)*(sizeof(ta_##type##_t))*stride+1; \ if (buf==NULL) { \ buf=(TA_Buffer *)janet_abstract(&ta_buffer_type,sizeof(TA_Buffer)); \ ta_buffer_init(buf,buf_size); \ @@ -184,7 +184,7 @@ void ta_put_##type(void *p, Janet key,Janet value) { \ tview->buffer=buf; \ tview->stride=stride; \ tview->size=size; \ - tview->data=(ta_##type##_t *)(buf->data+offset); \ + tview->data=(ta_##type##_t *)(buf->data+offset); \ tview->type=TA_TYPE_##type; \ return view; \ } @@ -214,7 +214,7 @@ BUILD_TYPE(float64) #define VIEW_ABSTRACT_DEFINE(type) {"ta/"#type,NULL,ta_mark,ta_get_##type,ta_put_##type} -static const JanetAbstractType ta_array_types[]= { +static const JanetAbstractType ta_array_types[] = { VIEW_ABSTRACT_DEFINE(uint8), VIEW_ABSTRACT_DEFINE(int8), VIEW_ABSTRACT_DEFINE(uint16), @@ -230,8 +230,8 @@ static const JanetAbstractType ta_array_types[]= { static int is_ta_type(Janet x) { if (janet_checktype(x, JANET_ABSTRACT)) { const JanetAbstractType *at = janet_abstract_type(janet_unwrap_abstract(x)); - for (size_t i=0; i2) - stride=(size_t)janet_getinteger(argv,2); - if (argc>3) - offset=(size_t)janet_getinteger(argv,3); - if (argc>4) { - if (is_ta_type(argv[4])) { - TA_View *view = (TA_View *)janet_unwrap_abstract(argv[4]); - offset=(view->buffer->data - (uint8_t *)(view->data))+offset*ta_type_sizes[view->type]; - stride*=view->stride; - buffer=view->buffer; - } else { - buffer=(TA_Buffer *)janet_getabstract(argv,4,&ta_buffer_type); - } + janet_arity(argc, 2, 5); + size_t offset = 0; + size_t stride = 1; + TA_Buffer *buffer = NULL; + const uint8_t *keyw = janet_getkeyword(argv, 0); + TA_Type type = get_ta_type_by_name(keyw); + size_t size = (size_t)janet_getinteger(argv, 1); + if (argc > 2) + stride = (size_t)janet_getinteger(argv, 2); + if (argc > 3) + offset = (size_t)janet_getinteger(argv, 3); + if (argc > 4) { + if (is_ta_type(argv[4])) { + TA_View *view = (TA_View *)janet_unwrap_abstract(argv[4]); + offset = (view->buffer->data - (uint8_t *)(view->data)) + offset * ta_type_sizes[view->type]; + stride *= view->stride; + buffer = view->buffer; + } else { + buffer = (TA_Buffer *)janet_getabstract(argv, 4, &ta_buffer_type); + } } - TA_View * view=janet_abstract(&ta_array_types[type],sizeof(TA_View)); + TA_View *view = janet_abstract(&ta_array_types[type], sizeof(TA_View)); switch (type) { - CASE_TYPE_INITIALIZE(uint8); - CASE_TYPE_INITIALIZE(int8); - CASE_TYPE_INITIALIZE(uint16); - CASE_TYPE_INITIALIZE(int16); - CASE_TYPE_INITIALIZE(uint32); - CASE_TYPE_INITIALIZE(int32); - CASE_TYPE_INITIALIZE(uint64); - CASE_TYPE_INITIALIZE(int64); - CASE_TYPE_INITIALIZE(float32); - CASE_TYPE_INITIALIZE(float64); + CASE_TYPE_INITIALIZE(uint8); + CASE_TYPE_INITIALIZE(int8); + CASE_TYPE_INITIALIZE(uint16); + CASE_TYPE_INITIALIZE(int16); + CASE_TYPE_INITIALIZE(uint32); + CASE_TYPE_INITIALIZE(int32); + CASE_TYPE_INITIALIZE(uint64); + CASE_TYPE_INITIALIZE(int64); + CASE_TYPE_INITIALIZE(float32); + CASE_TYPE_INITIALIZE(float64); } return janet_wrap_abstract(view); } @@ -289,9 +289,9 @@ static Janet cfun_typed_array_buffer(int32_t argc, Janet *argv) { TA_View *view = (TA_View *)janet_unwrap_abstract(argv[0]); return janet_wrap_abstract(view->buffer); } - size_t size=(size_t)janet_getinteger(argv,0); - TA_Buffer * buf=(TA_Buffer *)janet_abstract(&ta_buffer_type,sizeof(TA_Buffer)); - ta_buffer_init(buf,size); + size_t size = (size_t)janet_getinteger(argv, 0); + TA_Buffer *buf = (TA_Buffer *)janet_abstract(&ta_buffer_type, sizeof(TA_Buffer)); + ta_buffer_init(buf, size); return janet_wrap_abstract(buf); } @@ -301,46 +301,50 @@ static Janet cfun_typed_array_size(int32_t argc, Janet *argv) { TA_View *view = (TA_View *)janet_unwrap_abstract(argv[0]); return janet_wrap_number(view->size); } - TA_Buffer * buf=(TA_Buffer *)janet_getabstract(argv,0,&ta_buffer_type); + TA_Buffer *buf = (TA_Buffer *)janet_getabstract(argv, 0, &ta_buffer_type); return janet_wrap_number(buf->size); } static Janet cfun_typed_array_properties(int32_t argc, Janet *argv) { janet_fixarity(argc, 1); if (!is_ta_type(argv[0])) - janet_panic("expected typed array"); + janet_panic("expected typed array"); TA_View *view = (TA_View *)janet_unwrap_abstract(argv[0]); - JanetKV * props=janet_struct_begin(6); - janet_struct_put(props,janet_ckeywordv("size"),janet_wrap_number(view->size)); - janet_struct_put(props,janet_ckeywordv("byte-offset"),janet_wrap_number((uint8_t *)(view->data)-view->buffer->data)); - janet_struct_put(props,janet_ckeywordv("stride"),janet_wrap_number(view->stride)); - janet_struct_put(props,janet_ckeywordv("type"),janet_ckeywordv(ta_type_names[view->type])); - janet_struct_put(props,janet_ckeywordv("type-size"),janet_wrap_number(ta_type_sizes[view->type])); - janet_struct_put(props,janet_ckeywordv("buffer"),janet_wrap_abstract(view->buffer)); + JanetKV *props = janet_struct_begin(6); + janet_struct_put(props, janet_ckeywordv("size"), janet_wrap_number(view->size)); + janet_struct_put(props, janet_ckeywordv("byte-offset"), janet_wrap_number((uint8_t *)(view->data) - view->buffer->data)); + janet_struct_put(props, janet_ckeywordv("stride"), janet_wrap_number(view->stride)); + janet_struct_put(props, janet_ckeywordv("type"), janet_ckeywordv(ta_type_names[view->type])); + janet_struct_put(props, janet_ckeywordv("type-size"), janet_wrap_number(ta_type_sizes[view->type])); + janet_struct_put(props, janet_ckeywordv("buffer"), janet_wrap_abstract(view->buffer)); return janet_wrap_struct(janet_struct_end(props)); } static const JanetReg ta_cfuns[] = { - { "tarray/new", cfun_typed_array_new, + { + "tarray/new", cfun_typed_array_new, JDOC("(tarray/new type size [stride = 1 [offset = 0 [tarray | buffer]]] )\n\n" - "Create new typed array") + "Create new typed array") }, - { "tarray/buffer", cfun_typed_array_buffer, + { + "tarray/buffer", cfun_typed_array_buffer, JDOC("(tarray/buffer (array | size) )\n\n" - "return typed array buffer or create a new buffer ") + "return typed array buffer or create a new buffer ") }, - { "tarray/length", cfun_typed_array_size, + { + "tarray/length", cfun_typed_array_size, JDOC("(tarray/length (array | buffer) )\n\n" - "return typed array or buffer size ") + "return typed array or buffer size ") }, - { "tarray/properties", cfun_typed_array_properties, + { + "tarray/properties", cfun_typed_array_properties, JDOC("(tarray/properties array )\n\n" - "return typed array properties as a struct") + "return typed array properties as a struct") }, - {NULL,NULL,NULL} + {NULL, NULL, NULL} };