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
file annotate diff log raw
nkeynes@185
     1
#include <stdio.h>
nkeynes@185
     2
#include <assert.h>
nkeynes@185
     3
#include <math.h>
nkeynes@185
     4
nkeynes@185
     5
#define write_string(x) printf(x)
nkeynes@185
     6
#define write_int(x) printf( "%08X", x )
nkeynes@185
     7
nkeynes@185
     8
int do_fsca(int angle, float *a, float *b);
nkeynes@185
     9
float do_fsrra( float param );
nkeynes@185
    10
float do_fipr( float *vectora, float *vectorb );
nkeynes@185
    11
float do_fipr2( float *vectora, float *vectorb );
nkeynes@185
    12
void do_ftrv( float *matrix, float *vector );
nkeynes@185
    13
void do_ftrv2( float *matrix, float *vector );
nkeynes@185
    14
nkeynes@185
    15
unsigned int get_fpscr();
nkeynes@185
    16
void set_fpscr( unsigned int fpscr );
nkeynes@185
    17
nkeynes@185
    18
#define MAX_FLOAT_DIFF 0.00001
nkeynes@185
    19
nkeynes@185
    20
nkeynes@185
    21
int compare_float( float a, float b )
nkeynes@185
    22
{
nkeynes@185
    23
    if( a == b )
nkeynes@185
    24
	return 1;
nkeynes@185
    25
    float diff = (a>b?(a-b):(b-a));
nkeynes@185
    26
    return diff < MAX_FLOAT_DIFF;
nkeynes@185
    27
}
nkeynes@185
    28
nkeynes@185
    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; }
nkeynes@185
    30
nkeynes@185
    31
int test_fsca( int angle, float expect_a, float expect_b )
nkeynes@185
    32
{
nkeynes@185
    33
    float a = 10.0;
nkeynes@185
    34
    float b = 10.0;
nkeynes@185
    35
    do_fsca(angle, &a, &b );
nkeynes@185
    36
    
nkeynes@185
    37
    TEST_FEQUALS(expect_a, a);
nkeynes@185
    38
    TEST_FEQUALS(expect_b, b);
nkeynes@185
    39
}
nkeynes@185
    40
nkeynes@185
    41
int test_fsrra( float f, float expect )
nkeynes@185
    42
{
nkeynes@185
    43
    float f2 = do_fsrra( f );
nkeynes@185
    44
    TEST_FEQUALS( expect, f2 );
nkeynes@185
    45
}
nkeynes@185
    46
nkeynes@185
    47
void test_coercion( float f )
nkeynes@185
    48
{
nkeynes@185
    49
    double d = (double)f;
nkeynes@185
    50
    float q = (float)d;
nkeynes@185
    51
    unsigned int i = (unsigned int)d;
nkeynes@185
    52
    signed int j = (signed int)q;
nkeynes@185
    53
nkeynes@185
    54
    printf( "Coerce: %08X %08X%08X %08X %08X %08X\n", *((unsigned int *)&f),
nkeynes@185
    55
    	    *((unsigned int *)&d),  *(((unsigned int *)&d)+1),
nkeynes@185
    56
    	    *((unsigned int *)&q), i, j );
nkeynes@185
    57
}
nkeynes@185
    58
nkeynes@185
    59
void test_doublearr( int len )
nkeynes@185
    60
{
nkeynes@185
    61
    double arr[len];
nkeynes@185
    62
    unsigned int *iarr = (unsigned int *)&arr;
nkeynes@185
    63
    int i;
nkeynes@185
    64
    arr[0] = 2.5;
nkeynes@185
    65
    for( i=1; i<len; i++ ) {
nkeynes@185
    66
	arr[i] = (arr[i-1] * arr[i-1] + arr[0]) / 1.25 - 0.19;
nkeynes@185
    67
    }
nkeynes@185
    68
nkeynes@185
    69
    printf( "arr: " );
nkeynes@185
    70
    for( i=0; i<len; i++ ) {
nkeynes@185
    71
	printf( "%08X", *iarr++ );
nkeynes@185
    72
	printf( "%08X ", *iarr++ );
nkeynes@185
    73
    }
nkeynes@185
    74
    printf( "\n" );
nkeynes@185
    75
}
nkeynes@185
    76
nkeynes@185
    77
void test_floatarr( int len )
nkeynes@185
    78
{
nkeynes@185
    79
    float arr[len];
nkeynes@185
    80
    unsigned int *iarr = (unsigned int *)&arr;
nkeynes@185
    81
    int i;
nkeynes@185
    82
    arr[0] = 2.5;
nkeynes@185
    83
    for( i=1; i<len; i++ ) {
nkeynes@185
    84
	arr[i] = (arr[i-1] * arr[i-1] + arr[0]) / 1.25 - 0.19;
nkeynes@185
    85
    }
nkeynes@185
    86
nkeynes@185
    87
    write_string( "arr: " );
nkeynes@185
    88
    for( i=0; i<len; i++ ) {
nkeynes@185
    89
	write_int( *iarr++ );
nkeynes@185
    90
	write_string( " " );
nkeynes@185
    91
    }
nkeynes@185
    92
    write_string( "\n" );
nkeynes@185
    93
}
nkeynes@185
    94
nkeynes@185
    95
float __attribute__((aligned(8))) matrix[16] = { 1.26, 2.34, 5.67, 3.497, -1.23, 43.43, -45.68, 9.12, 
nkeynes@185
    96
			 12.1, 34.301, -297.354, 0.05, 0.123, 23.34, 9.99, 33.321 };
nkeynes@185
    97
float __attribute__((aligned(8))) vec1[4] = { 5.65, 9.98, -34.12, 0.043 };
nkeynes@185
    98
float __attribute__((aligned(8))) vec2[4] = { 3.45, -123, 4.54, 98.0909 };
nkeynes@185
    99
float __attribute__((aligned(8))) vec3[4] = { 1.25, 2.25, 3.75, 5.12 };
nkeynes@185
   100
float __attribute__((aligned(8))) vec4[4] = {-0.25, 8.9, -2.3, 9.19 };
nkeynes@185
   101
nkeynes@185
   102
void test_fipr( )
nkeynes@185
   103
{
nkeynes@185
   104
    float r = do_fipr( vec3, vec4 );
nkeynes@185
   105
    write_string( "fipr: " );
nkeynes@185
   106
    write_int( *(unsigned int *)&r );
nkeynes@185
   107
    write_string( "\n" );
nkeynes@185
   108
    
nkeynes@185
   109
    r = do_fipr2( vec4, vec3 );
nkeynes@185
   110
    write_string( "fipr: " );
nkeynes@185
   111
    write_int( *(unsigned int *)&r );
nkeynes@185
   112
    write_string( "\n" );
nkeynes@185
   113
    
nkeynes@185
   114
}
nkeynes@185
   115
nkeynes@185
   116
void test_ftrv( )
nkeynes@185
   117
{
nkeynes@185
   118
    
nkeynes@185
   119
    do_ftrv( matrix, vec1 );
nkeynes@185
   120
    write_string( "ftrv: " );
nkeynes@185
   121
    write_int( *(unsigned int *)&vec1[0] );
nkeynes@185
   122
    write_int( *(unsigned int *)&vec1[1] );
nkeynes@185
   123
    write_int( *(unsigned int *)&vec1[2] );
nkeynes@185
   124
    write_int( *(unsigned int *)&vec1[3] );
nkeynes@185
   125
    write_string( "\n" );
nkeynes@185
   126
    
nkeynes@185
   127
    do_ftrv2( matrix, vec2 );
nkeynes@185
   128
    
nkeynes@185
   129
    write_string( "ftrv: " );
nkeynes@185
   130
    write_int( *(unsigned int *)&vec2[0] );
nkeynes@185
   131
    write_int( *(unsigned int *)&vec2[1] );
nkeynes@185
   132
    write_int( *(unsigned int *)&vec2[2] );
nkeynes@185
   133
    write_int( *(unsigned int *)&vec2[3] );
nkeynes@185
   134
    write_string( "\n" );
nkeynes@185
   135
    
nkeynes@185
   136
}
nkeynes@185
   137
    
nkeynes@185
   138
nkeynes@185
   139
int main()
nkeynes@185
   140
{
nkeynes@185
   141
	write_int( get_fpscr() );
nkeynes@185
   142
	write_string( "\n" );
nkeynes@185
   143
	test_fsca( 0x00000000, 0, 1 );
nkeynes@185
   144
	test_fsca( 0x00011234, 0.43205646, 0.90184659 );
nkeynes@185
   145
	test_fsca( 0xFEDCBA98, -0.99121743, -0.13224234 );
nkeynes@185
   146
	test_fsrra( 25.0, 0.2 );
nkeynes@185
   147
	test_fsrra( 0.05, 4.4721361 );
nkeynes@185
   148
	test_fsrra( -12.345, nanf() );
nkeynes@185
   149
	test_coercion( -3.1415926 );
nkeynes@185
   150
	test_coercion( 123456789012346567.890 );
nkeynes@185
   151
	test_coercion( -2147483648.0 );
nkeynes@185
   152
	test_coercion( 5234.1234 );
nkeynes@185
   153
	test_doublearr( 5 );
nkeynes@185
   154
	test_floatarr( 5 );
nkeynes@185
   155
	test_fipr();
nkeynes@185
   156
	test_ftrv();
nkeynes@185
   157
	return 1;
nkeynes@185
   158
}
nkeynes@185
   159
	
.