on master help sign in
Unboxed Bits8/Bits16 using Int as the underlying representation
commite9a31f4c0f7fe8594ce9e85c04908585e1892300 authorJorge Acereda <jacereda@gmail.com> dateThu 26 Apr 2018, 21:05 (8 years ago) parentfb301a5
5 files changed+73−101
rts/idris_bitstring.c+2−8 rts/idris_gc.c+15−9 rts/idris_gmp.c+10−10 rts/idris_rts.c+40−52 rts/idris_rts.h+6−22
rts/idris_bitstring.c+2−8
@@ -3,17 +3,11 @@
33#include "idris_rts.h"
44
55VAL idris_b8const(VM *vm, uint8_t a) {
6− Bits8 * cl = iallocate(vm, sizeof(*cl), 0);
7− SETTY(cl, CT_BITS8);
8− cl->bits8 = a;
9− return (VAL)cl;
6+ return MKINT(a);
107}
118
129VAL idris_b16const(VM *vm, uint16_t a) {
13− Bits16 * cl = iallocate(vm, sizeof(*cl), 0);
14− SETTY(cl, CT_BITS16);
15− cl->bits16 = a;
16− return (VAL)cl;
10+ return MKINT(a);
1711}
1812
1913VAL idris_b32const(VM *vm, uint32_t a) {
rts/idris_gc.c+15−9
@@ -4,17 +4,27 @@
44#include "idris_bitstring.h"
55#include <assert.h>
66
7+static inline VAL plain(VM* vm, VAL x, size_t sz) {
8+ VAL cl = iallocate(vm, sz, 1);
9+ memcpy(cl, x, sz);
10+ return cl;
11+}
12+
713VAL copy(VM* vm, VAL x) {
814 int ar;
915 VAL cl;
10− if (x==NULL || ISINT(x)) {
16+ if (x==NULL) {
1117 return x;
1218 }
1319 switch(GETTY(x)) {
20+ case CT_INT: return x;
21+ case CT_BITS32: return plain(vm, x, sizeof(Bits32));
22+ case CT_BITS64: return plain(vm, x, sizeof(Bits64));
23+ case CT_FLOAT: return plain(vm, x, sizeof(Float));
1424 case CT_FWD:
1525 return GETPTR(x);
1626 case CT_CDATA:
17− cl = MKCDATAc(vm, GETCDATA(x));
27+ cl = plain(vm, x, sizeof(CDataC));
1828 c_heap_mark_item(GETCDATA(x));
1929 break;
2030 case CT_BIGINT:
@@ -29,22 +39,18 @@ VAL copy(VM* vm, VAL x) {
2939 case CT_ARRAY:
3040 case CT_STRING:
3141 case CT_REF:
32− case CT_FLOAT:
3342 case CT_STROFFSET:
3443 case CT_PTR:
3544 case CT_MANAGEDPTR:
36− case CT_BITS8:
37− case CT_BITS16:
38− case CT_BITS32:
39− case CT_BITS64:
4045 case CT_RAWDATA:
41− cl = iallocate(vm, x->hdr.sz, 1);
42− memcpy(cl, x, x->hdr.sz);
46+ cl = plain(vm, x, x->hdr.sz);
4347 break;
4448 default:
4549 cl = NULL;
50+ assert(0);
4651 break;
4752 }
53+ assert(x->hdr.sz >= sizeof(Fwd));
4854 SETTY(x, CT_FWD);
4955 ((Fwd*)x)->fwd = cl;
5056 return cl;
rts/idris_gmp.c+10−10
@@ -67,17 +67,17 @@ VAL MKBIGSI(VM* vm, signed long val) {
6767}
6868
6969static BigInt * getbig(VM * vm, VAL x) {
70− if (ISINT(x)) {
71− BigInt * cl = allocBig(vm);
72− mpz_set_si(*getmpz(cl), GETINT(x));
73− return cl;
74− } else {
75− switch(GETTY(x)) {
76− case CT_FWD:
77− return getbig(vm, ((Fwd*)x)->fwd);
78− default:
79− return (BigInt*)x;
70+ switch(GETTY(x)) {
71+ case CT_INT:
72+ {
73+ BigInt * cl = allocBig(vm);
74+ mpz_set_si(*getmpz(cl), GETINT(x));
75+ return cl;
8076 }
77+ case CT_FWD:
78+ return getbig(vm, ((Fwd*)x)->fwd);
79+ default:
80+ return (BigInt*)x;
8181 }
8282}
8383
rts/idris_rts.c+40−52
@@ -49,9 +49,8 @@ VM* init_vm(int stack_size, size_t heap_size,
4949 vm->ret = NULL;
5050 vm->reg1 = NULL;
5151#ifdef HAS_PTHREAD
52− vm->inbox = malloc(1024*sizeof(vm->inbox[0]));
53− assert(vm->inbox);
54− memset(vm->inbox, 0, 1024*sizeof(vm->inbox[0]));
52+ vm->inbox = malloc(1024*sizeof(VAL));
53+ memset(vm->inbox, 0, 1024*sizeof(VAL));
5554 vm->inbox_end = vm->inbox + 1024;
5655 vm->inbox_write = vm->inbox;
5756 vm->inbox_nextid = 1;
@@ -209,7 +208,7 @@ void * allocate(size_t sz, int lock) {
209208}
210209
211210void* iallocate(VM * vm, size_t isize, int outerlock) {
212−// return malloc(size);
211+// return malloc(isize);
213212 size_t size = aligned(isize);
214213
215214#ifdef HAS_PTHREAD
@@ -277,8 +276,7 @@ static VAL mkstrlen(VM* vm, const char * str, size_t len, int outer) {
277276 String * cl = allocStr(vm, len, outer);
278277 // hdr.u8 used to mark a null string
279278 cl->hdr.u8 = str == NULL;
280− if (!cl->hdr.u8)
281− memcpy(cl->str, str, len);
279+ memcpy(cl->str, str, len);
282280 return (VAL)cl;
283281}
284282
@@ -364,17 +362,11 @@ VAL MKMPTRc(VM* vm, void* ptr, size_t size) {
364362}
365363
366364VAL MKB8(VM* vm, uint8_t bits8) {
367− Bits8 * cl = iallocate(vm, sizeof(*cl), 1);
368− SETTY(cl, CT_BITS8);
369− cl->bits8 = bits8;
370− return (VAL)cl;
365+ return MKINT(bits8);
371366}
372367
373368VAL MKB16(VM* vm, uint16_t bits16) {
374− Bits16 * cl = iallocate(vm, sizeof(*cl), 1);
375− SETTY(cl, CT_BITS16);
376− cl->bits16 = bits16;
377− return (VAL)cl;
369+ return MKINT(bits16);
378370}
379371
380372VAL MKB32(VM* vm, uint32_t bits32) {
@@ -417,11 +409,10 @@ void dumpStack(VM* vm) {
417409void dumpVal(VAL v) {
418410 if (v==NULL) return;
419411 int i;
420− if (ISINT(v)) {
421− printf("%d ", (int)(GETINT(v)));
422− return;
423− }
424412 switch(GETTY(v)) {
413+ case CT_INT:
414+ printf("%d ", (int)(GETINT(v)));
415+ break;
425416 case CT_CON:
426417 {
427418 Con * cl = (Con*)v;
@@ -517,15 +508,10 @@ VAL idris_castBitsStr(VM* vm, VAL i) {
517508 ClosureType ty = GETTY(i);
518509
519510 switch (ty) {
520− case CT_BITS8:
521− // max length 8 bit unsigned int str 3 chars (256)
522− cl = allocStr(vm, 4, 0);
523− cl->slen = sprintf(cl->str, "%" PRIu8, GETBITS8(i));
524− break;
525− case CT_BITS16:
511+ case CT_INT: // 8/16 bits
526512 // max length 16 bit unsigned int str 5 chars (65,535)
527513 cl = allocStr(vm, 6, 0);
528− cl->slen = sprintf(cl->str, "%" PRIu16, GETBITS16(i));
514+ cl->slen = sprintf(cl->str, "%ld", GETBITS16(i));
529515 break;
530516 case CT_BITS32:
531517 // max length 32 bit unsigned int str 10 chars (4,294,967,295)
@@ -859,10 +845,12 @@ static void copyArray(VM* vm, VAL * dst, VAL * src, size_t len) {
859845static VAL doCopyTo(VM* vm, VAL x) {
860846 int ar;
861847 VAL cl;
862− if (x==NULL || ISINT(x)) {
848+ if (x==NULL) {
863849 return x;
864850 }
865851 switch(GETTY(x)) {
852+ case CT_INT:
853+ return x;
866854 case CT_CDATA:
867855 cl = MKCDATAc(vm, GETCDATA(x));
868856 break;
@@ -889,8 +877,6 @@ static VAL doCopyTo(VM* vm, VAL x) {
889877 case CT_FLOAT:
890878 case CT_PTR:
891879 case CT_MANAGEDPTR:
892− case CT_BITS8:
893− case CT_BITS16:
894880 case CT_BITS32:
895881 case CT_BITS64:
896882 case CT_RAWDATA:
@@ -901,6 +887,7 @@ static VAL doCopyTo(VM* vm, VAL x) {
901887 break;
902888 default:
903889 assert(0); // We're in trouble if this happens...
890+ cl = NULL;
904891 }
905892 return cl;
906893}
@@ -978,7 +965,7 @@ Msg* idris_checkInitMessages(VM* vm) {
978965 Msg* msg;
979966
980967 for (msg = vm->inbox; msg < vm->inbox_end && msg->msg != NULL; ++msg) {
981− if ((msg->channel_id & 1) == 1) { // init bit set
968+ if (msg->channel_id && 1 == 1) { // init bit set
982969 return msg;
983970 }
984971 }
@@ -1041,7 +1028,7 @@ Msg* idris_recvMessage(VM* vm) {
10411028
10421029Msg* idris_recvMessageFrom(VM* vm, int channel_id, VM* sender) {
10431030 Msg* msg;
1044− Msg* ret = malloc(sizeof(Msg));
1031+ Msg* ret;
10451032
10461033 struct timespec timeout;
10471034 int status;
@@ -1064,34 +1051,35 @@ Msg* idris_recvMessageFrom(VM* vm, int channel_id, VM* sender) {
10641051 }
10651052 pthread_mutex_unlock(&vm->inbox_block);
10661053
1067− if (msg != NULL) {
1068− ret->msg = msg->msg;
1069− ret->sender = msg->sender;
1070−
1071− pthread_mutex_lock(&(vm->inbox_lock));
1054+ if (msg == NULL) {
1055+ fprintf(stderr, "No messages waiting");
1056+ exit(-1);
1057+ }
10721058
1073− // Slide everything down after the message in the inbox,
1074− // Move the inbox_write pointer down, and clear the value at the
1075− // end - O(n) but it's easier since the message from a specific
1076− // sender could be anywhere in the inbox
1059+ ret = malloc(sizeof(Msg));
1060+ ret->msg = msg->msg;
1061+ ret->sender = msg->sender;
10771062
1078− for(;msg < vm->inbox_write; ++msg) {
1079− if (msg+1 != vm->inbox_end) {
1080− msg->sender = (msg + 1)->sender;
1081− msg->msg = (msg + 1)->msg;
1082− }
1083− }
1063+ pthread_mutex_lock(&(vm->inbox_lock));
10841064
1085− vm->inbox_write->msg = NULL;
1086− vm->inbox_write->sender = NULL;
1087− vm->inbox_write--;
1065+ // Slide everything down after the message in the inbox,
1066+ // Move the inbox_write pointer down, and clear the value at the
1067+ // end - O(n) but it's easier since the message from a specific
1068+ // sender could be anywhere in the inbox
10881069
1089− pthread_mutex_unlock(&(vm->inbox_lock));
1090− } else {
1091− fprintf(stderr, "No messages waiting");
1092− exit(-1);
1070+ for(;msg < vm->inbox_write; ++msg) {
1071+ if (msg+1 != vm->inbox_end) {
1072+ msg->sender = (msg + 1)->sender;
1073+ msg->msg = (msg + 1)->msg;
1074+ }
10931075 }
10941076
1077+ vm->inbox_write->msg = NULL;
1078+ vm->inbox_write->sender = NULL;
1079+ vm->inbox_write--;
1080+
1081+ pthread_mutex_unlock(&(vm->inbox_lock));
1082+
10951083 return ret;
10961084}
10971085#endif
rts/idris_rts.h+6−22
@@ -24,10 +24,9 @@
2424// Closures
2525typedef enum {
2626 CT_CON, CT_ARRAY, CT_INT, CT_BIGINT,
27− CT_FLOAT, CT_STRING, CT_STROFFSET, CT_BITS8,
28− CT_BITS16, CT_BITS32, CT_BITS64, CT_PTR,
29− CT_REF, CT_FWD, CT_MANAGEDPTR, CT_RAWDATA,
30− CT_CDATA
27+ CT_FLOAT, CT_STRING, CT_STROFFSET, CT_BITS32,
28+ CT_BITS64, CT_PTR, CT_REF, CT_FWD,
29+ CT_MANAGEDPTR, CT_RAWDATA, CT_CDATA
3130} ClosureType;
3231
3332typedef struct Hdr {
@@ -54,11 +53,6 @@ typedef struct Array {
5453 VAL array[0];
5554} Array;
5655
57−typedef struct Int {
58− Hdr hdr;
59− int i;
60−} Int;
61−
6256typedef struct BigInt {
6357 Hdr hdr;
6458 char big[0];
@@ -81,16 +75,6 @@ typedef struct StrOffset {
8175 size_t offset;
8276} StrOffset;
8377
84−typedef struct Bits8 {
85− Hdr hdr;
86− uint8_t bits8;
87−} Bits8;
88−
89−typedef struct Bits16 {
90− Hdr hdr;
91− uint16_t bits16;
92−} Bits16;
93−
9478typedef struct Bits32 {
9579 Hdr hdr;
9680 uint32_t bits32;
@@ -254,8 +238,8 @@ static inline size_t getstrlen(String * x) {
254238#define GETFLOAT(x) (((Float*)(x))->f)
255239#define GETCDATA(x) (((CDataC*)(x))->item)
256240
257−#define GETBITS8(x) (((Bits8*)(x))->bits8)
258−#define GETBITS16(x) (((Bits16*)(x))->bits16)
241+#define GETBITS8(x) (GETINT(x))
242+#define GETBITS16(x) (GETINT(x))
259243#define GETBITS32(x) (((Bits32*)(x))->bits32)
260244#define GETBITS64(x) (((Bits64*)(x))->bits64)
261245
@@ -268,7 +252,7 @@ static inline size_t getstrlen(String * x) {
268252
269253#define CELEM(x) (((x)->hdr.sz - sizeof(Array)) / sizeof(VAL))
270254
271−#define GETTY(x) ((ClosureType)((x)->hdr.ty))
255+#define GETTY(x) (ISINT(x)? CT_INT : (ClosureType)((x)->hdr.ty))
272256#define SETTY(x,t) ((x)->hdr.ty = t)
273257
274258// Integers, floats and operators