Skip to content

Commit ff820af

Browse files
Merge pull request #121 from LibRapid/cleanup
Merge cleanup into master Former-commit-id: f2fbb28 [formerly 43691eb] Former-commit-id: 3037a46
2 parents bb26cf2 + 3454787 commit ff820af

File tree

6 files changed

+332
-28
lines changed

6 files changed

+332
-28
lines changed

.github/workflows/build-docs.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ jobs:
4646
cd docs
4747
pip install -r requirements.txt
4848
49+
- name: Update Distribution
50+
run: |
51+
sudo apt-get dist-upgrade
52+
4953
- name: Build Documentation
5054
run: |
5155
cd docs

src/librapid/math/vector.hpp

+105-23
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,26 @@ namespace librapid {
77
template<typename Scalar, i64 Dims, typename StorageType>
88
class VecImpl {
99
public:
10-
using Mask = Vc::Mask<Scalar, struct Vc::simd_abi::fixed_size<Dims>>;
11-
1210
VecImpl() = default;
1311

1412
explicit VecImpl(const StorageType &arr) : m_data {arr} {}
1513

1614
template<typename T, typename ABI>
1715
explicit VecImpl(const Vc::Vector<T, ABI> &arr) : m_data {arr} {}
1816

19-
template<typename... Args>
17+
template<typename... Args, typename std::enable_if_t<sizeof...(Args) == Dims, int> = 0>
2018
VecImpl(Args... args) : m_data {static_cast<Scalar>(args)...} {
2119
static_assert(sizeof...(Args) <= Dims, "Invalid number of arguments");
2220
}
2321

22+
template<typename... Args, i64 size = sizeof...(Args),
23+
typename std::enable_if_t<size != Dims, int> = 0>
24+
VecImpl(Args... args) {
25+
static_assert(sizeof...(Args) <= Dims, "Invalid number of arguments");
26+
const Scalar expanded[] = {static_cast<Scalar>(args)...};
27+
for (i64 i = 0; i < size; i++) { m_data[i] = expanded[i]; }
28+
}
29+
2430
VecImpl(const VecImpl &other) = default;
2531
VecImpl(VecImpl &&other) noexcept = default;
2632
VecImpl &operator=(const VecImpl &other) = default;
@@ -76,32 +82,86 @@ namespace librapid {
7682
}
7783

7884
LR_FORCE_INLINE
79-
VecImpl<Scalar, Dims, Mask> operator>(const VecImpl &other) {
80-
return VecImpl<Scalar, Dims, Mask>(m_data > other.m_data);
81-
}
82-
83-
LR_FORCE_INLINE
84-
VecImpl<Scalar, Dims, Mask> operator<(const VecImpl &other) {
85-
return VecImpl<Scalar, Dims, Mask>(m_data < other.m_data);
86-
}
87-
88-
LR_FORCE_INLINE
89-
VecImpl<Scalar, Dims, Mask> operator>=(const VecImpl &other) {
90-
return VecImpl<Scalar, Dims, Mask>(m_data >= other.m_data);
85+
VecImpl operator>(const VecImpl &other) {
86+
VecImpl res(*this);
87+
for (i64 i = 0; i < Dims; ++i) {
88+
if (res[i] > other[i]) {
89+
res[i] = 1;
90+
} else {
91+
res[i] = 0;
92+
}
93+
}
94+
return res;
9195
}
9296

9397
LR_FORCE_INLINE
94-
VecImpl<Scalar, Dims, Mask> operator<=(const VecImpl &other) {
95-
return VecImpl<Scalar, Dims, Mask>(m_data <= other.m_data);
96-
}
98+
VecImpl cmp(const VecImpl &other, char mode[2]) {
99+
// Mode:
100+
// 0: ==
101+
// 1: !=
102+
// 2: <
103+
// 3: <=
104+
// 4: >
105+
// 5: >=
97106

98-
LR_FORCE_INLINE
99-
VecImpl<Scalar, Dims, Mask> operator==(const VecImpl &other) {
100-
return VecImpl<Scalar, Dims, Mask>(m_data == other.m_data);
107+
VecImpl res(*this);
108+
i16 modeInt = (mode[1] << 8) | mode[0];
109+
for (i64 i = 0; i < Dims; ++i) {
110+
switch (modeInt) {
111+
case ('e' << 8) | 'q':
112+
if (res[i] == other[i]) {
113+
res[i] = 1;
114+
} else {
115+
res[i] = 0;
116+
}
117+
break;
118+
case ('n' << 8) | 'e':
119+
if (res[i] != other[i]) {
120+
res[i] = 1;
121+
} else {
122+
res[i] = 0;
123+
}
124+
break;
125+
case ('l' << 8) | 't':
126+
if (res[i] < other[i]) {
127+
res[i] = 1;
128+
} else {
129+
res[i] = 0;
130+
}
131+
break;
132+
case ('l' << 8) | 'e':
133+
if (res[i] <= other[i]) {
134+
res[i] = 1;
135+
} else {
136+
res[i] = 0;
137+
}
138+
break;
139+
case ('g' << 8) | 't':
140+
if (res[i] > other[i]) {
141+
res[i] = 1;
142+
} else {
143+
res[i] = 0;
144+
}
145+
break;
146+
case ('g' << 8) | 'e':
147+
if (res[i] >= other[i]) {
148+
res[i] = 1;
149+
} else {
150+
res[i] = 0;
151+
}
152+
break;
153+
default: LR_ASSERT(false, "Invalid mode {}", mode);
154+
}
155+
}
156+
return res;
101157
}
102158

103-
LR_FORCE_INLINE
104-
VecImpl<Scalar, Dims, Mask> operator!=(const VecImpl &other) { return !(*this == other); }
159+
LR_FORCE_INLINE VecImpl operator<(const VecImpl &other) const { return cmp(other, "lt"); }
160+
LR_FORCE_INLINE VecImpl operator<=(const VecImpl &other) const { return cmp(other, "le"); }
161+
LR_FORCE_INLINE VecImpl operator>(const VecImpl &other) const { return cmp(other, "gt"); }
162+
LR_FORCE_INLINE VecImpl operator>=(const VecImpl &other) const { return cmp(other, "ge"); }
163+
LR_FORCE_INLINE VecImpl operator==(const VecImpl &other) const { return cmp(other, "eq"); }
164+
LR_FORCE_INLINE VecImpl operator!=(const VecImpl &other) const { return cmp(other, "ne"); }
105165

106166
LR_NODISCARD("") LR_INLINE Scalar mag2() const { return (m_data * m_data).sum(); }
107167
LR_NODISCARD("") LR_INLINE Scalar mag() const { return ::librapid::sqrt(mag2()); }
@@ -132,12 +192,34 @@ namespace librapid {
132192

133193
LR_FORCE_INLINE Vec<Scalar, 2> xy() const { return {x(), y()}; }
134194
LR_FORCE_INLINE Vec<Scalar, 2> yx() const { return {y(), x()}; }
195+
LR_FORCE_INLINE Vec<Scalar, 2> xz() const { return {x(), z()}; }
196+
LR_FORCE_INLINE Vec<Scalar, 2> zx() const { return {z(), x()}; }
197+
LR_FORCE_INLINE Vec<Scalar, 2> yz() const { return {y(), z()}; }
198+
LR_FORCE_INLINE Vec<Scalar, 2> zy() const { return {z(), y()}; }
135199
LR_FORCE_INLINE Vec<Scalar, 3> xyz() const { return {x(), y(), z()}; }
136200
LR_FORCE_INLINE Vec<Scalar, 3> xzy() const { return {x(), z(), y()}; }
137201
LR_FORCE_INLINE Vec<Scalar, 3> yxz() const { return {y(), x(), z()}; }
138202
LR_FORCE_INLINE Vec<Scalar, 3> yzx() const { return {y(), z(), x()}; }
139203
LR_FORCE_INLINE Vec<Scalar, 3> zxy() const { return {z(), x(), y()}; }
140204
LR_FORCE_INLINE Vec<Scalar, 3> zyx() const { return {z(), y(), x()}; }
205+
LR_FORCE_INLINE Vec<Scalar, 3> xyw() const { return {x(), y(), w()}; }
206+
LR_FORCE_INLINE Vec<Scalar, 3> xwy() const { return {x(), w(), y()}; }
207+
LR_FORCE_INLINE Vec<Scalar, 3> yxw() const { return {y(), x(), w()}; }
208+
LR_FORCE_INLINE Vec<Scalar, 3> ywx() const { return {y(), w(), x()}; }
209+
LR_FORCE_INLINE Vec<Scalar, 3> wxy() const { return {w(), x(), y()}; }
210+
LR_FORCE_INLINE Vec<Scalar, 3> wyx() const { return {w(), y(), x()}; }
211+
LR_FORCE_INLINE Vec<Scalar, 3> xzw() const { return {x(), z(), w()}; }
212+
LR_FORCE_INLINE Vec<Scalar, 3> xwz() const { return {x(), w(), z()}; }
213+
LR_FORCE_INLINE Vec<Scalar, 3> zxw() const { return {z(), x(), w()}; }
214+
LR_FORCE_INLINE Vec<Scalar, 3> zwx() const { return {z(), w(), x()}; }
215+
LR_FORCE_INLINE Vec<Scalar, 3> wxz() const { return {w(), x(), z()}; }
216+
LR_FORCE_INLINE Vec<Scalar, 3> wzx() const { return {w(), z(), x()}; }
217+
LR_FORCE_INLINE Vec<Scalar, 3> yzw() const { return {y(), z(), w()}; }
218+
LR_FORCE_INLINE Vec<Scalar, 3> ywz() const { return {y(), w(), z()}; }
219+
LR_FORCE_INLINE Vec<Scalar, 3> zyw() const { return {z(), y(), w()}; }
220+
LR_FORCE_INLINE Vec<Scalar, 3> zwy() const { return {z(), w(), y()}; }
221+
LR_FORCE_INLINE Vec<Scalar, 3> wyz() const { return {w(), y(), z()}; }
222+
LR_FORCE_INLINE Vec<Scalar, 3> wzy() const { return {w(), z(), y()}; }
141223
LR_FORCE_INLINE Vec<Scalar, 4> xyzw() const { return {x(), y(), z(), w()}; }
142224
LR_FORCE_INLINE Vec<Scalar, 4> xywz() const { return {x(), y(), w(), z()}; }
143225
LR_FORCE_INLINE Vec<Scalar, 4> xzyw() const { return {x(), z(), y(), w()}; }

src/librapid/python/generators/arrayInterface.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,10 @@
7676

7777
fIndex = [
7878
Function("__getitem__", [Argument(constRef, "this_"), Argument(int64_t, "index")], "return this_[index];"),
79-
Function("__setitem__", [Argument(ref, "this_"), Argument(int64_t, "index"), Argument(scalar, "val")], "this_[index] = val;"),
8079
Function("__setitem__", [Argument(ref, "this_"), Argument(int64_t, "index"), Argument(scalar, "val")], "this_[index] = val;")
8180
]
8281

83-
for i in range(1, 32):
82+
for i in range(1, 33):
8483
args = [Argument("int64_t", "index{}".format(ind)) for ind in range(i)]
8584
params = ", ".join(["index{}".format(ind) for ind in range(i)])
8685
fIndex.append(Function("__call__", [Argument(constRef, "this_")] + args, "return this_({}).get();".format(params)))

src/librapid/python/generators/vecInterface.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def generate(types):
8383
"""
8484

8585
# if dtype[-2] == 3 and dtype2[-2] == 3:
86-
if dtype[-2] == 3:
86+
if dtype[-1] == 3:
8787
tmp += f"""
8888
.def("cross", [](const librapid::{dtype[0]} &lhs, const librapid::{dtype[0]} &rhs) {{ return lhs.cross(rhs); }}, py::arg("other"))
8989
"""
@@ -99,7 +99,7 @@ def generate(types):
9999
.def_property("w", &librapid::{dtype[0]}::getW, &librapid::{dtype[0]}::setW)
100100
"""
101101

102-
toSwizzle = ["xy", "xyz", "xyzw"]
102+
toSwizzle = ["xy", "xz", "yz", "xyz", "xyw", "xzw", "yzw", "xyzw"]
103103
for swiz in toSwizzle:
104104
for perm in itertools.permutations(list(swiz)):
105105
joined = "".join(perm)

0 commit comments

Comments
 (0)