Search
lxdream.org :: lxdream/test/testmath.c
lxdream 0.9.1
released Jun 29
Download Now
filename test/testmath.c
changeset 185:6755a04c447f
next192:580d6c4d7802
author nkeynes
date Tue Jul 11 01:35:27 2006 +0000 (16 years ago)
permissions -rw-r--r--
last change First commit of system test framework. 3 initial test cases (incomplete):
testide, testmath, and testta
view annotate diff log raw
     1 #include <stdio.h>
     2 #include <assert.h>
     3 #include <math.h>
     5 #define write_string(x) printf(x)
     6 #define write_int(x) printf( "%08X", x )
     8 int do_fsca(int angle, float *a, float *b);
     9 float do_fsrra( float param );
    10 float do_fipr( float *vectora, float *vectorb );
    11 float do_fipr2( float *vectora, float *vectorb );
    12 void do_ftrv( float *matrix, float *vector );
    13 void do_ftrv2( float *matrix, float *vector );
    15 unsigned int get_fpscr();
    16 void set_fpscr( unsigned int fpscr );
    18 #define MAX_FLOAT_DIFF 0.00001
    21 int compare_float( float a, float b )
    22 {
    23     if( a == b )
    24 	return 1;
    25     float diff = (a>b?(a-b):(b-a));
    26     return diff < MAX_FLOAT_DIFF;
    27 }
    29 #define TEST_FEQUALS( a, b ) if( !compare_float(a,b) ) { printf( "Assertion failed at %s.%d: expected %.8f but was %.8f\n", __FILE__, __LINE__, a, b ); return 1; }
    31 int test_fsca( int angle, float expect_a, float expect_b )
    32 {
    33     float a = 10.0;
    34     float b = 10.0;
    35     do_fsca(angle, &a, &b );
    37     TEST_FEQUALS(expect_a, a);
    38     TEST_FEQUALS(expect_b, b);
    39 }
    41 int test_fsrra( float f, float expect )
    42 {
    43     float f2 = do_fsrra( f );
    44     TEST_FEQUALS( expect, f2 );
    45 }
    47 void test_coercion( float f )
    48 {
    49     double d = (double)f;
    50     float q = (float)d;
    51     unsigned int i = (unsigned int)d;
    52     signed int j = (signed int)q;
    54     printf( "Coerce: %08X %08X%08X %08X %08X %08X\n", *((unsigned int *)&f),
    55     	    *((unsigned int *)&d),  *(((unsigned int *)&d)+1),
    56     	    *((unsigned int *)&q), i, j );
    57 }
    59 void test_doublearr( int len )
    60 {
    61     double arr[len];
    62     unsigned int *iarr = (unsigned int *)&arr;
    63     int i;
    64     arr[0] = 2.5;
    65     for( i=1; i<len; i++ ) {
    66 	arr[i] = (arr[i-1] * arr[i-1] + arr[0]) / 1.25 - 0.19;
    67     }
    69     printf( "arr: " );
    70     for( i=0; i<len; i++ ) {
    71 	printf( "%08X", *iarr++ );
    72 	printf( "%08X ", *iarr++ );
    73     }
    74     printf( "\n" );
    75 }
    77 void test_floatarr( int len )
    78 {
    79     float arr[len];
    80     unsigned int *iarr = (unsigned int *)&arr;
    81     int i;
    82     arr[0] = 2.5;
    83     for( i=1; i<len; i++ ) {
    84 	arr[i] = (arr[i-1] * arr[i-1] + arr[0]) / 1.25 - 0.19;
    85     }
    87     write_string( "arr: " );
    88     for( i=0; i<len; i++ ) {
    89 	write_int( *iarr++ );
    90 	write_string( " " );
    91     }
    92     write_string( "\n" );
    93 }
    95 float __attribute__((aligned(8))) matrix[16] = { 1.26, 2.34, 5.67, 3.497, -1.23, 43.43, -45.68, 9.12, 
    96 			 12.1, 34.301, -297.354, 0.05, 0.123, 23.34, 9.99, 33.321 };
    97 float __attribute__((aligned(8))) vec1[4] = { 5.65, 9.98, -34.12, 0.043 };
    98 float __attribute__((aligned(8))) vec2[4] = { 3.45, -123, 4.54, 98.0909 };
    99 float __attribute__((aligned(8))) vec3[4] = { 1.25, 2.25, 3.75, 5.12 };
   100 float __attribute__((aligned(8))) vec4[4] = {-0.25, 8.9, -2.3, 9.19 };
   102 void test_fipr( )
   103 {
   104     float r = do_fipr( vec3, vec4 );
   105     write_string( "fipr: " );
   106     write_int( *(unsigned int *)&r );
   107     write_string( "\n" );
   109     r = do_fipr2( vec4, vec3 );
   110     write_string( "fipr: " );
   111     write_int( *(unsigned int *)&r );
   112     write_string( "\n" );
   114 }
   116 void test_ftrv( )
   117 {
   119     do_ftrv( matrix, vec1 );
   120     write_string( "ftrv: " );
   121     write_int( *(unsigned int *)&vec1[0] );
   122     write_int( *(unsigned int *)&vec1[1] );
   123     write_int( *(unsigned int *)&vec1[2] );
   124     write_int( *(unsigned int *)&vec1[3] );
   125     write_string( "\n" );
   127     do_ftrv2( matrix, vec2 );
   129     write_string( "ftrv: " );
   130     write_int( *(unsigned int *)&vec2[0] );
   131     write_int( *(unsigned int *)&vec2[1] );
   132     write_int( *(unsigned int *)&vec2[2] );
   133     write_int( *(unsigned int *)&vec2[3] );
   134     write_string( "\n" );
   136 }
   139 int main()
   140 {
   141 	write_int( get_fpscr() );
   142 	write_string( "\n" );
   143 	test_fsca( 0x00000000, 0, 1 );
   144 	test_fsca( 0x00011234, 0.43205646, 0.90184659 );
   145 	test_fsca( 0xFEDCBA98, -0.99121743, -0.13224234 );
   146 	test_fsrra( 25.0, 0.2 );
   147 	test_fsrra( 0.05, 4.4721361 );
   148 	test_fsrra( -12.345, nanf() );
   149 	test_coercion( -3.1415926 );
   150 	test_coercion( 123456789012346567.890 );
   151 	test_coercion( -2147483648.0 );
   152 	test_coercion( 5234.1234 );
   153 	test_doublearr( 5 );
   154 	test_floatarr( 5 );
   155 	test_fipr();
   156 	test_ftrv();
   157 	return 1;
   158 }
.