//==========================================================================
|
//==========================================================================
|
//
|
//
|
// fptest.cxx
|
// fptest.cxx
|
//
|
//
|
// Basic FPU test
|
// Basic FPU test
|
//
|
//
|
//==========================================================================
|
//==========================================================================
|
//####ECOSGPLCOPYRIGHTBEGIN####
|
//####ECOSGPLCOPYRIGHTBEGIN####
|
// -------------------------------------------
|
// -------------------------------------------
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
// Copyright (C) 2003 Nick Garnett
|
// Copyright (C) 2003 Nick Garnett
|
//
|
//
|
// eCos is free software; you can redistribute it and/or modify it under
|
// eCos is free software; you can redistribute it and/or modify it under
|
// the terms of the GNU General Public License as published by the Free
|
// the terms of the GNU General Public License as published by the Free
|
// Software Foundation; either version 2 or (at your option) any later version.
|
// Software Foundation; either version 2 or (at your option) any later version.
|
//
|
//
|
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
|
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
// for more details.
|
// for more details.
|
//
|
//
|
// You should have received a copy of the GNU General Public License along
|
// You should have received a copy of the GNU General Public License along
|
// with eCos; if not, write to the Free Software Foundation, Inc.,
|
// with eCos; if not, write to the Free Software Foundation, Inc.,
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
|
//
|
//
|
// As a special exception, if other files instantiate templates or use macros
|
// As a special exception, if other files instantiate templates or use macros
|
// or inline functions from this file, or you compile this file and link it
|
// or inline functions from this file, or you compile this file and link it
|
// with other works to produce a work based on this file, this file does not
|
// with other works to produce a work based on this file, this file does not
|
// by itself cause the resulting work to be covered by the GNU General Public
|
// by itself cause the resulting work to be covered by the GNU General Public
|
// License. However the source code for this file must still be made available
|
// License. However the source code for this file must still be made available
|
// in accordance with section (3) of the GNU General Public License.
|
// in accordance with section (3) of the GNU General Public License.
|
//
|
//
|
// This exception does not invalidate any other reasons why a work based on
|
// This exception does not invalidate any other reasons why a work based on
|
// this file might be covered by the GNU General Public License.
|
// this file might be covered by the GNU General Public License.
|
//
|
//
|
// -------------------------------------------
|
// -------------------------------------------
|
//####ECOSGPLCOPYRIGHTEND####
|
//####ECOSGPLCOPYRIGHTEND####
|
//==========================================================================
|
//==========================================================================
|
//#####DESCRIPTIONBEGIN####
|
//#####DESCRIPTIONBEGIN####
|
//
|
//
|
// Author(s): nickg@calivar.com
|
// Author(s): nickg@calivar.com
|
// Contributors: nickg@calivar.com
|
// Contributors: nickg@calivar.com
|
// Date: 2003-01-27
|
// Date: 2003-01-27
|
// Description: Simple FPU test. This is not very sophisticated as far
|
// Description: Simple FPU test. This is not very sophisticated as far
|
// as checking FPU performance or accuracy. It is more
|
// as checking FPU performance or accuracy. It is more
|
// concerned with checking that several threads doing FP
|
// concerned with checking that several threads doing FP
|
// operations do not interfere with eachother's use of the
|
// operations do not interfere with eachother's use of the
|
// FPU.
|
// FPU.
|
//
|
//
|
//####DESCRIPTIONEND####
|
//####DESCRIPTIONEND####
|
//==========================================================================
|
//==========================================================================
|
|
|
#include <pkgconf/kernel.h>
|
#include <pkgconf/kernel.h>
|
#include <pkgconf/hal.h>
|
#include <pkgconf/hal.h>
|
|
|
#include <cyg/hal/hal_arch.h>
|
#include <cyg/hal/hal_arch.h>
|
|
|
#include <cyg/kernel/kapi.h>
|
#include <cyg/kernel/kapi.h>
|
|
|
#include <cyg/infra/testcase.h>
|
#include <cyg/infra/testcase.h>
|
#include <cyg/infra/diag.h>
|
#include <cyg/infra/diag.h>
|
|
|
//#include <cyg/kernel/test/stackmon.h>
|
//#include <cyg/kernel/test/stackmon.h>
|
//#include CYGHWR_MEMORY_LAYOUT_H
|
//#include CYGHWR_MEMORY_LAYOUT_H
|
|
|
//==========================================================================
|
//==========================================================================
|
|
|
#if defined(CYGFUN_KERNEL_API_C) && \
|
#if defined(CYGFUN_KERNEL_API_C) && \
|
defined(CYGSEM_KERNEL_SCHED_MLQUEUE) && \
|
defined(CYGSEM_KERNEL_SCHED_MLQUEUE) && \
|
(CYGNUM_KERNEL_SCHED_PRIORITIES > 12)
|
(CYGNUM_KERNEL_SCHED_PRIORITIES > 12)
|
|
|
//==========================================================================
|
//==========================================================================
|
// Base priority for all threads.
|
// Base priority for all threads.
|
|
|
#define BASE_PRI 5
|
#define BASE_PRI 5
|
|
|
//==========================================================================
|
//==========================================================================
|
// Runtime
|
// Runtime
|
//
|
//
|
// This is the number of ticks that the program will run for. 3000
|
// This is the number of ticks that the program will run for. 3000
|
// ticks is equal to 30 seconds in the default configuration. For
|
// ticks is equal to 30 seconds in the default configuration. For
|
// simulators we reduce the run time to 3 simulated seconds.
|
// simulators we reduce the run time to 3 simulated seconds.
|
|
|
#define RUN_TICKS 3000
|
#define RUN_TICKS 3000
|
#define RUN_TICKS_SIM 300
|
#define RUN_TICKS_SIM 300
|
|
|
//==========================================================================
|
//==========================================================================
|
// Thread parameters
|
// Thread parameters
|
|
|
#define STACK_SIZE (CYGNUM_HAL_STACK_SIZE_MINIMUM)
|
#define STACK_SIZE (CYGNUM_HAL_STACK_SIZE_MINIMUM)
|
|
|
static cyg_uint8 stacks[3][STACK_SIZE];
|
static cyg_uint8 stacks[3][STACK_SIZE];
|
static cyg_handle_t thread[3];
|
static cyg_handle_t thread[3];
|
static cyg_thread thread_struct[3];
|
static cyg_thread thread_struct[3];
|
|
|
//==========================================================================
|
//==========================================================================
|
// Alarm parameters.
|
// Alarm parameters.
|
|
|
static cyg_alarm alarm_struct;
|
static cyg_alarm alarm_struct;
|
static cyg_handle_t alarm;
|
static cyg_handle_t alarm;
|
|
|
static cyg_count8 cur_thread = 0;
|
static cyg_count8 cur_thread = 0;
|
static cyg_count32 alarm_ticks = 0;
|
static cyg_count32 alarm_ticks = 0;
|
static cyg_count32 run_ticks = RUN_TICKS;
|
static cyg_count32 run_ticks = RUN_TICKS;
|
|
|
//==========================================================================
|
//==========================================================================
|
|
|
static int errors = 0;
|
static int errors = 0;
|
|
|
//==========================================================================
|
//==========================================================================
|
// Random number generator. Ripped out of the C library.
|
// Random number generator. Ripped out of the C library.
|
|
|
static int rand( unsigned int *seed )
|
static int rand( unsigned int *seed )
|
{
|
{
|
// This is the code supplied in Knuth Vol 2 section 3.6 p.185 bottom
|
// This is the code supplied in Knuth Vol 2 section 3.6 p.185 bottom
|
|
|
#define RAND_MAX 0x7fffffff
|
#define RAND_MAX 0x7fffffff
|
#define MM 2147483647 // a Mersenne prime
|
#define MM 2147483647 // a Mersenne prime
|
#define AA 48271 // this does well in the spectral test
|
#define AA 48271 // this does well in the spectral test
|
#define QQ 44488 // (long)(MM/AA)
|
#define QQ 44488 // (long)(MM/AA)
|
#define RR 3399 // MM % AA; it is important that RR<QQ
|
#define RR 3399 // MM % AA; it is important that RR<QQ
|
|
|
*seed = AA*(*seed % QQ) - RR*(unsigned int)(*seed/QQ);
|
*seed = AA*(*seed % QQ) - RR*(unsigned int)(*seed/QQ);
|
if (*seed < 0)
|
if (*seed < 0)
|
*seed += MM;
|
*seed += MM;
|
|
|
return (int)( *seed & RAND_MAX );
|
return (int)( *seed & RAND_MAX );
|
}
|
}
|
|
|
//==========================================================================
|
//==========================================================================
|
// Test calculation.
|
// Test calculation.
|
//
|
//
|
// Generates an array of random FP values and then repeatedly applies
|
// Generates an array of random FP values and then repeatedly applies
|
// a calculation to them and checks that the same result is reached
|
// a calculation to them and checks that the same result is reached
|
// each time. The calculation, in the macro CALC, is intended to make
|
// each time. The calculation, in the macro CALC, is intended to make
|
// maximum use of the FPU registers. However, the i386 compiler
|
// maximum use of the FPU registers. However, the i386 compiler
|
// doesn't let this expression get very complex before it starts
|
// doesn't let this expression get very complex before it starts
|
// spilling values out to memory.
|
// spilling values out to memory.
|
|
|
static void do_test( double *values,
|
static void do_test( double *values,
|
int count,
|
int count,
|
int loops,
|
int loops,
|
int test,
|
int test,
|
const char *name)
|
const char *name)
|
{
|
{
|
unsigned int i, j;
|
unsigned int i, j;
|
// volatiles necessary to force
|
// volatiles necessary to force
|
// values to 64 bits for comparison
|
// values to 64 bits for comparison
|
volatile double sum = 1.0;
|
volatile double sum = 1.0;
|
volatile double last_sum;
|
volatile double last_sum;
|
unsigned int seed;
|
unsigned int seed;
|
|
|
#define V(__i) (values[(__i)%count])
|
#define V(__i) (values[(__i)%count])
|
#define CALC ((V(i-1)*V(i+1))*(V(i-2)*V(i+2))*(V(i-3)*sum))
|
#define CALC ((V(i-1)*V(i+1))*(V(i-2)*V(i+2))*(V(i-3)*sum))
|
|
|
seed = ((unsigned int)&i)*count;
|
seed = ((unsigned int)&i)*count;
|
|
|
// Set up an array of values...
|
// Set up an array of values...
|
for( i = 0; i < count; i++ )
|
for( i = 0; i < count; i++ )
|
values[i] = (double)rand( &seed )/(double)0x7fffffff;
|
values[i] = (double)rand( &seed )/(double)0x7fffffff;
|
|
|
// Now calculate something from them...
|
// Now calculate something from them...
|
for( i = 0; i < count; i++ )
|
for( i = 0; i < count; i++ )
|
sum += CALC;
|
sum += CALC;
|
last_sum = sum;
|
last_sum = sum;
|
|
|
// Now recalculate the sum in a loop and look for errors
|
// Now recalculate the sum in a loop and look for errors
|
for( j = 0; j < loops ; j++ )
|
for( j = 0; j < loops ; j++ )
|
{
|
{
|
sum = 1.0;
|
sum = 1.0;
|
for( i = 0; i < count; i++ )
|
for( i = 0; i < count; i++ )
|
sum += CALC;
|
sum += CALC;
|
|
|
if( sum != last_sum )
|
if( sum != last_sum )
|
{
|
{
|
errors++;
|
errors++;
|
diag_printf("%s: Sum mismatch! %d sum=[%08x:%08x] last_sum=[%08x:%08x]\n",
|
diag_printf("%s: Sum mismatch! %d sum=[%08x:%08x] last_sum=[%08x:%08x]\n",
|
name,j,
|
name,j,
|
((cyg_uint32 *)&sum)[0],((cyg_uint32 *)&sum)[1],
|
((cyg_uint32 *)&sum)[0],((cyg_uint32 *)&sum)[1],
|
((cyg_uint32 *)&last_sum)[0],((cyg_uint32 *)&last_sum)[1]
|
((cyg_uint32 *)&last_sum)[0],((cyg_uint32 *)&last_sum)[1]
|
);
|
);
|
}
|
}
|
|
|
#if 0
|
#if 0
|
if( ((j*count)%1000000) == 0 )
|
if( ((j*count)%1000000) == 0 )
|
diag_printf("INFO:<%s: %2d calculations done>\n",name,j*count);
|
diag_printf("INFO:<%s: %2d calculations done>\n",name,j*count);
|
#endif
|
#endif
|
}
|
}
|
|
|
}
|
}
|
|
|
//==========================================================================
|
//==========================================================================
|
// Alarm handler
|
// Alarm handler
|
//
|
//
|
// This is called every tick. It lowers the priority of the currently
|
// This is called every tick. It lowers the priority of the currently
|
// running thread and raises the priority of the next. Thus we
|
// running thread and raises the priority of the next. Thus we
|
// implement a form of timelslicing between the threads at one tick
|
// implement a form of timelslicing between the threads at one tick
|
// granularity.
|
// granularity.
|
|
|
static void alarm_fn(cyg_handle_t alarm, cyg_addrword_t data)
|
static void alarm_fn(cyg_handle_t alarm, cyg_addrword_t data)
|
{
|
{
|
alarm_ticks++;
|
alarm_ticks++;
|
|
|
if( alarm_ticks >= run_ticks )
|
if( alarm_ticks >= run_ticks )
|
{
|
{
|
if( errors )
|
if( errors )
|
CYG_TEST_FAIL("Errors detected");
|
CYG_TEST_FAIL("Errors detected");
|
else
|
else
|
CYG_TEST_PASS("OK");
|
CYG_TEST_PASS("OK");
|
|
|
CYG_TEST_FINISH("FP Test done");
|
CYG_TEST_FINISH("FP Test done");
|
}
|
}
|
else
|
else
|
{
|
{
|
cyg_thread_set_priority( thread[cur_thread], BASE_PRI );
|
cyg_thread_set_priority( thread[cur_thread], BASE_PRI );
|
|
|
cur_thread = (cur_thread+1)%3;
|
cur_thread = (cur_thread+1)%3;
|
|
|
cyg_thread_set_priority( thread[cur_thread], BASE_PRI-1 );
|
cyg_thread_set_priority( thread[cur_thread], BASE_PRI-1 );
|
}
|
}
|
}
|
}
|
|
|
|
|
//==========================================================================
|
//==========================================================================
|
|
|
#define FP1_COUNT 1000
|
#define FP1_COUNT 1000
|
|
|
static double fpt1_values[FP1_COUNT];
|
static double fpt1_values[FP1_COUNT];
|
|
|
void fptest1( CYG_ADDRWORD id )
|
void fptest1( CYG_ADDRWORD id )
|
{
|
{
|
while(1)
|
while(1)
|
do_test( fpt1_values, FP1_COUNT, 2000000000, id, "fptest1" );
|
do_test( fpt1_values, FP1_COUNT, 2000000000, id, "fptest1" );
|
}
|
}
|
|
|
//==========================================================================
|
//==========================================================================
|
|
|
#define FP2_COUNT 10000
|
#define FP2_COUNT 10000
|
|
|
static double fpt2_values[FP2_COUNT];
|
static double fpt2_values[FP2_COUNT];
|
|
|
void fptest2( CYG_ADDRWORD id )
|
void fptest2( CYG_ADDRWORD id )
|
{
|
{
|
while(1)
|
while(1)
|
do_test( fpt2_values, FP2_COUNT, 2000000000, id, "fptest2" );
|
do_test( fpt2_values, FP2_COUNT, 2000000000, id, "fptest2" );
|
}
|
}
|
|
|
//==========================================================================
|
//==========================================================================
|
|
|
#define FP3_COUNT 100
|
#define FP3_COUNT 100
|
|
|
static double fpt3_values[FP3_COUNT];
|
static double fpt3_values[FP3_COUNT];
|
|
|
void fptest3( CYG_ADDRWORD id )
|
void fptest3( CYG_ADDRWORD id )
|
{
|
{
|
while(1)
|
while(1)
|
do_test( fpt3_values, FP3_COUNT, 2000000000, id, "fptest3" );
|
do_test( fpt3_values, FP3_COUNT, 2000000000, id, "fptest3" );
|
}
|
}
|
|
|
//==========================================================================
|
//==========================================================================
|
|
|
void fptest_main( void )
|
void fptest_main( void )
|
{
|
{
|
|
|
CYG_TEST_INIT();
|
CYG_TEST_INIT();
|
|
|
if( cyg_test_is_simulator )
|
if( cyg_test_is_simulator )
|
{
|
{
|
run_ticks = RUN_TICKS_SIM;
|
run_ticks = RUN_TICKS_SIM;
|
}
|
}
|
|
|
CYG_TEST_INFO("Run fptest in cyg_start");
|
CYG_TEST_INFO("Run fptest in cyg_start");
|
do_test( fpt3_values, FP3_COUNT, 1000, 0, "start" );
|
do_test( fpt3_values, FP3_COUNT, 1000, 0, "start" );
|
CYG_TEST_INFO( "cyg_start run done");
|
CYG_TEST_INFO( "cyg_start run done");
|
|
|
cyg_thread_create( BASE_PRI-1,
|
cyg_thread_create( BASE_PRI-1,
|
fptest1,
|
fptest1,
|
0,
|
0,
|
"fptest1",
|
"fptest1",
|
&stacks[0][0],
|
&stacks[0][0],
|
STACK_SIZE,
|
STACK_SIZE,
|
&thread[0],
|
&thread[0],
|
&thread_struct[0]);
|
&thread_struct[0]);
|
|
|
cyg_thread_resume( thread[0] );
|
cyg_thread_resume( thread[0] );
|
|
|
cyg_thread_create( BASE_PRI,
|
cyg_thread_create( BASE_PRI,
|
fptest2,
|
fptest2,
|
1,
|
1,
|
"fptest2",
|
"fptest2",
|
&stacks[1][0],
|
&stacks[1][0],
|
STACK_SIZE,
|
STACK_SIZE,
|
&thread[1],
|
&thread[1],
|
&thread_struct[1]);
|
&thread_struct[1]);
|
|
|
cyg_thread_resume( thread[1] );
|
cyg_thread_resume( thread[1] );
|
|
|
cyg_thread_create( BASE_PRI,
|
cyg_thread_create( BASE_PRI,
|
fptest3,
|
fptest3,
|
2,
|
2,
|
"fptest3",
|
"fptest3",
|
&stacks[2][0],
|
&stacks[2][0],
|
STACK_SIZE,
|
STACK_SIZE,
|
&thread[2],
|
&thread[2],
|
&thread_struct[2]);
|
&thread_struct[2]);
|
|
|
cyg_thread_resume( thread[2] );
|
cyg_thread_resume( thread[2] );
|
|
|
cyg_alarm_create( cyg_real_time_clock(),
|
cyg_alarm_create( cyg_real_time_clock(),
|
alarm_fn,
|
alarm_fn,
|
0,
|
0,
|
&alarm,
|
&alarm,
|
&alarm_struct );
|
&alarm_struct );
|
|
|
cyg_alarm_initialize( alarm, cyg_current_time()+1, 1 );
|
cyg_alarm_initialize( alarm, cyg_current_time()+1, 1 );
|
|
|
cyg_scheduler_start();
|
cyg_scheduler_start();
|
|
|
}
|
}
|
|
|
//==========================================================================
|
//==========================================================================
|
|
|
#ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
|
#ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
|
externC void
|
externC void
|
cyg_hal_invoke_constructors();
|
cyg_hal_invoke_constructors();
|
#endif
|
#endif
|
|
|
externC void
|
externC void
|
cyg_start( void )
|
cyg_start( void )
|
{
|
{
|
#ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
|
#ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
|
cyg_hal_invoke_constructors();
|
cyg_hal_invoke_constructors();
|
#endif
|
#endif
|
fptest_main();
|
fptest_main();
|
}
|
}
|
|
|
//==========================================================================
|
//==========================================================================
|
|
|
#else // CYGFUN_KERNEL_API_C...
|
#else // CYGFUN_KERNEL_API_C...
|
|
|
externC void
|
externC void
|
cyg_start( void )
|
cyg_start( void )
|
{
|
{
|
CYG_TEST_INIT();
|
CYG_TEST_INIT();
|
CYG_TEST_NA("FP test requires:\n"
|
CYG_TEST_NA("FP test requires:\n"
|
"CYGFUN_KERNEL_API_C && \n"
|
"CYGFUN_KERNEL_API_C && \n"
|
"CYGSEM_KERNEL_SCHED_MLQUEUE && \n"
|
"CYGSEM_KERNEL_SCHED_MLQUEUE && \n"
|
"(CYGNUM_KERNEL_SCHED_PRIORITIES > 12)\n");
|
"(CYGNUM_KERNEL_SCHED_PRIORITIES > 12)\n");
|
}
|
}
|
|
|
#endif // CYGFUN_KERNEL_API_C, etc.
|
#endif // CYGFUN_KERNEL_API_C, etc.
|
|
|
//==========================================================================
|
//==========================================================================
|
// EOF fptest.cxx
|
// EOF fptest.cxx
|
|
|