Search
lxdream.org :: lxdream/test/testmath.c
lxdream 0.9.1
released Jun 29
Download Now
filename test/testmath.c
changeset 192:580d6c4d7802
prev185:6755a04c447f
author nkeynes
date Sat Apr 19 02:30:04 2008 +0000 (16 years ago)
permissions -rw-r--r--
last change Change secondary colour to 3-components - makes osx happy and is probably more correct anyway
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
    20 static inline float inttof( unsigned int i )
    21 {
    22     return *((float *)&i);
    23 }
    25 static inline double longtod( unsigned long long l )
    26 {
    27     return *((double *)&l);
    28 }
    30 static inline unsigned int ftoint( float f )
    31 {
    32     return *((unsigned int *)&f);
    33 }
    35 static inline unsigned long long dtolong( double d )
    36 {
    37     return *((unsigned long long *)&d);
    38 }
    40 #define QNANF inttof(0x7FBFFFFF)
    41 #define QNAND longtod(0x7FF7FFFFFFFFFFFFLL)
    43 int compare_float( float a, float b )
    44 {
    45     if( ftoint(a) == ftoint(b) ) /* Exact bit-pattern match */
    46 	return 1;
    47     float diff = (a>b?(a-b):(b-a));
    48     return diff < MAX_FLOAT_DIFF;
    49 }
    51 #define FTOINT(a) (*((unsigned int *)&a))
    53 #define TEST_FEQUALS( a, b ) if( !compare_float(a,b) ) { printf( "Assertion failed at %s.%d: expected %.8f (%08X) but was %.8f (%08X)\n", __FILE__, __LINE__, a, FTOINT(a), b, FTOINT(b) ); return 1; }
    55 int test_fsca( int angle, float expect_a, float expect_b )
    56 {
    57     float a = 10.0;
    58     float b = 10.0;
    59     do_fsca(angle, &a, &b );
    61     TEST_FEQUALS(expect_a, a);
    62     TEST_FEQUALS(expect_b, b);
    63 }
    65 int test_fsrra( float f, float expect )
    66 {
    67     float f2 = do_fsrra( f );
    68     TEST_FEQUALS( expect, f2 );
    69 }
    71 void test_coercion( float f )
    72 {
    73     double d = (double)f;
    74     float q = (float)d;
    75     unsigned int i = (unsigned int)d;
    76     signed int j = (signed int)q;
    78     printf( "Coerce: %08X %08X%08X %08X %08X %08X\n", *((unsigned int *)&f),
    79     	    *((unsigned int *)&d),  *(((unsigned int *)&d)+1),
    80     	    *((unsigned int *)&q), i, j );
    81 }
    83 void test_doublearr( int len )
    84 {
    85     double arr[len];
    86     unsigned int *iarr = (unsigned int *)&arr;
    87     int i;
    88     arr[0] = 2.5;
    89     for( i=1; i<len; i++ ) {
    90 	arr[i] = (arr[i-1] * arr[i-1] + arr[0]) / 1.25 - 0.19;
    91     }
    93     printf( "arr: " );
    94     for( i=0; i<len; i++ ) {
    95 	printf( "%08X", *iarr++ );
    96 	printf( "%08X ", *iarr++ );
    97     }
    98     printf( "\n" );
    99 }
   101 void test_floatarr( int len )
   102 {
   103     float arr[len];
   104     unsigned int *iarr = (unsigned int *)&arr;
   105     int i;
   106     arr[0] = 2.5;
   107     for( i=1; i<len; i++ ) {
   108 	arr[i] = (arr[i-1] * arr[i-1] + arr[0]) / 1.25 - 0.19;
   109     }
   111     write_string( "arr: " );
   112     for( i=0; i<len; i++ ) {
   113 	write_int( *iarr++ );
   114 	write_string( " " );
   115     }
   116     write_string( "\n" );
   117 }
   119 float __attribute__((aligned(8))) matrix[16] = { 1.26, 2.34, 5.67, 3.497, -1.23, 43.43, -45.68, 9.12, 
   120 			 12.1, 34.301, -297.354, 0.05, 0.123, 23.34, 9.99, 33.321 };
   121 float __attribute__((aligned(8))) vec1[4] = { 5.65, 9.98, -34.12, 0.043 };
   122 float __attribute__((aligned(8))) vec2[4] = { 3.45, -123, 4.54, 98.0909 };
   123 float __attribute__((aligned(8))) vec3[4] = { 1.25, 2.25, 3.75, 5.12 };
   124 float __attribute__((aligned(8))) vec4[4] = {-0.25, 8.9, -2.3, 9.19 };
   126 void test_fipr( )
   127 {
   128     float r = do_fipr( vec3, vec4 );
   129     write_string( "fipr: " );
   130     write_int( *(unsigned int *)&r );
   131     write_string( "\n" );
   133     r = do_fipr2( vec4, vec3 );
   134     write_string( "fipr: " );
   135     write_int( *(unsigned int *)&r );
   136     write_string( "\n" );
   138 }
   140 void test_ftrv( )
   141 {
   143     do_ftrv( matrix, vec1 );
   144     write_string( "ftrv: " );
   145     write_int( *(unsigned int *)&vec1[0] );
   146     write_int( *(unsigned int *)&vec1[1] );
   147     write_int( *(unsigned int *)&vec1[2] );
   148     write_int( *(unsigned int *)&vec1[3] );
   149     write_string( "\n" );
   151     do_ftrv2( matrix, vec2 );
   153     write_string( "ftrv: " );
   154     write_int( *(unsigned int *)&vec2[0] );
   155     write_int( *(unsigned int *)&vec2[1] );
   156     write_int( *(unsigned int *)&vec2[2] );
   157     write_int( *(unsigned int *)&vec2[3] );
   158     write_string( "\n" );
   160 }
   163 int main()
   164 {
   165     fprintf( stderr, "LL: %d\n", sizeof(unsigned long long) );
   166     write_int( get_fpscr() );
   167     write_string( "\n" );
   168     test_fsca( 0x00000000, 0, 1 );
   169     test_fsca( 0x00011234, 0.43205646, 0.90184659 );
   170     test_fsca( 0xFEDCBA98, -0.99121743, -0.13224234 );
   171     test_fsrra( 25.0, 0.2 );
   172     test_fsrra( 0.05, 4.4721361 );
   173     test_fsrra( -12.345, QNANF );
   174     test_coercion( -3.1415926 );
   175     test_coercion( 123456789012346567.890 );
   176     test_coercion( -2147483648.0 );
   177     test_coercion( 5234.1234 );
   178     test_doublearr( 5 );
   179     test_floatarr( 5 );
   180     test_fipr();
   181     test_ftrv();
   182     return 1;
   183 }
.