Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Test / Source / Tests / Test_qtmul.cpp
1 //
2 //  Test_qtmul.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_qtmul.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/btQuaternion.h>
22
23 #define BT_OP(a, b)     ((a) *= (b))
24 // reference code for testing purposes
25 static inline btQuaternion& qtmul_ref(btQuaternion& q1, btQuaternion& q2);
26
27 static inline btQuaternion& qtmul_ref(btQuaternion& q1, btQuaternion& q2)
28 {
29     float x,y,z,w;
30     x = q1.w() * q2.x() + q1.x() * q2.w() + q1.y() * q2.z() - q1.z() * q2.y(),
31     y = q1.w() * q2.y() + q1.y() * q2.w() + q1.z() * q2.x() - q1.x() * q2.z(),
32     z = q1.w() * q2.z() + q1.z() * q2.w() + q1.x() * q2.y() - q1.y() * q2.x(),
33     w = q1.w() * q2.w() - q1.x() * q2.x() - q1.y() * q2.y() - q1.z() * q2.z();
34
35     q1.setValue(x, y, z, w);
36         return q1;
37 }
38
39 #define LOOPCOUNT 1024
40 #define NUM_CYCLES 1000
41
42 int Test_qtmul(void)
43 {
44     btQuaternion q1, q2, q3;
45         
46     float x, y, z, w, vNaN;
47     
48     // Init the data
49     x = RANDF_01;
50     y = RANDF_01;
51     z = RANDF_01;
52     w = RANDF_01;
53     vNaN = BT_NAN;     // w channel NaN
54     q1.setValue(x,y,z,w);
55         
56     x = RANDF_01;
57     y = RANDF_01;
58     z = RANDF_01;
59     w = RANDF_01;
60     q2.setValue(x,y,z,w);
61
62         q3 = q1;
63                 
64     btQuaternion correct_res, test_res;
65          
66     {
67                 float vNaN = BT_NAN;
68                 correct_res.setValue(vNaN, vNaN, vNaN, vNaN); 
69                 test_res.setValue(vNaN, vNaN, vNaN, vNaN);
70                 correct_res = qtmul_ref(q1, q2);
71                 test_res = BT_OP(q3,q2);
72            
73                 if( fabsf(correct_res.x() - test_res.x()) + 
74                         fabsf(correct_res.y() - test_res.y()) +
75                         fabsf(correct_res.z() - test_res.z()) +
76                         fabsf(correct_res.w() - test_res.w()) > FLT_EPSILON*10 )
77                 {       
78                         vlog( "Error - qtmul result error! "
79                                         "\ncorrect = (%10.4f, %10.4f, %10.4f, %10.4f) "
80                                         "\ntested  = (%10.4f, %10.4f, %10.4f, %10.4f) \n", 
81                                         correct_res.x(), correct_res.y(), 
82                     correct_res.z(), correct_res.w(),
83                                         test_res.x(), test_res.y(), 
84                     test_res.z(), test_res.w());
85                 
86                         return 1;
87                 }
88         }
89     
90 #define DATA_SIZE LOOPCOUNT
91
92         btQuaternion qt_arr1[DATA_SIZE];
93         btQuaternion qt_arr2[DATA_SIZE];
94
95     uint64_t scalarTime;
96     uint64_t vectorTime;
97     size_t j, k;
98
99         {
100         uint64_t startTime, bestTime, currentTime;
101         
102         bestTime = -1LL;
103         scalarTime = 0;
104         for (j = 0; j < NUM_CYCLES; j++) 
105                 {
106                         for( k = 0; k < DATA_SIZE; k++ )
107                         {
108                 x = RANDF_01;
109                 y = RANDF_01;
110                 z = RANDF_01;
111                                 w = RANDF_01;
112                                 qt_arr1[k].setValue(x,y,z,w);
113
114                 x = RANDF_01;
115                 y = RANDF_01;
116                 z = RANDF_01;
117                                 w = RANDF_01;
118                                 qt_arr2[k].setValue(x,y,z,w);
119                         }
120
121             startTime = ReadTicks();
122             for( k = 0; k < LOOPCOUNT; k++ )
123                         {
124                     qt_arr1[k] = qtmul_ref(qt_arr1[k], qt_arr2[k]);
125                         }
126                         currentTime = ReadTicks() - startTime;
127             scalarTime += currentTime;
128             if( currentTime < bestTime )
129                 bestTime = currentTime;
130         }
131         if( 0 == gReportAverageTimes )
132             scalarTime = bestTime;        
133         else
134             scalarTime /= NUM_CYCLES;
135     }
136     
137     {
138         uint64_t startTime, bestTime, currentTime;
139         
140         bestTime = -1LL;
141         vectorTime = 0;
142         for (j = 0; j < NUM_CYCLES; j++) 
143                 {
144                         for( k = 0; k < DATA_SIZE; k++ )
145                         {
146                 x = RANDF_01;
147                 y = RANDF_01;
148                 z = RANDF_01;
149                                 w = RANDF_01;
150                                 qt_arr1[k].setValue(x,y,z,w);
151
152                 x = RANDF_01;
153                 y = RANDF_01;
154                 z = RANDF_01;
155                                 w = RANDF_01;
156                                 qt_arr2[k].setValue(x,y,z,w);
157                         }
158
159             startTime = ReadTicks();
160             for( k = 0; k < LOOPCOUNT; k++ )
161                         {
162                                 qt_arr1[k] = BT_OP(qt_arr1[k], qt_arr2[k]);
163                         }
164                         currentTime = ReadTicks() - startTime;
165             vectorTime += currentTime;
166             if( currentTime < bestTime )
167                 bestTime = currentTime;
168         }
169         if( 0 == gReportAverageTimes )
170             vectorTime = bestTime;        
171         else
172             vectorTime /= NUM_CYCLES;
173     }
174
175     vlog( "Timing:\n" );
176     vlog( "     \t    scalar\t    vector\n" );
177     vlog( "    \t%10.4f\t%10.4f\n", TicksToCycles( scalarTime ) / LOOPCOUNT, 
178                                                                         TicksToCycles( vectorTime ) / LOOPCOUNT );
179
180     return 0;
181 }
182
183 #endif //BT_USE_SSE