From e786df74a85396e76ec92801a73b5517aae066a0 Mon Sep 17 00:00:00 2001 From: Louis Rubet Date: Fri, 21 Apr 2017 17:40:01 +0200 Subject: [PATCH] #34: going to significand stored in stack [first part] --- src/parse.h | 11 +++++++- src/rpn.cpp | 4 +-- src/stack.h | 72 +++++++++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 77 insertions(+), 10 deletions(-) diff --git a/src/parse.h b/src/parse.h index 4d7efe5..c0371f7 100644 --- a/src/parse.h +++ b/src/parse.h @@ -544,7 +544,16 @@ static ret_value parse(const char* entry, program& prog) // ex: entry="1 2+" -> vector = {"1", "2+"} -> first "1", second "2" and remaining_entry="+" // this remaining entry is treated as an entry if(_obj_from_string(main_entry, obj, obj_size, type, remaining_entry)) - prog.push_back(obj, obj_size, type); + { + if (type == cmd_number) + { + void* significand; + prog.allocate_back(obj_size, type, MPFR_128BITS_STORING_LENGTH, &significand); + + } + else + prog.push_back(obj, obj_size, type); + } main_entry = remaining_entry; } } diff --git a/src/rpn.cpp b/src/rpn.cpp index f84bae8..619ef87 100644 --- a/src/rpn.cpp +++ b/src/rpn.cpp @@ -193,7 +193,7 @@ struct number : public object _value = value; } unsigned int size() { return (unsigned int)sizeof(number); } - + void ensure_significand() { _value.mpfr._mpfr_d = (mp_limb_t*)_value.significand; @@ -229,7 +229,7 @@ int number::s_current_precision = number::s_default_precision; struct binary : public object { integer_t _value; - + // void set(integer_t value) { diff --git a/src/stack.h b/src/stack.h index efab231..16cde82 100644 --- a/src/stack.h +++ b/src/stack.h @@ -10,7 +10,9 @@ using namespace std; #include "debug.h" -#define ALLOC_BLOB (2*1024) +// allocation base size +#define ALLOC_STACK_CHUNK (64*1024) +#define ALLOC_BLOB_CHUNK (64*1024) #define LOCAL_COPY_PLACES 3 #define LOCAL_COPY_SIZE 128 @@ -28,9 +30,11 @@ private: public: stack() { - _base = (char*)malloc(ALLOC_BLOB); - _total_size = ALLOC_BLOB; + _base = (char*)malloc(ALLOC_STACK_CHUNK); + _blob = (char*)malloc(ALLOC_BLOB_CHUNK); + _total_size = ALLOC_STACK_CHUNK; _current = _base; + _current_blob = _base; _count = 0; } virtual ~stack() { free(_base); } @@ -40,7 +44,7 @@ public: if (_current + size > _base + _total_size) { //TODO gerer les pbs de memoire - _total_size += ALLOC_BLOB; + _total_size += ALLOC_STACK_CHUNK; _base = (char*)realloc(_base, _total_size); } @@ -48,11 +52,46 @@ public: memcpy(_current, obj, size); _vlen.push_back(size); _vpointer.push_back(_current); + _vlen_blob.push_back(0); _vtype.push_back(type); _count++; _current += size; } + void* allocate_back(unsigned int size, int type, unsigned int blob_size = 0, void** blob = NULL) + { + void* allocated; + + if (_current + size > _base + _total_size) + { + //TODO gerer les pbs de memoire + _total_size += ALLOC_STACK_CHUNK; + _base = (char*)realloc(_base, _total_size); + } + if (_current_blob + blob_size > _blob + _total_blob_size) + { + //TODO gerer les pbs de memoire + _total_blob_size += ALLOC_BLOB_CHUNK; + _blob = (char*)realloc(_base, _total_blob_size); + } + + _vlen.push_back(size); + _vpointer.push_back(_current); + allocated = _current; + _current += size; + + _vlen_blob.push_back(blob_size); + _vpointer_blob.push_back(_current_blob); + if (blob != NULL) + *blob = _current_blob; + _current_blob += blob_size; + + _vtype.push_back(type); + _count++; + + return allocated; + } + void* pop_back() { void* back = NULL; @@ -62,6 +101,11 @@ public: _current = _vpointer[_count - 1]; _vlen.pop_back(); _vpointer.pop_back(); + + _current_blob = _vpointer_blob[_count - 1]; + _vlen_blob.pop_back(); + _vpointer_blob.pop_back(); + _vtype.pop_back(); _count--; @@ -98,6 +142,14 @@ public: return NULL; } + void* back_blob() + { + if (_count>0) + return _vpointer[_count-1]; + else + return NULL; + } + unsigned int get_len(unsigned int index) { if (index<_count) @@ -141,7 +193,7 @@ public: // local objects copy void copy_obj_to_local(unsigned int index, unsigned int to_place) - { + { assert(to_place < LOCAL_COPY_PLACES); struct local_copy* local = (struct local_copy*)_places[to_place]; local->length = get_len(index); @@ -150,7 +202,7 @@ public: } void push_obj_from_local(unsigned int from_place) - { + { assert(from_place < LOCAL_COPY_PLACES); struct local_copy* local = (struct local_copy*)_places[from_place]; push_back(&local->blob, local->length, local->type); @@ -163,12 +215,18 @@ public: private: char* _base; + char* _blob; char* _current; - vector _vlen;// size of each entry in bytes + char* _current_blob; + vector _vpointer;//pointer on each entry + vector _vpointer_blob;//pointer on each entry blob + vector _vlen;// size of each entry in bytes + vector _vlen_blob;// size of each blob entry in bytes vector _vtype;//type of each entry unsigned int _count;// =_vlen.size()=_vpointer.size()=_vtype.size() unsigned int _total_size;//total allocated size in bytes + unsigned int _total_blob_size;//total allocated blob size in bytes char _places[LOCAL_COPY_PLACES][LOCAL_COPY_SIZE]; };