-
Notifications
You must be signed in to change notification settings - Fork 17
/
math_vec4.c
143 lines (119 loc) · 3.67 KB
/
math_vec4.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
The MIT License (MIT)
Copyright (c) 2015 Lachlan Tychsen-Smith ([email protected])
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "math_neon.h"
#ifdef __MATH_NEON
#include "arm_neon.h"
#endif
//vec4 scalar product
float dot4_c(float v0[4], float v1[4])
{
float r;
r = v0[0]*v1[0];
r += v0[1]*v1[1];
r += v0[2]*v1[2];
r += v0[3]*v1[3];
return r;
}
void normalize4_c(float v[4], float d[4])
{
float b, c, x;
union {
float f;
int i;
} a;
x = v[0]*v[0];
x += v[1]*v[1];
x += v[2]*v[2];
x += v[3]*v[3];
//fast invsqrt approx
a.f = x;
a.i = 0x5F3759DF - (a.i >> 1); //VRSQRTE
c = x * a.f;
b = (3.0f - c * a.f) * 0.5; //VRSQRTS
a.f = a.f * b;
c = x * a.f;
b = (3.0f - c * a.f) * 0.5;
a.f = a.f * b;
d[0] = v[0]*a.f;
d[1] = v[1]*a.f;
d[2] = v[2]*a.f;
d[3] = v[3]*a.f;
}
void normalize4_neon(float v[4], float d[4])
{
#ifdef __MATH_NEON
asm volatile (
"vld1.32 {d4, d5}, [%0] \n\t" //d2={x0,y0}, d3={z0, w0}
"vmul.f32 d0, d4, d4 \n\t" //d0= d4*d4
"vmla.f32 d0, d5, d5 \n\t" //d0 = d0 + d5*d5
"vpadd.f32 d0, d0 \n\t" //d0 = d[0] + d[1]
"vmov.f32 d1, d0 \n\t" //d1 = d0
"vrsqrte.f32 d0, d0 \n\t" //d0 = ~ 1.0 / sqrt(d0)
"vmul.f32 d2, d0, d1 \n\t" //d2 = d0 * d1
"vrsqrts.f32 d3, d2, d0 \n\t" //d3 = (3 - d0 * d2) / 2
"vmul.f32 d0, d0, d3 \n\t" //d0 = d0 * d3
"vmul.f32 d2, d0, d1 \n\t" //d2 = d0 * d1
"vrsqrts.f32 d3, d2, d0 \n\t" //d4 = (3 - d0 * d3) / 2
"vmul.f32 d0, d0, d3 \n\t" //d0 = d0 * d4
"vmul.f32 q2, q2, d0[0] \n\t" //d0= d2*d4
"vst1.32 {d4, d5}, [%1] \n\t" //d2={x0,y0}, d3={z0, w0}
:: "r"(v), "r"(d)
: "d0", "d1", "d2", "d3", "d4", "d5", "memory"
);
#else
normalize4_c(v, d);
#endif
}
float dot4_neon_hfp(float v0[4], float v1[4])
{
#ifdef __MATH_NEON
asm volatile (
"vld1.32 {d2, d3}, [%0] \n\t" //d2={x0,y0}, d3={z0, w0}
"vld1.32 {d4, d5}, [%1] \n\t" //d4={x1,y1}, d5={z1, w1}
"vmul.f32 d0, d2, d4 \n\t" //d0= d2*d4
"vmla.f32 d0, d3, d5 \n\t" //d0 = d0 + d3*d5
"vpadd.f32 d0, d0 \n\t" //d0 = d[0] + d[1]
:: "r"(v0), "r"(v1) :
);
#endif
}
#ifdef __MATH_NEON
float32_t dot4_neon(float32x4_t v0, float32x4_t v1)
{
float32x2_t a, b, c, d, r;
a = vget_high_f32(v0);
b = vget_low_f32(v0);
c = vget_high_f32(v1);
d = vget_low_f32(v1);
r = vmul_f32(a, c);
r = vmla_f32(r, b, d);
r = vpadd_f32(r, r);
return vget_lane_f32(r, 0);
}
#endif
float dot4_neon_sfp(float v0[4], float v1[4])
{
#ifdef __MATH_NEON
dot4_neon_hfp(v0, v1);
asm volatile ("vmov.f32 r0, s0 \n\t");
#else
return dot4_c(v0, v1);
#endif
};