-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathminmax.hpp
More file actions
173 lines (133 loc) · 3.5 KB
/
minmax.hpp
File metadata and controls
173 lines (133 loc) · 3.5 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#ifndef VXL_MINMAX_HPP
# define VXL_MINMAX_HPP
# pragma once
#if defined(__ARM_NEON)
#include "arm_neon.h"
#endif // __ARM_NEON
#include "vector.hpp"
namespace vxl
{
#if defined(__ARM_NEON)
//__attribute__ ((noinline))
inline auto min(vector<float, 2> const& v) noexcept
{
using vector_type = typename vector_traits<float, 2>::vector_type;
auto const tmp(float32x2_t(v.data_));
return vector<float, 2>{
vector_type(vpmin_f32(tmp, tmp))
};
}
//__attribute__ ((noinline))
inline auto max(vector<float, 2> const& v) noexcept
{
using vector_type = typename vector_traits<float, 2>::vector_type;
auto const tmp(float32x2_t(v.data_));
return vector<float, 2>{
vector_type(vpmax_f32(tmp, tmp))
};
}
//__attribute__ ((noinline))
inline auto min(vector<float, 3> const& v) noexcept
{
using vector_type = typename vector_traits<float, 3>::vector_type;
auto tmp(vpmin_f32(vget_low_f32(float32x4_t(v.data_)),
vdup_lane_f32(vget_high_f32(float32x4_t(v.data_)), 0)));
tmp = vpmin_f32(tmp, tmp);
return vector<float, 3>{
vector_type(vcombine_f32(tmp, tmp))
};
}
//__attribute__ ((noinline))
inline auto max(vector<float, 3> const& v) noexcept
{
using vector_type = typename vector_traits<float, 3>::vector_type;
auto tmp(vpmax_f32(vget_low_f32(float32x4_t(v.data_)),
vdup_lane_f32(vget_high_f32(float32x4_t(v.data_)), 0)));
tmp = vpmax_f32(tmp, tmp);
return vector<float, 3>{
vector_type(vcombine_f32(tmp, tmp))
};
}
//__attribute__ ((noinline))
inline auto min(vector<float, 4> const& v) noexcept
{
using vector_type = typename vector_traits<float, 4>::vector_type;
auto tmp(vpmin_f32(vget_low_f32(float32x4_t(v.data_)),
vget_high_f32(float32x4_t(v.data_))));
tmp = vpmin_f32(tmp, tmp);
return vector<float, 4>{
vector_type(vcombine_f32(tmp, tmp))
};
}
//__attribute__ ((noinline))
inline auto max(vector<float, 4> const& v) noexcept
{
using vector_type = typename vector_traits<float, 4>::vector_type;
auto tmp(vpmax_f32(vget_low_f32(float32x4_t(v.data_)),
vget_high_f32(float32x4_t(v.data_))));
tmp = vpmax_f32(tmp, tmp);
return vector<float, 4>{
vector_type(vcombine_f32(tmp, tmp))
};
}
#endif
namespace detail
{
namespace minmax
{
// 1 2 3 4
// m(1, 2) m(2, 3) m(3, 4) m(4, 1)
// m(1, 2, 3, 4) m(2, 3, 4, 1) m(3, 4, 1, 2) m(4, 1, 2, 3)
template <typename T, unsigned N, bool op, std::size_t ...Is>
inline constexpr auto minmax(typename vector_traits<T, N>::vector_type v,
std::index_sequence<Is...>) noexcept
{
decltype(v) sr;
if constexpr(op)
(
(
sr = detail::vector::pow2_shuffler<T, N, Is>(v,
std::make_index_sequence<sizeof(v) / sizeof(T)>()
),
v = select(v, sr, v < sr)
),
...
);
else
(
(
sr = detail::vector::pow2_shuffler<T, N, Is>(v,
std::make_index_sequence<sizeof(v) / sizeof(T)>()
),
v = select(v, sr, v > sr)
),
...
);
return v;
}
}
}
// min
template <typename T, unsigned N>
//__attribute__ ((noinline))
inline constexpr auto min(vector<T, N> const& v) noexcept
{
return vector<T, N>{
detail::minmax::minmax<T, N, true>(v.data_,
std::make_index_sequence<detail::vector::log<2>(N)>()
)
};
}
// max
template <typename T, unsigned N>
//__attribute__ ((noinline))
inline constexpr auto max(vector<T, N> const& v) noexcept
{
return vector<T, N>{
detail::minmax::minmax<T, N, false>(v.data_,
std::make_index_sequence<detail::vector::log<2>(N)>()
)
};
}
}
#endif // VXL_MINMAX_HPP