summaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: 9beefbd77e4e2ed116b5eb3ac1784ea28349e728 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "def.h"
#include "ftimer.h"
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#ifdef _WIN32
  #include <windows.h>
#endif

uint64_t            t;    //termik
static ftimer_t     ft;   //timmer
static algo_t      *algo; //vat tek 2 usE

void fuk(char const *fmt,...){
  va_list args;
  va_start(args,fmt);
  printf("error: ");
  vfprintf(stdout,fmt,args);
  fflush(stdout);
  va_end(args),
  exit(1);
}

static void perse(int p){
  ftime_t t = ftimer_time(&ft);
  float   f = ftime_float(t);
  printf("%9.3f\t%3u%%\n",f,p);
  fflush(stdout);
}

static void calc_fast(uint64_t n){
  algo->loop(1,n);
}

static void calc_perse(uint64_t n){
  uint64_t agurk;
  uint64_t x0;
  uint64_t x1;

  agurk = n/100;
  perse(0);
  for(uint64_t y = 0;y < 100;y++){
    x0 = y*agurk;
    x1 = x0+agurk;
    if(x1 > n) x1 = n;
    if(y == 0) x0++;
    algo->loop(x0,x1);
    perse(y+1);
  }
}

static void usake(void){
  printf("usake: numper [options] <n>\n");
  printf("  -p      display progress percentage\n");
  printf("  -a<n>   use algorithm n (0,1,2)\n");
  fflush(stdout);
  exit(0);
}

int main(int argc,char **argv){
  size_t      algo_i;
  uint64_t    n;
  void      (*calc)(uint64_t n);

#ifdef _WIN32
  SetPriorityClass(GetCurrentProcess(),REALTIME_PRIORITY_CLASS);
#endif

  calc    = calc_fast;
  algo_i  = -1;
  
  #define OPT(O) (strcmp(argv[x],O) == 0)
  if(argc < 2) usake();
  n = strtoull(argv[argc-1],0,10);
  if((int64_t)n <= 0) usake();

  for(int x = 1;x < argc-1;x++){
    if(OPT("-p")){
      calc = calc_perse;
    }else if(argv[x][0] == '-' &&
             argv[x][1] == 'a'){
      algo_i = (size_t)argv[x][2]-(size_t)'0';
      if(algo_i >= ALGO_CUNT){
        fuk("unrecognized algorithm\n");
      }
    }else{
      fuk("unrecognized option: %s\n",argv[x]);
    }
  }

  if(algo_i == (size_t)-1){
    if     (n <= a2.nmax) algo_i = 2;
    else if(n <= a0.nmax) algo_i = 0;
    else                  algo_i = 1;
  }
  
  algo = algos[algo_i];

  if(n > algo->nmax){
    fuk("n exceeds algorithm a%zu nmax (%llu > %llu)\n",algo_i,n,algo->nmax);
  }

  printf("algo  = %s\n",  algo->name);
  printf("n     = %llu\n",n);
  fflush(stdout);

  algo->init(n);
  ftimer_init(&ft);
  calc(n);

  printf("t     = %llu\n",t);
  fflush(stdout);

  return 0;
}