Skip to content

Commit

Permalink
Unboxed Bits8/Bits16 using Int as the underlying representation
Browse files Browse the repository at this point in the history
  • Loading branch information
jacereda committed Apr 26, 2018
1 parent fb301a5 commit e9a31f4
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 101 deletions.
10 changes: 2 additions & 8 deletions rts/idris_bitstring.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,11 @@
#include "idris_rts.h"

VAL idris_b8const(VM *vm, uint8_t a) {
Bits8 * cl = iallocate(vm, sizeof(*cl), 0);
SETTY(cl, CT_BITS8);
cl->bits8 = a;
return (VAL)cl;
return MKINT(a);
}

VAL idris_b16const(VM *vm, uint16_t a) {
Bits16 * cl = iallocate(vm, sizeof(*cl), 0);
SETTY(cl, CT_BITS16);
cl->bits16 = a;
return (VAL)cl;
return MKINT(a);
}

VAL idris_b32const(VM *vm, uint32_t a) {
Expand Down
24 changes: 15 additions & 9 deletions rts/idris_gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,27 @@
#include "idris_bitstring.h"
#include <assert.h>

static inline VAL plain(VM* vm, VAL x, size_t sz) {
VAL cl = iallocate(vm, sz, 1);
memcpy(cl, x, sz);
return cl;
}

VAL copy(VM* vm, VAL x) {
int ar;
VAL cl;
if (x==NULL || ISINT(x)) {
if (x==NULL) {
return x;
}
switch(GETTY(x)) {
case CT_INT: return x;
case CT_BITS32: return plain(vm, x, sizeof(Bits32));
case CT_BITS64: return plain(vm, x, sizeof(Bits64));
case CT_FLOAT: return plain(vm, x, sizeof(Float));
case CT_FWD:
return GETPTR(x);
case CT_CDATA:
cl = MKCDATAc(vm, GETCDATA(x));
cl = plain(vm, x, sizeof(CDataC));
c_heap_mark_item(GETCDATA(x));
break;
case CT_BIGINT:
Expand All @@ -29,22 +39,18 @@ VAL copy(VM* vm, VAL x) {
case CT_ARRAY:
case CT_STRING:
case CT_REF:
case CT_FLOAT:
case CT_STROFFSET:
case CT_PTR:
case CT_MANAGEDPTR:
case CT_BITS8:
case CT_BITS16:
case CT_BITS32:
case CT_BITS64:
case CT_RAWDATA:
cl = iallocate(vm, x->hdr.sz, 1);
memcpy(cl, x, x->hdr.sz);
cl = plain(vm, x, x->hdr.sz);
break;
default:
cl = NULL;
assert(0);
break;
}
assert(x->hdr.sz >= sizeof(Fwd));
SETTY(x, CT_FWD);
((Fwd*)x)->fwd = cl;
return cl;
Expand Down
20 changes: 10 additions & 10 deletions rts/idris_gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,17 @@ VAL MKBIGSI(VM* vm, signed long val) {
}

static BigInt * getbig(VM * vm, VAL x) {
if (ISINT(x)) {
BigInt * cl = allocBig(vm);
mpz_set_si(*getmpz(cl), GETINT(x));
return cl;
} else {
switch(GETTY(x)) {
case CT_FWD:
return getbig(vm, ((Fwd*)x)->fwd);
default:
return (BigInt*)x;
switch(GETTY(x)) {
case CT_INT:
{
BigInt * cl = allocBig(vm);
mpz_set_si(*getmpz(cl), GETINT(x));
return cl;
}
case CT_FWD:
return getbig(vm, ((Fwd*)x)->fwd);
default:
return (BigInt*)x;
}
}

Expand Down
92 changes: 40 additions & 52 deletions rts/idris_rts.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ VM* init_vm(int stack_size, size_t heap_size,
vm->ret = NULL;
vm->reg1 = NULL;
#ifdef HAS_PTHREAD
vm->inbox = malloc(1024*sizeof(vm->inbox[0]));
assert(vm->inbox);
memset(vm->inbox, 0, 1024*sizeof(vm->inbox[0]));
vm->inbox = malloc(1024*sizeof(VAL));
memset(vm->inbox, 0, 1024*sizeof(VAL));
vm->inbox_end = vm->inbox + 1024;
vm->inbox_write = vm->inbox;
vm->inbox_nextid = 1;
Expand Down Expand Up @@ -209,7 +208,7 @@ void * allocate(size_t sz, int lock) {
}

void* iallocate(VM * vm, size_t isize, int outerlock) {
// return malloc(size);
// return malloc(isize);
size_t size = aligned(isize);

#ifdef HAS_PTHREAD
Expand Down Expand Up @@ -277,8 +276,7 @@ static VAL mkstrlen(VM* vm, const char * str, size_t len, int outer) {
String * cl = allocStr(vm, len, outer);
// hdr.u8 used to mark a null string
cl->hdr.u8 = str == NULL;
if (!cl->hdr.u8)
memcpy(cl->str, str, len);
memcpy(cl->str, str, len);
return (VAL)cl;
}

Expand Down Expand Up @@ -364,17 +362,11 @@ VAL MKMPTRc(VM* vm, void* ptr, size_t size) {
}

VAL MKB8(VM* vm, uint8_t bits8) {
Bits8 * cl = iallocate(vm, sizeof(*cl), 1);
SETTY(cl, CT_BITS8);
cl->bits8 = bits8;
return (VAL)cl;
return MKINT(bits8);
}

VAL MKB16(VM* vm, uint16_t bits16) {
Bits16 * cl = iallocate(vm, sizeof(*cl), 1);
SETTY(cl, CT_BITS16);
cl->bits16 = bits16;
return (VAL)cl;
return MKINT(bits16);
}

VAL MKB32(VM* vm, uint32_t bits32) {
Expand Down Expand Up @@ -417,11 +409,10 @@ void dumpStack(VM* vm) {
void dumpVal(VAL v) {
if (v==NULL) return;
int i;
if (ISINT(v)) {
printf("%d ", (int)(GETINT(v)));
return;
}
switch(GETTY(v)) {
case CT_INT:
printf("%d ", (int)(GETINT(v)));
break;
case CT_CON:
{
Con * cl = (Con*)v;
Expand Down Expand Up @@ -517,15 +508,10 @@ VAL idris_castBitsStr(VM* vm, VAL i) {
ClosureType ty = GETTY(i);

switch (ty) {
case CT_BITS8:
// max length 8 bit unsigned int str 3 chars (256)
cl = allocStr(vm, 4, 0);
cl->slen = sprintf(cl->str, "%" PRIu8, GETBITS8(i));
break;
case CT_BITS16:
case CT_INT: // 8/16 bits
// max length 16 bit unsigned int str 5 chars (65,535)
cl = allocStr(vm, 6, 0);
cl->slen = sprintf(cl->str, "%" PRIu16, GETBITS16(i));
cl->slen = sprintf(cl->str, "%ld", GETBITS16(i));
break;
case CT_BITS32:
// max length 32 bit unsigned int str 10 chars (4,294,967,295)
Expand Down Expand Up @@ -859,10 +845,12 @@ static void copyArray(VM* vm, VAL * dst, VAL * src, size_t len) {
static VAL doCopyTo(VM* vm, VAL x) {
int ar;
VAL cl;
if (x==NULL || ISINT(x)) {
if (x==NULL) {
return x;
}
switch(GETTY(x)) {
case CT_INT:
return x;
case CT_CDATA:
cl = MKCDATAc(vm, GETCDATA(x));
break;
Expand All @@ -889,8 +877,6 @@ static VAL doCopyTo(VM* vm, VAL x) {
case CT_FLOAT:
case CT_PTR:
case CT_MANAGEDPTR:
case CT_BITS8:
case CT_BITS16:
case CT_BITS32:
case CT_BITS64:
case CT_RAWDATA:
Expand All @@ -901,6 +887,7 @@ static VAL doCopyTo(VM* vm, VAL x) {
break;
default:
assert(0); // We're in trouble if this happens...
cl = NULL;
}
return cl;
}
Expand Down Expand Up @@ -978,7 +965,7 @@ Msg* idris_checkInitMessages(VM* vm) {
Msg* msg;

for (msg = vm->inbox; msg < vm->inbox_end && msg->msg != NULL; ++msg) {
if ((msg->channel_id & 1) == 1) { // init bit set
if (msg->channel_id && 1 == 1) { // init bit set
return msg;
}
}
Expand Down Expand Up @@ -1041,7 +1028,7 @@ Msg* idris_recvMessage(VM* vm) {

Msg* idris_recvMessageFrom(VM* vm, int channel_id, VM* sender) {
Msg* msg;
Msg* ret = malloc(sizeof(Msg));
Msg* ret;

struct timespec timeout;
int status;
Expand All @@ -1064,34 +1051,35 @@ Msg* idris_recvMessageFrom(VM* vm, int channel_id, VM* sender) {
}
pthread_mutex_unlock(&vm->inbox_block);

if (msg != NULL) {
ret->msg = msg->msg;
ret->sender = msg->sender;

pthread_mutex_lock(&(vm->inbox_lock));
if (msg == NULL) {
fprintf(stderr, "No messages waiting");
exit(-1);
}

// Slide everything down after the message in the inbox,
// Move the inbox_write pointer down, and clear the value at the
// end - O(n) but it's easier since the message from a specific
// sender could be anywhere in the inbox
ret = malloc(sizeof(Msg));
ret->msg = msg->msg;
ret->sender = msg->sender;

for(;msg < vm->inbox_write; ++msg) {
if (msg+1 != vm->inbox_end) {
msg->sender = (msg + 1)->sender;
msg->msg = (msg + 1)->msg;
}
}
pthread_mutex_lock(&(vm->inbox_lock));

vm->inbox_write->msg = NULL;
vm->inbox_write->sender = NULL;
vm->inbox_write--;
// Slide everything down after the message in the inbox,
// Move the inbox_write pointer down, and clear the value at the
// end - O(n) but it's easier since the message from a specific
// sender could be anywhere in the inbox

pthread_mutex_unlock(&(vm->inbox_lock));
} else {
fprintf(stderr, "No messages waiting");
exit(-1);
for(;msg < vm->inbox_write; ++msg) {
if (msg+1 != vm->inbox_end) {
msg->sender = (msg + 1)->sender;
msg->msg = (msg + 1)->msg;
}
}

vm->inbox_write->msg = NULL;
vm->inbox_write->sender = NULL;
vm->inbox_write--;

pthread_mutex_unlock(&(vm->inbox_lock));

return ret;
}
#endif
Expand Down
28 changes: 6 additions & 22 deletions rts/idris_rts.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@
// Closures
typedef enum {
CT_CON, CT_ARRAY, CT_INT, CT_BIGINT,
CT_FLOAT, CT_STRING, CT_STROFFSET, CT_BITS8,
CT_BITS16, CT_BITS32, CT_BITS64, CT_PTR,
CT_REF, CT_FWD, CT_MANAGEDPTR, CT_RAWDATA,
CT_CDATA
CT_FLOAT, CT_STRING, CT_STROFFSET, CT_BITS32,
CT_BITS64, CT_PTR, CT_REF, CT_FWD,
CT_MANAGEDPTR, CT_RAWDATA, CT_CDATA
} ClosureType;

typedef struct Hdr {
Expand All @@ -54,11 +53,6 @@ typedef struct Array {
VAL array[0];
} Array;

typedef struct Int {
Hdr hdr;
int i;
} Int;

typedef struct BigInt {
Hdr hdr;
char big[0];
Expand All @@ -81,16 +75,6 @@ typedef struct StrOffset {
size_t offset;
} StrOffset;

typedef struct Bits8 {
Hdr hdr;
uint8_t bits8;
} Bits8;

typedef struct Bits16 {
Hdr hdr;
uint16_t bits16;
} Bits16;

typedef struct Bits32 {
Hdr hdr;
uint32_t bits32;
Expand Down Expand Up @@ -254,8 +238,8 @@ static inline size_t getstrlen(String * x) {
#define GETFLOAT(x) (((Float*)(x))->f)
#define GETCDATA(x) (((CDataC*)(x))->item)

#define GETBITS8(x) (((Bits8*)(x))->bits8)
#define GETBITS16(x) (((Bits16*)(x))->bits16)
#define GETBITS8(x) (GETINT(x))
#define GETBITS16(x) (GETINT(x))
#define GETBITS32(x) (((Bits32*)(x))->bits32)
#define GETBITS64(x) (((Bits64*)(x))->bits64)

Expand All @@ -268,7 +252,7 @@ static inline size_t getstrlen(String * x) {

#define CELEM(x) (((x)->hdr.sz - sizeof(Array)) / sizeof(VAL))

#define GETTY(x) ((ClosureType)((x)->hdr.ty))
#define GETTY(x) (ISINT(x)? CT_INT : (ClosureType)((x)->hdr.ty))
#define SETTY(x,t) ((x)->hdr.ty = t)

// Integers, floats and operators
Expand Down

0 comments on commit e9a31f4

Please sign in to comment.