util_math_int3.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. #ifndef __UTIL_MATH_INT3_H__
  17. #define __UTIL_MATH_INT3_H__
  18. #ifndef __UTIL_MATH_H__
  19. # error "Do not include this file directly, include util_types.h instead."
  20. #endif
  21. CCL_NAMESPACE_BEGIN
  22. /*******************************************************************************
  23. * Declaration.
  24. */
  25. #ifndef __KERNEL_OPENCL__
  26. ccl_device_inline int3 min(int3 a, int3 b);
  27. ccl_device_inline int3 max(int3 a, int3 b);
  28. ccl_device_inline int3 clamp(const int3 &a, int mn, int mx);
  29. ccl_device_inline int3 clamp(const int3 &a, int3 &mn, int mx);
  30. #endif /* !__KERNEL_OPENCL__ */
  31. /*******************************************************************************
  32. * Definition.
  33. */
  34. #ifndef __KERNEL_OPENCL__
  35. ccl_device_inline int3 min(int3 a, int3 b)
  36. {
  37. # if defined(__KERNEL_SSE__) && defined(__KERNEL_SSE41__)
  38. return int3(_mm_min_epi32(a.m128, b.m128));
  39. # else
  40. return make_int3(min(a.x, b.x), min(a.y, b.y), min(a.z, b.z));
  41. # endif
  42. }
  43. ccl_device_inline int3 max(int3 a, int3 b)
  44. {
  45. # if defined(__KERNEL_SSE__) && defined(__KERNEL_SSE41__)
  46. return int3(_mm_max_epi32(a.m128, b.m128));
  47. # else
  48. return make_int3(max(a.x, b.x), max(a.y, b.y), max(a.z, b.z));
  49. # endif
  50. }
  51. ccl_device_inline int3 clamp(const int3 &a, int mn, int mx)
  52. {
  53. # ifdef __KERNEL_SSE__
  54. return min(max(a, make_int3(mn)), make_int3(mx));
  55. # else
  56. return make_int3(clamp(a.x, mn, mx), clamp(a.y, mn, mx), clamp(a.z, mn, mx));
  57. # endif
  58. }
  59. ccl_device_inline int3 clamp(const int3 &a, int3 &mn, int mx)
  60. {
  61. # ifdef __KERNEL_SSE__
  62. return min(max(a, mn), make_int3(mx));
  63. # else
  64. return make_int3(clamp(a.x, mn.x, mx), clamp(a.y, mn.y, mx), clamp(a.z, mn.z, mx));
  65. # endif
  66. }
  67. ccl_device_inline bool operator==(const int3 &a, const int3 &b)
  68. {
  69. return a.x == b.x && a.y == b.y && a.z == b.z;
  70. }
  71. ccl_device_inline bool operator!=(const int3 &a, const int3 &b)
  72. {
  73. return !(a == b);
  74. }
  75. ccl_device_inline bool operator<(const int3 &a, const int3 &b)
  76. {
  77. return a.x < b.x && a.y < b.y && a.z < b.z;
  78. }
  79. ccl_device_inline int3 operator+(const int3 &a, const int3 &b)
  80. {
  81. # ifdef __KERNEL_SSE__
  82. return int3(_mm_add_epi32(a.m128, b.m128));
  83. # else
  84. return make_int3(a.x + b.x, a.y + b.y, a.z + b.z);
  85. # endif
  86. }
  87. ccl_device_inline int3 operator-(const int3 &a, const int3 &b)
  88. {
  89. # ifdef __KERNEL_SSE__
  90. return int3(_mm_sub_epi32(a.m128, b.m128));
  91. # else
  92. return make_int3(a.x - b.x, a.y - b.y, a.z - b.z);
  93. # endif
  94. }
  95. #endif /* !__KERNEL_OPENCL__ */
  96. CCL_NAMESPACE_END
  97. #endif /* __UTIL_MATH_INT3_H__ */