Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-core / src / tests / benchmark_nco.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2002,2004 Free Software Foundation, Inc.
4  *
5  * This file is part of GNU Radio
6  *
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  *
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 #include <stdio.h>
26 #include <sys/time.h>
27 #ifdef HAVE_SYS_RESOURCE_H
28 #include <sys/resource.h>
29 #endif
30 #include <unistd.h>
31 #include <gr_nco.h>
32 #include <gr_fxpt_nco.h>
33 #include <string.h>
34
35 #define ITERATIONS      20000000
36 #define BLOCK_SIZE      (10 * 1000)     // fits in cache
37
38 #define FREQ    5003.123
39
40 static double
41 timeval_to_double (const struct timeval *tv)
42 {
43   return (double) tv->tv_sec + (double) tv->tv_usec * 1e-6;
44 }
45
46
47 static void
48 benchmark (void test (float *x, float *y), const char *implementation_name)
49 {
50 #ifdef HAVE_SYS_RESOURCE_H
51   struct rusage rusage_start;
52   struct rusage rusage_stop;
53 #else
54   double clock_start;
55   double clock_end;
56 #endif
57   float output[2*BLOCK_SIZE];
58   float *x = &output[0], *y = &output[BLOCK_SIZE];
59
60   // touch memory
61   memset(output, 0, 2*BLOCK_SIZE*sizeof(float));
62
63   // get starting CPU usage
64 #ifdef HAVE_SYS_RESOURCE_H
65   if (getrusage (RUSAGE_SELF, &rusage_start) < 0){
66     perror ("getrusage");
67     exit (1);
68   }
69 #else
70   clock_start = (double) clock() * (1000000. / CLOCKS_PER_SEC);
71 #endif
72   // do the actual work
73
74   test (x, y);
75
76   // get ending CPU usage
77
78 #ifdef HAVE_SYS_RESOURCE_H
79   if (getrusage (RUSAGE_SELF, &rusage_stop) < 0){
80     perror ("getrusage");
81     exit (1);
82   }
83
84   // compute results
85
86   double user =
87     timeval_to_double (&rusage_stop.ru_utime)
88     - timeval_to_double (&rusage_start.ru_utime);
89
90   double sys =
91     timeval_to_double (&rusage_stop.ru_stime)
92     - timeval_to_double (&rusage_start.ru_stime);
93
94   double total = user + sys;
95 #else
96   clock_end = (double) clock () * (1000000. / CLOCKS_PER_SEC);
97   double total = clock_end - clock_start;
98 #endif
99
100   printf ("%18s:  cpu: %6.3f  steps/sec: %10.3e\n",
101           implementation_name, total, ITERATIONS / total);
102 }
103
104 // ----------------------------------------------------------------
105 // Don't compare the _vec with other functions since memory store's
106 // are involved.
107
108 void basic_sincos_vec (float *x, float *y)
109 {
110   gr_nco<float,float>   nco;
111
112   nco.set_freq (2 * M_PI / FREQ);
113
114   for (int i = 0; i < ITERATIONS/BLOCK_SIZE; i++){
115     for (int j = 0; j < BLOCK_SIZE; j++){
116       nco.sincos (&x[2*j+1], &x[2*j]);
117       nco.step ();
118     }
119   }
120 }
121
122 void native_sincos_vec (float *x, float *y)
123 {
124   gr_nco<float,float>   nco;
125
126   nco.set_freq (2 * M_PI / FREQ);
127  
128   for (int i = 0; i < ITERATIONS/BLOCK_SIZE; i++){
129     nco.sincos ((gr_complex*)x, BLOCK_SIZE);
130   }
131 }
132
133 void fxpt_sincos_vec (float *x, float *y)
134 {
135   gr_fxpt_nco   nco;
136   
137   nco.set_freq (2 * M_PI / FREQ);
138   
139   for (int i = 0; i < ITERATIONS/BLOCK_SIZE; i++){
140     nco.sincos ((gr_complex*)x, BLOCK_SIZE);
141   }
142 }
143
144 // ----------------------------------------------------------------
145
146 void native_sincos (float *x, float *y)
147 {
148   gr_nco<float,float>   nco;
149
150   nco.set_freq (2 * M_PI / FREQ);
151
152   for (int i = 0; i < ITERATIONS; i++){
153     nco.sincos (x, y);
154     nco.step ();
155   }
156 }
157
158 void fxpt_sincos (float *x, float *y)
159 {
160   gr_fxpt_nco   nco;
161
162   nco.set_freq (2 * M_PI / FREQ);
163
164   for (int i = 0; i < ITERATIONS; i++){
165     nco.sincos (x, y);
166     nco.step ();
167   }
168 }
169
170 // ----------------------------------------------------------------
171
172 void native_sin (float *x, float *y)
173 {
174   gr_nco<float,float>   nco;
175
176   nco.set_freq (2 * M_PI / FREQ);
177
178   for (int i = 0; i < ITERATIONS; i++){
179     *x = nco.sin ();
180     nco.step ();
181   }
182 }
183
184 void fxpt_sin (float *x, float *y)
185 {
186   gr_fxpt_nco   nco;
187
188   nco.set_freq (2 * M_PI / FREQ);
189
190   for (int i = 0; i < ITERATIONS; i++){
191     *x = nco.sin ();
192     nco.step ();
193   }
194 }
195
196 // ----------------------------------------------------------------
197
198 void nop_fct (float *x, float *y)
199 {
200 }
201
202 void nop_loop (float *x, float *y)
203 {
204   for (int i = 0; i < ITERATIONS; i++){
205     nop_fct (x, y);
206   }
207 }
208
209 int
210 main (int argc, char **argv)
211 {
212   benchmark (nop_loop, "nop loop");
213   benchmark (native_sin, "native sine");
214   benchmark (fxpt_sin, "fxpt sine");
215   benchmark (native_sincos, "native sin/cos");
216   benchmark (fxpt_sincos, "fxpt sin/cos");
217   benchmark (basic_sincos_vec, "basic sin/cos vec");
218   benchmark (native_sincos_vec, "native sin/cos vec");
219   benchmark (fxpt_sincos_vec, "fxpt sin/cos vec");
220 }