1 //
2 //  Test_3x3mulVM.cpp
3 //  BulletTest
4 //
5 //  Copyright (c) 2011 Apple Inc.
6 //
7 
8 #include "LinearMath/btScalar.h"
9 #if defined(BT_USE_SSE_IN_API) || defined(BT_USE_NEON)
10 
11 #include "Test_3x3mulVM.h"
12 #include "vector.h"
13 #include "Utils.h"
14 #include "main.h"
15 #include <math.h>
16 #include <string.h>
17 
18 #include <LinearMath/btMatrix3x3.h>
19 
20 #define LOOPCOUNT 1000
21 #define ARRAY_SIZE 128
22 
rand_f4(void)23 static inline btSimdFloat4 rand_f4(void)
24 {
25 	return btAssign128(RANDF_01, RANDF_01, RANDF_01, BT_NAN);  // w channel NaN
26 }
27 
M3x3mulVM_ref(const btVector3 & v,const btMatrix3x3 & m)28 static btVector3 M3x3mulVM_ref(const btVector3 &v, const btMatrix3x3 &m)
29 {
30 	return btVector3(m.tdotx(v), m.tdoty(v), m.tdotz(v));
31 }
32 
Test_3x3mulVM(void)33 int Test_3x3mulVM(void)
34 {
35 	// Init an array flanked by guard pages
36 	btVector3 in1[ARRAY_SIZE];
37 	btMatrix3x3 in2[ARRAY_SIZE];
38 	btVector3 out[ARRAY_SIZE];
39 	btVector3 out2[ARRAY_SIZE];
40 
41 	// Init the data
42 	size_t i, j;
43 	for (i = 0; i < ARRAY_SIZE; i++)
44 	{
45 		in1[i] = btVector3(rand_f4());
46 		in2[i] = btMatrix3x3(rand_f4(), rand_f4(), rand_f4());
47 
48 		out[i] = M3x3mulVM_ref(in1[i], in2[i]);
49 		out2[i] = (in1[i] * in2[i]);
50 
51 		if (fabsf(out[i].m_floats[0] - out2[i].m_floats[0]) +
52 				fabsf(out[i].m_floats[1] - out2[i].m_floats[1]) +
53 				fabsf(out[i].m_floats[2] - out2[i].m_floats[2]) +
54 				fabsf(out[i].m_floats[3] - out2[i].m_floats[3]) >
55 			FLT_EPSILON * 4)
56 		{
57 			vlog("Error - M3x3mulVM result error! ");
58 			vlog("failure @ %ld\n", i);
59 			vlog(
60 				"\ncorrect = (%10.4f, %10.4f, %10.4f, %10.4f) "
61 				"\ntested  = (%10.4f, %10.4f, %10.4f, %10.4f) \n",
62 				out[i].m_floats[0], out[i].m_floats[1], out[i].m_floats[2], out[i].m_floats[3],
63 				out2[i].m_floats[0], out2[i].m_floats[1], out2[i].m_floats[2], out2[i].m_floats[3]);
64 
65 			return 1;
66 		}
67 	}
68 
69 	uint64_t scalarTime, vectorTime;
70 	uint64_t startTime, bestTime, currentTime;
71 	bestTime = -1LL;
72 	scalarTime = 0;
73 	for (j = 0; j < LOOPCOUNT; j++)
74 	{
75 		startTime = ReadTicks();
76 		for (i = 0; i < ARRAY_SIZE; i++)
77 			out[i] = M3x3mulVM_ref(in1[i], in2[i]);
78 		currentTime = ReadTicks() - startTime;
79 		scalarTime += currentTime;
80 		if (currentTime < bestTime)
81 			bestTime = currentTime;
82 	}
83 	if (0 == gReportAverageTimes)
84 		scalarTime = bestTime;
85 	else
86 		scalarTime /= LOOPCOUNT;
87 
88 	bestTime = -1LL;
89 	vectorTime = 0;
90 	for (j = 0; j < LOOPCOUNT; j++)
91 	{
92 		startTime = ReadTicks();
93 		for (i = 0; i < ARRAY_SIZE; i++)
94 			out2[i] = (in1[i] * in2[i]);
95 		currentTime = ReadTicks() - startTime;
96 		vectorTime += currentTime;
97 		if (currentTime < bestTime)
98 			bestTime = currentTime;
99 	}
100 	if (0 == gReportAverageTimes)
101 		vectorTime = bestTime;
102 	else
103 		vectorTime /= LOOPCOUNT;
104 
105 	vlog("Timing:\n");
106 	vlog("\t    scalar\t    vector\n");
107 	vlog("\t%10.2f\t%10.2f\n", TicksToCycles(scalarTime) / ARRAY_SIZE, TicksToCycles(vectorTime) / ARRAY_SIZE);
108 
109 	return 0;
110 }
111 #endif  //BT_USE_SSE
112