]> rtime.felk.cvut.cz Git - fpga/lx-cpu1/lx-rocon.git/blob - sw/app/rocon/math_sqrtll_test.c
RoCoN: debug print includes actual action/PWM output for now.
[fpga/lx-cpu1/lx-rocon.git] / sw / app / rocon / math_sqrtll_test.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #ifdef OMK_FOR_TARGET
5  #include <cpu_def.h>
6  #include <system_def.h>
7  #include <lt_timer.h>
8  unsigned long sqrtll(unsigned long long x);
9  unsigned long sqrtll_approx(unsigned long long x);
10  #define main sqrtll_main
11  #ifdef LPC_TIM0
12   #define sqrtll_ticks() (LPC_TIM0->TC)
13  #endif /*LPC_TIM0*/
14 #else /*OMK_FOR_TARGET*/
15  #if 0
16   #include "math_sqrtll.c"
17  #else
18   #define main sqrtll_main
19  #endif
20  #define lt_mstime_update() do {} while(0)
21  #define cli() do {} while(0)
22  #define sti() do {} while(0)
23  int actual_msec;
24 #endif /*OMK_FOR_TARGET*/
25
26 #ifndef sqrtll_ticks
27   #define sqrtll_ticks() 0
28 #endif
29
30 int main(int argc, char *argv[])
31 {
32   unsigned long long sqr;
33   unsigned long res,inp,incr,cnt,time;
34   unsigned short spent;
35
36   char *s="a";
37
38   if (argc>=2)
39     s=argv[1];
40
41   if(*s!='a'){
42     inp=strtoul(s,NULL,0);
43     sqr=(unsigned long long int)inp*inp;
44     cli();
45     spent=sqrtll_ticks();
46     res=sqrtll_approx(sqr);
47     spent=sqrtll_ticks()-spent;
48     sti();
49     printf("sqrtll: inp=%lu, res=%lu, spent=%u\n",inp,res,(int)spent);
50   }else{
51     inp=0; incr=1; cnt=0;
52     int eps=1;
53     lt_mstime_update();
54     time=actual_msec;
55     do{
56       sqr=(unsigned long long int)inp * inp;
57       res=sqrtll_approx(sqr);
58       if(0||(labs(inp-res)) >= eps){
59         printf("\nsqrt error %lu -> %lu diff %ld sqr 0x%llx\n",inp,res,inp-res,sqr);
60         eps<<=1;
61         res=sqrtll_approx(sqr);
62       }
63       cnt++;
64       if(inp>0xffffffff-incr)
65         break;
66       inp+=incr;
67       if(inp&0xfff) continue;
68       if(inp&((incr<<19)-1)) continue;
69       incr<<=1;
70       printf(".");fflush(NULL);
71     }while(1);
72     lt_mstime_update();
73     time=actual_msec-time;
74     printf("\nsqrtll OK, spent %ld cnt %ld\n",time,cnt);
75   }
76   return 0;
77 }