Blender  V2.93
util_math_int4.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2017 Blender Foundation
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 #ifndef __UTIL_MATH_INT4_H__
18 #define __UTIL_MATH_INT4_H__
19 
20 #ifndef __UTIL_MATH_H__
21 # error "Do not include this file directly, include util_types.h instead."
22 #endif
23 
25 
26 /*******************************************************************************
27  * Declaration.
28  */
29 
30 #ifndef __KERNEL_GPU__
31 ccl_device_inline int4 operator+(const int4 &a, const int4 &b);
33 ccl_device_inline int4 operator>>(const int4 &a, int i);
34 ccl_device_inline int4 operator<<(const int4 &a, int i);
35 ccl_device_inline int4 operator<(const int4 &a, const int4 &b);
36 ccl_device_inline int4 operator>=(const int4 &a, const int4 &b);
37 ccl_device_inline int4 operator&(const int4 &a, const int4 &b);
40 ccl_device_inline int4 clamp(const int4 &a, const int4 &mn, const int4 &mx);
41 ccl_device_inline int4 select(const int4 &mask, const int4 &a, const int4 &b);
42 #endif /* __KERNEL_GPU__ */
43 
44 /*******************************************************************************
45  * Definition.
46  */
47 
48 #ifndef __KERNEL_GPU__
50 {
51 # ifdef __KERNEL_SSE__
52  return int4(_mm_add_epi32(a.m128, b.m128));
53 # else
54  return make_int4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
55 # endif
56 }
57 
59 {
60  return a = a + b;
61 }
62 
64 {
65 # ifdef __KERNEL_SSE__
66  return int4(_mm_srai_epi32(a.m128, i));
67 # else
68  return make_int4(a.x >> i, a.y >> i, a.z >> i, a.w >> i);
69 # endif
70 }
71 
73 {
74 # ifdef __KERNEL_SSE__
75  return int4(_mm_slli_epi32(a.m128, i));
76 # else
77  return make_int4(a.x << i, a.y << i, a.z << i, a.w << i);
78 # endif
79 }
80 
82 {
83 # ifdef __KERNEL_SSE__
84  return int4(_mm_cmplt_epi32(a.m128, b.m128));
85 # else
86  return make_int4(a.x < b.x, a.y < b.y, a.z < b.z, a.w < b.w);
87 # endif
88 }
89 
91 {
92 # ifdef __KERNEL_SSE__
93  return int4(_mm_xor_si128(_mm_set1_epi32(0xffffffff), _mm_cmplt_epi32(a.m128, b.m128)));
94 # else
95  return make_int4(a.x >= b.x, a.y >= b.y, a.z >= b.z, a.w >= b.w);
96 # endif
97 }
98 
100 {
101 # ifdef __KERNEL_SSE__
102  return int4(_mm_and_si128(a.m128, b.m128));
103 # else
104  return make_int4(a.x & b.x, a.y & b.y, a.z & b.z, a.w & b.w);
105 # endif
106 }
107 
109 {
110 # if defined(__KERNEL_SSE__) && defined(__KERNEL_SSE41__)
111  return int4(_mm_min_epi32(a.m128, b.m128));
112 # else
113  return make_int4(min(a.x, b.x), min(a.y, b.y), min(a.z, b.z), min(a.w, b.w));
114 # endif
115 }
116 
118 {
119 # if defined(__KERNEL_SSE__) && defined(__KERNEL_SSE41__)
120  return int4(_mm_max_epi32(a.m128, b.m128));
121 # else
122  return make_int4(max(a.x, b.x), max(a.y, b.y), max(a.z, b.z), max(a.w, b.w));
123 # endif
124 }
125 
126 ccl_device_inline int4 clamp(const int4 &a, const int4 &mn, const int4 &mx)
127 {
128  return min(max(a, mn), mx);
129 }
130 
131 ccl_device_inline int4 select(const int4 &mask, const int4 &a, const int4 &b)
132 {
133 # ifdef __KERNEL_SSE__
134  const __m128 m = _mm_cvtepi32_ps(mask);
135  /* TODO(sergey): avoid cvt. */
136  return int4(_mm_castps_si128(
137  _mm_or_ps(_mm_and_ps(m, _mm_castsi128_ps(a)), _mm_andnot_ps(m, _mm_castsi128_ps(b)))));
138 # else
139  return make_int4(
140  (mask.x) ? a.x : b.x, (mask.y) ? a.y : b.y, (mask.z) ? a.z : b.z, (mask.w) ? a.w : b.w);
141 # endif
142 }
143 
145 {
146 # ifdef __KERNEL_SSE__
147  return int4(_mm_loadu_si128((__m128i *)v));
148 # else
149  return make_int4(v[0], v[1], v[2], v[3]);
150 # endif
151 }
152 #endif /* __KERNEL_GPU__ */
153 
155 
156 #endif /* __UTIL_MATH_INT4_H__ */
ATTR_WARN_UNUSED_RESULT const BMVert * v
int x
Definition: btConvexHull.h:149
int w
Definition: btConvexHull.h:149
int y
Definition: btConvexHull.h:149
int z
Definition: btConvexHull.h:149
#define ccl_device_inline
#define CCL_NAMESPACE_END
#define make_int4(x, y, z, w)
static unsigned a[3]
Definition: RandGen.cpp:92
ccl_device_inline float4 mask(const int4 &mask, const float4 &a)
ccl_device_inline int4 load_int4(const int *v)
ccl_device_inline int4 operator>>(const int4 &a, int i)
ccl_device_inline int4 operator<<(const int4 &a, int i)
ccl_device_inline int4 operator<(const int4 &a, const int4 &b)
ccl_device_inline int4 operator&(const int4 &a, const int4 &b)
ccl_device_inline int4 clamp(const int4 &a, const int4 &mn, const int4 &mx)
ccl_device_inline int4 min(int4 a, int4 b)
ccl_device_inline int4 max(int4 a, int4 b)
CCL_NAMESPACE_BEGIN ccl_device_inline int4 operator+(const int4 &a, const int4 &b)
ccl_device_inline int4 operator>=(const int4 &a, const int4 &b)
ccl_device_inline int4 select(const int4 &mask, const int4 &a, const int4 &b)
ccl_device_inline int4 operator+=(int4 &a, const int4 &b)