X-Git-Url: https://ginac.de/CLN/cln.git//cln.git?a=blobdiff_plain;f=src%2Fvector%2Fcl_GV_I.cc;h=dc8aab3224b992dc8a77fa9dee8ea8ab3ae91dc0;hb=3af2cde18b3aabed4c808b0113daa81c2263b0bd;hp=7fdcd81ffce392b52f1496042af76d7422495a16;hpb=6fdd87f5e762926b273583db2d6440862584596f;p=cln.git diff --git a/src/vector/cl_GV_I.cc b/src/vector/cl_GV_I.cc index 7fdcd81..dc8aab3 100644 --- a/src/vector/cl_GV_I.cc +++ b/src/vector/cl_GV_I.cc @@ -1,9 +1,7 @@ // cl_make_heap_GV_I(). // General includes. -#include "cl_sysdep.h" - -CL_PROVIDE(cl_GV_I) +#include "base/cl_sysdep.h" // Specification. #include "cln/GV_integer.h" @@ -11,10 +9,10 @@ CL_PROVIDE(cl_GV_I) // Implementation. -#include "cl_I.h" -#include "cl_DS.h" -#include "cln/abort.h" -#include "cl_offsetof.h" +#include "integer/cl_I.h" +#include "base/digitseq/cl_DS.h" +#include "cln/exception.h" +#include "base/cl_offsetof.h" namespace cln { @@ -29,18 +27,19 @@ namespace cln { static void cl_gvector_integer_destructor (cl_heap* pointer) { -#if (defined(__mips__) || defined(__mips64__)) && !defined(__GNUC__) // workaround SGI CC bug - (*(cl_heap_GV_I*)pointer).~cl_heap_GV(); -#else (*(cl_heap_GV_I*)pointer).~cl_heap_GV_I(); -#endif } -cl_class cl_class_gvector_integer = { - cl_gvector_integer_destructor, - 0 -}; - +// XXX: Ugh, this needs to be non-const (and non-static) for overriding +// the printing function (see cl_GV_I_debug.cc) +cl_class& cl_class_gvector_integer() +{ + static cl_class instance = { + cl_gvector_integer_destructor, + 0 + }; + return instance; +} static inline cl_heap_GV_I * outcast (cl_GV_inner* vec) { @@ -56,7 +55,7 @@ static inline const cl_heap_GV_I * outcast (const cl_GV_inner* vec) struct cl_GV_I_vectorops { cl_GV_vectorops ops; - sintL m; // for maxbits + sintC m; // for maxbits }; static inline cl_GV_I_vectorops* outcast (cl_GV_vectorops* vectorops) @@ -70,44 +69,44 @@ static inline cl_GV_I_vectorops* outcast (cl_GV_vectorops* vectorops) struct cl_heap_GV_I_general : public cl_heap_GV_I { cl_I data[1]; // Standard allocation disabled. - void* operator new (size_t size) { unused size; cl_abort(); return (void*)1; } + void* operator new (size_t size) = delete; // Standard deallocation disabled. - void operator delete (void* ptr) { unused ptr; cl_abort(); } + void operator delete (void* ptr) = delete; // No default constructor. cl_heap_GV_I_general (); }; -static const cl_I general_element (const cl_GV_inner* vec, uintL index) +static const cl_I general_element (const cl_GV_inner* vec, std::size_t index) { return ((const cl_heap_GV_I_general *) outcast(vec))->data[index]; } -static void general_set_element (cl_GV_inner* vec, uintL index, const cl_I& x) +static void general_set_element (cl_GV_inner* vec, std::size_t index, const cl_I& x) { ((cl_heap_GV_I_general *) outcast(vec))->data[index] = x; } static void general_do_delete (cl_GV_inner* vec) { - var cl_heap_GV_I_general* hv = (cl_heap_GV_I_general *) outcast(vec); - var uintL len = hv->v.length(); - for (var uintL i = 0; i < len; i++) + cl_heap_GV_I_general* hv = (cl_heap_GV_I_general *) outcast(vec); + std::size_t len = hv->v.size(); + for (std::size_t i = 0; i < len; i++) hv->data[i].~cl_I(); } -static void general_copy_elements (const cl_GV_inner* srcvec, uintL srcindex, cl_GV_inner* destvec, uintL destindex, uintL count) +static void general_copy_elements (const cl_GV_inner* srcvec, std::size_t srcindex, cl_GV_inner* destvec, std::size_t destindex, std::size_t count) { if (count > 0) { - var const cl_heap_GV_I_general* srcv = + const cl_heap_GV_I_general* srcv = (const cl_heap_GV_I_general *) outcast(srcvec); - var cl_heap_GV_I_general* destv = + cl_heap_GV_I_general* destv = (cl_heap_GV_I_general *) outcast(destvec); - var uintL srclen = srcv->v.length(); - var uintL destlen = destv->v.length(); + std::size_t srclen = srcv->v.size(); + std::size_t destlen = destv->v.size(); if (!(srcindex <= srcindex+count && srcindex+count <= srclen)) - cl_abort(); + throw runtime_exception(); if (!(destindex <= destindex+count && destindex+count <= destlen)) - cl_abort(); + throw runtime_exception(); do { destv->data[destindex++] = srcv->data[srcindex++]; } while (--count > 0); @@ -122,13 +121,13 @@ static cl_GV_I_vectorops general_vectorops = {{ -1 }; -cl_heap_GV_I* cl_make_heap_GV_I (uintL len) +cl_heap_GV_I* cl_make_heap_GV_I (std::size_t len) { - var cl_heap_GV_I_general* hv = (cl_heap_GV_I_general*) malloc_hook(offsetofa(cl_heap_GV_I_general,data)+sizeof(cl_I)*len); + cl_heap_GV_I_general* hv = (cl_heap_GV_I_general*) malloc_hook(offsetofa(cl_heap_GV_I_general,data)+sizeof(cl_I)*len); hv->refcount = 1; - hv->type = &cl_class_gvector_integer; + hv->type = &cl_class_gvector_integer(); new (&hv->v) cl_GV_inner (len,&general_vectorops.ops); - for (var uintL i = 0; i < len; i++) + for (std::size_t i = 0; i < len; i++) init1(cl_I, hv->data[i]) (); return hv; } @@ -140,30 +139,30 @@ cl_heap_GV_I* cl_make_heap_GV_I (uintL len) struct cl_heap_GV_I_bits##m : public cl_heap_GV_I { \ uint_t data[1]; \ /* Standard allocation disabled. */ \ - void* operator new (size_t size) { unused size; cl_abort(); return (void*)1; } \ + void* operator new (size_t size) = delete; \ /* Standard deallocation disabled. */ \ - void operator delete (void* ptr) { unused ptr; cl_abort(); } \ + void operator delete (void* ptr) = delete; \ /* No default constructor. */ \ cl_heap_GV_I_bits##m (); \ }; \ -static const cl_I bits##m##_element (const cl_GV_inner* vec, uintL index); \ -static void bits##m##_set_element (cl_GV_inner* vec, uintL index, const cl_I& x); \ -static void bits##m##_copy_elements (const cl_GV_inner* srcvec, uintL srcindex, cl_GV_inner* destvec, uintL destindex, uintL count) \ +static const cl_I bits##m##_element (const cl_GV_inner* vec, std::size_t index); \ +static void bits##m##_set_element (cl_GV_inner* vec, std::size_t index, const cl_I& x); \ +static void bits##m##_copy_elements (const cl_GV_inner* srcvec, std::size_t srcindex, cl_GV_inner* destvec, std::size_t destindex, std::size_t count) \ { \ if (count > 0) { \ - var const cl_heap_GV_I_bits##m * srcv = \ + const cl_heap_GV_I_bits##m * srcv = \ (const cl_heap_GV_I_bits##m *) outcast(srcvec); \ - var cl_heap_GV_I_bits##m * destv = \ + cl_heap_GV_I_bits##m * destv = \ (cl_heap_GV_I_bits##m *) outcast(destvec); \ - var uintL srclen = srcv->v.length(); \ - var uintL destlen = destv->v.length(); \ + std::size_t srclen = srcv->v.size(); \ + std::size_t destlen = destv->v.size(); \ if (!(srcindex <= srcindex+count && srcindex+count <= srclen)) \ - cl_abort(); \ + throw runtime_exception(); \ if (!(destindex <= destindex+count && destindex+count <= destlen)) \ - cl_abort(); \ + throw runtime_exception(); \ if (m == intDsize) { \ - var const uintD* srcptr = &srcv->data[srcindex]; \ - var uintD* destptr = &destv->data[destindex]; \ + const uintD* srcptr = &srcv->data[srcindex]; \ + uintD* destptr = &destv->data[destindex]; \ do { \ *destptr++ = *srcptr++; \ } while (--count > 0); \ @@ -181,12 +180,12 @@ static cl_GV_I_vectorops bits##m##_vectorops = {{ \ static void bits_do_delete (cl_GV_inner* vec) { - unused vec; + cl_unused vec; } // Copy bits srcptr.bits[srcindex..srcindex+count-1] into destptr.bits[destindex..destindex+count-1]. // Assumes that all range checks have already been performed. -static void bits_copy (const uintD* srcptr, uintL srcindex, uintD* destptr, uintL destindex, uintL count) +static void bits_copy (const uintD* srcptr, std::size_t srcindex, uintD* destptr, std::size_t destindex, std::size_t count) { srcptr += floor(srcindex,intDsize); destptr += floor(destindex,intDsize); @@ -206,7 +205,7 @@ static void bits_copy (const uintD* srcptr, uintL srcindex, uintD* destptr, uint count -= intDsize-srcindex; } // Now srcindex and destindex can be assumed to be 0. - var uintL count1 = count%intDsize; + std::size_t count1 = count%intDsize; count = floor(count,intDsize); if (count > 0) { do { @@ -217,8 +216,8 @@ static void bits_copy (const uintD* srcptr, uintL srcindex, uintD* destptr, uint *destptr ^= (*destptr ^ *srcptr) & (uintD)(bit(count1)-1); } } else { - var uintL i = destindex - srcindex; - var uintD tmp; + std::size_t i = destindex - srcindex; + uintD tmp; if (destindex >= srcindex) { // i > 0 if (count <= intDsize-destindex) { *destptr ^= (*destptr ^ (*srcptr << i)) & ((uintD)(bit(count)-1) << destindex); @@ -239,9 +238,9 @@ static void bits_copy (const uintD* srcptr, uintL srcindex, uintD* destptr, uint } srcptr++; // tmp now contains the low i bits to be put into *destptr. - var uintL count1 = count%intDsize; + std::size_t count1 = count%intDsize; count = floor(count,intDsize); - var uintD lastdest; + uintD lastdest; if (count == 0) lastdest = tmp; else { @@ -277,73 +276,73 @@ static void bits_copy (const uintD* srcptr, uintL srcindex, uintD* destptr, uint DEFINE_cl_heap_GV_I_bits(1,uintD) -static const cl_I bits1_element (const cl_GV_inner* vec, uintL index) +static const cl_I bits1_element (const cl_GV_inner* vec, std::size_t index) { return (unsigned int)((((const cl_heap_GV_I_bits1 *) outcast(vec))->data[index/intDsize] >> (index%intDsize)) & 0x1); } -static void bits1_set_element (cl_GV_inner* vec, uintL index, const cl_I& x) +static void bits1_set_element (cl_GV_inner* vec, std::size_t index, const cl_I& x) { - var uintV xval; + uintV xval; if (fixnump(x)) { xval = FN_to_UV(x); if (xval <= 0x1) { - var uintD* ptr = &((cl_heap_GV_I_bits1 *) outcast(vec))->data[index/intDsize]; + uintD* ptr = &((cl_heap_GV_I_bits1 *) outcast(vec))->data[index/intDsize]; index = index%intDsize; *ptr = *ptr ^ ((*ptr ^ ((uintD)xval << index)) & ((uintD)0x1 << index)); return; } } - cl_abort(); + throw runtime_exception(); } DEFINE_cl_heap_GV_I_bits(2,uintD) -static const cl_I bits2_element (const cl_GV_inner* vec, uintL index) +static const cl_I bits2_element (const cl_GV_inner* vec, std::size_t index) { return (unsigned int)((((const cl_heap_GV_I_bits2 *) outcast(vec))->data[index/(intDsize/2)] >> (2*(index%(intDsize/2)))) & 0x3); } -static void bits2_set_element (cl_GV_inner* vec, uintL index, const cl_I& x) +static void bits2_set_element (cl_GV_inner* vec, std::size_t index, const cl_I& x) { - var uintV xval; + uintV xval; if (fixnump(x)) { xval = FN_to_UV(x); if (xval <= 0x3) { - var uintD* ptr = &((cl_heap_GV_I_bits2 *) outcast(vec))->data[index/(intDsize/2)]; + uintD* ptr = &((cl_heap_GV_I_bits2 *) outcast(vec))->data[index/(intDsize/2)]; index = 2*(index%(intDsize/2)); *ptr = *ptr ^ ((*ptr ^ ((uintD)xval << index)) & ((uintD)0x3 << index)); return; } } - cl_abort(); + throw runtime_exception(); } DEFINE_cl_heap_GV_I_bits(4,uintD) -static const cl_I bits4_element (const cl_GV_inner* vec, uintL index) +static const cl_I bits4_element (const cl_GV_inner* vec, std::size_t index) { return (unsigned int)((((const cl_heap_GV_I_bits4 *) outcast(vec))->data[index/(intDsize/4)] >> (4*(index%(intDsize/4)))) & 0xF); } -static void bits4_set_element (cl_GV_inner* vec, uintL index, const cl_I& x) +static void bits4_set_element (cl_GV_inner* vec, std::size_t index, const cl_I& x) { - var uintV xval; + uintV xval; if (fixnump(x)) { xval = FN_to_UV(x); if (xval <= 0xF) { - var uintD* ptr = &((cl_heap_GV_I_bits4 *) outcast(vec))->data[index/(intDsize/4)]; + uintD* ptr = &((cl_heap_GV_I_bits4 *) outcast(vec))->data[index/(intDsize/4)]; index = 4*(index%(intDsize/4)); *ptr = *ptr ^ ((*ptr ^ ((uintD)xval << index)) & ((uintD)0xF << index)); return; } } - cl_abort(); + throw runtime_exception(); } DEFINE_cl_heap_GV_I_bits(8,uintD) -static const cl_I bits8_element (const cl_GV_inner* vec, uintL index) +static const cl_I bits8_element (const cl_GV_inner* vec, std::size_t index) { #if CL_CPU_BIG_ENDIAN_P return (unsigned int)((((const cl_heap_GV_I_bits8 *) outcast(vec))->data[index/(intDsize/8)] >> (8*(index%(intDsize/8)))) & 0xFF); @@ -352,14 +351,14 @@ static const cl_I bits8_element (const cl_GV_inner* vec, uintL index) return (unsigned int)(((uint8*)(((const cl_heap_GV_I_bits8 *) outcast(vec))->data))[index]); #endif } -static void bits8_set_element (cl_GV_inner* vec, uintL index, const cl_I& x) +static void bits8_set_element (cl_GV_inner* vec, std::size_t index, const cl_I& x) { - var uintV xval; + uintV xval; if (fixnump(x)) { xval = FN_to_UV(x); if (xval <= 0xFF) { #if CL_CPU_BIG_ENDIAN_P - var uintD* ptr = &((cl_heap_GV_I_bits8 *) outcast(vec))->data[index/(intDsize/8)]; + uintD* ptr = &((cl_heap_GV_I_bits8 *) outcast(vec))->data[index/(intDsize/8)]; index = 8*(index%(intDsize/8)); *ptr = *ptr ^ ((*ptr ^ ((uintD)xval << index)) & ((uintD)0xFF << index)); #else @@ -369,13 +368,13 @@ static void bits8_set_element (cl_GV_inner* vec, uintL index, const cl_I& return; } } - cl_abort(); + throw runtime_exception(); } DEFINE_cl_heap_GV_I_bits(16,uintD) -static const cl_I bits16_element (const cl_GV_inner* vec, uintL index) +static const cl_I bits16_element (const cl_GV_inner* vec, std::size_t index) { #if CL_CPU_BIG_ENDIAN_P return (unsigned int)((((const cl_heap_GV_I_bits16 *) outcast(vec))->data[index/(intDsize/16)] >> (16*(index%(intDsize/16)))) & 0xFFFF); @@ -384,14 +383,14 @@ static const cl_I bits16_element (const cl_GV_inner* vec, uintL index) return (unsigned int)(((uint16*)(((const cl_heap_GV_I_bits16 *) outcast(vec))->data))[index]); #endif } -static void bits16_set_element (cl_GV_inner* vec, uintL index, const cl_I& x) +static void bits16_set_element (cl_GV_inner* vec, std::size_t index, const cl_I& x) { - var uintV xval; + uintV xval; if (fixnump(x)) { xval = FN_to_UV(x); if (xval <= 0xFFFF) { #if CL_CPU_BIG_ENDIAN_P - var uintD* ptr = &((cl_heap_GV_I_bits16 *) outcast(vec))->data[index/(intDsize/16)]; + uintD* ptr = &((cl_heap_GV_I_bits16 *) outcast(vec))->data[index/(intDsize/16)]; index = 16*(index%(intDsize/16)); *ptr = *ptr ^ ((*ptr ^ ((uintD)xval << index)) & ((uintD)0xFFFF << index)); #else @@ -401,13 +400,13 @@ static void bits16_set_element (cl_GV_inner* vec, uintL index, const cl_I& return; } } - cl_abort(); + throw runtime_exception(); } DEFINE_cl_heap_GV_I_bits(32,uintD) -static const cl_I bits32_element (const cl_GV_inner* vec, uintL index) +static const cl_I bits32_element (const cl_GV_inner* vec, std::size_t index) { #if (intDsize==32) return (unsigned long)(((const cl_heap_GV_I_bits32 *) outcast(vec))->data[index]); @@ -418,13 +417,13 @@ static const cl_I bits32_element (const cl_GV_inner* vec, uintL index) return (unsigned long)(((uint32*)(((const cl_heap_GV_I_bits32 *) outcast(vec))->data))[index]); #endif } -static void bits32_set_element (cl_GV_inner* vec, uintL index, const cl_I& x) +static void bits32_set_element (cl_GV_inner* vec, std::size_t index, const cl_I& x) { - var uint32 xval = cl_I_to_UL(x); + uint32 xval = cl_I_to_UL(x); #if (intDsize==32) ((cl_heap_GV_I_bits32 *) outcast(vec))->data[index] = xval; #elif CL_CPU_BIG_ENDIAN_P - var uintD* ptr = &((cl_heap_GV_I_bits32 *) outcast(vec))->data[index/(intDsize/32)]; + uintD* ptr = &((cl_heap_GV_I_bits32 *) outcast(vec))->data[index/(intDsize/32)]; index = 32*(index%(intDsize/32)); *ptr = *ptr ^ ((*ptr ^ ((uintD)xval << index)) & ((uintD)0xFFFFFFFF << index)); #else @@ -443,10 +442,10 @@ static cl_GV_I_vectorops* bits_vectorops[6] = { &bits32_vectorops }; -cl_heap_GV_I* cl_make_heap_GV_I (uintL len, sintL m) +cl_heap_GV_I* cl_make_heap_GV_I (std::size_t len, sintC m) { // Determine log2(bits). - var uintL log2_bits; + uintL log2_bits; switch (m) { case 0: case 1: log2_bits = 0; break; @@ -468,28 +467,42 @@ cl_heap_GV_I* cl_make_heap_GV_I (uintL len, sintL m) return cl_make_heap_GV_I(len); } // For room allocation purposes, be pessimistic: assume the uintD case (since intDsize>=32). - var uintL words = // ceiling(len*2^log2_bits,intDsize) - (((sintL)len-1)>>(log2_intDsize-log2_bits))+1; - var cl_heap_GV_I_bits32* hv = (cl_heap_GV_I_bits32*) malloc_hook(offsetofa(cl_heap_GV_I_bits32,data)+sizeof(uintD)*words); + std::size_t words = // ceiling(len*2^log2_bits,intDsize) + (((sintC)len-1)>>(log2_intDsize-log2_bits))+1; + cl_heap_GV_I_bits32* hv = (cl_heap_GV_I_bits32*) malloc_hook(offsetofa(cl_heap_GV_I_bits32,data)+sizeof(uintD)*words); hv->refcount = 1; - hv->type = &cl_class_gvector_integer; + hv->type = &cl_class_gvector_integer(); new (&hv->v) cl_GV_inner (len,&bits_vectorops[log2_bits]->ops); - var uintD* ptr = (uintD*)(hv->data); - for (var uintL i = 0; i < words; i++) + uintD* ptr = (uintD*)(hv->data); + for (std::size_t i = 0; i < words; i++) ptr[i] = 0; return (cl_heap_GV_I*) hv; } -sintL cl_heap_GV_I::maxbits () const +sintC cl_heap_GV_I::maxbits () const { return outcast(v.vectorops)->m; } // An empty vector. -const cl_GV_I cl_null_GV_I = cl_GV_I((uintL)0); +const cl_GV_I cl_null_GV_I = cl_null_GV_I; + +int cl_GV_I_init_helper::count = 0; + +cl_GV_I_init_helper::cl_GV_I_init_helper() +{ + if (count++ == 0) + new ((void *)&cl_null_GV_I) cl_GV_I((std::size_t)0); +} + +cl_GV_I_init_helper::~cl_GV_I_init_helper() +{ + if (--count == 0) { + // Nothing to clean up + } +}; } // namespace cln -CL_PROVIDE_END(cl_GV_I)