diff options
author | jonsykkel <jonrevold@gmail.com> | 2020-04-04 01:32:41 +0200 |
---|---|---|
committer | jonsykkel <jonrevold@gmail.com> | 2020-04-04 01:32:41 +0200 |
commit | 60ca2e95652a7101e97076af549381868ad20aad (patch) | |
tree | 5f0285f66ec9c79e382d777daa7d11cc1e6378e7 | |
parent | 21427fd699d2c80ee0f432bd60112a871cef157a (diff) | |
download | ct_sequence-60ca2e95652a7101e97076af549381868ad20aad.tar.gz |
DOSNT VORK
-rw-r--r-- | makefile | 2 | ||||
-rw-r--r-- | src/main.c | 104 |
2 files changed, 68 insertions, 38 deletions
@@ -56,7 +56,7 @@ ifneq ($(run),) run_cmd := @echo "run $(bin)" run_cmd += && cd bin run_cmd += && echo "----------------------------------------------------------------" - run_cmd += && time ./$(out_file) + run_cmd += && time ./$(out_file) 5000000000 run_cmd += ; echo "----------------------------------------------------------------" run_cmd += && cd .. endif @@ -4,14 +4,21 @@ #include <string.h> #include <okelib2/okelib2.h> -#define UINT40_MAX ((1ULL<<40)-1) +#define U40_MAX ((1ULL<<40)-1) +#define U32_MAX ((1ULL<<32)-1) typedef struct uint40_t{ uint8_t n[5]; }uint40_t; -static void primt(uint64_t x,uint64_t t){ - printf("%llu: %llu\n",x+1,t); +static ftimer_t ft; +static uint40_t *h40; +static uint32_t *h32; + +static void fuk(char const *str){ + printf("error!( %s\n",str); + fflush(stdout); + exit(1); } static uint64_t u40_to_u64(uint40_t x){ @@ -26,71 +33,94 @@ static uint40_t u64_to_u40(uint64_t x){ return r; } -static void perse(ftimer_t *ft,int p){ +static void perse(int p){ char buf[FMTFTIME_BUFSIZE]; - ftime_t t = ftimer_time(ft); + ftime_t t = ftimer_time(&ft); fmtftime(buf,t); printf("%12s %3u%%\n",buf,p); fflush(stdout); } -int main(int argc,char **argv){ - uint40_t *h; - uint64_t t; - uint64_t max; - - /* - if(argc < 2){ - printf("usake: numper <num>\n"); - return 1; +static void init(uint64_t n){ + size_t h32_n; + size_t h40_n; + + if(n > U32_MAX+1){ + h32_n = U32_MAX+1; + h40_n = n-h32_n; + h40 = malloc(sizeof(uint40_t)*h40_n); + if(!h40) fuk("memory"); + memset(h40,0xFF,sizeof(uint40_t)*h40_n); + }else{ + h32_n = n; + h40_n = 0; } + h32 = malloc(sizeof(uint32_t)*h32_n); + if(!h32) fuk("memory"); + memset(h32,0xFF,sizeof(uint32_t)*h32_n); +} - max = strtoull(argv[1],0,10); - */ +static int load_store(uint64_t t,uint64_t in,uint64_t *out){ + uint64_t r; + int unsee; + if(in > U32_MAX-1){ + r = u40_to_u64(h40[t]); + unsee = r == U40_MAX; + h40[t] = u64_to_u40(in); + }else{ + r = h32[t]; + unsee = r == U32_MAX; + h32[t] = in; + } + *out = r; + return unsee; +} - max = 5000000000; - max = 1000000000; +int main(int argc,char **argv){ + uint64_t t; + uint64_t n; + uint64_t agurk; + uint64_t outer; + uint64_t last; - //h = calloc(sizeof(uint_t)*max,1); - h = malloc(sizeof(uint40_t)*max); - if(!h){ - printf("failed to allogate mamorY\n"); + if(argc < 2){ + printf("usake: numper <n>\n"); fflush(stdout); return 1; } - memset(h,0xFF,sizeof(uint40_t)*max); - t = 0; + n = strtoull(argv[1],0,10); + printf("n = %llu\n",n); fflush(stdout); + init(n); - ftimer_t ft; - uint64_t agurk = max/100; - uint64_t outer = max/agurk+1; - uint64_t last = max%agurk; + t = 0; + agurk = n/100; + outer = n/agurk+1; + last = n%agurk; if(last == 0){ last = agurk; outer--; } ftimer_init(&ft); - perse(&ft,0); - for(uint64_t y = 0;y < outer;y++){ + perse(0); + for(uint64_t y = 0;y < outer;y++){ uint64_t beg = y*agurk; uint64_t end = beg+(y == outer-1 ? last : agurk); if(y == 0) beg++; - //printf("%12llu -> %12llu\n",beg,end); for(uint64_t x = beg;x < end;x++){ - uint64_t ht; + uint64_t z; + int unsee; - ht = u40_to_u64(h[t]); - h[t] = u64_to_u40(x); - t = ht == UINT40_MAX ? 0 : x-ht; + unsee = load_store(t,x,&z); + t = unsee ? 0 : x-z; } - perse(&ft,(y+1)*100/outer); + perse((y+1)*100/outer); } - primt(max-1,t); + printf("result = %llu\n",t); fflush(stdout); return 0; } |