Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Test / Source / Tests / Test_3x3mulMV.cpp
1 //
2 //  Test_3x3mulMV.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_3x3mulMV.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_01, RANDF_01, RANDF_01, BT_NAN );      // w channel NaN
29 }
30
31 static btVector3 M3x3mulMV_ref( const btMatrix3x3 &m, const btVector3 &v )
32 {
33         return btVector3(m[0].dot(v), m[1].dot(v), m[2].dot(v));
34 }
35
36 int Test_3x3mulMV(void)
37 {
38     // Init an array flanked by guard pages
39     btMatrix3x3 in1[ARRAY_SIZE];
40     btVector3   in2[ARRAY_SIZE];
41     btVector3   out[ARRAY_SIZE];
42     btVector3   out2[ARRAY_SIZE];
43     
44     // Init the data
45     size_t i, j;
46     for( i = 0; i < ARRAY_SIZE; i++ )
47     {
48         in1[i] = btMatrix3x3(rand_f4(), rand_f4(), rand_f4() );   
49         in2[i] = btVector3(rand_f4());   
50         
51         out[i] = M3x3mulMV_ref(in1[i], in2[i]);
52         out2[i] = (in1[i] * in2[i]);
53
54                 if( fabsf(out[i].m_floats[0] - out2[i].m_floats[0]) + 
55                         fabsf(out[i].m_floats[1] - out2[i].m_floats[1]) +
56                         fabsf(out[i].m_floats[2] - out2[i].m_floats[2]) +
57                         fabsf(out[i].m_floats[3] - out2[i].m_floats[3]) > FLT_EPSILON*4 )
58                 {       
59                         vlog( "Error - M3x3mulMV result error! ");
60             vlog( "failure @ %ld\n", i);
61                         vlog(   "\ncorrect = (%10.4f, %10.4f, %10.4f, %10.4f) "
62                                         "\ntested  = (%10.4f, %10.4f, %10.4f, %10.4f) \n", 
63                                         out[i].m_floats[0], out[i].m_floats[1], out[i].m_floats[2], out[i].m_floats[3], 
64                                         out2[i].m_floats[0], out2[i].m_floats[1], out2[i].m_floats[2], out2[i].m_floats[3]);
65                 
66                         return 1;
67                 }
68     }
69     
70     uint64_t scalarTime, vectorTime;
71     uint64_t startTime, bestTime, currentTime;
72     bestTime = -1LL;
73     scalarTime = 0;
74     for (j = 0; j < LOOPCOUNT; j++) 
75     {
76         startTime = ReadTicks();
77         for( i = 0; i < ARRAY_SIZE; i++ )
78             out[i] = M3x3mulMV_ref(in1[i], in2[i]);
79         currentTime = ReadTicks() - startTime;
80         scalarTime += currentTime;
81         if( currentTime < bestTime )
82             bestTime = currentTime;
83     }
84     if( 0 == gReportAverageTimes )
85         scalarTime = bestTime;        
86     else
87         scalarTime /= LOOPCOUNT;
88     
89     bestTime = -1LL;
90     vectorTime = 0;
91     for (j = 0; j < LOOPCOUNT; j++) 
92     {
93         startTime = ReadTicks();
94         for( i = 0; i < ARRAY_SIZE; i++ )
95             out2[i] = (in1[i] * in2[i]);
96         currentTime = ReadTicks() - startTime;
97         vectorTime += currentTime;
98         if( currentTime < bestTime )
99             bestTime = currentTime;
100     }
101     if( 0 == gReportAverageTimes )
102         vectorTime = bestTime;        
103     else
104         vectorTime /= LOOPCOUNT;
105     
106     vlog( "Timing:\n" );
107     vlog( "\t    scalar\t    vector\n" );
108     vlog( "\t%10.2f\t%10.2f\n", TicksToCycles( scalarTime ) / ARRAY_SIZE, TicksToCycles( vectorTime ) / ARRAY_SIZE );
109     
110     return 0;
111 }
112 #endif //BT_USE_SSE