-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmin_max.h
More file actions
120 lines (99 loc) · 3.16 KB
/
min_max.h
File metadata and controls
120 lines (99 loc) · 3.16 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
/*
Copyright (C) 2017-2026 Topological Manifold
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "error.h"
#include <algorithm>
#include <array>
#include <cstddef>
#include <span>
namespace ns
{
namespace min_max_implementation
{
template <bool COMPUTE_MIN, unsigned COUNT, typename T>
constexpr T min_max_value_array(const std::span<const T> p)
{
static_assert(COUNT > 0);
ASSERT(!p.empty());
ASSERT(p.size() % COUNT == 0);
const T* ptr = p.data();
std::array<T, COUNT> m;
for (unsigned i = 0; i < COUNT; ++i)
{
m[i] = ptr[i];
}
ptr += COUNT;
const T* const end = p.data() + p.size();
for (; ptr != end; ptr += COUNT)
{
for (unsigned i = 0; i < COUNT; ++i)
{
m[i] = COMPUTE_MIN ? std::min(m[i], ptr[i]) : std::max(m[i], ptr[i]);
}
}
T res = m[0];
for (unsigned i = 1; i < COUNT; ++i)
{
res = COMPUTE_MIN ? std::min(res, m[i]) : std::max(res, m[i]);
}
return res;
}
template <bool COMPUTE_MIN, typename T>
constexpr T min_max_value(const std::span<const T> p)
{
static_assert(std::is_same_v<T, float> || std::is_same_v<T, double>);
ASSERT(!p.empty());
// AVX, 256 bits
constexpr unsigned COUNT = std::is_same_v<T, float> ? 8 : 4;
const T* ptr = p.data();
T res;
if (p.size() >= 2 * COUNT)
{
const std::size_t count = (p.size() / COUNT) * COUNT;
res = min_max_value_array<COMPUTE_MIN, COUNT>(std::span<const T>(p.data(), count));
ptr += count;
}
else
{
res = *ptr++;
}
const T* const end = p.data() + p.size();
for (; ptr != end; ++ptr)
{
res = COMPUTE_MIN ? std::min(res, *ptr) : std::max(res, *ptr);
}
return res;
}
}
template <typename T>
constexpr T min_value(const std::span<const T> p)
{
static_assert(std::is_same_v<T, float> || std::is_same_v<T, double>);
if (p.empty())
{
error("No data for finding minimum value");
}
return min_max_implementation::min_max_value<true>(p);
}
template <typename T>
constexpr T max_value(const std::span<const T> p)
{
static_assert(std::is_same_v<T, float> || std::is_same_v<T, double>);
if (p.empty())
{
error("No data for finding maximum value");
}
return min_max_implementation::min_max_value<false>(p);
}
}