summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjonsykkel <jonrevold@gmail.com>2020-04-05 04:11:47 +0200
committerjonsykkel <jonrevold@gmail.com>2020-04-05 04:11:47 +0200
commit8471ac0de185aed90bd7d3a12dded3d467a08a33 (patch)
treeee3507b82b58ebb5fda32783785501092dabdcf5
parent1516b56e05f18dcf41b64ad909af120f55929685 (diff)
downloadct_sequence-8471ac0de185aed90bd7d3a12dded3d467a08a33.tar.gz
vork
-rw-r--r--src/a0.c35
-rw-r--r--src/a1.c45
-rw-r--r--src/a3.c60
-rw-r--r--src/def.h9
-rw-r--r--src/main.c34
5 files changed, 91 insertions, 92 deletions
diff --git a/src/a0.c b/src/a0.c
new file mode 100644
index 0000000..5eed926
--- /dev/null
+++ b/src/a0.c
@@ -0,0 +1,35 @@
+#include "def.h"
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+void a0_init (uint64_t n);
+void a0_loop (uint64_t x0,uint64_t x1);
+
+algo_t a0 = {
+ .name = "a0",
+ .nmax = UINT32_MAX,
+ .init = a0_init,
+ .loop = a0_loop,
+};
+
+static uint32_t *m;
+
+void a0_init(uint64_t n){
+ size_t size;
+
+ size = sizeof(uint32_t)*n;
+ m = calloc(size,1);
+ if(!m) fuk("memory\n");
+}
+
+void a0_loop(uint64_t x0,uint64_t x1){
+ uint32_t x1_32 = x1;
+ for(uint32_t x = x0;x < x1_32;x++){
+ uint32_t z;
+ z = m[t];
+ m[t] = x;
+ t = z == 0 ? 0 : x-z;
+ }
+}
+
diff --git a/src/a1.c b/src/a1.c
index 4afab2e..7cecf41 100644
--- a/src/a1.c
+++ b/src/a1.c
@@ -3,33 +3,58 @@
#include <stdlib.h>
#include <string.h>
+#define BITS 36
+
void a1_init (uint64_t n);
void a1_loop (uint64_t x0,uint64_t x1);
algo_t a1 = {
.name = "a1",
- .nmax = UINT32_MAX,
+ .nmax = 1ULL<<BITS,
.init = a1_init,
.loop = a1_loop,
};
-static uint32_t *m;
+static uint8_t *m;
+static uint64_t load_store(uint64_t t,uint64_t i);
void a1_init(uint64_t n){
size_t size;
- size = sizeof(uint32_t)*n;
+ size = sizeof(uint8_t)*n*BITS>>3;
m = calloc(size,1);
- if(!m) fuk("memory");
+ if(!m) fuk("memory\n");
}
void a1_loop(uint64_t x0,uint64_t x1){
- uint32_t x1_32 = x1;
- for(uint32_t x = x0;x < x1_32;x++){
- uint32_t z;
- z = m[t];
- m[t] = x;
- t = z == 0 ? 0 : x-z;
+ for(uint64_t x = x0;x < x1;x++){
+ uint64_t z;
+ z = load_store(t,x);
+ t = z == 0 ? 0 : x-z;
+ }
+}
+
+static uint64_t load_store(uint64_t t,uint64_t i){
+ uint64_t r;
+ uint8_t *p;
+
+ r = 0;
+ p = m+(t*BITS>>3);
+ memcpy(&r,p,5);
+ if(t & 1){
+ r >>= 4;
+ }else{
+ r &= 0x0000000FFFFFFFFF;
+ }
+
+ if(t & 1){
+ i <<= 4;
+ i |= p[0] & 0x0F;
+ }else{
+ i |= (uint64_t)(p[4] & 0xF0)<<32;
}
+ memcpy(p,&i,5);
+
+ return r;
}
diff --git a/src/a3.c b/src/a3.c
deleted file mode 100644
index 65e9160..0000000
--- a/src/a3.c
+++ /dev/null
@@ -1,60 +0,0 @@
-#include "def.h"
-#include <stdint.h>
-#include <stdlib.h>
-#include <string.h>
-
-#define BITS 36
-
-void a3_init (uint64_t n);
-void a3_loop (uint64_t x0,uint64_t x1);
-
-algo_t a3 = {
- .name = "a1",
- .nmax = 1ULL<<BITS,
- .init = a3_init,
- .loop = a3_loop,
-};
-
-static uint8_t *m;
-static uint64_t load_store(uint64_t t,uint64_t i);
-
-void a3_init(uint64_t n){
- size_t size;
-
- size = sizeof(uint8_t)*n*BITS>>3;
- m = calloc(size,1);
- if(!m) fuk("memory");
-}
-
-void a3_loop(uint64_t x0,uint64_t x1){
- for(uint64_t x = x0;x < x1;x++){
- uint64_t z;
- z = load_store(t,x);
- t = z == 0 ? 0 : x-z;
- }
-}
-
-static uint64_t load_store(uint64_t t,uint64_t i){
- uint64_t r;
- uint8_t *p;
-
- r = 0;
- p = m+(t*BITS>>3);
- memcpy(&r,p,5);
- if(t & 1){
- r >>= 4;
- }else{
- r &= 0x0000000FFFFFFFFF;
- }
-
- if(t & 1){
- i <<= 4;
- i |= p[0] & 0x0F;
- }else{
- i |= (uint64_t)(p[4] & 0xF0)<<32;
- }
- memcpy(p,&i,5);
-
- return r;
-}
-
diff --git a/src/def.h b/src/def.h
index 0540636..6be37fb 100644
--- a/src/def.h
+++ b/src/def.h
@@ -3,6 +3,8 @@
#include <stdint.h>
+#define ALGO_CUNT 2
+
typedef struct algo_t{
char const *name;
uint64_t nmax;
@@ -12,8 +14,13 @@ typedef struct algo_t{
}algo_t;
extern uint64_t t;
+extern algo_t a0;
extern algo_t a1;
-extern algo_t a3;
+
+static algo_t *algos[ALGO_CUNT] = {
+ [0] = &a0,
+ [1] = &a1,
+};
void fuk (char const *fmt,...);
diff --git a/src/main.c b/src/main.c
index 207d96a..f02eae9 100644
--- a/src/main.c
+++ b/src/main.c
@@ -18,8 +18,8 @@ static algo_t *algo;
void fuk(char const *fmt,...){
va_list args;
va_start(args,fmt);
+ printf("ERROR: ");
vfprintf(stdout,fmt,args);
- putc('\n',stdout);
fflush(stdout);
va_end(args),
exit(1);
@@ -63,7 +63,7 @@ static void usake(void){
}
int main(int argc,char **argv){
- size_t algo_n;
+ size_t algo_i;
uint64_t n;
void (*calc)(uint64_t n);
@@ -72,7 +72,7 @@ int main(int argc,char **argv){
#endif
calc = calc_fast;
- algo_n = -1;
+ algo_i = -1;
if(argc < 2){
usake();
@@ -83,10 +83,10 @@ int main(int argc,char **argv){
if(OPT("-p")){
calc = calc_perse;
}else if(argv[x][0] == '-' && argv[x][1] == 'a'){
- algo_n = (size_t)argv[x][2]-(size_t)'0';
- if(algo_n > 9){
- printf("unrecognized algorithm\n");
- algo_n = -1;
+ algo_i = (size_t)argv[x][2]-(size_t)'0';
+ if(algo_i >= ALGO_CUNT){
+ printf("unrecognized algorithm: %zu\n",algo_i);
+ algo_i = -1;
}
}else{
printf("unrecognized option: %s\n",argv[x]);
@@ -96,23 +96,15 @@ int main(int argc,char **argv){
n = strtoull(argv[argc-1],0,10);
if((int64_t)n <= 0) usake();
- if(algo_n != (size_t)-1){
- switch(algo_n){
- case 1: algo = &a1; break;
- case 3: algo = &a3; break;
- default:
- printf("unurecognized algorithm: %zu\n",algo_n);
- algo_n = -1;
- }
- }
-
- if(algo_n == (size_t)-1){
- if(n <= a1.nmax){
- algo = &a1;
+ if(algo_i == (size_t)-1){
+ if(n <= a0.nmax){
+ algo_i = 0;
}else{
- algo = &a3;
+ algo_i = 1;
}
}
+
+ algo = algos[algo_i];
printf("algo = %s\n", algo->name);
printf("n = %llu\n",n);