1 |
786 |
skrzyp |
//==========================================================================
|
2 |
|
|
//
|
3 |
|
|
// fptest.cxx
|
4 |
|
|
//
|
5 |
|
|
// Basic FPU test
|
6 |
|
|
//
|
7 |
|
|
//==========================================================================
|
8 |
|
|
// ####ECOSGPLCOPYRIGHTBEGIN####
|
9 |
|
|
// -------------------------------------------
|
10 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
11 |
|
|
// Copyright (C) 2003 Free Software Foundation, Inc.
|
12 |
|
|
//
|
13 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
14 |
|
|
// the terms of the GNU General Public License as published by the Free
|
15 |
|
|
// Software Foundation; either version 2 or (at your option) any later
|
16 |
|
|
// version.
|
17 |
|
|
//
|
18 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT
|
19 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
20 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
21 |
|
|
// for more details.
|
22 |
|
|
//
|
23 |
|
|
// You should have received a copy of the GNU General Public License
|
24 |
|
|
// along with eCos; if not, write to the Free Software Foundation, Inc.,
|
25 |
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
26 |
|
|
//
|
27 |
|
|
// As a special exception, if other files instantiate templates or use
|
28 |
|
|
// macros or inline functions from this file, or you compile this file
|
29 |
|
|
// and link it with other works to produce a work based on this file,
|
30 |
|
|
// this file does not by itself cause the resulting work to be covered by
|
31 |
|
|
// the GNU General Public License. However the source code for this file
|
32 |
|
|
// must still be made available in accordance with section (3) of the GNU
|
33 |
|
|
// General Public License v2.
|
34 |
|
|
//
|
35 |
|
|
// This exception does not invalidate any other reasons why a work based
|
36 |
|
|
// on this file might be covered by the GNU General Public License.
|
37 |
|
|
// -------------------------------------------
|
38 |
|
|
// ####ECOSGPLCOPYRIGHTEND####
|
39 |
|
|
//==========================================================================
|
40 |
|
|
//#####DESCRIPTIONBEGIN####
|
41 |
|
|
//
|
42 |
|
|
// Author(s): nickg@calivar.com
|
43 |
|
|
// Contributors: nickg@calivar.com
|
44 |
|
|
// Date: 2003-01-27
|
45 |
|
|
// Description: Simple FPU test. This is not very sophisticated as far
|
46 |
|
|
// as checking FPU performance or accuracy. It is more
|
47 |
|
|
// concerned with checking that several threads doing FP
|
48 |
|
|
// operations do not interfere with eachother's use of the
|
49 |
|
|
// FPU.
|
50 |
|
|
//
|
51 |
|
|
//####DESCRIPTIONEND####
|
52 |
|
|
//==========================================================================
|
53 |
|
|
|
54 |
|
|
#include <pkgconf/kernel.h>
|
55 |
|
|
#include <pkgconf/hal.h>
|
56 |
|
|
|
57 |
|
|
#include <cyg/hal/hal_arch.h>
|
58 |
|
|
|
59 |
|
|
#include <cyg/kernel/kapi.h>
|
60 |
|
|
|
61 |
|
|
#include <cyg/infra/testcase.h>
|
62 |
|
|
#include <cyg/infra/diag.h>
|
63 |
|
|
|
64 |
|
|
//#include <cyg/kernel/test/stackmon.h>
|
65 |
|
|
#include CYGHWR_MEMORY_LAYOUT_H
|
66 |
|
|
|
67 |
|
|
//==========================================================================
|
68 |
|
|
|
69 |
|
|
#if defined(CYGFUN_KERNEL_API_C) && \
|
70 |
|
|
defined(CYGSEM_KERNEL_SCHED_MLQUEUE) && \
|
71 |
|
|
(CYGNUM_KERNEL_SCHED_PRIORITIES > 12) && \
|
72 |
|
|
(CYGMEM_REGION_ram_SIZE >= (49152-4096))
|
73 |
|
|
|
74 |
|
|
//==========================================================================
|
75 |
|
|
// Base priority for all threads.
|
76 |
|
|
|
77 |
|
|
#define BASE_PRI 5
|
78 |
|
|
|
79 |
|
|
//==========================================================================
|
80 |
|
|
// Runtime
|
81 |
|
|
//
|
82 |
|
|
// This is the number of ticks that the program will run for. 3000
|
83 |
|
|
// ticks is equal to 30 seconds in the default configuration. For
|
84 |
|
|
// simulators we reduce the run time to 3 simulated seconds.
|
85 |
|
|
|
86 |
|
|
#define RUN_TICKS 3000
|
87 |
|
|
#define RUN_TICKS_SIM 300
|
88 |
|
|
|
89 |
|
|
//==========================================================================
|
90 |
|
|
// Thread parameters
|
91 |
|
|
|
92 |
|
|
#define STACK_SIZE (CYGNUM_HAL_STACK_SIZE_MINIMUM)
|
93 |
|
|
|
94 |
|
|
static cyg_uint8 stacks[3][STACK_SIZE];
|
95 |
|
|
static cyg_handle_t thread[3];
|
96 |
|
|
static cyg_thread thread_struct[3];
|
97 |
|
|
|
98 |
|
|
//==========================================================================
|
99 |
|
|
// Alarm parameters.
|
100 |
|
|
|
101 |
|
|
static cyg_alarm alarm_struct;
|
102 |
|
|
static cyg_handle_t alarm;
|
103 |
|
|
|
104 |
|
|
static cyg_count8 cur_thread = 0;
|
105 |
|
|
static cyg_count32 alarm_ticks = 0;
|
106 |
|
|
static cyg_count32 run_ticks = RUN_TICKS;
|
107 |
|
|
|
108 |
|
|
//==========================================================================
|
109 |
|
|
|
110 |
|
|
static int errors = 0;
|
111 |
|
|
|
112 |
|
|
//==========================================================================
|
113 |
|
|
// Random number generator. Ripped out of the C library.
|
114 |
|
|
|
115 |
|
|
static int rand( unsigned int *seed )
|
116 |
|
|
{
|
117 |
|
|
// This is the code supplied in Knuth Vol 2 section 3.6 p.185 bottom
|
118 |
|
|
|
119 |
|
|
#define RAND_MAX 0x7fffffff
|
120 |
|
|
#define MM 2147483647 // a Mersenne prime
|
121 |
|
|
#define AA 48271 // this does well in the spectral test
|
122 |
|
|
#define QQ 44488 // (long)(MM/AA)
|
123 |
|
|
#define RR 3399 // MM % AA; it is important that RR<QQ
|
124 |
|
|
|
125 |
|
|
*seed = AA*(*seed % QQ) - RR*(unsigned int)(*seed/QQ);
|
126 |
|
|
if (*seed < 0)
|
127 |
|
|
*seed += MM;
|
128 |
|
|
|
129 |
|
|
return (int)( *seed & RAND_MAX );
|
130 |
|
|
}
|
131 |
|
|
|
132 |
|
|
//==========================================================================
|
133 |
|
|
// Test calculation.
|
134 |
|
|
//
|
135 |
|
|
// Generates an array of random FP values and then repeatedly applies
|
136 |
|
|
// a calculation to them and checks that the same result is reached
|
137 |
|
|
// each time. The calculation, in the macro CALC, is intended to make
|
138 |
|
|
// maximum use of the FPU registers. However, the i386 compiler
|
139 |
|
|
// doesn't let this expression get very complex before it starts
|
140 |
|
|
// spilling values out to memory.
|
141 |
|
|
|
142 |
|
|
static void do_test( double *values,
|
143 |
|
|
int count,
|
144 |
|
|
int loops,
|
145 |
|
|
int test,
|
146 |
|
|
const char *name)
|
147 |
|
|
{
|
148 |
|
|
unsigned int i, j;
|
149 |
|
|
// volatiles necessary to force
|
150 |
|
|
// values to 64 bits for comparison
|
151 |
|
|
volatile double sum = 1.0;
|
152 |
|
|
volatile double last_sum;
|
153 |
|
|
unsigned int seed;
|
154 |
|
|
|
155 |
|
|
#define V(__i) (values[(__i)%count])
|
156 |
|
|
#define CALC ((V(i-1)*V(i+1))*(V(i-2)*V(i+2))*(V(i-3)*sum))
|
157 |
|
|
|
158 |
|
|
seed = ((unsigned int)&i)*count;
|
159 |
|
|
|
160 |
|
|
// Set up an array of values...
|
161 |
|
|
for( i = 0; i < count; i++ )
|
162 |
|
|
values[i] = (double)rand( &seed )/(double)0x7fffffff;
|
163 |
|
|
|
164 |
|
|
// Now calculate something from them...
|
165 |
|
|
for( i = 0; i < count; i++ )
|
166 |
|
|
sum += CALC;
|
167 |
|
|
last_sum = sum;
|
168 |
|
|
|
169 |
|
|
// Now recalculate the sum in a loop and look for errors
|
170 |
|
|
for( j = 0; j < loops ; j++ )
|
171 |
|
|
{
|
172 |
|
|
sum = 1.0;
|
173 |
|
|
for( i = 0; i < count; i++ )
|
174 |
|
|
sum += CALC;
|
175 |
|
|
|
176 |
|
|
if( sum != last_sum )
|
177 |
|
|
{
|
178 |
|
|
union double_int_union {
|
179 |
|
|
double d;
|
180 |
|
|
cyg_uint32 i[2];
|
181 |
|
|
} diu_sum, diu_lastsum;
|
182 |
|
|
|
183 |
|
|
diu_sum.d = sum;
|
184 |
|
|
diu_lastsum.d = last_sum;
|
185 |
|
|
|
186 |
|
|
errors++;
|
187 |
|
|
if (sizeof(double) != 2*sizeof(cyg_uint32)) {
|
188 |
|
|
diag_printf("Warning: sizeof(double) != 2*sizeof(cyg_uint32), therefore next line may\n"
|
189 |
|
|
"have invalid sum/last_sum values\n");
|
190 |
|
|
}
|
191 |
|
|
diag_printf("%s: Sum mismatch! %d sum=[%08x:%08x] last_sum=[%08x:%08x]\n",
|
192 |
|
|
name,j, diu_sum.i[0], diu_sum.i[1], diu_lastsum.i[0], diu_lastsum.i[1] );
|
193 |
|
|
}
|
194 |
|
|
|
195 |
|
|
#if 0
|
196 |
|
|
if( ((j*count)%1000000) == 0 )
|
197 |
|
|
diag_printf("INFO:<%s: %2d calculations done>\n",name,j*count);
|
198 |
|
|
#endif
|
199 |
|
|
}
|
200 |
|
|
|
201 |
|
|
}
|
202 |
|
|
|
203 |
|
|
//==========================================================================
|
204 |
|
|
// Alarm handler
|
205 |
|
|
//
|
206 |
|
|
// This is called every tick. It lowers the priority of the currently
|
207 |
|
|
// running thread and raises the priority of the next. Thus we
|
208 |
|
|
// implement a form of timelslicing between the threads at one tick
|
209 |
|
|
// granularity.
|
210 |
|
|
|
211 |
|
|
static void alarm_fn(cyg_handle_t alarm, cyg_addrword_t data)
|
212 |
|
|
{
|
213 |
|
|
alarm_ticks++;
|
214 |
|
|
|
215 |
|
|
if( alarm_ticks >= run_ticks )
|
216 |
|
|
{
|
217 |
|
|
if( errors )
|
218 |
|
|
CYG_TEST_FAIL("Errors detected");
|
219 |
|
|
else
|
220 |
|
|
CYG_TEST_PASS("OK");
|
221 |
|
|
|
222 |
|
|
CYG_TEST_FINISH("FP Test done");
|
223 |
|
|
}
|
224 |
|
|
else
|
225 |
|
|
{
|
226 |
|
|
cyg_thread_set_priority( thread[cur_thread], BASE_PRI );
|
227 |
|
|
|
228 |
|
|
cur_thread = (cur_thread+1)%3;
|
229 |
|
|
|
230 |
|
|
cyg_thread_set_priority( thread[cur_thread], BASE_PRI-1 );
|
231 |
|
|
}
|
232 |
|
|
}
|
233 |
|
|
|
234 |
|
|
|
235 |
|
|
//==========================================================================
|
236 |
|
|
|
237 |
|
|
#define FP1_COUNT 1000
|
238 |
|
|
|
239 |
|
|
static double fpt1_values[FP1_COUNT];
|
240 |
|
|
|
241 |
|
|
void fptest1( CYG_ADDRWORD id )
|
242 |
|
|
{
|
243 |
|
|
while(1)
|
244 |
|
|
do_test( fpt1_values, FP1_COUNT, 2000000000, id, "fptest1" );
|
245 |
|
|
}
|
246 |
|
|
|
247 |
|
|
//==========================================================================
|
248 |
|
|
#if (CYGMEM_REGION_ram_SIZE / 8 / 2) < 10000
|
249 |
|
|
#define FP2_COUNT (CYGMEM_REGION_ram_SIZE / 8 / 2)
|
250 |
|
|
#else
|
251 |
|
|
#define FP2_COUNT 10000
|
252 |
|
|
#endif
|
253 |
|
|
|
254 |
|
|
static double fpt2_values[FP2_COUNT];
|
255 |
|
|
|
256 |
|
|
void fptest2( CYG_ADDRWORD id )
|
257 |
|
|
{
|
258 |
|
|
while(1)
|
259 |
|
|
do_test( fpt2_values, FP2_COUNT, 2000000000, id, "fptest2" );
|
260 |
|
|
}
|
261 |
|
|
|
262 |
|
|
//==========================================================================
|
263 |
|
|
|
264 |
|
|
#define FP3_COUNT 100
|
265 |
|
|
|
266 |
|
|
static double fpt3_values[FP3_COUNT];
|
267 |
|
|
|
268 |
|
|
void fptest3( CYG_ADDRWORD id )
|
269 |
|
|
{
|
270 |
|
|
while(1)
|
271 |
|
|
do_test( fpt3_values, FP3_COUNT, 2000000000, id, "fptest3" );
|
272 |
|
|
}
|
273 |
|
|
|
274 |
|
|
//==========================================================================
|
275 |
|
|
|
276 |
|
|
void fptest_main( void )
|
277 |
|
|
{
|
278 |
|
|
|
279 |
|
|
CYG_TEST_INIT();
|
280 |
|
|
|
281 |
|
|
if( cyg_test_is_simulator )
|
282 |
|
|
{
|
283 |
|
|
run_ticks = RUN_TICKS_SIM;
|
284 |
|
|
}
|
285 |
|
|
|
286 |
|
|
CYG_TEST_INFO("Run fptest in cyg_start");
|
287 |
|
|
do_test( fpt3_values, FP3_COUNT, 1000, 0, "start" );
|
288 |
|
|
CYG_TEST_INFO( "cyg_start run done");
|
289 |
|
|
|
290 |
|
|
cyg_thread_create( BASE_PRI-1,
|
291 |
|
|
fptest1,
|
292 |
|
|
0,
|
293 |
|
|
"fptest1",
|
294 |
|
|
&stacks[0][0],
|
295 |
|
|
STACK_SIZE,
|
296 |
|
|
&thread[0],
|
297 |
|
|
&thread_struct[0]);
|
298 |
|
|
|
299 |
|
|
cyg_thread_resume( thread[0] );
|
300 |
|
|
|
301 |
|
|
cyg_thread_create( BASE_PRI,
|
302 |
|
|
fptest2,
|
303 |
|
|
1,
|
304 |
|
|
"fptest2",
|
305 |
|
|
&stacks[1][0],
|
306 |
|
|
STACK_SIZE,
|
307 |
|
|
&thread[1],
|
308 |
|
|
&thread_struct[1]);
|
309 |
|
|
|
310 |
|
|
cyg_thread_resume( thread[1] );
|
311 |
|
|
|
312 |
|
|
cyg_thread_create( BASE_PRI,
|
313 |
|
|
fptest3,
|
314 |
|
|
2,
|
315 |
|
|
"fptest3",
|
316 |
|
|
&stacks[2][0],
|
317 |
|
|
STACK_SIZE,
|
318 |
|
|
&thread[2],
|
319 |
|
|
&thread_struct[2]);
|
320 |
|
|
|
321 |
|
|
cyg_thread_resume( thread[2] );
|
322 |
|
|
|
323 |
|
|
cyg_alarm_create( cyg_real_time_clock(),
|
324 |
|
|
alarm_fn,
|
325 |
|
|
0,
|
326 |
|
|
&alarm,
|
327 |
|
|
&alarm_struct );
|
328 |
|
|
|
329 |
|
|
cyg_alarm_initialize( alarm, cyg_current_time()+1, 1 );
|
330 |
|
|
|
331 |
|
|
cyg_scheduler_start();
|
332 |
|
|
|
333 |
|
|
}
|
334 |
|
|
|
335 |
|
|
//==========================================================================
|
336 |
|
|
|
337 |
|
|
#ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
|
338 |
|
|
externC void
|
339 |
|
|
cyg_hal_invoke_constructors();
|
340 |
|
|
#endif
|
341 |
|
|
|
342 |
|
|
externC void
|
343 |
|
|
cyg_start( void )
|
344 |
|
|
{
|
345 |
|
|
#ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
|
346 |
|
|
cyg_hal_invoke_constructors();
|
347 |
|
|
#endif
|
348 |
|
|
fptest_main();
|
349 |
|
|
}
|
350 |
|
|
|
351 |
|
|
//==========================================================================
|
352 |
|
|
|
353 |
|
|
#else // CYGFUN_KERNEL_API_C...
|
354 |
|
|
|
355 |
|
|
externC void
|
356 |
|
|
cyg_start( void )
|
357 |
|
|
{
|
358 |
|
|
CYG_TEST_INIT();
|
359 |
|
|
CYG_TEST_INFO("FP test requires:\n"
|
360 |
|
|
"CYGFUN_KERNEL_API_C && \n"
|
361 |
|
|
"CYGSEM_KERNEL_SCHED_MLQUEUE && \n"
|
362 |
|
|
"(CYGNUM_KERNEL_SCHED_PRIORITIES > 12) &&\n"
|
363 |
|
|
"(CYGMEM_REGION_ram_SIZE >= (49152-4096))\n");
|
364 |
|
|
CYG_TEST_NA("FP test requirements");
|
365 |
|
|
}
|
366 |
|
|
|
367 |
|
|
#endif // CYGFUN_KERNEL_API_C, etc.
|
368 |
|
|
|
369 |
|
|
//==========================================================================
|
370 |
|
|
// EOF fptest.cxx
|