llsimdtypes.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /**
  2. * @file llsimdtypes.h
  3. * @brief Declaration of basic SIMD math related types
  4. *
  5. * $LicenseInfo:firstyear=2010&license=viewergpl$
  6. *
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #ifndef LL_SIMD_TYPES_H
  33. #define LL_SIMD_TYPES_H
  34. #if LL_DEBUG
  35. # include "llmemory.h" // For ll_assert_aligned()
  36. #endif
  37. typedef __m128 LLQuad;
  38. #if LL_WINDOWS
  39. # pragma warning(push)
  40. // Disable warning about casting int to bool for this class.
  41. # pragma warning( disable : 4800 3 )
  42. #endif // LL_WINDOWS
  43. class LLBool32
  44. {
  45. public:
  46. LL_INLINE LLBool32()
  47. {
  48. }
  49. LL_INLINE LLBool32(int rhs)
  50. : mBool(rhs)
  51. {
  52. }
  53. LL_INLINE LLBool32(unsigned int rhs)
  54. : mBool(rhs)
  55. {
  56. }
  57. LL_INLINE LLBool32(bool rhs)
  58. : mBool((int)rhs)
  59. {
  60. }
  61. LL_INLINE LLBool32& operator=(bool rhs)
  62. {
  63. mBool = (int)rhs;
  64. return *this;
  65. }
  66. LL_INLINE bool operator==(bool rhs) const
  67. {
  68. return (const bool&)mBool == rhs;
  69. }
  70. LL_INLINE bool operator!=(bool rhs) const
  71. {
  72. return (const bool&)mBool != rhs;
  73. }
  74. LL_INLINE operator bool() const
  75. {
  76. return (const bool&)mBool;
  77. }
  78. private:
  79. int mBool;
  80. };
  81. #if LL_WINDOWS
  82. # pragma warning(pop)
  83. #endif
  84. class alignas(16) LLSimdScalar
  85. {
  86. public:
  87. LL_INLINE LLSimdScalar() noexcept
  88. {
  89. }
  90. LL_INLINE LLSimdScalar(LLQuad q) noexcept
  91. {
  92. mQ = q;
  93. }
  94. LL_INLINE LLSimdScalar(F32 f) noexcept
  95. {
  96. mQ = _mm_set_ss(f);
  97. }
  98. LL_INLINE F32 getF32() const
  99. {
  100. F32 ret;
  101. _mm_store_ss(&ret, mQ);
  102. return ret;
  103. }
  104. LL_INLINE LLSimdScalar getAbs() const
  105. {
  106. alignas(16) thread_local const U32 F_ABS_MASK_4A[4] = {
  107. 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF
  108. };
  109. #if LL_DEBUG
  110. ll_assert_aligned(F_ABS_MASK_4A, 16);
  111. #endif
  112. return _mm_and_ps(mQ, *reinterpret_cast<const LLQuad*>(F_ABS_MASK_4A));
  113. }
  114. LL_INLINE void setMax(const LLSimdScalar& a, const LLSimdScalar& b)
  115. {
  116. mQ = _mm_max_ss(a, b);
  117. }
  118. LL_INLINE void setMin(const LLSimdScalar& a, const LLSimdScalar& b)
  119. {
  120. mQ = _mm_min_ss(a, b);
  121. }
  122. LL_INLINE LLSimdScalar& operator=(F32 rhs)
  123. {
  124. mQ = _mm_set_ss(rhs);
  125. return *this;
  126. }
  127. LL_INLINE LLSimdScalar& operator+=(const LLSimdScalar& rhs)
  128. {
  129. mQ = _mm_add_ss(mQ, rhs);
  130. return *this;
  131. }
  132. LL_INLINE LLSimdScalar& operator-=(const LLSimdScalar& rhs)
  133. {
  134. mQ = _mm_sub_ss(mQ, rhs);
  135. return *this;
  136. }
  137. LL_INLINE LLSimdScalar& operator*=(const LLSimdScalar& rhs)
  138. {
  139. mQ = _mm_mul_ss(mQ, rhs);
  140. return *this;
  141. }
  142. LL_INLINE LLSimdScalar& operator/=(const LLSimdScalar& rhs)
  143. {
  144. mQ = _mm_div_ss(mQ, rhs);
  145. return *this;
  146. }
  147. LL_INLINE operator LLQuad() const
  148. {
  149. return mQ;
  150. }
  151. LL_INLINE const LLQuad& getQuad() const
  152. {
  153. return mQ;
  154. }
  155. LL_INLINE LLBool32 isApproximatelyEqual(const LLSimdScalar& rhs,
  156. F32 tolerance = F_APPROXIMATELY_ZERO) const
  157. {
  158. const LLSimdScalar tol(tolerance);
  159. const LLSimdScalar diff = _mm_sub_ss(mQ, rhs.mQ);
  160. const LLSimdScalar abs_diff = diff.getAbs();
  161. return _mm_comile_ss(abs_diff, tol); // return abs_diff <= tol;
  162. }
  163. static LL_INLINE const LLSimdScalar& getZero()
  164. {
  165. extern const LLQuad F_ZERO_4A;
  166. return reinterpret_cast<const LLSimdScalar&>(F_ZERO_4A);
  167. }
  168. private:
  169. LLQuad mQ;
  170. };
  171. LL_INLINE LLSimdScalar operator+(const LLSimdScalar& a, const LLSimdScalar& b)
  172. {
  173. LLSimdScalar t(a);
  174. t += b;
  175. return t;
  176. }
  177. LL_INLINE LLSimdScalar operator-(const LLSimdScalar& a, const LLSimdScalar& b)
  178. {
  179. LLSimdScalar t(a);
  180. t -= b;
  181. return t;
  182. }
  183. LL_INLINE LLSimdScalar operator*(const LLSimdScalar& a, const LLSimdScalar& b)
  184. {
  185. LLSimdScalar t(a);
  186. t *= b;
  187. return t;
  188. }
  189. LL_INLINE LLSimdScalar operator/(const LLSimdScalar& a, const LLSimdScalar& b)
  190. {
  191. LLSimdScalar t(a);
  192. t /= b;
  193. return t;
  194. }
  195. LL_INLINE LLSimdScalar operator-(const LLSimdScalar& a)
  196. {
  197. alignas(16) thread_local const U32 signMask[4] = {
  198. 0x80000000, 0x80000000, 0x80000000, 0x80000000
  199. };
  200. #if LL_DEBUG
  201. ll_assert_aligned(signMask, 16);
  202. #endif
  203. return _mm_xor_ps(*reinterpret_cast<const LLQuad*>(signMask), a);
  204. }
  205. LL_INLINE LLBool32 operator==(const LLSimdScalar& a, const LLSimdScalar& b)
  206. {
  207. return _mm_comieq_ss(a, b);
  208. }
  209. LL_INLINE LLBool32 operator!=(const LLSimdScalar& a, const LLSimdScalar& b)
  210. {
  211. return _mm_comineq_ss(a, b);
  212. }
  213. LL_INLINE LLBool32 operator<(const LLSimdScalar& a, const LLSimdScalar& b)
  214. {
  215. return _mm_comilt_ss(a, b);
  216. }
  217. LL_INLINE LLBool32 operator<=(const LLSimdScalar& a, const LLSimdScalar& b)
  218. {
  219. return _mm_comile_ss(a, b);
  220. }
  221. LL_INLINE LLBool32 operator>(const LLSimdScalar& a, const LLSimdScalar& b)
  222. {
  223. return _mm_comigt_ss(a, b);
  224. }
  225. LL_INLINE LLBool32 operator>=(const LLSimdScalar& a, const LLSimdScalar& b)
  226. {
  227. return _mm_comige_ss(a, b);
  228. }
  229. #endif //LL_SIMD_TYPES_H