Grid 0.7.0
Lattice_comparison_utils.h
Go to the documentation of this file.
1 /*************************************************************************************
2
3 Grid physics library, www.github.com/paboyle/Grid
4
5 Source file: ./lib/lattice/Lattice_comparison_utils.h
6
7 Copyright (C) 2015
8
9Author: Azusa Yamaguchi <ayamaguc@staffmail.ed.ac.uk>
10Author: Peter Boyle <paboyle@ph.ed.ac.uk>
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License along
23 with this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25
26 See the full license in the file "LICENSE" in the top level distribution directory
27 *************************************************************************************/
28 /* END LEGAL */
29
30#pragma once
31
33
35 // This implementation is a bit poor.
36 //
37 // Only support relational logical operations (<, > etc)
38 // on scalar objects. Therefore can strip any tensor structures.
39 //
40 // Should guard this with isGridTensor<> enable if?
42 //
43 // Generic list of functors
44 //
45 template<class lobj,class robj> class veq {
46 public:
47 accelerator vInteger operator()(const lobj &lhs, const robj &rhs)
48 {
49 return (lhs) == (rhs);
50 }
51 };
52 template<class lobj,class robj> class vne {
53 public:
54 accelerator vInteger operator()(const lobj &lhs, const robj &rhs)
55 {
56 return (lhs) != (rhs);
57 }
58 };
59 template<class lobj,class robj> class vlt {
60 public:
61 accelerator vInteger operator()(const lobj &lhs, const robj &rhs)
62 {
63 return (lhs) < (rhs);
64 }
65 };
66 template<class lobj,class robj> class vle {
67 public:
68 accelerator vInteger operator()(const lobj &lhs, const robj &rhs)
69 {
70 return (lhs) <= (rhs);
71 }
72 };
73 template<class lobj,class robj> class vgt {
74 public:
75 accelerator vInteger operator()(const lobj &lhs, const robj &rhs)
76 {
77 return (lhs) > (rhs);
78 }
79 };
80 template<class lobj,class robj> class vge {
81 public:
82 accelerator vInteger operator()(const lobj &lhs, const robj &rhs)
83 {
84 return (lhs) >= (rhs);
85 }
86 };
87
88 // Generic list of functors
89 template<class lobj,class robj> class seq {
90 public:
91 accelerator Integer operator()(const lobj &lhs, const robj &rhs)
92 {
93 return (lhs) == (rhs);
94 }
95 };
96 template<class lobj,class robj> class sne {
97 public:
98 accelerator Integer operator()(const lobj &lhs, const robj &rhs)
99 {
100 return (lhs) != (rhs);
101 }
102 };
103 template<class lobj,class robj> class slt {
104 public:
105 accelerator Integer operator()(const lobj &lhs, const robj &rhs)
106 {
107 return (lhs) < (rhs);
108 }
109 };
110 template<class lobj,class robj> class sle {
111 public:
112 accelerator Integer operator()(const lobj &lhs, const robj &rhs)
113 {
114 return (lhs) <= (rhs);
115 }
116 };
117 template<class lobj,class robj> class sgt {
118 public:
119 accelerator Integer operator()(const lobj &lhs, const robj &rhs)
120 {
121 return (lhs) > (rhs);
122 }
123 };
124 template<class lobj,class robj> class sge {
125 public:
126 accelerator Integer operator()(const lobj &lhs, const robj &rhs)
127 {
128 return (lhs) >= (rhs);
129 }
130 };
131
133 // Integer and real get extra relational functions.
135 template<class sfunctor, class vsimd,IfNotComplex<vsimd> = 0>
136 accelerator_inline vInteger Comparison(sfunctor sop,const vsimd & lhs, const vsimd & rhs)
137 {
138 typedef typename vsimd::scalar_type scalar;
139 ExtractBuffer<scalar> vlhs(vsimd::Nsimd()); // Use functors to reduce this to single implementation
140 ExtractBuffer<scalar> vrhs(vsimd::Nsimd());
141 ExtractBuffer<Integer> vpred(vsimd::Nsimd());
142 vInteger ret;
143 extract<vsimd,scalar>(lhs,vlhs);
144 extract<vsimd,scalar>(rhs,vrhs);
145 for(int s=0;s<vsimd::Nsimd();s++){
146 vpred[s] = sop(vlhs[s],vrhs[s]);
147 }
148 merge<vInteger,Integer>(ret,vpred);
149 return ret;
150 }
151
152 template<class sfunctor, class vsimd,IfNotComplex<vsimd> = 0>
153 accelerator_inline vInteger Comparison(sfunctor sop,const vsimd & lhs, const typename vsimd::scalar_type & rhs)
154 {
155 typedef typename vsimd::scalar_type scalar;
156 ExtractBuffer<scalar> vlhs(vsimd::Nsimd()); // Use functors to reduce this to single implementation
157 ExtractBuffer<Integer> vpred(vsimd::Nsimd());
158 vInteger ret;
159 extract<vsimd,scalar>(lhs,vlhs);
160 for(int s=0;s<vsimd::Nsimd();s++){
161 vpred[s] = sop(vlhs[s],rhs);
162 }
163 merge<vInteger,Integer>(ret,vpred);
164 return ret;
165 }
166
167 template<class sfunctor, class vsimd,IfNotComplex<vsimd> = 0>
168 accelerator_inline vInteger Comparison(sfunctor sop,const typename vsimd::scalar_type & lhs, const vsimd & rhs)
169 {
170 typedef typename vsimd::scalar_type scalar;
171 ExtractBuffer<scalar> vrhs(vsimd::Nsimd()); // Use functors to reduce this to single implementation
172 ExtractBuffer<Integer> vpred(vsimd::Nsimd());
173 vInteger ret;
174 extract<vsimd,scalar>(rhs,vrhs);
175 for(int s=0;s<vsimd::Nsimd();s++){
176 vpred[s] = sop(lhs,vrhs[s]);
177 }
178 merge<vInteger,Integer>(ret,vpred);
179 return ret;
180 }
181
182#define DECLARE_RELATIONAL_EQ(op,functor) \
183 template<class vsimd,IfSimd<vsimd> = 0>\
184 accelerator_inline vInteger operator op (const vsimd & lhs, const vsimd & rhs)\
185 {\
186 typedef typename vsimd::scalar_type scalar;\
187 return Comparison(functor<scalar,scalar>(),lhs,rhs);\
188 }\
189 template<class vsimd,IfSimd<vsimd> = 0>\
190 accelerator_inline vInteger operator op (const vsimd & lhs, const typename vsimd::scalar_type & rhs) \
191 {\
192 typedef typename vsimd::scalar_type scalar;\
193 return Comparison(functor<scalar,scalar>(),lhs,rhs);\
194 }\
195 template<class vsimd,IfSimd<vsimd> = 0>\
196 accelerator_inline vInteger operator op (const typename vsimd::scalar_type & lhs, const vsimd & rhs) \
197 {\
198 typedef typename vsimd::scalar_type scalar;\
199 return Comparison(functor<scalar,scalar>(),lhs,rhs);\
200 }\
201 template<class vsimd>\
202 accelerator_inline vInteger operator op(const iScalar<vsimd> &lhs,const typename vsimd::scalar_type &rhs) \
203 { \
204 return lhs._internal op rhs; \
205 } \
206 template<class vsimd>\
207 accelerator_inline vInteger operator op(const typename vsimd::scalar_type &lhs,const iScalar<vsimd> &rhs) \
208 { \
209 return lhs op rhs._internal; \
210 } \
211
212#define DECLARE_RELATIONAL(op,functor) \
213 DECLARE_RELATIONAL_EQ(op,functor) \
214 template<class vsimd>\
215 accelerator_inline vInteger operator op(const iScalar<vsimd> &lhs,const iScalar<vsimd> &rhs)\
216 { \
217 return lhs._internal op rhs._internal; \
218 }
219
226
227#undef DECLARE_RELATIONAL
228
230
231
232
#define accelerator_inline
#define accelerator
Grid_simd< Integer, SIMD_Itype > vInteger
#define DECLARE_RELATIONAL_EQ(op, functor)
accelerator_inline vInteger Comparison(sfunctor sop, const vsimd &lhs, const vsimd &rhs)
#define DECLARE_RELATIONAL(op, functor)
#define NAMESPACE_BEGIN(A)
Definition Namespace.h:35
#define NAMESPACE_END(A)
Definition Namespace.h:36
uint32_t Integer
Definition Simd.h:58
AcceleratorVector< __T,GRID_MAX_SIMD > ExtractBuffer
accelerator void extract(const vobj &vec, ExtractBuffer< sobj > &extracted)
accelerator void merge(vobj &vec, ExtractBuffer< sobj > &extracted)
accelerator Integer operator()(const lobj &lhs, const robj &rhs)
accelerator Integer operator()(const lobj &lhs, const robj &rhs)
accelerator Integer operator()(const lobj &lhs, const robj &rhs)
accelerator Integer operator()(const lobj &lhs, const robj &rhs)
accelerator Integer operator()(const lobj &lhs, const robj &rhs)
accelerator Integer operator()(const lobj &lhs, const robj &rhs)
accelerator vInteger operator()(const lobj &lhs, const robj &rhs)
accelerator vInteger operator()(const lobj &lhs, const robj &rhs)
accelerator vInteger operator()(const lobj &lhs, const robj &rhs)
accelerator vInteger operator()(const lobj &lhs, const robj &rhs)
accelerator vInteger operator()(const lobj &lhs, const robj &rhs)
accelerator vInteger operator()(const lobj &lhs, const robj &rhs)