OWL
Loading...
Searching...
No Matches
compare.h
1// ======================================================================== //
2// Copyright 2018-2019 Ingo Wald //
3// //
4// Licensed under the Apache License, Version 2.0 (the "License"); //
5// you may not use this file except in compliance with the License. //
6// You may obtain a copy of the License at //
7// //
8// http://www.apache.org/licenses/LICENSE-2.0 //
9// //
10// Unless required by applicable law or agreed to in writing, software //
11// distributed under the License is distributed on an "AS IS" BASIS, //
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
13// See the License for the specific language governing permissions and //
14// limitations under the License. //
15// ======================================================================== //
16
17#pragma once
18
19namespace owl {
20 namespace common {
21
22 // ------------------------------------------------------------------
23 // ==
24 // ------------------------------------------------------------------
25
26#if __CUDACC__
27 template<typename T>
28 inline __both__ bool operator==(const vec_t<T,2> &a, const vec_t<T,2> &b)
29 { return (a.x==b.x) & (a.y==b.y); }
30
31 template<typename T>
32 inline __both__ bool operator==(const vec_t<T,3> &a, const vec_t<T,3> &b)
33 { return (a.x==b.x) & (a.y==b.y) & (a.z==b.z); }
34
35 template<typename T>
36 inline __both__ bool operator==(const vec_t<T,4> &a, const vec_t<T,4> &b)
37 { return (a.x==b.x) & (a.y==b.y) & (a.z==b.z) & (a.w==b.w); }
38#else
39 template<typename T>
40 inline __both__ bool operator==(const vec_t<T,2> &a, const vec_t<T,2> &b)
41 { return a.x==b.x && a.y==b.y; }
42
43 template<typename T>
44 inline __both__ bool operator==(const vec_t<T,3> &a, const vec_t<T,3> &b)
45 { return a.x==b.x && a.y==b.y && a.z==b.z; }
46
47 template<typename T>
48 inline __both__ bool operator==(const vec_t<T,4> &a, const vec_t<T,4> &b)
49 { return a.x==b.x && a.y==b.y && a.z==b.z && a.w==b.w; }
50#endif
51
52 // ------------------------------------------------------------------
53 // !=
54 // ------------------------------------------------------------------
55
56 template<typename T, int N>
57 inline __both__ bool operator!=(const vec_t<T,N> &a, const vec_t<T,N> &b)
58 { return !(a==b); }
59
60
61 // ------------------------------------------------------------------
62 // comparison operators returning result _vector_
63 // ------------------------------------------------------------------
64
65 // ------------------------------------------------------------------
66 // not (!)
67 // ------------------------------------------------------------------
68
69 template<typename T>
70 inline __both__ auto nt(const vec_t<T,2> &a)
71 -> vec_t<decltype(!a.x),2>
72 { return { !a.x, !a.y }; }
73
74 template<typename T>
75 inline __both__ auto nt(const vec_t<T,3> &a)
76 -> vec_t<decltype(!a.x),3>
77 { return { !a.x, !a.y, !a.z }; }
78
79 template<typename T>
80 inline __both__ auto nt(const vec_t<T,4> &a)
81 -> vec_t<decltype(!a.x),4>
82 { return { !a.x, !a.y, !a.z, !a.w }; }
83
84 // ------------------------------------------------------------------
85 // eq (==)
86 // ------------------------------------------------------------------
87
88 template<typename T>
89 inline __both__ auto eq(const vec_t<T,2> &a, const vec_t<T,2> &b)
90 -> vec_t<decltype(a.x==b.x),2>
91 { return { a.x==b.x, a.y==b.y }; }
92
93 template<typename T>
94 inline __both__ auto eq(const vec_t<T,3> &a, const vec_t<T,3> &b)
95 -> vec_t<decltype(a.x==b.x),3>
96 { return { a.x==b.x, a.y==b.y, a.z==b.z }; }
97
98 template<typename T>
99 inline __both__ auto eq(const vec_t<T,4> &a, const vec_t<T,4> &b)
100 -> vec_t<decltype(a.x==b.x),4>
101 { return { a.x==b.x, a.y==b.y, a.z==b.z, a.w==b.w }; }
102
103 // ------------------------------------------------------------------
104 // neq (!=)
105 // ------------------------------------------------------------------
106
107 template<typename T, int N>
108 inline __both__ auto neq(const vec_t<T,N> &a, const vec_t<T,N> &b)
109 -> decltype(nt(eq(a,b)))
110 { return nt(eq(a,b)); }
111
112
113
114 // ------------------------------------------------------------------
115 // reduce
116 // ------------------------------------------------------------------
117
118 template<typename T, int N>
119 inline __both__ bool any(const vec_t<T,N> &a)
120 { for (int i=0;i<N;++i) if (a[i]) return true; return false; }
121
122 template<typename T, int N>
123 inline __both__ bool all(const vec_t<T,N> &a)
124 { for (int i=0;i<N;++i) if (!a[i]) return false; return true; }
125
126 // template<typename T>
127 // inline __both__ bool any(const vec_t<T,3> &a)
128 // { return a[i] | b[i] | c[i]; }
129
130 // ------------------------------------------------------------------
131 // select
132 // ------------------------------------------------------------------
133
134 template<typename T>
135 inline __both__ vec_t<T,2> select(const vec_t<bool,2> &mask,
136 const vec_t<T,2> &a,
137 const vec_t<T,2> &b)
138 { return { mask.x?a.x:b.x, mask.y?a.y:b.y }; }
139
140 template<typename T>
141 inline __both__ vec_t<T,3> select(const vec_t<bool,3> &mask,
142 const vec_t<T,3> &a,
143 const vec_t<T,3> &b)
144 { return { mask.x?a.x:b.x, mask.y?a.y:b.y, mask.z?a.z:b.z }; }
145
146 template<typename T>
147 inline __both__ vec_t<T,4> select(const vec_t<bool,4> &mask,
148 const vec_t<T,4> &a,
149 const vec_t<T,4> &b)
150 { return { mask.x?a.x:b.x, mask.y?a.y:b.y, mask.z?a.z:b.z }; }
151
152 template<typename T, int N>
153 inline __both__ vec_t<T,N> select(const vec_t<bool,N> &mask,
154 const vec_t<T,N> &a,
155 const vec_t<T,N> &b)
156 {
157 vec_t<T,N> res;
158 for (int i=0; i<N; ++i)
159 res[i] = mask[i]?a[i]:b[i];
160 return res;
161 }
162
163 } // ::owl::common
164} // ::owl