Search
lxdream.org :: lxdream/test/testmath.c :: diff
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
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/test/testmath.c Tue Jul 11 01:35:27 2006 +0000
1.3 @@ -0,0 +1,159 @@
1.4 +#include <stdio.h>
1.5 +#include <assert.h>
1.6 +#include <math.h>
1.7 +
1.8 +#define write_string(x) printf(x)
1.9 +#define write_int(x) printf( "%08X", x )
1.10 +
1.11 +int do_fsca(int angle, float *a, float *b);
1.12 +float do_fsrra( float param );
1.13 +float do_fipr( float *vectora, float *vectorb );
1.14 +float do_fipr2( float *vectora, float *vectorb );
1.15 +void do_ftrv( float *matrix, float *vector );
1.16 +void do_ftrv2( float *matrix, float *vector );
1.17 +
1.18 +unsigned int get_fpscr();
1.19 +void set_fpscr( unsigned int fpscr );
1.20 +
1.21 +#define MAX_FLOAT_DIFF 0.00001
1.22 +
1.23 +
1.24 +int compare_float( float a, float b )
1.25 +{
1.26 + if( a == b )
1.27 + return 1;
1.28 + float diff = (a>b?(a-b):(b-a));
1.29 + return diff < MAX_FLOAT_DIFF;
1.30 +}
1.31 +
1.32 +#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; }
1.33 +
1.34 +int test_fsca( int angle, float expect_a, float expect_b )
1.35 +{
1.36 + float a = 10.0;
1.37 + float b = 10.0;
1.38 + do_fsca(angle, &a, &b );
1.39 +
1.40 + TEST_FEQUALS(expect_a, a);
1.41 + TEST_FEQUALS(expect_b, b);
1.42 +}
1.43 +
1.44 +int test_fsrra( float f, float expect )
1.45 +{
1.46 + float f2 = do_fsrra( f );
1.47 + TEST_FEQUALS( expect, f2 );
1.48 +}
1.49 +
1.50 +void test_coercion( float f )
1.51 +{
1.52 + double d = (double)f;
1.53 + float q = (float)d;
1.54 + unsigned int i = (unsigned int)d;
1.55 + signed int j = (signed int)q;
1.56 +
1.57 + printf( "Coerce: %08X %08X%08X %08X %08X %08X\n", *((unsigned int *)&f),
1.58 + *((unsigned int *)&d), *(((unsigned int *)&d)+1),
1.59 + *((unsigned int *)&q), i, j );
1.60 +}
1.61 +
1.62 +void test_doublearr( int len )
1.63 +{
1.64 + double arr[len];
1.65 + unsigned int *iarr = (unsigned int *)&arr;
1.66 + int i;
1.67 + arr[0] = 2.5;
1.68 + for( i=1; i<len; i++ ) {
1.69 + arr[i] = (arr[i-1] * arr[i-1] + arr[0]) / 1.25 - 0.19;
1.70 + }
1.71 +
1.72 + printf( "arr: " );
1.73 + for( i=0; i<len; i++ ) {
1.74 + printf( "%08X", *iarr++ );
1.75 + printf( "%08X ", *iarr++ );
1.76 + }
1.77 + printf( "\n" );
1.78 +}
1.79 +
1.80 +void test_floatarr( int len )
1.81 +{
1.82 + float arr[len];
1.83 + unsigned int *iarr = (unsigned int *)&arr;
1.84 + int i;
1.85 + arr[0] = 2.5;
1.86 + for( i=1; i<len; i++ ) {
1.87 + arr[i] = (arr[i-1] * arr[i-1] + arr[0]) / 1.25 - 0.19;
1.88 + }
1.89 +
1.90 + write_string( "arr: " );
1.91 + for( i=0; i<len; i++ ) {
1.92 + write_int( *iarr++ );
1.93 + write_string( " " );
1.94 + }
1.95 + write_string( "\n" );
1.96 +}
1.97 +
1.98 +float __attribute__((aligned(8))) matrix[16] = { 1.26, 2.34, 5.67, 3.497, -1.23, 43.43, -45.68, 9.12,
1.99 + 12.1, 34.301, -297.354, 0.05, 0.123, 23.34, 9.99, 33.321 };
1.100 +float __attribute__((aligned(8))) vec1[4] = { 5.65, 9.98, -34.12, 0.043 };
1.101 +float __attribute__((aligned(8))) vec2[4] = { 3.45, -123, 4.54, 98.0909 };
1.102 +float __attribute__((aligned(8))) vec3[4] = { 1.25, 2.25, 3.75, 5.12 };
1.103 +float __attribute__((aligned(8))) vec4[4] = {-0.25, 8.9, -2.3, 9.19 };
1.104 +
1.105 +void test_fipr( )
1.106 +{
1.107 + float r = do_fipr( vec3, vec4 );
1.108 + write_string( "fipr: " );
1.109 + write_int( *(unsigned int *)&r );
1.110 + write_string( "\n" );
1.111 +
1.112 + r = do_fipr2( vec4, vec3 );
1.113 + write_string( "fipr: " );
1.114 + write_int( *(unsigned int *)&r );
1.115 + write_string( "\n" );
1.116 +
1.117 +}
1.118 +
1.119 +void test_ftrv( )
1.120 +{
1.121 +
1.122 + do_ftrv( matrix, vec1 );
1.123 + write_string( "ftrv: " );
1.124 + write_int( *(unsigned int *)&vec1[0] );
1.125 + write_int( *(unsigned int *)&vec1[1] );
1.126 + write_int( *(unsigned int *)&vec1[2] );
1.127 + write_int( *(unsigned int *)&vec1[3] );
1.128 + write_string( "\n" );
1.129 +
1.130 + do_ftrv2( matrix, vec2 );
1.131 +
1.132 + write_string( "ftrv: " );
1.133 + write_int( *(unsigned int *)&vec2[0] );
1.134 + write_int( *(unsigned int *)&vec2[1] );
1.135 + write_int( *(unsigned int *)&vec2[2] );
1.136 + write_int( *(unsigned int *)&vec2[3] );
1.137 + write_string( "\n" );
1.138 +
1.139 +}
1.140 +
1.141 +
1.142 +int main()
1.143 +{
1.144 + write_int( get_fpscr() );
1.145 + write_string( "\n" );
1.146 + test_fsca( 0x00000000, 0, 1 );
1.147 + test_fsca( 0x00011234, 0.43205646, 0.90184659 );
1.148 + test_fsca( 0xFEDCBA98, -0.99121743, -0.13224234 );
1.149 + test_fsrra( 25.0, 0.2 );
1.150 + test_fsrra( 0.05, 4.4721361 );
1.151 + test_fsrra( -12.345, nanf() );
1.152 + test_coercion( -3.1415926 );
1.153 + test_coercion( 123456789012346567.890 );
1.154 + test_coercion( -2147483648.0 );
1.155 + test_coercion( 5234.1234 );
1.156 + test_doublearr( 5 );
1.157 + test_floatarr( 5 );
1.158 + test_fipr();
1.159 + test_ftrv();
1.160 + return 1;
1.161 +}
1.162 +
.