Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Test / Source / Tests / Test_3x3transpose.cpp
1 //
2 //  Test_3x3transpose.cpp
3 //  BulletTest
4 //
5 //  Copyright (c) 2011 Apple Inc.
6 //
7
8
9 #include "LinearMath/btScalar.h"
10 #if defined (BT_USE_SSE_IN_API) || defined (BT_USE_NEON)
11
12
13 #include "Test_3x3transpose.h"
14 #include "vector.h"
15 #include "Utils.h"
16 #include "main.h"
17 #include <math.h>
18 #include <string.h>
19
20 #include <LinearMath/btMatrix3x3.h>
21
22 #define LOOPCOUNT 1000
23 #define ARRAY_SIZE 1024 
24
25 static inline btSimdFloat4 rand_f4(void)
26 {
27     return btAssign128( RANDF, RANDF, RANDF, BT_NAN );      // w channel NaN
28 }
29
30 static btMatrix3x3 Transpose( btMatrix3x3 &in )
31 {
32     btVector3 row0 = in.getRow(0);
33     btVector3 row1 = in.getRow(1);
34     btVector3 row2 = in.getRow(2);
35         btVector3 col0 = btAssign128(row0.x(), row1.x(), row2.x(), 0 );
36         btVector3 col1 = btAssign128(row0.y(), row1.y(), row2.y(), 0 );
37         btVector3 col2 = btAssign128(row0.z(), row1.z(), row2.z(), 0);
38         return btMatrix3x3( col0, col1, col2);
39 }
40
41 static int operator!= ( const btMatrix3x3 &a, const btMatrix3x3 &b )
42 {
43     if( a.getRow(0) != b.getRow(0) )
44         return 1;
45     if( a.getRow(1) != b.getRow(1) )
46         return 1;
47     if( a.getRow(2) != b.getRow(2) )
48         return 1;
49     return 0;
50 }
51
52 int Test_3x3transpose(void)
53 {
54     // Init an array flanked by guard pages
55     btMatrix3x3 in[ARRAY_SIZE];
56     btMatrix3x3 out[ARRAY_SIZE];
57     btMatrix3x3 out2[ARRAY_SIZE];
58     
59     // Init the data
60     size_t i, j;
61     for( i = 0; i < ARRAY_SIZE; i++ )
62     {
63         in[i] = btMatrix3x3(rand_f4(), rand_f4(), rand_f4() );   
64      
65         out[i] = Transpose(in[i]);
66         out2[i] = in[i].transpose();
67         
68         if( out[i] != out2[i] )
69         {
70             printf( "failure @ %ld\n", i);
71             return -1;
72         }
73     }
74
75     uint64_t scalarTime, vectorTime;
76     uint64_t startTime, bestTime, currentTime;
77     bestTime = -1LL;
78     scalarTime = 0;
79     for (j = 0; j < LOOPCOUNT; j++) {
80         startTime = ReadTicks();
81         for( i = 0; i < ARRAY_SIZE; i++ )
82             out[i] = Transpose(in[i]);
83         currentTime = ReadTicks() - startTime;
84         scalarTime += currentTime;
85         if( currentTime < bestTime )
86             bestTime = currentTime;
87     }
88     if( 0 == gReportAverageTimes )
89         scalarTime = bestTime;        
90     else
91         scalarTime /= LOOPCOUNT;
92
93     bestTime = -1LL;
94     vectorTime = 0;
95     for (j = 0; j < LOOPCOUNT; j++) {
96         startTime = ReadTicks();
97         for( i = 0; i < ARRAY_SIZE; i++ )
98             out[i] = in[i].transpose();
99         currentTime = ReadTicks() - startTime;
100         vectorTime += currentTime;
101         if( currentTime < bestTime )
102             bestTime = currentTime;
103     }
104     if( 0 == gReportAverageTimes )
105         vectorTime = bestTime;        
106     else
107         vectorTime /= LOOPCOUNT;
108         
109     vlog( "Timing:\n" );
110     vlog( "\t    scalar\t    vector\n" );
111     vlog( "\t%10.2f\t%10.2f\n", TicksToCycles( scalarTime ) / ARRAY_SIZE, TicksToCycles( vectorTime ) / ARRAY_SIZE );
112         
113     return 0;
114 }
115 #endif //BT_USE_SSE
116