VCTR
Loading...
Searching...
No Matches
CompareOp.h
1/*
2 ==============================================================================
3 DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
5 Copyright 2026 - by sonible GmbH.
6
7 This file is part of VCTR - Versatile Container Templates Reconceptualized.
8
9 VCTR is free software: you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License version 3
11 only, as published by the Free Software Foundation.
12
13 VCTR is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License version 3 for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 version 3 along with VCTR. If not, see <https://www.gnu.org/licenses/>.
20 ==============================================================================
21*/
22
23namespace vctr
24{
25
26#if (! VCTR_X64)
27// These are defined in immintrin.h – defining them here for non x86 builds
28// to be able to assign the CompareOp enum cross-platform
29// NOLINTBEGIN (*-reserved-identifier)
30constexpr auto _CMP_LT_OQ = 0;
31constexpr auto _CMP_LE_OQ = 1;
32constexpr auto _CMP_GT_OQ = 2;
33constexpr auto _CMP_GE_OQ = 3;
34constexpr auto _CMP_EQ_OQ = 4;
35constexpr auto _CMP_NEQ_OQ = 5;
36// NOLINTEND (*-reserved-identifier)
37#endif
38
40enum class CompareOp : int
41{
42 less = _CMP_LT_OQ, // <
43 lessOrEqual = _CMP_LE_OQ, // <=
44 greater = _CMP_GT_OQ, // >
45 greaterOrEqual = _CMP_GE_OQ, // >=
46 equal = _CMP_EQ_OQ, // ==
47 notEqual = _CMP_NEQ_OQ // !=
48};
49
50namespace is
51{
52// clang-format off
54template <class Lhs, CompareOp op, class Rhs>
55concept comparableByWith = (op == CompareOp::less && requires (const Lhs& lhs, const Rhs& rhs) { lhs < rhs; }) ||
56 (op == CompareOp::lessOrEqual && requires (const Lhs& lhs, const Rhs& rhs) { lhs <= rhs; }) ||
57 (op == CompareOp::greater && requires (const Lhs& lhs, const Rhs& rhs) { lhs > rhs; }) ||
58 (op == CompareOp::greaterOrEqual && requires (const Lhs& lhs, const Rhs& rhs) { lhs >= rhs; }) ||
59 (op == CompareOp::equal && requires (const Lhs& lhs, const Rhs& rhs) { lhs == rhs; }) ||
60 (op == CompareOp::notEqual && requires (const Lhs& lhs, const Rhs& rhs) { lhs != rhs; });
61// clang-format on
62
64template <class T, CompareOp op>
66}
67
74template <CompareOp>
76
77template<>
79{
80 template <class Lhs, is::comparableByWith<CompareOp::less, Lhs> Rhs>
81 constexpr bool operator() (const Lhs& lhs, const Rhs& rhs) const { return lhs < rhs; }
82};
83
84template<>
85struct ScalarCompareOp<CompareOp::lessOrEqual>
86{
87 template <class Lhs, is::comparableByWith<CompareOp::lessOrEqual, Lhs> Rhs>
88 constexpr bool operator() (const Lhs& lhs, const Rhs& rhs) const { return lhs <= rhs; }
89};
90
91template<>
92struct ScalarCompareOp<CompareOp::greater>
93{
94 template <class Lhs, is::comparableByWith<CompareOp::greater, Lhs> Rhs>
95 constexpr bool operator() (const Lhs& lhs, const Rhs& rhs) const { return lhs > rhs; }
96};
97
98template<>
99struct ScalarCompareOp<CompareOp::greaterOrEqual>
100{
101 template <class Lhs, is::comparableByWith<CompareOp::greaterOrEqual, Lhs> Rhs>
102 constexpr bool operator() (const Lhs& lhs, const Rhs& rhs) const { return lhs >= rhs; }
103};
104
105template<>
107{
108 template <class Lhs, is::comparableByWith<CompareOp::equal, Lhs> Rhs>
109 constexpr bool operator() (const Lhs& lhs, const Rhs& rhs) const { return lhs == rhs; }
110};
111
112template<>
113struct ScalarCompareOp<CompareOp::notEqual>
114{
115 template <class Lhs, is::comparableByWith<CompareOp::notEqual, Lhs> Rhs>
116 constexpr bool operator() (const Lhs& lhs, const Rhs& rhs) const { return lhs != rhs; }
117};
118
120template <CompareOp op, class Lhs, is::comparableByWith<op, Lhs> Rhs>
121constexpr bool scalarCompare (const Lhs& lhs, const Rhs& rhs)
122{
123 return ScalarCompareOp<op>{} (lhs, rhs);
124}
125
126}
Constrains an instance of Lhs to be comparable by op with Rhs.
Definition: CompareOp.h:55
Constrains two instances of T to be comparable by the operation specified by op.
Definition: CompareOp.h:65
The main namespace of the VCTR project.
Definition: Array.h:24
constexpr bool scalarCompare(const Lhs &lhs, const Rhs &rhs)
Compares two values using the compare operation specified by CompareOp.
Definition: CompareOp.h:121
CompareOp
Possible types of (SIMD) compare operations.
Definition: CompareOp.h:41
Helper struct for scalar fallback evaluation of compare operations specified via CompareOp constants.
Definition: CompareOp.h:75