Revert "[3.0] Version downgrade (1.2.0 to 1.1.45)"
[platform/core/uifw/dali-core.git] / dali / internal / common / math.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/common/math.h>
20
21 //EXTERNAL INCLUDES
22 #include <cmath>
23
24 void Dali::Internal::TransformVector3( Vec3 result, const Mat4 m, const Vec3 v )
25 {
26 #ifndef __ARM_NEON__
27
28   result[0] = v[0] * m[0] + v[1] * m[4] + v[2] * m[8];
29   result[1] = v[0] * m[1] + v[1] * m[5] + v[2] * m[9];
30   result[2] = v[0] * m[2] + v[1] * m[6] + v[2] * m[10];
31
32 #else
33
34   Vec4 temp = { v[0], v[1], v[2], 0.0f };
35   Vec4 tempResult;
36
37   asm volatile ( "VLD1.F32   {q0}, [%1]     \n\t"  //Load "temp" from memory to register q0
38                  "VLD1.F32   {q1}, [%0]!    \n\t"  //Load first row of the matrix from memory to register q1
39                  "VMUL.F32   q2, q1, d0[0]  \n\t"  //q2 = (m[0..3] * v.x)
40                  "VLD1.F32   {q1}, [%0]!    \n\t"  //Load second row of the matrix from memory
41                  "VMLA.F32   q2, q1, d0[1]  \n\t"  //q2 = (m[0..3] * v.x) + (m[4..7] * v.y)
42                  "VLD1.F32   {q1}, [%0]!    \n\t"  //Load third row of the matrix from memory
43                  "VMLA.F32   q2, q1, d1[0]  \n\t"  //q2 = (m[0..3] * v.x) + (m[4..7] * v.y) + (m[8...11] * v.z)
44                  "VST1.F32   {q2}, [%2]     \n\t"  //Write the result back to memory
45                  :
46                  : "r"(m), "r"(temp), "r"(tempResult)
47                  : "q0", "q1", "q2", "memory" );
48
49   result[0] = tempResult[0];
50   result[1] = tempResult[1];
51   result[2] = tempResult[2];
52
53 #endif
54 }
55
56 float Dali::Internal::Length( const Vec3 v )
57 {
58   return sqrtf(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
59 }
60
61 void Dali::Internal::MultiplyVectorBySize( Vec2& result, const Vec2 v, const Size3 s )
62 {
63   result[0] = v[0] * s[0];
64   result[1] = v[1] * s[1];
65 }
66
67 void Dali::Internal::MultiplyVectorBySize( Vec3& result, const Vec3 v, const Size3 s )
68 {
69   result[0] = v[0] * s[0];
70   result[1] = v[1] * s[1];
71   result[2] = v[2] * s[2];
72 }
73
74 void Dali::Internal::MultiplyVectorBySize( Vec4& result, const Vec4 v, const Size3 s )
75 {
76   result[0] = v[0] * s[0];
77   result[1] = v[1] * s[1];
78   result[2] = v[2] * s[2];
79   result[3] = 1.0f;
80 }
81
82 void Dali::Internal::MultiplyVectorByMatrix4( Vec2& result, const Mat4 m, const Vec2 v )
83 {
84   result[0] = v[0] * m[0] + v[1] * m[4] + m[12];
85   result[1] = v[0] * m[1] + v[1] * m[5] + m[13];
86 }
87
88 void Dali::Internal::MultiplyVectorByMatrix4( Vec3& result, const Mat4 m, const Vec3 v )
89 {
90   result[0] = v[0] * m[0] + v[1] * m[4] + v[2] * m[8]  +  m[12];
91   result[1] = v[0] * m[1] + v[1] * m[5] + v[2] * m[9]  +  m[13];
92   result[2] = v[0] * m[2] + v[1] * m[6] + v[2] * m[10] +  m[14];
93 }
94
95 void Dali::Internal::MultiplyVectorByMatrix4( Vec4& result, const Mat4 m, const Vec4 rhs )
96 {
97   result[0] = rhs[0] * m[0] + rhs[1] * m[4] + rhs[2] * m[8]  +  rhs[3] * m[12];
98   result[1] = rhs[0] * m[1] + rhs[1] * m[5] + rhs[2] * m[9]  +  rhs[3] * m[13];
99   result[2] = rhs[0] * m[2] + rhs[1] * m[6] + rhs[2] * m[10] +  rhs[3] * m[14];
100   result[3] = rhs[0] * m[3] + rhs[1] * m[7] + rhs[2] * m[11] +  rhs[3] * m[15];
101 }
102
103 void Dali::Internal::MultiplyMatrices( float* result, const Mat4 lhs, const Mat4 rhs )
104 {
105   for( int i=0; i < 4; i++ )
106   {
107     // i<<2 gives the first vector / column
108     int loc = i<<2;
109     int loc1 = loc + 1;
110     int loc2 = loc + 2;
111     int loc3 = loc + 3;
112     float value0 = lhs[loc];
113     float value1 = lhs[loc1];
114     float value2 = lhs[loc2];
115     float value3 = lhs[loc3];
116     result[loc]  = (value0 * rhs[0]) +
117                    (value1 * rhs[4]) +
118                    (value2 * rhs[8]) +
119                    (value3 * rhs[12]);
120
121     result[loc1] = (value0 * rhs[1]) +
122                    (value1 * rhs[5]) +
123                    (value2 * rhs[9]) +
124                    (value3 * rhs[13]);
125
126     result[loc2] = (value0 * rhs[2]) +
127                    (value1 * rhs[6]) +
128                    (value2 * rhs[10])+
129                    (value3 * rhs[14]);
130
131     result[loc3] = (value0 * rhs[3]) +
132                    (value1 * rhs[7]) +
133                    (value2 * rhs[11])+
134                    (value3 * rhs[15]);
135   }
136 }