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