From: Thomas Walker Lynch Date: Sun, 9 Mar 2025 09:28:23 +0000 (+0000) Subject: continued work on Core.lib.c X-Git-Url: https://git.reasoningtechnology.com/style/rt_dark_doc.css?a=commitdiff_plain;h=04d77bfbdaf9fa8a7948e7eb4c5899d75da9b608;p=N continued work on Core.lib.c --- diff --git "a/developer/cc\360\237\226\211/Core.lib.c" "b/developer/cc\360\237\226\211/Core.lib.c" index 1ca9e9e..4e05a7f 100644 --- "a/developer/cc\360\237\226\211/Core.lib.c" +++ "b/developer/cc\360\237\226\211/Core.lib.c" @@ -10,6 +10,8 @@ In contrast, an allocation does not exist if it has zero bytes. + It is better to separate the tableau than have a union, and let the + optimizer figure out the memory life times, and what can be reused. */ @@ -37,70 +39,86 @@ typedef uint8_t (*ReadFn8)(Area * ,uint8_t *); typedef uint64_t (*ReadFn64)(Area * ,uint64_t *); - typedef struct { - Core·Area *read; - Core·Area *write; - ReadFn8 read_fn_8; - ReadFn64 read_fn_64; - uint8_t *read_pt; - uint8_t *write_pt; - Core·Step·Status status; - } Core·TableauFace; - - extern __thread Core·TableauFace tl; - typedef enum{ - Core·Step·perfect_fit = 0 - ,Core·Step·argument_guard // something wrong with the arguments to step - ,Core·Step·read_surplus - ,Core·Step·read_surplus_write_gap - ,Core·Step·write_available - ,Core·Step·write_gap - } Core·Step·Status; - - typedef Core·Step·Fn (*Core·Step·Fn)(); - - typdef struct Core·Area; + ,Core·Copy·Status·uninitialied = 0 + ,Core·Copy·Status·argument_guard = 1 + ,Core·Copy·Status·perfect_fit = 2 + ,Core·Copy·Status·read_surplus = 4 + ,Core·Copy·Status·read_surplus_write_gap = 8 + ,Core·Copy·Status·write_available = 16 + ,Core·Copy·Status·write_gap = 32 + }Core·Copy·Status; + + struct{ + struct{ + Core·Area *read; + Core·Area *write; + ReadFn8 read_fn_8; + ReadFn64 read_fn_64; + uint8_t *read_pt; + uint8_t *write_pt; + Core·Copy·Status status; + }copy; + } Core·TableauFace + Core·TableauFace tf; + + typedef Core·Copy·Fn (*Core·Copy·Fn)(); typedef struct{ + // void * pointer operations void *offset_8(void *p ,size_t Δ); void *offset_64(void *p ,size_t Δ); bool is_aligned_64(void *p); void *floor_64(void *p); void *ceiling_64(void *p); - void Core·Area·init_pe(Core·Area *area ,void *position ,extent_t extent); - void Core·Area·init_pp(Core·Area *area ,void *position_left ,void *position_right); - void Core·Area·set_position(Core·Area *area ,uint8_t *new_position); - void Core·Area·set_position_left(Core·Area *area ,uint8_t *new_position); - extent_t Core·Area·set_extent(Core·Area *area); - void Core·Area·set_position_right(Core·Area *area ,uint8_t *new_position_right); - - uint8_t *Core·Area·position(Core·Area *area); - uint8_t *Core·Area·position_left(Core·Area *area); - uint8_t *Core·Area·position_right(Core·Area *area); - extent_t Core·Area·extent(Core·Area *area); - uint8_t Core·Area·length_Kung(Core·Area *area); // saturates at 3 - - bool Core·Area·encloses_pt(uint8_t *pt ,Core·Area *area); - bool Core·Area·encloses_pt_strictly(uint8_t *pt ,Core·Area *area); - bool Core·Area·encloses_area(Core·Area *outer ,Core·Area *inner); - bool Core·Area·encloses_area_strictly(Core·Area *outer ,Core·Area *inner); - bool Core·Area·overlap(Core·Area *a ,Core·Area *b); - bool Core·Area·nonexistent(Core·Area *area); + // initialize an area + void Area·init_pe(Core·Area *area ,void *position ,extent_t extent); + void Area·init_pp(Core·Area *area ,void *position_left ,void *position_right); + void Area·set_position(Core·Area *area ,uint8_t *new_position); + void Area·set_position_left(Core·Area *area ,uint8_t *new_position); // synonym + extent_t Area·set_extent(Core·Area *area); + void Area·set_position_right(Core·Area *area ,uint8_t *new_position_right); + + // read area properties + bool Area·empty(Core·Area *area); + uint8_t *Area·position(Core·Area *area); + uint8_t *Area·position_left(Core·Area *area); // synonym + uint8_t *Area·position_right(Core·Area *area); + extent_t Area·extent(Core·Area *area); + uint8_t Area·length_Kung(Core·Area *area); // saturates at 3 + + // area relationships + bool Area·encloses_pt(uint8_t *pt ,Core·Area *area); + bool Area·encloses_pt_strictly(uint8_t *pt ,Core·Area *area); + bool Area·encloses_area(Core·Area *outer ,Core·Area *inner); + bool Area·encloses_area_strictly(Core·Area *outer ,Core·Area *inner); + bool Area·overlap(Core·Area *a ,Core·Area *b); void Area·largest_aligned_64(Core·Area *outer ,Core·Area *inner_64); uint8_t *Area·complement(Core·Area *area ,uint8_t *r); + // read from an area + uint8_t Area·read_8_zero(Core·Area *area ,void *r); + uint8_t Area·read_8_fwd(Core·Area *area ,void *r); + uint8_t Area·read_8_rev(Core·Area *area ,uint8_t *r); - Core·Step·Status Core·step(Core·Step·Fn fn ,Core·TableauFace *ap); - Core·Step·Fn Step·copy_8; - Core·Step·Fn Step·copy_16; - Core·Step·Fn Step·write_hex; - Core·Step·Fn Step·read_hex; + uint64_t Area·read_64_zero(Core·Area *area ,void *r); + uint64_t Area·read_64_fwd(Core·Area *area ,void *r); + uint64_t Area·read_64_rev(Core·Area *area_64 ,uint8_t *r); - } Core·M; + // hex conversion + uint16_t byte_to_hex(uint8_t byte); + uint8_t hex_to_byte(uint16_t hex); + // copy one area to another, possibly with a transformation + Copy·Status Core·copy(Core·Copy·Fn fn ,Core·TableauFace *ap); + Copy·Fn Copy·byte_by_byte; + Copy·Fn Copy·by_word_64; + Copy·Fn Copy·write_hex; + Copy·Fn Copy·read_hex; + + } Core·M; Core·M m; // initialized in the implementation; @@ -110,84 +128,105 @@ // Implementation #ifdef Core·IMPLEMENTATION - - // declarations available only to IMPLEMENTATION - - #ifdef Core·DEBUG - #include - #endif - - typedef struct{ - uint8_t *position; - extent_t extent; - } Core·Area - - typedef struct { - union { - struct { - ReadFn8 read_fn; - } copy_8; - struct { - ReadFn64 read_fn; - Area *area_64; - } copy_64; - struct { - uint8_t (*hex_to_byte)(uint16_t); - } read_hex; - struct { - uint16_t (*byte_to_hex)(uint8_t); - } write_hex; - }; - } Core·TableauLocal; + // declarations available to all of the IMPLEMENTATION go here + // + #ifdef Core·DEBUG + #include + #endif + + typedef struct{ + uint8_t *position; + extent_t extent; + } Core·Area + + // I removed the unions, as they are debugging hazards, and also might confuse the optimizer + typedef struct{ + struct{ + ReadFn8 read_fn; + } byte_by_byte; + struct{ + ReadFn64 read_fn; + Area area_64; + } copy_64; + struct{ + uint8_t (*hex_to_byte)(uint16_t); + } read_hex; + struct{ + uint16_t (*byte_to_hex)(uint8_t); + } write_hex; + } Core·TableauLocal; // this part goes into Copylib.a - // yes this is empty ,so there is no Copylib.a #ifndef LOCAL - __thread Core·TableauFace tf; - __thread Core·TableauLocal tl; #endif #ifdef LOCAL - extern __thread Core·TableauLocal tl; //---------------------------------------- // Position/Pointer/Address whatever you want to call it - // point to the next uint64_t in an array of uint64_t - // the increment can be negative Local void *Core·offset_8(void *p ,size_t Δ){ + #ifdef Core·Debug + if(!p){ + fprintf(stderr,"Core·offset_8:: given NULL `p'"); + return NULL; + } + #endif return (void *)((uint8_t *)p) + Δ; } + Local void *Core·offset_64(void *p ,size_t Δ){ + #ifdef Core·Debug + if(!p){ + fprintf(stderr,"Core·offset_64:: given NULL `p'"); + return NULL; + } + #endif return (void *)((uint64_t *)p) + Δ; } Local bool Core·is_aligned_64(void *p){ + #ifdef Core·Debug + if(!p){ + fprintf(stderr,"Core·is_aligned_64:: given NULL `p'"); + return false; + } + #endif return ((uintptr_t)p & 0x7) == 0; } // find the lowest address in an 8 byte aligned window // returns the byte pointer to the least address byte in the window Local void *Core·floor_64(void *p){ - return (uintptr_t)p & ~(uintptr_t)0x7; + #ifdef Core·Debug + if(!p){ + fprintf(stderr,"Core·floor_64:: given NULL `p'"); + return NULL; + } + #endif + return (void *)((uintptr_t)p & ~(uintptr_t)0x7); } // find the largest address in an 8 byte aligned window // returns the byte pointer to the greatest address byte in the window Local void *Core·ceiling_64(void *p){ - return (uintptr_t)p | 0x7; + #ifdef Core·Debug + if(!p){ + fprintf(stderr,"Core·ceiling_64:: given NULL `p'"); + return NULL; + } + #endif + return (void *)((uintptr_t)p | 0x7); } - //---------------------------------------- // Area + // initialize an area + Local void Core·Area·set_position(Core·Area *area ,void *new_position){ area->position = new_position; } - Local void Core·Area·set_position_left(Core·Area *area ,void *new_position){ - area->position = new_position; - } Local extent_t Core·Area·set_extent(Core·Area *area ,exent_t extent){ return area->extent = extent; } @@ -203,28 +242,39 @@ Core·Area·set_position_right(position_right); } - Local uint8_t *Core·Area·position(Core·Area *area){ + // read area properties + + Local bool Core·Area·empty(Core·Area *area){ #ifdef Core·Debug if(!area){ - fprintf(stderr,"Core·Area·position:: given NULL area); - return NULL; + fprintf(stderr,"Core·Area·empty:: given NULL area"); + return true; } #endif - return area->position; + return area->position == NULL; } - Local uint8_t *Core·Area·position_left(Core·Area *area){ + + // Requesting a NULL position is a logical error, because a NULL position + // means the Area is empty and has no position. Instead, use the `empty` + // predicate. + Local uint8_t *Core·Area·position(Core·Area *area){ #ifdef Core·Debug if(!area){ - fprintf(stderr,"Core·Area·position_left:: given NULL area); + fprintf(stderr,"Core·Area·position:: given NULL area"); return NULL; } + if(!area->position){ + fprintf(stderr,"Core·Area·position:: request for position when it is NULL"); + } #endif return area->position; } + + Local uint8_t *Core·Area·position_right(Core·Area *area){ #ifdef Core·Debug if(!area){ - fprintf(stderr,"Core·Area·position_right:: given NULL area); + fprintf(stderr,"Core·Area·position_right:: given NULL area"); return NULL; } #endif @@ -233,7 +283,7 @@ Local extent_t Core·Area·extent(Core·Area *area){ #ifdef Core·Debug if(!area){ - fprintf(stderr,"Core·Area·extent:: given NULL area); + fprintf(stderr,"Core·Area·extent:: given NULL area"); return 0; } #endif @@ -273,14 +323,10 @@ // 4. interval 0 wholly contains interval 1 Local bool Core·Area·overlap(Core·Area *area0 ,Core·Area *area1){ return - Core·Area·position_right(area0) >= (Core·Area·position_left(area1) + Core·Area·position_right(area0) >= Core·Area·position_left(area1) && Core·Area·position_left(area0) <= Core·Area·position_right(area1); } - Local bool Core·Area·nonexistent(Core·Area *area){ - return !!Core·Area·position_left(area); - } - // find the largest contained interval aligned on 64 bit boundaries static void Core·Area·largest_aligned_64(Core·Area *outer ,Core·Area *inner_64){ uintptr_t p0 = (uintptr_t)Core·Area·position_left(outer); @@ -304,7 +350,15 @@ } //---------------------------------------- - // step-able functions + // read functions + + // consider instead using `copy_zero` + Local uint8_t Core·Area·read_8_zero(Core·Area *area ,void *r){ + return 0; + } + Local uint64_t Core·Area·read_64_zero(Core·Area *area ,void *r){ + return 0; + } Local uint8_t Core·Area·read_8_fwd(Core·Area *area ,void *r){ #ifdef Core·Debug @@ -334,49 +388,32 @@ } Local uint8_t Core·Area·read_8_rev(Core·Area *area ,uint8_t *r){ - #ifdef Core·Debug - if(!area || !r){ - fprintf(stderr,"Core·Area·read_8_fwd:: area r: %p %p\n" ,area ,r); - return 0; - } - if(!Core·Area·enclose_pt(area ,r) ){ - fprintf(stderr,"Core·Area·read_8_rev:: out of interval read\n"); - } - #endif return *(Core·complement(area ,r)); } Local uint64_t Core·Area·read_64_rev(Core·Area *area_64 ,uint8_t *r){ - #ifdef Core·Debug - if(!area || !r){ - fprintf(stderr,"Core·Area·read_8_fwd:: area r: %p %p\n" ,area ,r); - return 0; - } - if(!Core·Area·enclose_pt(area ,r) ){ - fprintf(stderr,"Core·Area·read_64_rev:: out of interval read\n"); - } - #endif return __builtin_bswap64( *(uint64_t *)Core·floor_64(Core·complement(area_64 ,r)) ); } //---------------------------------------- - // Step + // Copy - // Step function using trampoline execution model - Local Core·Step·Status Core·step(Core·Step·Fn fn){ + // Copy function using trampoline execution model + Local Core·Copy·Status Core·copy(Core·Copy·Fn fn){ #ifdef Core·Debug - if(!fn || !ap){ - fprintf(stderr,"Core·step:: fn ap: %p %p\n" ,fn ,ap); - return Core·Step·argument_guard + if(!fn){ + fprintf(stderr,"Core·copy:: given null function"); + return Core·Copy·argument_guard } if( - fn != Core·copy_8_fwd - && fn != Core·copy_8_rev - && fn != Core·copy_64_fwd - && fn != Core·copy_64_rev + true + && fn != Core·Copy·by_word_64 + && fn != Core·Copy·byte_by_byte + && fn != Core·write_hex + && fn != Core·read_hex ){ - fprintf(stderr,"Core·step:: unrecognized step-able function\n"); - return Core·Step·argument_guard + fprintf(stderr,"Core·copy:: unrecognized copy function\n"); + return Core·Copy·argument_guard ) #endif @@ -385,378 +422,461 @@ } //---------------------------------------- - // step copy_8 + // copy byte_by_byte - Core·Step·Fn Core·Step·copy_8;; - Core·Step·Fn Core·Step·Copy8·bulk; + Core·Copy·Fn Core·Copy·byte_by_byte; + Core·Copy·Fn Core·Copy·Copy8·bulk; - Local Core·Step·Fn Core·copy_8(){ - - // Assign the correct read function based on byte order - if(Core·tableau.ap->reverse_byte_order) - Core·tableau.copy_8.read_fn = Core·read_8_rev; - else - Core·tableau.copy_8.read_fn = Core·read_8_fwd; - - // Determine the appropriate case and dispatch - if(Core·tableau.ap->read_extent == Core·tableau.ap->write_extent) + Local Core·Copy·Fn Core·Copy·byte_by_byte(){ + if(Core·Area·extent(Core·tf.copy.read) == Core·Area·extent(Core·tf.copy.write)) return Core·Copy8·perfect_fit; - if(Core·tableau.ap->read_extent > Core·tableau.ap->write_extent) + if(Core·Area·extent(Core·tf.copy.read) > Core·Area·extent(Core·tf.copy.write)) return Core·Copy8·read_surplus; return Core·Copy8·write_available; } - Local Core·Step·Fn Core·Copy8·perfect_fit(){ - uint8_t *r = (uint8_t *) Core·tableau.ap->read0; - uint8_t *r1 = (uint8_t *) (r + Core·tableau.ap->read_extent); - uint8_t *w = (uint8_t *) Core·tableau.ap->write0; + Core·Copy·Fn Core·Copy·byte_by_byte; + Core·Copy·Fn Core·Copy·Copy8·bulk; + + Local Core·Copy·Fn Core·Copy·byte_by_byte(){ + if(Core·Area·extent(Core·tf.copy.read) == Core·Area·extent(Core·tf.copy.write)){ + return Core·Copy8·perfect_fit; + } + if(Core·Area·extent(Core·tf.copy.read) > Core·Area·extent(Core·tf.copy.write)){ + return Core·Copy8·read_surplus; + } + return Core·Copy8·write_available; + } + + Local Core·Copy·Fn Core·Copy8·perfect_fit(){ + uint8_t **r = &Core·tf.copy.read_pt; + uint8_t *r1 = Core·Area·position_right(Core·tf.copy.read); + uint8_t **w = &Core·tf.copy.write_pt; do{ - *w = Core·tableau.copy_8.read_fn(Core·tableau.ap->read0 ,r1 ,r); - if(r == r1) break; - r++; - w++; + **w = Core·tf.copy.read_fn_8(Core·tf.copy.read ,r1 ,*r); + if(*r == r1) break; + (*r)++; + (*w)++; }while(true); - Core·tableau.ap->read0 = NULL; // Buffer exhausted - Core·tableau.ap->write0 = NULL; // Buffer exhausted - Core·tableau.status = Core·Step·perfect_fit; + Core·tf.copy.status = Core·Copy·Status·perfect_fit; return NULL; } - Local Core·Step·Fn Core·Copy8·read_surplus(){ - uint8_t *r = (uint8_t *) Core·tableau.ap->read0; - uint8_t *r1 = (uint8_t *) (r + Core·tableau.ap->write_extent); - uint8_t *w = (uint8_t *) Core·tableau.ap->write0; + Local Core·Copy·Fn Core·Copy8·read_surplus(){ + uint8_t **r = &Core·tf.copy.read_pt; + uint8_t *r1 = Core·Area·position_right(Core·tf.copy.read); + uint8_t **w = &Core·tf.copy.write_pt; do{ - *w = Core·tableau.copy_8.read_fn(Core·tableau.ap->read0 ,r1 ,r); - if(r == r1) break; - r++; - w++; + **w = Core·tf.copy.read_fn_8(Core·tf.copy.read ,r1 ,*r); + if(*r == r1) break; + (*r)++; + (*w)++; }while(true); - Core·tableau.ap->read0 = r; // Advance read pointer - Core·tableau.ap->read_extent -= Core·tableau.ap->write_extent; - Core·tableau.ap->write0 = NULL; // Write buffer exhausted - Core·tableau.status = Core·Step·read_surplus; + Core·tf.copy.status = Core·Copy·Status·read_surplus; return NULL; } - Local Core·Step·Fn Core·Copy8·write_available(){ - uint8_t *r = (uint8_t *) Core·tableau.ap->read0; - uint8_t *r1 = (uint8_t *) (r + Core·tableau.ap->read_extent); - uint8_t *w = (uint8_t *) Core·tableau.ap->write0; - uint8_t *w1 = (uint8_t *) (w + Core·tableau.ap->write_extent); + Local Core·Copy·Fn Core·Copy8·write_available(){ + uint8_t **r = &Core·tf.copy.read_pt; + uint8_t *r1 = Core·Area·position_right(Core·tf.copy.read); + uint8_t **w = &Core·tf.copy.write_pt; + uint8_t *w1 = Core·Area·position_right(Core·tf.copy.write); do{ - *w = Core·tableau.copy_8.read_fn(Core·tableau.ap->read0 ,r1 ,r); - if(w == w1) break; - r++; - w++; + **w = Core·tf.copy.read_fn_8(Core·tf.copy.read ,r1 ,*r); + if(*w == w1) break; + (*r)++; + (*w)++; }while(true); - Core·tableau.ap->write0 = w; // Advance write pointer - Core·tableau.ap->write_extent -= Core·tableau.ap->read_extent; - Core·tableau.ap->read0 = NULL; // Read buffer exhausted - Core·tableau.status = Core·Step·write_available; + Core·tf.copy.status = Core·Copy·Status·write_available; return NULL; } + //---------------------------------------- + // copy copy_64 + + //---------------------------------------- + // copy copy_64 + + // 64-bit copy function with updated TableauFace terminology + Core·Copy·Fn Core·Copy·by_word_64; + Core·Copy·Fn Core·Copy64·leadin; + Core·Copy·Fn Core·Copy64·bulk; + Core·Copy·Fn Core·Copy64·tail; //---------------------------------------- - // step copy_64 + // copy copy_64 - // 64-bit copy function with updated TableauFace terminology - Core·Step·Fn Core·copy_64; - Core·Step·Fn Core·Copy64·leadin; - Core·Step·Fn Core·Copy64·bulk; - Core·Step·Fn Core·Copy64·tail; + // 64-bit copy function with updated TableauFace terminology + Core·Copy·Fn Core·Copy·by_word_64; + Core·Copy·Fn Core·Copy64·leadin; + Core·Copy·Fn Core·Copy64·bulk; + Core·Copy·Fn Core·Copy64·tail; // Initialize the copy_64 process - Local Core·Step·Fn Core·copy_64(){ - - // Assign the correct read function based on byte order - if(Core·tableau.ap->reverse_byte_order) - Core·tableau.copy_64.read_fn = Core·read_64_rev; - else - Core·tableau.copy_64.read_fn = Core·read_64_fwd; - - // Determine aligned 64-bit word boundaries - Core·tableau.copy_64.r0_64 = Core·least_full_64( - Core·tableau.ap->read0 ,Core·tableau.ap->read0 + Core·tableau.ap->read_extent - ); - Core·tableau.copy_64.r1_64 = Core·greatest_full_64( - Core·tableau.ap->read0 ,Core·tableau.ap->read0 + Core·tableau.ap->read_extent - ); + Local Core·Copy·Fn Core·Copy·by_word_64(){ + // Determine the largest 64-bit aligned region within the read area + Core·Area·largest_aligned_64(Core·tf.copy.read ,&Core·tl.copy_64.area_64); // Choose the correct function based on alignment - if(Core·tableau.copy_64.r0_64 == NULL) return Core·Copy64·tail; - if(Core·is_aligned_64(Core·tableau.ap->read0)) return Core·Copy64·bulk; + if(Core·Area·empty(&Core·tl.copy_64.area_64)) return Core·Copy64·tail; + if(Core·is_aligned_64(Core·Area·position(Core·tf.copy.read))) return Core·Copy64·bulk; return Core·Copy64·leadin; } // Lead-in byte copy (until alignment) - Local Core·Step·Fn Core·Copy64·leadin(){ - uint8_t *r = (uint8_t *) Core·tableau.ap->read0; - uint8_t *w = (uint8_t *) Core·tableau.ap->write0; - uint8_t *r0_64 = (uint8_t *) Core·tableau.copy_64.r0_64; + Local Core·Copy·Fn Core·Copy64·leadin(){ + uint8_t **r = &Core·tf.copy.read_pt; + uint8_t *r0_64 = Core·Area·position(&Core·tl.copy_64.area_64); + uint8_t **w = &Core·tf.copy.write_pt; do{ - *w++ = Core·tableau.copy_8.read_fn(Core·tableau.ap->read0 ,r0_64 ,r); - if(r == r0_64) break; - r++; - }while(1); - - Core·tableau.ap->read0 = r; - Core·tableau.ap->write0 = w; + **w = Core·tf.copy.read_fn_8(Core·tf.copy.read ,r0_64 ,*r); + if(*r == r0_64) break; + (*r)++; + (*w)++; + }while(true); return Core·Copy64·bulk; } // Bulk word copy - Local Core·Step·Fn Core·Copy64·bulk(){ - uint64_t *r64 = (uint64_t *) Core·tableau.ap->read0; - uint64_t *w64 = (uint64_t *) Core·tableau.ap->write0; - uint64_t *r1_64 = Core·tableau.copy_64.r1_64; + Local Core·Copy·Fn Core·Copy64·bulk(){ + uint64_t **r = (uint64_t **)&Core·tf.copy.read_pt; + uint64_t **w = (uint64_t **)&Core·tf.copy.write_pt; + uint64_t *r1_64 = Core·Area·position_right(&Core·tl.copy_64.area_64); do{ - *w64++ = Core·tableau.copy_64.read_fn( - Core·tableau.copy_64.r0_64 ,Core·tableau.copy_64.r1_64 ,r64 - ); - if(r64 == r1_64) break; - r64++; - }while(1); - - Core·tableau.ap->read0 = r64; - Core·tableau.ap->write0 = w64; + **w = Core·tf.copy.read_fn_64(Core·tf.copy.read ,r1_64 ,*r); + if(*r == r1_64) break; + (*r)++; + (*w)++; + }while(true); return Core·Copy64·tail; } // Tail byte copy (unaligned trailing bytes) - Local Core·Step·Fn Core·Copy64·tail(){ - uint8_t *r = (uint8_t *) Core·tableau.ap->read0; - uint8_t *w = (uint8_t *) Core·tableau.ap->write0; - uint8_t *r1 = (uint8_t *) Core·tableau.copy_64.r1_64; + Local Core·Copy·Fn Core·Copy64·tail(){ + uint8_t **r = &Core·tf.copy.read_pt; + uint8_t *r1 = Core·Area·position_right(&Core·tl.copy_64.area_64); + uint8_t **w = &Core·tf.copy.write_pt; do{ - *w++ = Core·tableau.copy_8.read_fn(Core·tableau.ap->read0 ,r1 ,r); - if(r == r1) break; - r++; - }while(1); - - Core·tableau.ap->read0 = r; - Core·tableau.ap->write0 = w; + **w = Core·tf.copy.read_fn_8(Core·tf.copy.read ,r1 ,*r); + if(*r == r1) break; + (*r)++; + (*w)++; + }while(true); - Core·tableau.status = Core·Step·perfect_fit; + Core·tf.copy.status = Core·Copy·Status·perfect_fit; return NULL; } //---------------------------------------- - // step write hex + // copy write hex + + Local uint16_t Core·byte_to_hex(uint8_t byte){ + static const char hex_digits[] = "0123456789ABCDEF"; + return + (hex_digits[byte >> 4] << 8) + | hex_digits[byte & 0x0F]; + } // Forward Declarations - Core·Step·Fn Core·Step·write_hex; - Core·Step·Fn Core·Step·write_hex_bulk; - Core·Step·Fn Core·Step·write_hex_read_surplus; - Core·Step·Fn Core·Step·write_hex_write_available; - - Core·Step·Fn Core·Step·read_hex; - Core·Step·Fn Core·Step·read_hex_bulk; - Core·Step·Fn Core·Step·read_hex_read_surplus; - Core·Step·Fn Core·Step·read_hex_write_available; - - // Hex Encoding: Initialize Step - Local Core·Step·Fn Core·Step·write_hex(){ - if(Core·tableau.area_pairing->read_extent == (Core·tableau.area_pairing->write_extent >> 1)){ - return Core·Step·write_hex_bulk; + Core·Copy·Fn Core·Copy·write_hex; + Core·Copy·Fn Core·Copy·write_hex_bulk; + Core·Copy·Fn Core·Copy·write_hex_read_surplus; + Core·Copy·Fn Core·Copy·write_hex_write_available; + + // Hex Encoding: Initialize Copy + Local Core·Copy·Fn Core·Copy·write_hex(){ + if(Core·Area·extent(Core·tf.copy.read) == (Core·Area·extent(Core·tf.copy.write) >> 1)){ + return Core·Copy·write_hex_bulk; } - if(Core·tableau.area_pairing->read_extent > (Core·tableau.area_pairing->write_extent >> 1)){ - return Core·Step·write_hex_read_surplus; + if(Core·Area·extent(Core·tf.copy.read) > (Core·Area·extent(Core·tf.copy.write) >> 1)){ + return Core·Copy·write_hex_read_surplus; } - return Core·Step·write_hex_write_available; + return Core·Copy·write_hex_write_available; } // Hex Encoding: Bulk Processing (Perfect Fit) - Local Core·Step·Fn Core·Step·write_hex_bulk(){ - uint8_t *r = (uint8_t *)Core·tableau.area_pairing->read0; - uint8_t *r1 = r + Core·tableau.area_pairing->read_extent; - uint8_t *w = (uint8_t *)Core·tableau.area_pairing->write0; + Local Core·Copy·Fn Core·Copy·write_hex_bulk(){ + uint8_t **r = &Core·tf.copy.read_pt; + uint8_t *r1 = Core·Area·position_right(Core·tf.copy.read); + uint8_t **w = &Core·tf.copy.write_pt; do { - *(uint16_t *)w = Core·tableau.write_hex.byte_to_hex(*r); - if(r == r1) break; - r++; - w += 2; - } while(1); - - Core·tableau.area_pairing->read0 = NULL; - Core·tableau.area_pairing->write0 = NULL; - Core·tableau.area_pairing->read_extent = 0; - Core·tableau.area_pairing->write_extent = 0; - Core·tableau.status = Core·Step·perfect_fit; + *(uint16_t *)*w = Core·tf.write_hex.byte_to_hex(**r); + if(*r == r1) break; + (*r)++; + (*w) += 2; + } while(true); + + Core·tf.copy.status = Core·Copy·Status·perfect_fit; return NULL; } // Hex Encoding: Read Surplus - Local Core·Step·Fn Core·Step·write_hex_read_surplus(){ - uint8_t *r = (uint8_t *)Core·tableau.area_pairing->read0; - uint8_t *w = (uint8_t *)Core·tableau.area_pairing->write0; - size_t limit = Core·tableau.area_pairing->write_extent >> 1; - uint8_t *r1 = r + limit; + Local Core·Copy·Fn Core·Copy·write_hex_read_surplus(){ + uint8_t **r = &Core·tf.copy.read_pt; + uint8_t *r1 = Core·Area·position_right(Core·tf.copy.write); + uint8_t **w = &Core·tf.copy.write_pt; do { - *(uint16_t *)w = Core·tableau.write_hex.byte_to_hex(*r); - if(r == r1) break; - r++; - w += 2; - } while(1); - - Core·tableau.area_pairing->read0 = r + 1; - Core·tableau.area_pairing->read_extent -= limit; - Core·tableau.area_pairing->write0 = NULL; - Core·tableau.area_pairing->write_extent = 0; - Core·tableau.status = Core·Step·read_surplus; + *(uint16_t *)*w = Core·tf.write_hex.byte_to_hex(**r); + if(*r == r1) break; + (*r)++; + (*w) += 2; + } while(true); + + Core·tf.copy.status = Core·Copy·Status·read_surplus; return NULL; } // Hex Encoding: Write Available - Local Core·Step·Fn Core·Step·write_hex_write_available(){ - uint8_t *r = (uint8_t *)Core·tableau.area_pairing->read0; - uint8_t *w = (uint8_t *)Core·tableau.area_pairing->write0; - size_t limit = Core·tableau.area_pairing->read_extent; - uint8_t *r1 = r + limit; + Local Core·Copy·Fn Core·Copy·write_hex_write_available(){ + uint8_t **r = &Core·tf.copy.read_pt; + uint8_t *r1 = Core·Area·position_right(Core·tf.copy.read); + uint8_t **w = &Core·tf.copy.write_pt; + uint8_t *w1 = Core·Area·position_right(Core·tf.copy.write); + + do { + *(uint16_t *)*w = Core·tf.write_hex.byte_to_hex(**r); + if(*w == w1) break; + (*r)++; + (*w) += 2; + } while(true); + + Core·tf.copy.status = Core·Copy·Status·write_available; + return NULL; + } + + //---------------------------------------- + // copy read hex + + Local uint8_t Core·hex_to_byte(uint16_t hex){ + uint8_t high = hex >> 8; + uint8_t low = hex & 0xFF; + + high = + (high >= '0' && high <= '9') ? (high - '0') + : (high >= 'A' && high <= 'F') ? (high - 'A' + 10) + : (high >= 'a' && high <= 'f') ? (high - 'a' + 10) + : 0; + + low = + (low >= '0' && low <= '9') ? (low - '0') + : (low >= 'A' && low <= 'F') ? (low - 'A' + 10) + : (low >= 'a' && low <= 'f') ? (low - 'a' + 10) + : 0; + + return (high << 4) | low; + } + + Core·Copy·Fn Core·Copy·read_hex; + Core·Copy·Fn Core·Copy·read_hex_bulk; + Core·Copy·Fn Core·Copy·read_hex_read_surplus; + Core·Copy·Fn Core·Copy·read_hex_write_available; + + Local Core·Copy·Fn Core·Copy·read_hex(){ + if((Core·Area·extent(Core·tf.copy.read) >> 1) == Core·Area·extent(Core·tf.copy.write)){ + return Core·Copy·read_hex_bulk; + } + if((Core·Area·extent(Core·tf.copy.read) >> 1) > Core·Area·extent(Core·tf.copy.write)){ + return Core·Copy·read_hex_read_surplus; + } + return Core·Copy·read_hex_write_available; + } + + // Hex Decoding: Bulk Processing (Perfect Fit) + Local Core·Copy·Fn Core·Copy·read_hex_bulk(){ + uint8_t **r = &Core·tf.copy.read_pt; + uint8_t *r1 = Core·Area·position_right(Core·tf.copy.read); + uint8_t **w = &Core·tf.copy.write_pt; + + do { + **w = Core·tf.read_hex.hex_to_byte(*(uint16_t *)*r); + if(*r == r1) break; + (*r) += 2; + (*w)++; + } while(true); + + Core·tf.copy.status = Core·Copy·Status·perfect_fit; + return NULL; + } + + // Hex Decoding: Read Surplus + Local Core·Copy·Fn Core·Copy·read_hex_read_surplus(){ + uint8_t **r = &Core·tf.copy.read_pt; + uint8_t *r1 = Core·Area·position_right(Core·tf.copy.write); + uint8_t **w = &Core·tf.copy.write_pt; do { - *(uint16_t *)w = Core·tableau.write_hex.byte_to_hex(*r); - if(r == r1) break; - r++; - w += 2; - } while(1); - - Core·tableau.area_pairing->read0 = NULL; - Core·tableau.area_pairing->read_extent = 0; - Core·tableau.area_pairing->write0 = w + 2; - Core·tableau.area_pairing->write_extent -= limit << 1; - Core·tableau.status = Core·Step·write_available; + **w = Core·tf.read_hex.hex_to_byte(*(uint16_t *)*r); + if(*r == r1) break; + (*r) += 2; + (*w)++; + } while(true); + + Core·tf.copy.status = Core·Copy·Status·read_surplus; + return NULL; + } + + // Hex Decoding: Write Available + Local Core·Copy·Fn Core·Copy·read_hex_write_available(){ + uint8_t **r = &Core·tf.copy.read_pt; + uint8_t *r1 = Core·Area·position_right(Core·tf.copy.read); + uint8_t **w = &Core·tf.copy.write_pt; + uint8_t *w1 = Core·Area·position_right(Core·tf.copy.write); + + do { + **w = Core·tf.read_hex.hex_to_byte(*(uint16_t *)*r); + if(*w == w1) break; + (*r) += 2; + (*w)++; + } while(true); + + Core·tf.copy.status = Core·Copy·Status·write_available; return NULL; } //---------------------------------------- - // step read hex + // Tableaux - Local Core·Step·Fn Core·Step·read_hex(){ - if((Core·tableau.area_pairing->read_extent >> 1) == Core·tableau.area_pairing->write_extent){ - return Core·Step·read_hex_bulk; + Core·TableauFace tf = { + .Copy.read = NULL + ,Copy.write = NULL + ,Copy..read_fn_8 = read_8_fwd + ,Copy..read_fn_64 = read_64_fwd + ,Copy..read_pt = NULL + ,Copy..write_pt = NULL + ,Copy..status = Core·Copy·Status·uninitialied + } + + //---------------------------------------- + // copy read hex + + Core·Copy·Fn Core·Copy·read_hex; + Core·Copy·Fn Core·Copy·read_hex_bulk; + Core·Copy·Fn Core·Copy·read_hex_read_surplus; + Core·Copy·Fn Core·Copy·read_hex_write_available; + + Local Core·Copy·Fn Core·Copy·read_hex(){ + if((Core·Area·extent(Core·tf.copy.read) >> 1) == Core·Area·extent(Core·tf.copy.write)){ + return Core·Copy·read_hex_bulk; } - if((Core·tableau.area_pairing->read_extent >> 1) > Core·tableau.area_pairing->write_extent){ - return Core·Step·read_hex_read_surplus; + if((Core·Area·extent(Core·tf.copy.read) >> 1) > Core·Area·extent(Core·tf.copy.write)){ + return Core·Copy·read_hex_read_surplus; } - return Core·Step·read_hex_write_available; + return Core·Copy·read_hex_write_available; } // Hex Decoding: Bulk Processing (Perfect Fit) - Local Core·Step·Fn Core·Step·read_hex_bulk(){ - uint8_t *r = (uint8_t *)Core·tableau.area_pairing->read0; - uint8_t *r1 = r + Core·tableau.area_pairing->read_extent; - uint8_t *w = (uint8_t *)Core·tableau.area_pairing->write0; + Local Core·Copy·Fn Core·Copy·read_hex_bulk(){ + uint8_t **r = &Core·tf.copy.read_pt; + uint8_t *r1 = Core·Area·position_right(Core·tf.copy.read); + uint8_t **w = &Core·tf.copy.write_pt; do { - *w = Core·tableau.read_hex.hex_to_byte(*(uint16_t *)r); - if(r == r1) break; - r += 2; - w++; - } while(1); - - Core·tableau.area_pairing->read0 = NULL; - Core·tableau.area_pairing->write0 = NULL; - Core·tableau.area_pairing->read_extent = 0; - Core·tableau.area_pairing->write_extent = 0; - Core·tableau.status = Core·Step·perfect_fit; + **w = Core·tf.read_hex.hex_to_byte(*(uint16_t *)*r); + if(*r == r1) break; + (*r) += 2; + (*w)++; + } while(true); + + Core·tf.copy.status = Core·Copy·Status·perfect_fit; return NULL; } // Hex Decoding: Read Surplus - Local Core·Step·Fn Core·Step·read_hex_read_surplus(){ - uint8_t *r = (uint8_t *)Core·tableau.area_pairing->read0; - uint8_t *w = (uint8_t *)Core·tableau.area_pairing->write0; - size_t limit = Core·tableau.area_pairing->write_extent; - uint8_t *r1 = r + (limit << 1); + Local Core·Copy·Fn Core·Copy·read_hex_read_surplus(){ + uint8_t **r = &Core·tf.copy.read_pt; + uint8_t *r1 = Core·Area·position_right(Core·tf.copy.write); + uint8_t **w = &Core·tf.copy.write_pt; do { - *w = Core·tableau.read_hex.hex_to_byte(*(uint16_t *)r); - if(r == r1) break; - r += 2; - w++; - } while(1); - - Core·tableau.area_pairing->read0 = r + 2; - Core·tableau.area_pairing->read_extent -= limit << 1; - Core·tableau.area_pairing->write0 = NULL; - Core·tableau.area_pairing->write_extent = 0; - Core·tableau.status = Core·Step·read_surplus; + **w = Core·tf.read_hex.hex_to_byte(*(uint16_t *)*r); + if(*r == r1) break; + (*r) += 2; + (*w)++; + } while(true); + + Core·tf.copy.status = Core·Copy·Status·read_surplus; return NULL; } // Hex Decoding: Write Available - Local Core·Step·Fn Core·Step·read_hex_write_available(){ - uint8_t *r = (uint8_t *)Core·tableau.area_pairing->read0; - uint8_t *w = (uint8_t *)Core·tableau.area_pairing->write0; - size_t limit = Core·tableau.area_pairing->read_extent >> 1; - uint8_t *r1 = r + (limit << 1); + Local Core·Copy·Fn Core·Copy·read_hex_write_available(){ + uint8_t **r = &Core·tf.copy.read_pt; + uint8_t *r1 = Core·Area·position_right(Core·tf.copy.read); + uint8_t **w = &Core·tf.copy.write_pt; + uint8_t *w1 = Core·Area·position_right(Core·tf.copy.write); do { - *w = Core·tableau.read_hex.hex_to_byte(*(uint16_t *)r); - if(r == r1) break; - r += 2; - w++; - } while(1); - - Core·tableau.area_pairing->read0 = NULL; - Core·tableau.area_pairing->read_extent = 0; - Core·tableau.area_pairing->write0 = w + 1; - Core·tableau.area_pairing->write_extent -= limit; - Core·tableau.status = Core·Step·write_available; + **w = Core·tf.read_hex.hex_to_byte(*(uint16_t *)*r); + if(*w == w1) break; + (*r) += 2; + (*w)++; + } while(true); + + Core·tf.copy.status = Core·Copy·Status·write_available; return NULL; } + //---------------------------------------- + // Initialization Block + Core·M m = { .Area·init_pe = Core·Area·init_pe ,.Area·init_pp = Core·Area·init_pp + ,.Area·set_position = Core·Area·set_position + ,.Area·set_position_left = Core·Area·set_position + ,.Area·set_position_right = Core·Area·set_position_right + ,.Area·set_extent = Core·Area·set_extent ,.Area·position = Core·Area·position - ,.Area·position_left = Core·Area·position_left + ,.Area·position_left = Core·Area·position ,.Area·position_right = Core·Area·position_right - ,.Area·set_position = Core·Area·set_position ,.Area·extent = Core·Area·extent - ,.Area·set_extent = Core·Area·set_extent - ,.Area·set_position = Core·Area·set_exent ,.Area·length_Kung = Core·Area·length_Kung + ,.Area·empty = Core·Area·empty - .Area·encloses_pt = Core·Area·encloses_pt + ,.Area·encloses_pt = Core·Area·encloses_pt ,.Area·encloses_pt_strictly = Core·Area·encloses_pt_strictly ,.Area·encloses_area = Core·Area·encloses_area ,.Area·encloses_area_strictly = Core·Area·encloses_area_strictly ,.Area·overlap = Core·Area·overlap - ,.Core·Area·nonexistent = Core·Area·nonexistent - ,.Core·Area·length_Kung = Core·Area·length_Kung + ,.Area·largest_aligned_64 = Core·Area·largest_aligned_64 + ,.Area·complement = Core·Area·complement + + ,.Area·read_8_zero = Core·Area·read_8_zero + ,.Area·read_8_fwd = Core·Area·read_8_fwd + ,.Area·read_8_rev = Core·Area·read_8_rev + ,.Area·read_64_zero = Core·Area·read_64_zero + ,.Area·read_64_fwd = Core·Area·read_64_fwd + ,.Area·read_64_rev = Core·Area·read_64_rev ,.is_aligned_64 = Core·is_aligned_64 ,.floor_64 = Core·floor_64 ,.ceiling_64 = Core·ceiling_64 - ,.greatest_full_64 = Core·greatest_full_64 - ,.least_full_64 = Core·least_full_64 ,.offset_8 = Core·offset_8 ,.offset_64 = Core·offset_64 - ,.Core·step = Core·step + ,.byte_to_hex = Core·byte_to_hex + ,.hex_to_byte = Core·hex_to_byte - ,.copy_8 = Core·copy_8 - ,.copy_16 = Core·copy_16 - ,.Step·write_hex = Core·Step·write_hex - ,.Step·read_hex = Core·Step·read_hex + ,.Core·copy = Core·copy + ,.Copy·byte_by_byte = Core·Copy·byte_by_byte + ,.Copy·by_word_64 = Core·Copy·by_word_64 + ,.Copy·write_hex = Core·Copy·write_hex + ,.Copy·read_hex = Core·Copy·read_hex }; #endif // LOCAL diff --git "a/document\360\237\226\211/RT_C_namespace.org" "b/document\360\237\226\211/RT_C_namespace.org" index 5c04139..39aa868 100644 --- "a/document\360\237\226\211/RT_C_namespace.org" +++ "b/document\360\237\226\211/RT_C_namespace.org" @@ -3,17 +3,17 @@ * 1. Namespaces in RT C -C does not provide a built-in mechanism for namespaces, so RT C adopts a *prefix plus scoping operator* convention. The part before the “=·=” (for example, “N32”) is treated as the namespace name, and the “=·=” symbol is a stand-in for an (as yet) unofficial scoping operator. +C does not provide a built-in mechanism for namespaces, so RT C adopts a *prefix plus scoping operator* convention. The part before the “·” (for example, “N32”) is treated as the namespace name, and the “·” symbol is a stand-in for an (as yet) unofficial scoping operator. -For instance, in “=N32·allocate_array=”: -- “=N32=” is the namespace prefix. -- “=·=” (the middle dot) is our ad hoc scoping operator. +For instance, in “N32·allocate_array=”: +- “N32” is the namespace prefix. +- “·” (the middle dot) is our ad hoc scoping operator. - “=allocate_array=” is the actual function name. ** Advantages of the “Project·Name” Convention 1. **Collision Avoidance**: Prefixing everything with “N32·” (or another module name) ensures identifiers do not collide with those from other libraries or system headers. 2. **Clarity**: Readers can see at a glance which library an identifier belongs to. -3. **Easy to Evolve**: If a future version of C introduces formal namespaces, the “=N32=” prefix and “=·=” operator could map directly into that syntax. +3. **Easy to Evolve**: If a future version of C introduces formal namespaces, the “=N32=” prefix and “·” operator could map directly into that syntax. 4. **Organized Reusability**: In large codebases with multiple modules, each module has a unique prefix (“N8·”, “N16·”, “N32·”, etc.), making it straightforward to locate definitions and references in a text search. ** Example Namespace Prefix