Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

5173 lines
169 KiB

  1. /*++
  2. Copyright (c) Microsoft Corporation. All rights reserved.
  3. Module Name:
  4. xnamathconvert.inl
  5. Abstract:
  6. XNA math library for Windows and Xbox 360: Conversion, loading, and storing functions.
  7. --*/
  8. #if defined(_MSC_VER) && (_MSC_VER > 1000)
  9. #pragma once
  10. #endif
  11. #ifndef __XNAMATHCONVERT_INL__
  12. #define __XNAMATHCONVERT_INL__
  13. #define XM_PACK_FACTOR (FLOAT)(1 << 22)
  14. #define XM_UNPACK_FACTOR_UNSIGNED (FLOAT)(1 << 23)
  15. #define XM_UNPACK_FACTOR_SIGNED XM_PACK_FACTOR
  16. #define XM_UNPACK_UNSIGNEDN_OFFSET(BitsX, BitsY, BitsZ, BitsW) \
  17. {-XM_UNPACK_FACTOR_UNSIGNED / (FLOAT)((1 << (BitsX)) - 1), \
  18. -XM_UNPACK_FACTOR_UNSIGNED / (FLOAT)((1 << (BitsY)) - 1), \
  19. -XM_UNPACK_FACTOR_UNSIGNED / (FLOAT)((1 << (BitsZ)) - 1), \
  20. -XM_UNPACK_FACTOR_UNSIGNED / (FLOAT)((1 << (BitsW)) - 1)}
  21. #define XM_UNPACK_UNSIGNEDN_SCALE(BitsX, BitsY, BitsZ, BitsW) \
  22. {XM_UNPACK_FACTOR_UNSIGNED / (FLOAT)((1 << (BitsX)) - 1), \
  23. XM_UNPACK_FACTOR_UNSIGNED / (FLOAT)((1 << (BitsY)) - 1), \
  24. XM_UNPACK_FACTOR_UNSIGNED / (FLOAT)((1 << (BitsZ)) - 1), \
  25. XM_UNPACK_FACTOR_UNSIGNED / (FLOAT)((1 << (BitsW)) - 1)}
  26. #define XM_UNPACK_SIGNEDN_SCALE(BitsX, BitsY, BitsZ, BitsW) \
  27. {-XM_UNPACK_FACTOR_SIGNED / (FLOAT)((1 << ((BitsX) - 1)) - 1), \
  28. -XM_UNPACK_FACTOR_SIGNED / (FLOAT)((1 << ((BitsY) - 1)) - 1), \
  29. -XM_UNPACK_FACTOR_SIGNED / (FLOAT)((1 << ((BitsZ) - 1)) - 1), \
  30. -XM_UNPACK_FACTOR_SIGNED / (FLOAT)((1 << ((BitsW) - 1)) - 1)}
  31. //#define XM_UNPACK_SIGNEDN_OFFSET(BitsX, BitsY, BitsZ, BitsW) \
  32. // {-XM_UNPACK_FACTOR_SIGNED / (FLOAT)((1 << ((BitsX) - 1)) - 1) * 3.0f, \
  33. // -XM_UNPACK_FACTOR_SIGNED / (FLOAT)((1 << ((BitsY) - 1)) - 1) * 3.0f, \
  34. // -XM_UNPACK_FACTOR_SIGNED / (FLOAT)((1 << ((BitsZ) - 1)) - 1) * 3.0f, \
  35. // -XM_UNPACK_FACTOR_SIGNED / (FLOAT)((1 << ((BitsW) - 1)) - 1) * 3.0f}
  36. #define XM_PACK_UNSIGNEDN_SCALE(BitsX, BitsY, BitsZ, BitsW) \
  37. {-(FLOAT)((1 << (BitsX)) - 1) / XM_PACK_FACTOR, \
  38. -(FLOAT)((1 << (BitsY)) - 1) / XM_PACK_FACTOR, \
  39. -(FLOAT)((1 << (BitsZ)) - 1) / XM_PACK_FACTOR, \
  40. -(FLOAT)((1 << (BitsW)) - 1) / XM_PACK_FACTOR}
  41. #define XM_PACK_SIGNEDN_SCALE(BitsX, BitsY, BitsZ, BitsW) \
  42. {-(FLOAT)((1 << ((BitsX) - 1)) - 1) / XM_PACK_FACTOR, \
  43. -(FLOAT)((1 << ((BitsY) - 1)) - 1) / XM_PACK_FACTOR, \
  44. -(FLOAT)((1 << ((BitsZ) - 1)) - 1) / XM_PACK_FACTOR, \
  45. -(FLOAT)((1 << ((BitsW) - 1)) - 1) / XM_PACK_FACTOR}
  46. #define XM_PACK_OFFSET XMVectorSplatConstant(3, 0)
  47. //#define XM_UNPACK_OFFSET XM_PACK_OFFSET
  48. /****************************************************************************
  49. *
  50. * Data conversion
  51. *
  52. ****************************************************************************/
  53. //------------------------------------------------------------------------------
  54. XMFINLINE FLOAT XMConvertHalfToFloat
  55. (
  56. HALF Value
  57. )
  58. {
  59. #if defined(_XM_NO_INTRINSICS_) || defined(_XM_SSE_INTRINSICS_)
  60. UINT Mantissa;
  61. UINT Exponent;
  62. UINT Result;
  63. Mantissa = (UINT)(Value & 0x03FF);
  64. if ((Value & 0x7C00) != 0) // The value is normalized
  65. {
  66. Exponent = (UINT)((Value >> 10) & 0x1F);
  67. }
  68. else if (Mantissa != 0) // The value is denormalized
  69. {
  70. // Normalize the value in the resulting float
  71. Exponent = 1;
  72. do
  73. {
  74. Exponent--;
  75. Mantissa <<= 1;
  76. } while ((Mantissa & 0x0400) == 0);
  77. Mantissa &= 0x03FF;
  78. }
  79. else // The value is zero
  80. {
  81. Exponent = (UINT)-112;
  82. }
  83. Result = ((Value & 0x8000) << 16) | // Sign
  84. ((Exponent + 112) << 23) | // Exponent
  85. (Mantissa << 13); // Mantissa
  86. return *(FLOAT*)&Result;
  87. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  88. #endif
  89. }
  90. //------------------------------------------------------------------------------
  91. XMINLINE FLOAT* XMConvertHalfToFloatStream
  92. (
  93. FLOAT* pOutputStream,
  94. UINT OutputStride,
  95. CONST HALF* pInputStream,
  96. UINT InputStride,
  97. UINT HalfCount
  98. )
  99. {
  100. #if defined(_XM_NO_INTRINSICS_) || defined(_XM_SSE_INTRINSICS_)
  101. UINT i;
  102. BYTE* pHalf = (BYTE*)pInputStream;
  103. BYTE* pFloat = (BYTE*)pOutputStream;
  104. XMASSERT(pOutputStream);
  105. XMASSERT(pInputStream);
  106. for (i = 0; i < HalfCount; i++)
  107. {
  108. *(FLOAT*)pFloat = XMConvertHalfToFloat(*(HALF*)pHalf);
  109. pHalf += InputStride;
  110. pFloat += OutputStride;
  111. }
  112. return pOutputStream;
  113. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  114. #endif // _XM_VMX128_INTRINSICS_
  115. }
  116. //------------------------------------------------------------------------------
  117. XMFINLINE HALF XMConvertFloatToHalf
  118. (
  119. FLOAT Value
  120. )
  121. {
  122. #if defined(_XM_NO_INTRINSICS_) || defined(_XM_SSE_INTRINSICS_)
  123. UINT Result;
  124. UINT IValue = ((UINT *)(&Value))[0];
  125. UINT Sign = (IValue & 0x80000000U) >> 16U;
  126. IValue = IValue & 0x7FFFFFFFU; // Hack off the sign
  127. if (IValue > 0x47FFEFFFU)
  128. {
  129. // The number is too large to be represented as a half. Saturate to infinity.
  130. Result = 0x7FFFU;
  131. }
  132. else
  133. {
  134. if (IValue < 0x38800000U)
  135. {
  136. // The number is too small to be represented as a normalized half.
  137. // Convert it to a denormalized value.
  138. UINT Shift = 113U - (IValue >> 23U);
  139. IValue = (0x800000U | (IValue & 0x7FFFFFU)) >> Shift;
  140. }
  141. else
  142. {
  143. // Rebias the exponent to represent the value as a normalized half.
  144. IValue += 0xC8000000U;
  145. }
  146. Result = ((IValue + 0x0FFFU + ((IValue >> 13U) & 1U)) >> 13U)&0x7FFFU;
  147. }
  148. return (HALF)(Result|Sign);
  149. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  150. #endif
  151. }
  152. //------------------------------------------------------------------------------
  153. XMINLINE HALF* XMConvertFloatToHalfStream
  154. (
  155. HALF* pOutputStream,
  156. UINT OutputStride,
  157. CONST FLOAT* pInputStream,
  158. UINT InputStride,
  159. UINT FloatCount
  160. )
  161. {
  162. #if defined(_XM_NO_INTRINSICS_) || defined(_XM_SSE_INTRINSICS_)
  163. UINT i;
  164. BYTE* pFloat = (BYTE*)pInputStream;
  165. BYTE* pHalf = (BYTE*)pOutputStream;
  166. XMASSERT(pOutputStream);
  167. XMASSERT(pInputStream);
  168. for (i = 0; i < FloatCount; i++)
  169. {
  170. *(HALF*)pHalf = XMConvertFloatToHalf(*(FLOAT*)pFloat);
  171. pFloat += InputStride;
  172. pHalf += OutputStride;
  173. }
  174. return pOutputStream;
  175. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  176. #endif // _XM_VMX128_INTRINSICS_
  177. }
  178. //------------------------------------------------------------------------------
  179. #if defined(_XM_NO_INTRINSICS_) || defined(_XM_SSE_INTRINSICS_)
  180. // For VMX128, these routines are all defines in the main header
  181. #pragma warning(push)
  182. #pragma warning(disable:4701) // Prevent warnings about 'Result' potentially being used without having been initialized
  183. XMINLINE XMVECTOR XMConvertVectorIntToFloat
  184. (
  185. FXMVECTOR VInt,
  186. UINT DivExponent
  187. )
  188. {
  189. #if defined(_XM_NO_INTRINSICS_)
  190. UINT ElementIndex;
  191. FLOAT fScale;
  192. XMVECTOR Result;
  193. XMASSERT(DivExponent<32);
  194. fScale = 1.0f / (FLOAT)(1U << DivExponent);
  195. ElementIndex = 0;
  196. do {
  197. INT iTemp = (INT)VInt.u[ElementIndex];
  198. Result.v[ElementIndex] = ((FLOAT)iTemp) * fScale;
  199. } while (++ElementIndex<4);
  200. return Result;
  201. #else // _XM_SSE_INTRINSICS_
  202. XMASSERT(DivExponent<32);
  203. // Convert to floats
  204. XMVECTOR vResult = _mm_cvtepi32_ps(reinterpret_cast<const __m128i *>(&VInt)[0]);
  205. // Convert DivExponent into 1.0f/(1<<DivExponent)
  206. UINT uScale = 0x3F800000U - (DivExponent << 23);
  207. // Splat the scalar value
  208. __m128i vScale = _mm_set1_epi32(uScale);
  209. vResult = _mm_mul_ps(vResult,reinterpret_cast<const __m128 *>(&vScale)[0]);
  210. return vResult;
  211. #endif
  212. }
  213. //------------------------------------------------------------------------------
  214. XMINLINE XMVECTOR XMConvertVectorFloatToInt
  215. (
  216. FXMVECTOR VFloat,
  217. UINT MulExponent
  218. )
  219. {
  220. #if defined(_XM_NO_INTRINSICS_)
  221. UINT ElementIndex;
  222. XMVECTOR Result;
  223. FLOAT fScale;
  224. XMASSERT(MulExponent<32);
  225. // Get the scalar factor.
  226. fScale = (FLOAT)(1U << MulExponent);
  227. ElementIndex = 0;
  228. do {
  229. INT iResult;
  230. FLOAT fTemp = VFloat.v[ElementIndex]*fScale;
  231. if (fTemp <= -(65536.0f*32768.0f)) {
  232. iResult = (-0x7FFFFFFF)-1;
  233. } else if (fTemp > (65536.0f*32768.0f)-128.0f) {
  234. iResult = 0x7FFFFFFF;
  235. } else {
  236. iResult = (INT)fTemp;
  237. }
  238. Result.u[ElementIndex] = (UINT)iResult;
  239. } while (++ElementIndex<4);
  240. return Result;
  241. #else // _XM_SSE_INTRINSICS_
  242. XMASSERT(MulExponent<32);
  243. static const XMVECTORF32 g_XMMaxInt = {65536.0f*32768.0f-128.0f,65536.0f*32768.0f-128.0f,65536.0f*32768.0f-128.0f,65536.0f*32768.0f-128.0f};
  244. XMVECTOR vResult = _mm_set_ps1((FLOAT)(1U << MulExponent));
  245. vResult = _mm_mul_ps(vResult,VFloat);
  246. // In case of positive overflow, detect it
  247. XMVECTOR vOverflow = _mm_cmpgt_ps(vResult,g_XMMaxInt);
  248. // Float to int conversion
  249. __m128i vResulti = _mm_cvttps_epi32(vResult);
  250. // If there was positive overflow, set to 0x7FFFFFFF
  251. vResult = _mm_and_ps(vOverflow,g_XMAbsMask);
  252. vOverflow = _mm_andnot_ps(vOverflow,reinterpret_cast<const __m128 *>(&vResulti)[0]);
  253. vOverflow = _mm_or_ps(vOverflow,vResult);
  254. return vOverflow;
  255. #endif
  256. }
  257. //------------------------------------------------------------------------------
  258. XMINLINE XMVECTOR XMConvertVectorUIntToFloat
  259. (
  260. FXMVECTOR VUInt,
  261. UINT DivExponent
  262. )
  263. {
  264. #if defined(_XM_NO_INTRINSICS_)
  265. UINT ElementIndex;
  266. FLOAT fScale;
  267. XMVECTOR Result;
  268. XMASSERT(DivExponent<32);
  269. fScale = 1.0f / (FLOAT)(1U << DivExponent);
  270. ElementIndex = 0;
  271. do {
  272. Result.v[ElementIndex] = (FLOAT)VUInt.u[ElementIndex] * fScale;
  273. } while (++ElementIndex<4);
  274. return Result;
  275. #else // _XM_SSE_INTRINSICS_
  276. XMASSERT(DivExponent<32);
  277. static const XMVECTORF32 g_XMFixUnsigned = {32768.0f*65536.0f,32768.0f*65536.0f,32768.0f*65536.0f,32768.0f*65536.0f};
  278. // For the values that are higher than 0x7FFFFFFF, a fixup is needed
  279. // Determine which ones need the fix.
  280. XMVECTOR vMask = _mm_and_ps(VUInt,g_XMNegativeZero);
  281. // Force all values positive
  282. XMVECTOR vResult = _mm_xor_ps(VUInt,vMask);
  283. // Convert to floats
  284. vResult = _mm_cvtepi32_ps(reinterpret_cast<const __m128i *>(&vResult)[0]);
  285. // Convert 0x80000000 -> 0xFFFFFFFF
  286. __m128i iMask = _mm_srai_epi32(reinterpret_cast<const __m128i *>(&vMask)[0],31);
  287. // For only the ones that are too big, add the fixup
  288. vMask = _mm_and_ps(reinterpret_cast<const __m128 *>(&iMask)[0],g_XMFixUnsigned);
  289. vResult = _mm_add_ps(vResult,vMask);
  290. // Convert DivExponent into 1.0f/(1<<DivExponent)
  291. UINT uScale = 0x3F800000U - (DivExponent << 23);
  292. // Splat
  293. iMask = _mm_set1_epi32(uScale);
  294. vResult = _mm_mul_ps(vResult,reinterpret_cast<const __m128 *>(&iMask)[0]);
  295. return vResult;
  296. #endif
  297. }
  298. //------------------------------------------------------------------------------
  299. XMINLINE XMVECTOR XMConvertVectorFloatToUInt
  300. (
  301. FXMVECTOR VFloat,
  302. UINT MulExponent
  303. )
  304. {
  305. #if defined(_XM_NO_INTRINSICS_)
  306. UINT ElementIndex;
  307. XMVECTOR Result;
  308. FLOAT fScale;
  309. XMASSERT(MulExponent<32);
  310. // Get the scalar factor.
  311. fScale = (FLOAT)(1U << MulExponent);
  312. ElementIndex = 0;
  313. do {
  314. UINT uResult;
  315. FLOAT fTemp = VFloat.v[ElementIndex]*fScale;
  316. if (fTemp <= 0.0f) {
  317. uResult = 0;
  318. } else if (fTemp >= (65536.0f*65536.0f)) {
  319. uResult = 0xFFFFFFFFU;
  320. } else {
  321. uResult = (UINT)fTemp;
  322. }
  323. Result.u[ElementIndex] = uResult;
  324. } while (++ElementIndex<4);
  325. return Result;
  326. #else // _XM_SSE_INTRINSICS_
  327. XMASSERT(MulExponent<32);
  328. static const XMVECTORF32 g_XMMaxUInt = {65536.0f*65536.0f-256.0f,65536.0f*65536.0f-256.0f,65536.0f*65536.0f-256.0f,65536.0f*65536.0f-256.0f};
  329. static const XMVECTORF32 g_XMUnsignedFix = {32768.0f*65536.0f,32768.0f*65536.0f,32768.0f*65536.0f,32768.0f*65536.0f};
  330. XMVECTOR vResult = _mm_set_ps1(static_cast<float>(1U << MulExponent));
  331. vResult = _mm_mul_ps(vResult,VFloat);
  332. // Clamp to >=0
  333. vResult = _mm_max_ps(vResult,g_XMZero);
  334. // Any numbers that are too big, set to 0xFFFFFFFFU
  335. XMVECTOR vOverflow = _mm_cmpgt_ps(vResult,g_XMMaxUInt);
  336. XMVECTOR vValue = g_XMUnsignedFix;
  337. // Too large for a signed integer?
  338. XMVECTOR vMask = _mm_cmpge_ps(vResult,vValue);
  339. // Zero for number's lower than 0x80000000, 32768.0f*65536.0f otherwise
  340. vValue = _mm_and_ps(vValue,vMask);
  341. // Perform fixup only on numbers too large (Keeps low bit precision)
  342. vResult = _mm_sub_ps(vResult,vValue);
  343. __m128i vResulti = _mm_cvttps_epi32(vResult);
  344. // Convert from signed to unsigned pnly if greater than 0x80000000
  345. vMask = _mm_and_ps(vMask,g_XMNegativeZero);
  346. vResult = _mm_xor_ps(reinterpret_cast<const __m128 *>(&vResulti)[0],vMask);
  347. // On those that are too large, set to 0xFFFFFFFF
  348. vResult = _mm_or_ps(vResult,vOverflow);
  349. return vResult;
  350. #endif
  351. }
  352. #pragma warning(pop)
  353. #endif // _XM_NO_INTRINSICS_ || _XM_SSE_INTRINSICS_
  354. /****************************************************************************
  355. *
  356. * Vector and matrix load operations
  357. *
  358. ****************************************************************************/
  359. //------------------------------------------------------------------------------
  360. XMFINLINE XMVECTOR XMLoadInt(CONST UINT* pSource)
  361. {
  362. #if defined(_XM_NO_INTRINSICS_)
  363. XMVECTOR V;
  364. XMASSERT(pSource);
  365. XMASSERT(((UINT_PTR)pSource & 3) == 0);
  366. V.u[0] = *pSource;
  367. return V;
  368. #elif defined(_XM_SSE_INTRINSICS_)
  369. XMASSERT(pSource);
  370. XMASSERT(((UINT_PTR)pSource & 3) == 0);
  371. __m128i V = _mm_set_epi32( 0, 0, 0, *pSource );
  372. return reinterpret_cast<__m128 *>(&V)[0];
  373. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  374. #endif // _XM_VMX128_INTRINSICS_
  375. }
  376. //------------------------------------------------------------------------------
  377. XMFINLINE XMVECTOR XMLoadFloat(CONST FLOAT* pSource)
  378. {
  379. #if defined(_XM_NO_INTRINSICS_)
  380. XMVECTOR V;
  381. XMASSERT(pSource);
  382. XMASSERT(((UINT_PTR)pSource & 3) == 0);
  383. V.v[0] = *pSource;
  384. return V;
  385. #elif defined(_XM_SSE_INTRINSICS_)
  386. XMASSERT(pSource);
  387. XMASSERT(((UINT_PTR)pSource & 3) == 0);
  388. return _mm_load_ss( pSource );
  389. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  390. #endif // _XM_VMX128_INTRINSICS_
  391. }
  392. //------------------------------------------------------------------------------
  393. XMFINLINE XMVECTOR XMLoadInt2
  394. (
  395. CONST UINT* pSource
  396. )
  397. {
  398. #if defined(_XM_NO_INTRINSICS_)
  399. XMVECTOR V;
  400. XMASSERT(pSource);
  401. V.u[0] = pSource[0];
  402. V.u[1] = pSource[1];
  403. return V;
  404. #elif defined(_XM_SSE_INTRINSICS_)
  405. XMASSERT(pSource);
  406. __m128i V = _mm_set_epi32( 0, 0, *(pSource+1), *pSource );
  407. return reinterpret_cast<__m128 *>(&V)[0];
  408. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  409. #endif // _XM_VMX128_INTRINSICS_
  410. }
  411. //------------------------------------------------------------------------------
  412. XMFINLINE XMVECTOR XMLoadInt2A
  413. (
  414. CONST UINT* pSource
  415. )
  416. {
  417. #if defined(_XM_NO_INTRINSICS_)
  418. XMVECTOR V;
  419. XMASSERT(pSource);
  420. XMASSERT(((UINT_PTR)pSource & 0xF) == 0);
  421. V.u[0] = pSource[0];
  422. V.u[1] = pSource[1];
  423. return V;
  424. #elif defined(_XM_SSE_INTRINSICS_)
  425. XMASSERT(pSource);
  426. __m128i V = _mm_loadl_epi64( (const __m128i*)pSource );
  427. return reinterpret_cast<__m128 *>(&V)[0];
  428. #else // _XM_VMX128_INTRINSICS_
  429. #endif // _XM_VMX128_INTRINSICS_
  430. }
  431. //------------------------------------------------------------------------------
  432. XMFINLINE XMVECTOR XMLoadFloat2
  433. (
  434. CONST XMFLOAT2* pSource
  435. )
  436. {
  437. #if defined(_XM_NO_INTRINSICS_)
  438. XMVECTOR V;
  439. XMASSERT(pSource);
  440. ((UINT *)(&V.x))[0] = ((const UINT *)(&pSource->x))[0];
  441. ((UINT *)(&V.y))[0] = ((const UINT *)(&pSource->y))[0];
  442. V.z = 0.0f;
  443. V.w = 0.0f;
  444. return V;
  445. #elif defined(_XM_SSE_INTRINSICS_)
  446. // This reads 2 floats past the memory that should be ignored.
  447. XMASSERT(pSource);
  448. return _mm_loadu_ps( &pSource->x );
  449. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  450. #endif // _XM_VMX128_INTRINSICS_
  451. }
  452. //------------------------------------------------------------------------------
  453. XMFINLINE XMVECTOR XMLoadFloat2A
  454. (
  455. CONST XMFLOAT2A* pSource
  456. )
  457. {
  458. #if defined(_XM_NO_INTRINSICS_)
  459. XMVECTOR V;
  460. XMASSERT(pSource);
  461. XMASSERT(((UINT_PTR)pSource & 0xF) == 0);
  462. V.v[0] = pSource->x;
  463. V.v[1] = pSource->y;
  464. return V;
  465. #elif defined(_XM_SSE_INTRINSICS_)
  466. XMASSERT(pSource);
  467. // This reads 2 floats past the memory that should be ignored.
  468. return _mm_load_ps( &pSource->x );
  469. #else // _XM_VMX128_INTRINSICS_
  470. #endif // _XM_VMX128_INTRINSICS_
  471. }
  472. //------------------------------------------------------------------------------
  473. XMFINLINE XMVECTOR XMLoadHalf2
  474. (
  475. CONST XMHALF2* pSource
  476. )
  477. {
  478. #if defined(_XM_NO_INTRINSICS_)
  479. XMASSERT(pSource);
  480. {
  481. XMVECTOR vResult = {
  482. XMConvertHalfToFloat(pSource->x),
  483. XMConvertHalfToFloat(pSource->y),
  484. 0.0f,
  485. 0.0f
  486. };
  487. return vResult;
  488. }
  489. #elif defined(_XM_SSE_INTRINSICS_)
  490. XMASSERT(pSource);
  491. XMVECTOR vResult = {
  492. XMConvertHalfToFloat(pSource->x),
  493. XMConvertHalfToFloat(pSource->y),
  494. 0.0f,
  495. 0.0f
  496. };
  497. return vResult;
  498. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  499. #endif // _XM_VMX128_INTRINSICS_
  500. }
  501. //------------------------------------------------------------------------------
  502. XMFINLINE XMVECTOR XMLoadShortN2
  503. (
  504. CONST XMSHORTN2* pSource
  505. )
  506. {
  507. #if defined(_XM_NO_INTRINSICS_)
  508. XMASSERT(pSource);
  509. XMASSERT(pSource->x != -32768);
  510. XMASSERT(pSource->y != -32768);
  511. {
  512. XMVECTOR vResult = {
  513. (FLOAT)pSource->x * (1.0f/32767.0f),
  514. (FLOAT)pSource->y * (1.0f/32767.0f),
  515. 0.0f,
  516. 0.0f
  517. };
  518. return vResult;
  519. }
  520. #elif defined(_XM_SSE_INTRINSICS_)
  521. XMASSERT(pSource);
  522. XMASSERT(pSource->x != -32768);
  523. XMASSERT(pSource->y != -32768);
  524. // Splat the two shorts in all four entries (WORD alignment okay,
  525. // DWORD alignment preferred)
  526. __m128 vTemp = _mm_load_ps1(reinterpret_cast<const float *>(&pSource->x));
  527. // Mask x&0xFFFF, y&0xFFFF0000,z&0,w&0
  528. vTemp = _mm_and_ps(vTemp,g_XMMaskX16Y16);
  529. // x needs to be sign extended
  530. vTemp = _mm_xor_ps(vTemp,g_XMFlipX16Y16);
  531. // Convert to floating point numbers
  532. vTemp = _mm_cvtepi32_ps(reinterpret_cast<const __m128i *>(&vTemp)[0]);
  533. // x - 0x8000 to undo the signed order.
  534. vTemp = _mm_add_ps(vTemp,g_XMFixX16Y16);
  535. // Convert 0-32767 to 0.0f-1.0f
  536. return _mm_mul_ps(vTemp,g_XMNormalizeX16Y16);
  537. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  538. #endif // _XM_VMX128_INTRINSICS_
  539. }
  540. //------------------------------------------------------------------------------
  541. XMFINLINE XMVECTOR XMLoadShort2
  542. (
  543. CONST XMSHORT2* pSource
  544. )
  545. {
  546. #if defined(_XM_NO_INTRINSICS_)
  547. XMVECTOR V;
  548. XMASSERT(pSource);
  549. XMASSERT(pSource->x != -32768);
  550. XMASSERT(pSource->y != -32768);
  551. V.v[0] = (FLOAT)pSource->x;
  552. V.v[1] = (FLOAT)pSource->y;
  553. return V;
  554. #elif defined(_XM_SSE_INTRINSICS_)
  555. static const XMVECTORF32 g_XMFixupY16 = {1.0f,1.0f/65536.0f,0.0f,0.0f};
  556. XMASSERT(pSource);
  557. XMASSERT(pSource->x != -32768);
  558. XMASSERT(pSource->y != -32768);
  559. // Splat the two shorts in all four entries (WORD alignment okay,
  560. // DWORD alignment preferred)
  561. __m128 vTemp = _mm_load_ps1(reinterpret_cast<const float *>(&pSource->x));
  562. // Mask x&0xFFFF, y&0xFFFF0000,z&0,w&0
  563. vTemp = _mm_and_ps(vTemp,g_XMMaskX16Y16);
  564. // x needs to be sign extended
  565. vTemp = _mm_xor_ps(vTemp,g_XMFlipX16Y16);
  566. // Convert to floating point numbers
  567. vTemp = _mm_cvtepi32_ps(reinterpret_cast<const __m128i *>(&vTemp)[0]);
  568. // x - 0x8000 to undo the signed order.
  569. vTemp = _mm_add_ps(vTemp,g_XMFixX16Y16);
  570. // Y is 65536 too large
  571. return _mm_mul_ps(vTemp,g_XMFixupY16);
  572. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  573. #endif // _XM_VMX128_INTRINSICS_
  574. }
  575. //------------------------------------------------------------------------------
  576. XMFINLINE XMVECTOR XMLoadUShortN2
  577. (
  578. CONST XMUSHORTN2* pSource
  579. )
  580. {
  581. #if defined(_XM_NO_INTRINSICS_)
  582. XMVECTOR V;
  583. XMASSERT(pSource);
  584. V.v[0] = (FLOAT)pSource->x / 65535.0f;
  585. V.v[1] = (FLOAT)pSource->y / 65535.0f;
  586. return V;
  587. #elif defined(_XM_SSE_INTRINSICS_)
  588. static const XMVECTORF32 g_XMFixupY16 = {1.0f/65535.0f,1.0f/(65535.0f*65536.0f),0.0f,0.0f};
  589. static const XMVECTORI32 g_XMFlipY = {0,0x80000000,0,0};
  590. static const XMVECTORF32 g_XMFixaddY16 = {0,32768.0f*65536.0f,0,0};
  591. XMASSERT(pSource);
  592. // Splat the two shorts in all four entries (WORD alignment okay,
  593. // DWORD alignment preferred)
  594. __m128 vTemp = _mm_load_ps1(reinterpret_cast<const float *>(&pSource->x));
  595. // Mask x&0xFFFF, y&0xFFFF0000,z&0,w&0
  596. vTemp = _mm_and_ps(vTemp,g_XMMaskX16Y16);
  597. // y needs to be sign flipped
  598. vTemp = _mm_xor_ps(vTemp,g_XMFlipY);
  599. // Convert to floating point numbers
  600. vTemp = _mm_cvtepi32_ps(reinterpret_cast<const __m128i *>(&vTemp)[0]);
  601. // y + 0x8000 to undo the signed order.
  602. vTemp = _mm_add_ps(vTemp,g_XMFixaddY16);
  603. // Y is 65536 times too large
  604. vTemp = _mm_mul_ps(vTemp,g_XMFixupY16);
  605. return vTemp;
  606. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  607. #endif // _XM_VMX128_INTRINSICS_
  608. }
  609. //------------------------------------------------------------------------------
  610. XMFINLINE XMVECTOR XMLoadUShort2
  611. (
  612. CONST XMUSHORT2* pSource
  613. )
  614. {
  615. #if defined(_XM_NO_INTRINSICS_)
  616. XMVECTOR V;
  617. XMASSERT(pSource);
  618. V.v[0] = (FLOAT)pSource->x;
  619. V.v[1] = (FLOAT)pSource->y;
  620. return V;
  621. #elif defined(_XM_SSE_INTRINSICS_)
  622. static const XMVECTORF32 g_XMFixupY16 = {1.0f,1.0f/65536.0f,0.0f,0.0f};
  623. static const XMVECTORI32 g_XMFlipY = {0,0x80000000,0,0};
  624. static const XMVECTORF32 g_XMFixaddY16 = {0,32768.0f,0,0};
  625. XMASSERT(pSource);
  626. // Splat the two shorts in all four entries (WORD alignment okay,
  627. // DWORD alignment preferred)
  628. __m128 vTemp = _mm_load_ps1(reinterpret_cast<const float *>(&pSource->x));
  629. // Mask x&0xFFFF, y&0xFFFF0000,z&0,w&0
  630. vTemp = _mm_and_ps(vTemp,g_XMMaskX16Y16);
  631. // y needs to be sign flipped
  632. vTemp = _mm_xor_ps(vTemp,g_XMFlipY);
  633. // Convert to floating point numbers
  634. vTemp = _mm_cvtepi32_ps(reinterpret_cast<const __m128i *>(&vTemp)[0]);
  635. // Y is 65536 times too large
  636. vTemp = _mm_mul_ps(vTemp,g_XMFixupY16);
  637. // y + 0x8000 to undo the signed order.
  638. vTemp = _mm_add_ps(vTemp,g_XMFixaddY16);
  639. return vTemp;
  640. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  641. #endif // _XM_VMX128_INTRINSICS_
  642. }
  643. //------------------------------------------------------------------------------
  644. XMFINLINE XMVECTOR XMLoadInt3
  645. (
  646. CONST UINT* pSource
  647. )
  648. {
  649. #if defined(_XM_NO_INTRINSICS_)
  650. XMVECTOR V;
  651. XMASSERT(pSource);
  652. V.u[0] = pSource[0];
  653. V.u[1] = pSource[1];
  654. V.u[2] = pSource[2];
  655. return V;
  656. #elif defined(_XM_SSE_INTRINSICS_)
  657. XMASSERT(pSource);
  658. __m128i V = _mm_set_epi32( 0, *(pSource+2), *(pSource+1), *pSource );
  659. return reinterpret_cast<__m128 *>(&V)[0];
  660. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  661. #endif // _XM_VMX128_INTRINSICS_
  662. }
  663. //------------------------------------------------------------------------------
  664. XMFINLINE XMVECTOR XMLoadInt3A
  665. (
  666. CONST UINT* pSource
  667. )
  668. {
  669. #if defined(_XM_NO_INTRINSICS_)
  670. XMVECTOR V;
  671. XMASSERT(pSource);
  672. XMASSERT(((UINT_PTR)pSource & 0xF) == 0);
  673. V.u[0] = pSource[0];
  674. V.u[1] = pSource[1];
  675. V.u[2] = pSource[2];
  676. return V;
  677. #elif defined(_XM_SSE_INTRINSICS_)
  678. XMASSERT(pSource);
  679. // Reads an extra integer that is 'undefined'
  680. __m128i V = _mm_load_si128( (const __m128i*)pSource );
  681. return reinterpret_cast<__m128 *>(&V)[0];
  682. #else // _XM_VMX128_INTRINSICS_
  683. #endif // _XM_VMX128_INTRINSICS_
  684. }
  685. //------------------------------------------------------------------------------
  686. XMFINLINE XMVECTOR XMLoadFloat3
  687. (
  688. CONST XMFLOAT3* pSource
  689. )
  690. {
  691. #if defined(_XM_NO_INTRINSICS_)
  692. XMVECTOR V;
  693. XMASSERT(pSource);
  694. ((UINT *)(&V.x))[0] = ((const UINT *)(&pSource->x))[0];
  695. ((UINT *)(&V.y))[0] = ((const UINT *)(&pSource->y))[0];
  696. ((UINT *)(&V.z))[0] = ((const UINT *)(&pSource->z))[0];
  697. V.w = 0.0f;
  698. return V;
  699. #elif defined(_XM_SSE_INTRINSICS_)
  700. XMASSERT(pSource);
  701. // This reads 1 floats past the memory that should be ignored.
  702. return _mm_loadu_ps( &pSource->x );
  703. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  704. #endif // _XM_VMX128_INTRINSICS_
  705. }
  706. //------------------------------------------------------------------------------
  707. XMFINLINE XMVECTOR XMLoadFloat3A
  708. (
  709. CONST XMFLOAT3A* pSource
  710. )
  711. {
  712. #if defined(_XM_NO_INTRINSICS_)
  713. XMVECTOR V;
  714. XMASSERT(pSource);
  715. XMASSERT(((UINT_PTR)pSource & 0xF) == 0);
  716. V.v[0] = pSource->x;
  717. V.v[1] = pSource->y;
  718. V.v[2] = pSource->z;
  719. return V;
  720. #elif defined(_XM_SSE_INTRINSICS_)
  721. XMASSERT(pSource);
  722. // This reads 1 floats past the memory that should be ignored.
  723. return _mm_load_ps( &pSource->x );
  724. #else // _XM_VMX128_INTRINSICS_
  725. #endif // _XM_VMX128_INTRINSICS_
  726. }
  727. //------------------------------------------------------------------------------
  728. XMFINLINE XMVECTOR XMLoadUHenDN3
  729. (
  730. CONST XMUHENDN3* pSource
  731. )
  732. {
  733. #if defined(_XM_NO_INTRINSICS_)
  734. XMVECTOR V;
  735. UINT Element;
  736. XMASSERT(pSource);
  737. Element = pSource->v & 0x7FF;
  738. V.v[0] = (FLOAT)Element / 2047.0f;
  739. Element = (pSource->v >> 11) & 0x7FF;
  740. V.v[1] = (FLOAT)Element / 2047.0f;
  741. Element = (pSource->v >> 22) & 0x3FF;
  742. V.v[2] = (FLOAT)Element / 1023.0f;
  743. return V;
  744. #elif defined(_XM_SSE_INTRINSICS_)
  745. static const XMVECTORI32 g_XMUHenDN3And = {0x7FF,0x7ff<<11,0x3FF<<22,0};
  746. static const XMVECTORI32 g_XMUHenDN3Xor = {0,0,0x80000000,0};
  747. static const XMVECTORF32 g_XMUHenDN3Add = {0,0,32768.0f*65536.0f,0};
  748. static const XMVECTORF32 g_XMUHenDN3Mul = {1.0f/2047.0f,1.0f/(2047.0f*2048.0f),1.0f/(1023.0f*2048.0f*2048.0f),0};
  749. XMASSERT(pSource);
  750. // Get the 32 bit value and splat it
  751. XMVECTOR vResult = _mm_load_ps1(reinterpret_cast<const float *>(&pSource->v));
  752. // Mask off x, y and z
  753. vResult = _mm_and_ps(vResult,g_XMUHenDN3And);
  754. // Convert x and y to unsigned
  755. vResult = _mm_xor_ps(vResult,g_XMUHenDN3Xor);
  756. // Convert to float
  757. vResult = _mm_cvtepi32_ps(reinterpret_cast<const __m128i *>(&vResult)[0]);
  758. // Convert x and y back to signed
  759. vResult = _mm_add_ps(vResult,g_XMUHenDN3Add);
  760. // Normalize x,y and z to -1.0f-1.0f
  761. vResult = _mm_mul_ps(vResult,g_XMUHenDN3Mul);
  762. return vResult;
  763. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  764. #endif // _XM_VMX128_INTRINSICS_
  765. }
  766. //------------------------------------------------------------------------------
  767. XMFINLINE XMVECTOR XMLoadUHenD3
  768. (
  769. CONST XMUHEND3* pSource
  770. )
  771. {
  772. #if defined(_XM_NO_INTRINSICS_)
  773. XMVECTOR V;
  774. UINT Element;
  775. XMASSERT(pSource);
  776. Element = pSource->v & 0x7FF;
  777. V.v[0] = (FLOAT)Element;
  778. Element = (pSource->v >> 11) & 0x7FF;
  779. V.v[1] = (FLOAT)Element;
  780. Element = (pSource->v >> 22) & 0x3FF;
  781. V.v[2] = (FLOAT)Element;
  782. return V;
  783. #elif defined(_XM_SSE_INTRINSICS_)
  784. static const XMVECTORI32 g_XMUHenD3And = {0x7FF,0x7ff<<11,0x3FF<<22,0};
  785. static const XMVECTORI32 g_XMUHenD3Xor = {0,0,0x80000000,0};
  786. static const XMVECTORF32 g_XMUHenD3Add = {0,0,32768.0f*65536.0f,0};
  787. static const XMVECTORF32 g_XMUHenD3Mul = {1.0f,1.0f/2048.0f,1.0f/(2048.0f*2048.0f),0};
  788. XMASSERT(pSource);
  789. // Get the 32 bit value and splat it
  790. XMVECTOR vResult = _mm_load_ps1(reinterpret_cast<const float *>(&pSource->v));
  791. // Mask off x, y and z
  792. vResult = _mm_and_ps(vResult,g_XMUHenD3And);
  793. // Convert x and y to unsigned
  794. vResult = _mm_xor_ps(vResult,g_XMUHenD3Xor);
  795. // Convert to float
  796. vResult = _mm_cvtepi32_ps(reinterpret_cast<const __m128i *>(&vResult)[0]);
  797. // Convert x and y back to signed
  798. vResult = _mm_add_ps(vResult,g_XMUHenD3Add);
  799. // Normalize x and y to -1024-1023.0f and z to -512-511.0f
  800. vResult = _mm_mul_ps(vResult,g_XMUHenD3Mul);
  801. return vResult;
  802. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  803. #endif // _XM_VMX128_INTRINSICS_
  804. }
  805. //------------------------------------------------------------------------------
  806. XMFINLINE XMVECTOR XMLoadHenDN3
  807. (
  808. CONST XMHENDN3* pSource
  809. )
  810. {
  811. #if defined(_XM_NO_INTRINSICS_)
  812. XMVECTOR V;
  813. UINT Element;
  814. static CONST UINT SignExtendXY[] = {0x00000000, 0xFFFFF800};
  815. static CONST UINT SignExtendZ[] = {0x00000000, 0xFFFFFC00};
  816. XMASSERT(pSource);
  817. XMASSERT((pSource->v & 0x7FF) != 0x400);
  818. XMASSERT(((pSource->v >> 11) & 0x7FF) != 0x400);
  819. XMASSERT(((pSource->v >> 22) & 0x3FF) != 0x200);
  820. Element = pSource->v & 0x7FF;
  821. V.v[0] = (FLOAT)(SHORT)(Element | SignExtendXY[Element >> 10]) / 1023.0f;
  822. Element = (pSource->v >> 11) & 0x7FF;
  823. V.v[1] = (FLOAT)(SHORT)(Element | SignExtendXY[Element >> 10]) / 1023.0f;
  824. Element = (pSource->v >> 22) & 0x3FF;
  825. V.v[2] = (FLOAT)(SHORT)(Element | SignExtendZ[Element >> 9]) / 511.0f;
  826. return V;
  827. #elif defined(_XM_SSE_INTRINSICS_)
  828. static const XMVECTORI32 g_XMHenDN3And = {0x7FF,0x7ff<<11,0x3FF<<22,0};
  829. static const XMVECTORI32 g_XMHenDN3Xor = {0x400,0x400<<11,0,0};
  830. static const XMVECTORF32 g_XMHenDN3Add = {-1024.0f,-1024.0f*2048.0f,0,0};
  831. static const XMVECTORF32 g_XMHenDN3Mul = {1.0f/1023.0f,1.0f/(1023.0f*2048.0f),1.0f/(511.0f*2048.0f*2048.0f),0};
  832. XMASSERT(pSource);
  833. XMASSERT((pSource->v & 0x7FF) != 0x400);
  834. XMASSERT(((pSource->v >> 11) & 0x7FF) != 0x400);
  835. XMASSERT(((pSource->v >> 22) & 0x3FF) != 0x200);
  836. // Get the 32 bit value and splat it
  837. XMVECTOR vResult = _mm_load_ps1(reinterpret_cast<const float *>(&pSource->v));
  838. // Mask off x, y and z
  839. vResult = _mm_and_ps(vResult,g_XMHenDN3And);
  840. // Convert x and y to unsigned
  841. vResult = _mm_xor_ps(vResult,g_XMHenDN3Xor);
  842. // Convert to float
  843. vResult = _mm_cvtepi32_ps(reinterpret_cast<const __m128i *>(&vResult)[0]);
  844. // Convert x and y back to signed
  845. vResult = _mm_add_ps(vResult,g_XMHenDN3Add);
  846. // Normalize x,y and z to -1.0f-1.0f
  847. vResult = _mm_mul_ps(vResult,g_XMHenDN3Mul);
  848. return vResult;
  849. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  850. #endif // _XM_VMX128_INTRINSICS_
  851. }
  852. //------------------------------------------------------------------------------
  853. XMFINLINE XMVECTOR XMLoadHenD3
  854. (
  855. CONST XMHEND3* pSource
  856. )
  857. {
  858. #if defined(_XM_NO_INTRINSICS_)
  859. XMVECTOR V;
  860. UINT Element;
  861. static CONST UINT SignExtendXY[] = {0x00000000, 0xFFFFF800};
  862. static CONST UINT SignExtendZ[] = {0x00000000, 0xFFFFFC00};
  863. XMASSERT(pSource);
  864. XMASSERT((pSource->v & 0x7FF) != 0x400);
  865. XMASSERT(((pSource->v >> 11) & 0x7FF) != 0x400);
  866. XMASSERT(((pSource->v >> 22) & 0x3FF) != 0x200);
  867. Element = pSource->v & 0x7FF;
  868. V.v[0] = (FLOAT)(SHORT)(Element | SignExtendXY[Element >> 10]);
  869. Element = (pSource->v >> 11) & 0x7FF;
  870. V.v[1] = (FLOAT)(SHORT)(Element | SignExtendXY[Element >> 10]);
  871. Element = (pSource->v >> 22) & 0x3FF;
  872. V.v[2] = (FLOAT)(SHORT)(Element | SignExtendZ[Element >> 9]);
  873. return V;
  874. #elif defined(_XM_SSE_INTRINSICS_)
  875. static const XMVECTORI32 g_XMHenD3And = {0x7FF,0x7ff<<11,0x3FF<<22,0};
  876. static const XMVECTORI32 g_XMHenD3Xor = {0x400,0x400<<11,0,0};
  877. static const XMVECTORF32 g_XMHenD3Add = {-1024.0f,-1024.0f*2048.0f,0,0};
  878. static const XMVECTORF32 g_XMHenD3Mul = {1.0f,1.0f/2048.0f,1.0f/(2048.0f*2048.0f),0};
  879. XMASSERT(pSource);
  880. XMASSERT((pSource->v & 0x7FF) != 0x400);
  881. XMASSERT(((pSource->v >> 11) & 0x7FF) != 0x400);
  882. XMASSERT(((pSource->v >> 22) & 0x3FF) != 0x200);
  883. // Get the 32 bit value and splat it
  884. XMVECTOR vResult = _mm_load_ps1(reinterpret_cast<const float *>(&pSource->v));
  885. // Mask off x, y and z
  886. vResult = _mm_and_ps(vResult,g_XMHenD3And);
  887. // Convert x and y to unsigned
  888. vResult = _mm_xor_ps(vResult,g_XMHenD3Xor);
  889. // Convert to float
  890. vResult = _mm_cvtepi32_ps(reinterpret_cast<const __m128i *>(&vResult)[0]);
  891. // Convert x and y back to signed
  892. vResult = _mm_add_ps(vResult,g_XMHenD3Add);
  893. // Normalize x and y to -1024-1023.0f and z to -512-511.0f
  894. vResult = _mm_mul_ps(vResult,g_XMHenD3Mul);
  895. return vResult;
  896. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  897. #endif // _XM_VMX128_INTRINSICS_
  898. }
  899. //------------------------------------------------------------------------------
  900. XMFINLINE XMVECTOR XMLoadUDHenN3
  901. (
  902. CONST XMUDHENN3* pSource
  903. )
  904. {
  905. #if defined(_XM_NO_INTRINSICS_)
  906. XMVECTOR V;
  907. UINT Element;
  908. XMASSERT(pSource);
  909. Element = pSource->v & 0x3FF;
  910. V.v[0] = (FLOAT)Element / 1023.0f;
  911. Element = (pSource->v >> 10) & 0x7FF;
  912. V.v[1] = (FLOAT)Element / 2047.0f;
  913. Element = (pSource->v >> 21) & 0x7FF;
  914. V.v[2] = (FLOAT)Element / 2047.0f;
  915. return V;
  916. #elif defined(_XM_SSE_INTRINSICS_)
  917. static const XMVECTORI32 g_XMUDHenN3And = {0x3FF,0x7ff<<10,0x7FF<<21,0};
  918. static const XMVECTORI32 g_XMUDHenN3Xor = {0,0,0x80000000,0};
  919. static const XMVECTORF32 g_XMUDHenN3Add = {0,0,32768.0f*65536.0f,0};
  920. static const XMVECTORF32 g_XMUDHenN3Mul = {1.0f/1023.0f,1.0f/(2047.0f*1024.0f),1.0f/(2047.0f*1024.0f*2048.0f),0};
  921. XMASSERT(pSource);
  922. // Get the 32 bit value and splat it
  923. XMVECTOR vResult = _mm_load_ps1(reinterpret_cast<const float *>(&pSource->v));
  924. // Mask off x, y and z
  925. vResult = _mm_and_ps(vResult,g_XMUDHenN3And);
  926. // Convert x and y to unsigned
  927. vResult = _mm_xor_ps(vResult,g_XMUDHenN3Xor);
  928. // Convert to float
  929. vResult = _mm_cvtepi32_ps(reinterpret_cast<const __m128i *>(&vResult)[0]);
  930. // Convert x and y back to signed
  931. vResult = _mm_add_ps(vResult,g_XMUDHenN3Add);
  932. // Normalize x,y and z to -1.0f-1.0f
  933. vResult = _mm_mul_ps(vResult,g_XMUDHenN3Mul);
  934. return vResult;
  935. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  936. #endif // _XM_VMX128_INTRINSICS_
  937. }
  938. //------------------------------------------------------------------------------
  939. XMFINLINE XMVECTOR XMLoadUDHen3
  940. (
  941. CONST XMUDHEN3* pSource
  942. )
  943. {
  944. #if defined(_XM_NO_INTRINSICS_)
  945. XMVECTOR V;
  946. UINT Element;
  947. XMASSERT(pSource);
  948. Element = pSource->v & 0x3FF;
  949. V.v[0] = (FLOAT)Element;
  950. Element = (pSource->v >> 10) & 0x7FF;
  951. V.v[1] = (FLOAT)Element;
  952. Element = (pSource->v >> 21) & 0x7FF;
  953. V.v[2] = (FLOAT)Element;
  954. return V;
  955. #elif defined(_XM_SSE_INTRINSICS_)
  956. static const XMVECTORI32 g_XMUDHen3And = {0x3FF,0x7ff<<10,0x7FF<<21,0};
  957. static const XMVECTORI32 g_XMUDHen3Xor = {0,0,0x80000000,0};
  958. static const XMVECTORF32 g_XMUDHen3Add = {0,0,32768.0f*65536.0f,0};
  959. static const XMVECTORF32 g_XMUDHen3Mul = {1.0f,1.0f/1024.0f,1.0f/(1024.0f*2048.0f),0};
  960. XMASSERT(pSource);
  961. // Get the 32 bit value and splat it
  962. XMVECTOR vResult = _mm_load_ps1(reinterpret_cast<const float *>(&pSource->v));
  963. // Mask off x, y and z
  964. vResult = _mm_and_ps(vResult,g_XMUDHen3And);
  965. // Convert x and y to unsigned
  966. vResult = _mm_xor_ps(vResult,g_XMUDHen3Xor);
  967. // Convert to float
  968. vResult = _mm_cvtepi32_ps(reinterpret_cast<const __m128i *>(&vResult)[0]);
  969. // Convert x and y back to signed
  970. vResult = _mm_add_ps(vResult,g_XMUDHen3Add);
  971. // Normalize x to 0-1023.0f and y and z to 0-2047.0f
  972. vResult = _mm_mul_ps(vResult,g_XMUDHen3Mul);
  973. return vResult;
  974. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  975. #endif // _XM_VMX128_INTRINSICS_
  976. }
  977. //------------------------------------------------------------------------------
  978. XMFINLINE XMVECTOR XMLoadDHenN3
  979. (
  980. CONST XMDHENN3* pSource
  981. )
  982. {
  983. #if defined(_XM_NO_INTRINSICS_)
  984. XMVECTOR V;
  985. UINT Element;
  986. static CONST UINT SignExtendX[] = {0x00000000, 0xFFFFFC00};
  987. static CONST UINT SignExtendYZ[] = {0x00000000, 0xFFFFF800};
  988. XMASSERT(pSource);
  989. XMASSERT((pSource->v & 0x3FF) != 0x200);
  990. XMASSERT(((pSource->v >> 10) & 0x7FF) != 0x400);
  991. XMASSERT(((pSource->v >> 21) & 0x7FF) != 0x400);
  992. Element = pSource->v & 0x3FF;
  993. V.v[0] = (FLOAT)(SHORT)(Element | SignExtendX[Element >> 9]) / 511.0f;
  994. Element = (pSource->v >> 10) & 0x7FF;
  995. V.v[1] = (FLOAT)(SHORT)(Element | SignExtendYZ[Element >> 10]) / 1023.0f;
  996. Element = (pSource->v >> 21) & 0x7FF;
  997. V.v[2] = (FLOAT)(SHORT)(Element | SignExtendYZ[Element >> 10]) / 1023.0f;
  998. return V;
  999. #elif defined(_XM_SSE_INTRINSICS_)
  1000. static const XMVECTORI32 g_XMDHenN3And = {0x3FF,0x7ff<<10,0x7FF<<21,0};
  1001. static const XMVECTORI32 g_XMDHenN3Xor = {0x200,0x400<<10,0,0};
  1002. static const XMVECTORF32 g_XMDHenN3Add = {-512.0f,-1024.0f*1024.0f,0,0};
  1003. static const XMVECTORF32 g_XMDHenN3Mul = {1.0f/511.0f,1.0f/(1023.0f*1024.0f),1.0f/(1023.0f*1024.0f*2048.0f),0};
  1004. XMASSERT(pSource);
  1005. XMASSERT((pSource->v & 0x3FF) != 0x200);
  1006. XMASSERT(((pSource->v >> 10) & 0x7FF) != 0x400);
  1007. XMASSERT(((pSource->v >> 21) & 0x7FF) != 0x400);
  1008. // Get the 32 bit value and splat it
  1009. XMVECTOR vResult = _mm_load_ps1(reinterpret_cast<const float *>(&pSource->v));
  1010. // Mask off x, y and z
  1011. vResult = _mm_and_ps(vResult,g_XMDHenN3And);
  1012. // Convert x and y to unsigned
  1013. vResult = _mm_xor_ps(vResult,g_XMDHenN3Xor);
  1014. // Convert to float
  1015. vResult = _mm_cvtepi32_ps(reinterpret_cast<const __m128i *>(&vResult)[0]);
  1016. // Convert x and y back to signed
  1017. vResult = _mm_add_ps(vResult,g_XMDHenN3Add);
  1018. // Normalize x,y and z to -1.0f-1.0f
  1019. vResult = _mm_mul_ps(vResult,g_XMDHenN3Mul);
  1020. return vResult;
  1021. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  1022. #endif // _XM_VMX128_INTRINSICS_
  1023. }
  1024. //------------------------------------------------------------------------------
  1025. XMFINLINE XMVECTOR XMLoadDHen3
  1026. (
  1027. CONST XMDHEN3* pSource
  1028. )
  1029. {
  1030. #if defined(_XM_NO_INTRINSICS_)
  1031. XMVECTOR V;
  1032. UINT Element;
  1033. static CONST UINT SignExtendX[] = {0x00000000, 0xFFFFFC00};
  1034. static CONST UINT SignExtendYZ[] = {0x00000000, 0xFFFFF800};
  1035. XMASSERT(pSource);
  1036. XMASSERT((pSource->v & 0x3FF) != 0x200);
  1037. XMASSERT(((pSource->v >> 10) & 0x7FF) != 0x400);
  1038. XMASSERT(((pSource->v >> 21) & 0x7FF) != 0x400);
  1039. Element = pSource->v & 0x3FF;
  1040. V.v[0] = (FLOAT)(SHORT)(Element | SignExtendX[Element >> 9]);
  1041. Element = (pSource->v >> 10) & 0x7FF;
  1042. V.v[1] = (FLOAT)(SHORT)(Element | SignExtendYZ[Element >> 10]);
  1043. Element = (pSource->v >> 21) & 0x7FF;
  1044. V.v[2] = (FLOAT)(SHORT)(Element | SignExtendYZ[Element >> 10]);
  1045. return V;
  1046. #elif defined(_XM_SSE_INTRINSICS_)
  1047. static const XMVECTORI32 g_XMDHen3And = {0x3FF,0x7ff<<10,0x7FF<<21,0};
  1048. static const XMVECTORI32 g_XMDHen3Xor = {0x200,0x400<<10,0,0};
  1049. static const XMVECTORF32 g_XMDHen3Add = {-512.0f,-1024.0f*1024.0f,0,0};
  1050. static const XMVECTORF32 g_XMDHen3Mul = {1.0f,1.0f/1024.0f,1.0f/(1024.0f*2048.0f),0};
  1051. XMASSERT(pSource);
  1052. XMASSERT((pSource->v & 0x3FF) != 0x200);
  1053. XMASSERT(((pSource->v >> 10) & 0x7FF) != 0x400);
  1054. XMASSERT(((pSource->v >> 21) & 0x7FF) != 0x400);
  1055. // Get the 32 bit value and splat it
  1056. XMVECTOR vResult = _mm_load_ps1(reinterpret_cast<const float *>(&pSource->v));
  1057. // Mask off x, y and z
  1058. vResult = _mm_and_ps(vResult,g_XMDHen3And);
  1059. // Convert x and y to unsigned
  1060. vResult = _mm_xor_ps(vResult,g_XMDHen3Xor);
  1061. // Convert to float
  1062. vResult = _mm_cvtepi32_ps(reinterpret_cast<const __m128i *>(&vResult)[0]);
  1063. // Convert x and y back to signed
  1064. vResult = _mm_add_ps(vResult,g_XMDHen3Add);
  1065. // Normalize x to -210-511.0f and y and z to -1024-1023.0f
  1066. vResult = _mm_mul_ps(vResult,g_XMDHen3Mul);
  1067. return vResult;
  1068. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  1069. #endif // _XM_VMX128_INTRINSICS_
  1070. }
  1071. //------------------------------------------------------------------------------
  1072. XMFINLINE XMVECTOR XMLoadInt4
  1073. (
  1074. CONST UINT* pSource
  1075. )
  1076. {
  1077. #if defined(_XM_NO_INTRINSICS_)
  1078. XMVECTOR V;
  1079. XMASSERT(pSource);
  1080. V.u[0] = pSource[0];
  1081. V.u[1] = pSource[1];
  1082. V.u[2] = pSource[2];
  1083. V.u[3] = pSource[3];
  1084. return V;
  1085. #elif defined(_XM_SSE_INTRINSICS_)
  1086. XMASSERT(pSource);
  1087. __m128i V = _mm_loadu_si128( (const __m128i*)pSource );
  1088. return reinterpret_cast<__m128 *>(&V)[0];
  1089. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  1090. #endif // _XM_VMX128_INTRINSICS_
  1091. }
  1092. //------------------------------------------------------------------------------
  1093. XMFINLINE XMVECTOR XMLoadInt4A
  1094. (
  1095. CONST UINT* pSource
  1096. )
  1097. {
  1098. #if defined(_XM_NO_INTRINSICS_)
  1099. XMVECTOR V;
  1100. XMASSERT(pSource);
  1101. XMASSERT(((UINT_PTR)pSource & 0xF) == 0);
  1102. V.u[0] = pSource[0];
  1103. V.u[1] = pSource[1];
  1104. V.u[2] = pSource[2];
  1105. V.u[3] = pSource[3];
  1106. return V;
  1107. #elif defined(_XM_SSE_INTRINSICS_)
  1108. XMASSERT(pSource);
  1109. XMASSERT(((UINT_PTR)pSource & 0xF) == 0);
  1110. __m128i V = _mm_load_si128( (const __m128i*)pSource );
  1111. return reinterpret_cast<__m128 *>(&V)[0];
  1112. #else // _XM_VMX128_INTRINSICS_
  1113. #endif // _XM_VMX128_INTRINSICS_
  1114. }
  1115. //------------------------------------------------------------------------------
  1116. XMFINLINE XMVECTOR XMLoadFloat4
  1117. (
  1118. CONST XMFLOAT4* pSource
  1119. )
  1120. {
  1121. #if defined(_XM_NO_INTRINSICS_)
  1122. XMVECTOR V;
  1123. XMASSERT(pSource);
  1124. ((UINT *)(&V.x))[0] = ((const UINT *)(&pSource->x))[0];
  1125. ((UINT *)(&V.y))[0] = ((const UINT *)(&pSource->y))[0];
  1126. ((UINT *)(&V.z))[0] = ((const UINT *)(&pSource->z))[0];
  1127. ((UINT *)(&V.w))[0] = ((const UINT *)(&pSource->w))[0];
  1128. return V;
  1129. #elif defined(_XM_SSE_INTRINSICS_)
  1130. XMASSERT(pSource);
  1131. return _mm_loadu_ps( &pSource->x );
  1132. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  1133. #endif // _XM_VMX128_INTRINSICS_
  1134. }
  1135. //------------------------------------------------------------------------------
  1136. XMFINLINE XMVECTOR XMLoadFloat4A
  1137. (
  1138. CONST XMFLOAT4A* pSource
  1139. )
  1140. {
  1141. #if defined(_XM_NO_INTRINSICS_)
  1142. XMVECTOR V;
  1143. XMASSERT(pSource);
  1144. XMASSERT(((UINT_PTR)pSource & 0xF) == 0);
  1145. V.v[0] = pSource->x;
  1146. V.v[1] = pSource->y;
  1147. V.v[2] = pSource->z;
  1148. V.v[3] = pSource->w;
  1149. return V;
  1150. #elif defined(_XM_SSE_INTRINSICS_)
  1151. XMASSERT(pSource);
  1152. XMASSERT(((UINT_PTR)pSource & 0xF) == 0);
  1153. return _mm_load_ps( &pSource->x );
  1154. #else // _XM_VMX128_INTRINSICS_
  1155. #endif // _XM_VMX128_INTRINSICS_
  1156. }
  1157. //------------------------------------------------------------------------------
  1158. XMFINLINE XMVECTOR XMLoadHalf4
  1159. (
  1160. CONST XMHALF4* pSource
  1161. )
  1162. {
  1163. #if defined(_XM_NO_INTRINSICS_)
  1164. XMASSERT(pSource);
  1165. {
  1166. XMVECTOR vResult = {
  1167. XMConvertHalfToFloat(pSource->x),
  1168. XMConvertHalfToFloat(pSource->y),
  1169. XMConvertHalfToFloat(pSource->z),
  1170. XMConvertHalfToFloat(pSource->w)
  1171. };
  1172. return vResult;
  1173. }
  1174. #elif defined(_XM_SSE_INTRINSICS_)
  1175. XMASSERT(pSource);
  1176. XMVECTOR vResult = {
  1177. XMConvertHalfToFloat(pSource->x),
  1178. XMConvertHalfToFloat(pSource->y),
  1179. XMConvertHalfToFloat(pSource->z),
  1180. XMConvertHalfToFloat(pSource->w)
  1181. };
  1182. return vResult;
  1183. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  1184. #endif // _XM_VMX128_INTRINSICS_
  1185. }
  1186. //------------------------------------------------------------------------------
  1187. XMFINLINE XMVECTOR XMLoadShortN4
  1188. (
  1189. CONST XMSHORTN4* pSource
  1190. )
  1191. {
  1192. #if defined(_XM_NO_INTRINSICS_)
  1193. XMASSERT(pSource);
  1194. XMASSERT(pSource->x != -32768);
  1195. XMASSERT(pSource->y != -32768);
  1196. XMASSERT(pSource->z != -32768);
  1197. XMASSERT(pSource->w != -32768);
  1198. {
  1199. XMVECTOR vResult = {
  1200. (FLOAT)pSource->x * (1.0f/32767.0f),
  1201. (FLOAT)pSource->y * (1.0f/32767.0f),
  1202. (FLOAT)pSource->z * (1.0f/32767.0f),
  1203. (FLOAT)pSource->w * (1.0f/32767.0f)
  1204. };
  1205. return vResult;
  1206. }
  1207. #elif defined(_XM_SSE_INTRINSICS_)
  1208. XMASSERT(pSource);
  1209. XMASSERT(pSource->x != -32768);
  1210. XMASSERT(pSource->y != -32768);
  1211. XMASSERT(pSource->z != -32768);
  1212. XMASSERT(pSource->w != -32768);
  1213. // Splat the color in all four entries (x,z,y,w)
  1214. __m128d vIntd = _mm_load1_pd(reinterpret_cast<const double *>(&pSource->x));
  1215. // Shift x&0ffff,z&0xffff,y&0xffff0000,w&0xffff0000
  1216. __m128 vTemp = _mm_and_ps(reinterpret_cast<const __m128 *>(&vIntd)[0],g_XMMaskX16Y16Z16W16);
  1217. // x and z are unsigned! Flip the bits to convert the order to signed
  1218. vTemp = _mm_xor_ps(vTemp,g_XMFlipX16Y16Z16W16);
  1219. // Convert to floating point numbers
  1220. vTemp = _mm_cvtepi32_ps(reinterpret_cast<const __m128i *>(&vTemp)[0]);
  1221. // x and z - 0x8000 to complete the conversion
  1222. vTemp = _mm_add_ps(vTemp,g_XMFixX16Y16Z16W16);
  1223. // Convert -32767-32767 to -1.0f-1.0f
  1224. vTemp = _mm_mul_ps(vTemp,g_XMNormalizeX16Y16Z16W16);
  1225. // Very important! The entries are x,z,y,w, flip it to x,y,z,w
  1226. vTemp = _mm_shuffle_ps(vTemp,vTemp,_MM_SHUFFLE(3,1,2,0));
  1227. return vTemp;
  1228. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  1229. #endif // _XM_VMX128_INTRINSICS_
  1230. }
  1231. //------------------------------------------------------------------------------
  1232. XMFINLINE XMVECTOR XMLoadShort4
  1233. (
  1234. CONST XMSHORT4* pSource
  1235. )
  1236. {
  1237. #if defined(_XM_NO_INTRINSICS_)
  1238. XMVECTOR V;
  1239. XMASSERT(pSource);
  1240. XMASSERT(pSource->x != -32768);
  1241. XMASSERT(pSource->y != -32768);
  1242. XMASSERT(pSource->z != -32768);
  1243. XMASSERT(pSource->w != -32768);
  1244. V.v[0] = (FLOAT)pSource->x;
  1245. V.v[1] = (FLOAT)pSource->y;
  1246. V.v[2] = (FLOAT)pSource->z;
  1247. V.v[3] = (FLOAT)pSource->w;
  1248. return V;
  1249. #elif defined(_XM_SSE_INTRINSICS_)
  1250. static const XMVECTORF32 g_XMFixupY16W16 = {1.0f,1.0f,1.0f/65536.0f,1.0f/65536.0f};
  1251. XMASSERT(pSource);
  1252. XMASSERT(pSource->x != -32768);
  1253. XMASSERT(pSource->y != -32768);
  1254. XMASSERT(pSource->z != -32768);
  1255. XMASSERT(pSource->w != -32768);
  1256. // Splat the color in all four entries (x,z,y,w)
  1257. __m128d vIntd = _mm_load1_pd(reinterpret_cast<const double *>(&pSource->x));
  1258. // Shift x&0ffff,z&0xffff,y&0xffff0000,w&0xffff0000
  1259. __m128 vTemp = _mm_and_ps(reinterpret_cast<const __m128 *>(&vIntd)[0],g_XMMaskX16Y16Z16W16);
  1260. // x and z are unsigned! Flip the bits to convert the order to signed
  1261. vTemp = _mm_xor_ps(vTemp,g_XMFlipX16Y16Z16W16);
  1262. // Convert to floating point numbers
  1263. vTemp = _mm_cvtepi32_ps(reinterpret_cast<const __m128i *>(&vTemp)[0]);
  1264. // x and z - 0x8000 to complete the conversion
  1265. vTemp = _mm_add_ps(vTemp,g_XMFixX16Y16Z16W16);
  1266. // Fix y and w because they are 65536 too large
  1267. vTemp = _mm_mul_ps(vTemp,g_XMFixupY16W16);
  1268. // Very important! The entries are x,z,y,w, flip it to x,y,z,w
  1269. return _mm_shuffle_ps(vTemp,vTemp,_MM_SHUFFLE(3,1,2,0));
  1270. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  1271. #endif // _XM_VMX128_INTRINSICS_
  1272. }
  1273. //------------------------------------------------------------------------------
  1274. XMFINLINE XMVECTOR XMLoadUShortN4
  1275. (
  1276. CONST XMUSHORTN4* pSource
  1277. )
  1278. {
  1279. #if defined(_XM_NO_INTRINSICS_)
  1280. XMVECTOR V;
  1281. XMASSERT(pSource);
  1282. V.v[0] = (FLOAT)pSource->x / 65535.0f;
  1283. V.v[1] = (FLOAT)pSource->y / 65535.0f;
  1284. V.v[2] = (FLOAT)pSource->z / 65535.0f;
  1285. V.v[3] = (FLOAT)pSource->w / 65535.0f;
  1286. return V;
  1287. #elif defined(_XM_SSE_INTRINSICS_)
  1288. XMASSERT(pSource);
  1289. static const XMVECTORF32 g_XMFixupY16W16 = {1.0f/65535.0f,1.0f/65535.0f,1.0f/(65535.0f*65536.0f),1.0f/(65535.0f*65536.0f)};
  1290. static const XMVECTORI32 g_XMFlipY16W16 = {0x00000000, 0x00000000, 0x80000000, 0x80000000};
  1291. static const XMVECTORF32 g_XMFixaddY16W16 = {0, 0, 32768.0f*65536.0f, 32768.0f*65536.0f};
  1292. XMASSERT(pSource);
  1293. // Splat the color in all four entries (x,z,y,w)
  1294. __m128d vIntd = _mm_load1_pd(reinterpret_cast<const double *>(&pSource->x));
  1295. // Shift x&0ffff,z&0xffff,y&0xffff0000,w&0xffff0000
  1296. __m128 vTemp = _mm_and_ps(reinterpret_cast<const __m128 *>(&vIntd)[0],g_XMMaskX16Y16Z16W16);
  1297. // y and w are signed! Flip the bits to convert the order to unsigned
  1298. vTemp = _mm_xor_ps(vTemp,g_XMFlipY16W16);
  1299. // Convert to floating point numbers
  1300. vTemp = _mm_cvtepi32_ps(reinterpret_cast<const __m128i *>(&vTemp)[0]);
  1301. // y and w + 0x8000 to complete the conversion
  1302. vTemp = _mm_add_ps(vTemp,g_XMFixaddY16W16);
  1303. // Fix y and w because they are 65536 too large
  1304. vTemp = _mm_mul_ps(vTemp,g_XMFixupY16W16);
  1305. // Very important! The entries are x,z,y,w, flip it to x,y,z,w
  1306. return _mm_shuffle_ps(vTemp,vTemp,_MM_SHUFFLE(3,1,2,0));
  1307. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  1308. #endif // _XM_VMX128_INTRINSICS_
  1309. }
  1310. //------------------------------------------------------------------------------
  1311. XMFINLINE XMVECTOR XMLoadUShort4
  1312. (
  1313. CONST XMUSHORT4* pSource
  1314. )
  1315. {
  1316. #if defined(_XM_NO_INTRINSICS_)
  1317. XMVECTOR V;
  1318. XMASSERT(pSource);
  1319. V.v[0] = (FLOAT)pSource->x;
  1320. V.v[1] = (FLOAT)pSource->y;
  1321. V.v[2] = (FLOAT)pSource->z;
  1322. V.v[3] = (FLOAT)pSource->w;
  1323. return V;
  1324. #elif defined(_XM_SSE_INTRINSICS_)
  1325. XMASSERT(pSource);
  1326. static const XMVECTORF32 g_XMFixupY16W16 = {1.0f,1.0f,1.0f/65536.0f,1.0f/65536.0f};
  1327. static const XMVECTORI32 g_XMFlipY16W16 = {0x00000000, 0x00000000, 0x80000000, 0x80000000};
  1328. static const XMVECTORF32 g_XMFixaddY16W16 = {0, 0, 32768.0f, 32768.0f};
  1329. XMASSERT(pSource);
  1330. // Splat the color in all four entries (x,z,y,w)
  1331. __m128d vIntd = _mm_load1_pd(reinterpret_cast<const double *>(&pSource->x));
  1332. // Shift x&0ffff,z&0xffff,y&0xffff0000,w&0xffff0000
  1333. __m128 vTemp = _mm_and_ps(reinterpret_cast<const __m128 *>(&vIntd)[0],g_XMMaskX16Y16Z16W16);
  1334. // y and w are signed! Flip the bits to convert the order to unsigned
  1335. vTemp = _mm_xor_ps(vTemp,g_XMFlipY16W16);
  1336. // Convert to floating point numbers
  1337. vTemp = _mm_cvtepi32_ps(reinterpret_cast<const __m128i *>(&vTemp)[0]);
  1338. // Fix y and w because they are 65536 too large
  1339. vTemp = _mm_mul_ps(vTemp,g_XMFixupY16W16);
  1340. // y and w + 0x8000 to complete the conversion
  1341. vTemp = _mm_add_ps(vTemp,g_XMFixaddY16W16);
  1342. // Very important! The entries are x,z,y,w, flip it to x,y,z,w
  1343. return _mm_shuffle_ps(vTemp,vTemp,_MM_SHUFFLE(3,1,2,0));
  1344. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  1345. #endif // _XM_VMX128_INTRINSICS_
  1346. }
  1347. //------------------------------------------------------------------------------
  1348. XMFINLINE XMVECTOR XMLoadXIcoN4
  1349. (
  1350. CONST XMXICON4* pSource
  1351. )
  1352. {
  1353. #if defined(_XM_NO_INTRINSICS_)
  1354. XMVECTOR V;
  1355. UINT Element;
  1356. static CONST UINT SignExtend[] = {0x00000000, 0xFFF00000};
  1357. XMASSERT(pSource);
  1358. XMASSERT((pSource->v & 0xFFFFFull) != 0x80000ull);
  1359. XMASSERT(((pSource->v >> 20) & 0xFFFFFull) != 0x80000ull);
  1360. XMASSERT(((pSource->v >> 40) & 0xFFFFFull) != 0x80000ull);
  1361. Element = (UINT)pSource->v & 0xFFFFF;
  1362. V.v[0] = (FLOAT)(INT)(Element | SignExtend[Element >> 19]) / 524287.0f;
  1363. Element = (UINT)(pSource->v >> 20) & 0xFFFFF;
  1364. V.v[1] = (FLOAT)(INT)(Element | SignExtend[Element >> 19]) / 524287.0f;
  1365. Element = (UINT)(pSource->v >> 40) & 0xFFFFF;
  1366. V.v[2] = (FLOAT)(INT)(Element | SignExtend[Element >> 19]) / 524287.0f;
  1367. V.v[3] = (FLOAT)(pSource->v >> 60) / 15.0f;
  1368. return V;
  1369. #elif defined(_XM_SSE_INTRINSICS_)
  1370. XMASSERT((pSource->v & 0xFFFFFull) != 0x80000ull);
  1371. XMASSERT(((pSource->v >> 20) & 0xFFFFFull) != 0x80000ull);
  1372. XMASSERT(((pSource->v >> 40) & 0xFFFFFull) != 0x80000ull);
  1373. static const XMVECTORI32 g_XMLoadXIcoN4And = {0xFFFFF,0xFFFFF000,0xFFFFF,0xF0000000};
  1374. static const XMVECTORI32 g_XMLoadXIcoN4Xor = {0x80000,0x00000000,0x80000,0x80000000};
  1375. static const XMVECTORF32 g_XMLoadXIcoN4Add = {-8.0f*65536.0f,0,-8.0f*65536.0f,32768.0f*65536.0f};
  1376. static const XMVECTORF32 g_XMLoadXIcoN4Mul = {1.0f/524287.0f,1.0f/(524287.0f*4096.0f),1.0f/524287.0f,1.0f/(15.0f*4096.0f*65536.0f)};
  1377. XMASSERT(pSource);
  1378. // Grab the 64 bit structure
  1379. __m128d vResultd = _mm_load_sd(reinterpret_cast<const double *>(&pSource->v));
  1380. // By shifting down 8 bits, y and z are in seperate 32 bit elements
  1381. __m128i vResulti = _mm_srli_si128(reinterpret_cast<const __m128i *>(&vResultd)[0],8/8);
  1382. // vResultd has x and w, vResulti has y and z, merge into one as x,w,y,z
  1383. XMVECTOR vTemp = _mm_shuffle_ps(reinterpret_cast<const __m128 *>(&vResultd)[0],reinterpret_cast<const __m128 *>(&vResulti)[0],_MM_SHUFFLE(1,0,1,0));
  1384. // Fix the entries to x,y,z,w
  1385. vTemp = _mm_shuffle_ps(vTemp,vTemp,_MM_SHUFFLE(1,3,2,0));
  1386. // Mask x,y,z and w
  1387. vTemp = _mm_and_ps(vTemp,g_XMLoadXIcoN4And);
  1388. // x and z are unsigned! Flip the bits to convert the order to signed
  1389. vTemp = _mm_xor_ps(vTemp,g_XMLoadXIcoN4Xor);
  1390. // Convert to floating point numbers
  1391. vTemp = _mm_cvtepi32_ps(reinterpret_cast<const __m128i *>(&vTemp)[0]);
  1392. // x and z - 0x80 to complete the conversion
  1393. vTemp = _mm_add_ps(vTemp,g_XMLoadXIcoN4Add);
  1394. // Fix y and w because they are too large
  1395. vTemp = _mm_mul_ps(vTemp,g_XMLoadXIcoN4Mul);
  1396. return vTemp;
  1397. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  1398. #endif // _XM_VMX128_INTRINSICS_
  1399. }
  1400. //------------------------------------------------------------------------------
  1401. XMFINLINE XMVECTOR XMLoadXIco4
  1402. (
  1403. CONST XMXICO4* pSource
  1404. )
  1405. {
  1406. #if defined(_XM_NO_INTRINSICS_)
  1407. XMVECTOR V;
  1408. UINT Element;
  1409. static CONST UINT SignExtend[] = {0x00000000, 0xFFF00000};
  1410. XMASSERT(pSource);
  1411. XMASSERT((pSource->v & 0xFFFFFull) != 0x80000ull);
  1412. XMASSERT(((pSource->v >> 20) & 0xFFFFFull) != 0x80000ull);
  1413. XMASSERT(((pSource->v >> 40) & 0xFFFFFull) != 0x80000ull);
  1414. Element = (UINT)pSource->v & 0xFFFFF;
  1415. V.v[0] = (FLOAT)(INT)(Element | SignExtend[Element >> 19]);
  1416. Element = (UINT)(pSource->v >> 20) & 0xFFFFF;
  1417. V.v[1] = (FLOAT)(INT)(Element | SignExtend[Element >> 19]);
  1418. Element = (UINT)(pSource->v >> 40) & 0xFFFFF;
  1419. V.v[2] = (FLOAT)(INT)(Element | SignExtend[Element >> 19]);
  1420. V.v[3] = (FLOAT)(pSource->v >> 60);
  1421. return V;
  1422. #elif defined(_XM_SSE_INTRINSICS_)
  1423. XMASSERT((pSource->v & 0xFFFFFull) != 0x80000ull);
  1424. XMASSERT(((pSource->v >> 20) & 0xFFFFFull) != 0x80000ull);
  1425. XMASSERT(((pSource->v >> 40) & 0xFFFFFull) != 0x80000ull);
  1426. static const XMVECTORI32 g_XMLoadXIco4And = {0xFFFFF,0xFFFFF000,0xFFFFF,0xF0000000};
  1427. static const XMVECTORI32 g_XMLoadXIco4Xor = {0x80000,0x00000000,0x80000,0x80000000};
  1428. static const XMVECTORF32 g_XMLoadXIco4Add = {-8.0f*65536.0f,0,-8.0f*65536.0f,32768.0f*65536.0f};
  1429. static const XMVECTORF32 g_XMLoadXIco4Mul = {1.0f,1.0f/4096.0f,1.0f,1.0f/(4096.0f*65536.0f)};
  1430. XMASSERT(pSource);
  1431. // Grab the 64 bit structure
  1432. __m128d vResultd = _mm_load_sd(reinterpret_cast<const double *>(&pSource->v));
  1433. // By shifting down 8 bits, y and z are in seperate 32 bit elements
  1434. __m128i vResulti = _mm_srli_si128(reinterpret_cast<const __m128i *>(&vResultd)[0],8/8);
  1435. // vResultd has x and w, vResulti has y and z, merge into one as x,w,y,z
  1436. XMVECTOR vTemp = _mm_shuffle_ps(reinterpret_cast<const __m128 *>(&vResultd)[0],reinterpret_cast<const __m128 *>(&vResulti)[0],_MM_SHUFFLE(1,0,1,0));
  1437. // Fix the entries to x,y,z,w
  1438. vTemp = _mm_shuffle_ps(vTemp,vTemp,_MM_SHUFFLE(1,3,2,0));
  1439. // Mask x,y,z and w
  1440. vTemp = _mm_and_ps(vTemp,g_XMLoadXIco4And);
  1441. // x and z are unsigned! Flip the bits to convert the order to signed
  1442. vTemp = _mm_xor_ps(vTemp,g_XMLoadXIco4Xor);
  1443. // Convert to floating point numbers
  1444. vTemp = _mm_cvtepi32_ps(reinterpret_cast<const __m128i *>(&vTemp)[0]);
  1445. // x and z - 0x80 to complete the conversion
  1446. vTemp = _mm_add_ps(vTemp,g_XMLoadXIco4Add);
  1447. // Fix y and w because they are too large
  1448. vTemp = _mm_mul_ps(vTemp,g_XMLoadXIco4Mul);
  1449. return vTemp;
  1450. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  1451. #endif // _XM_VMX128_INTRINSICS_
  1452. }
  1453. //------------------------------------------------------------------------------
  1454. XMFINLINE XMVECTOR XMLoadUIcoN4
  1455. (
  1456. CONST XMUICON4* pSource
  1457. )
  1458. {
  1459. #if defined(_XM_NO_INTRINSICS_)
  1460. XMVECTOR V;
  1461. XMASSERT(pSource);
  1462. V.v[0] = (FLOAT)(pSource->v & 0xFFFFF) / 1048575.0f;
  1463. V.v[1] = (FLOAT)((pSource->v >> 20) & 0xFFFFF) / 1048575.0f;
  1464. V.v[2] = (FLOAT)((pSource->v >> 40) & 0xFFFFF) / 1048575.0f;
  1465. V.v[3] = (FLOAT)(pSource->v >> 60) / 15.0f;
  1466. return V;
  1467. #elif defined(_XM_SSE_INTRINSICS_)
  1468. static const XMVECTORI32 g_XMLoadUIcoN4And = {0xFFFFF,0xFFFFF000,0xFFFFF,0xF0000000};
  1469. static const XMVECTORI32 g_XMLoadUIcoN4Xor = {0x00000,0x80000000,0x00000,0x80000000};
  1470. static const XMVECTORF32 g_XMLoadUIcoN4Add = {0,32768.0f*65536.0f,0,32768.0f*65536.0f};
  1471. static const XMVECTORF32 g_XMLoadUIcoN4Mul = {1.0f/1048575.0f,1.0f/(1048575.0f*4096.0f),1.0f/1048575.0f,1.0f/(15.0f*4096.0f*65536.0f)};
  1472. XMASSERT(pSource);
  1473. // Grab the 64 bit structure
  1474. __m128d vResultd = _mm_load_sd(reinterpret_cast<const double *>(&pSource->v));
  1475. // By shifting down 8 bits, y and z are in seperate 32 bit elements
  1476. __m128i vResulti = _mm_srli_si128(reinterpret_cast<const __m128i *>(&vResultd)[0],8/8);
  1477. // vResultd has x and w, vResulti has y and z, merge into one as x,w,y,z
  1478. XMVECTOR vTemp = _mm_shuffle_ps(reinterpret_cast<const __m128 *>(&vResultd)[0],reinterpret_cast<const __m128 *>(&vResulti)[0],_MM_SHUFFLE(1,0,1,0));
  1479. // Fix the entries to x,y,z,w
  1480. vTemp = _mm_shuffle_ps(vTemp,vTemp,_MM_SHUFFLE(1,3,2,0));
  1481. // Mask x,y,z and w
  1482. vTemp = _mm_and_ps(vTemp,g_XMLoadUIcoN4And);
  1483. // x and z are unsigned! Flip the bits to convert the order to signed
  1484. vTemp = _mm_xor_ps(vTemp,g_XMLoadUIcoN4Xor);
  1485. // Convert to floating point numbers
  1486. vTemp = _mm_cvtepi32_ps(reinterpret_cast<const __m128i *>(&vTemp)[0]);
  1487. // x and z - 0x80 to complete the conversion
  1488. vTemp = _mm_add_ps(vTemp,g_XMLoadUIcoN4Add);
  1489. // Fix y and w because they are too large
  1490. vTemp = _mm_mul_ps(vTemp,g_XMLoadUIcoN4Mul);
  1491. return vTemp;
  1492. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  1493. #endif // _XM_VMX128_INTRINSICS_
  1494. }
  1495. //------------------------------------------------------------------------------
  1496. XMFINLINE XMVECTOR XMLoadUIco4
  1497. (
  1498. CONST XMUICO4* pSource
  1499. )
  1500. {
  1501. #if defined(_XM_NO_INTRINSICS_)
  1502. XMVECTOR V;
  1503. XMASSERT(pSource);
  1504. V.v[0] = (FLOAT)(pSource->v & 0xFFFFF);
  1505. V.v[1] = (FLOAT)((pSource->v >> 20) & 0xFFFFF);
  1506. V.v[2] = (FLOAT)((pSource->v >> 40) & 0xFFFFF);
  1507. V.v[3] = (FLOAT)(pSource->v >> 60);
  1508. return V;
  1509. #elif defined(_XM_SSE_INTRINSICS_)
  1510. static const XMVECTORI32 g_XMLoadUIco4And = {0xFFFFF,0xFFFFF000,0xFFFFF,0xF0000000};
  1511. static const XMVECTORI32 g_XMLoadUIco4Xor = {0x00000,0x80000000,0x00000,0x80000000};
  1512. static const XMVECTORF32 g_XMLoadUIco4Add = {0,32768.0f*65536.0f,0,32768.0f*65536.0f};
  1513. static const XMVECTORF32 g_XMLoadUIco4Mul = {1.0f,1.0f/4096.0f,1.0f,1.0f/(4096.0f*65536.0f)};
  1514. XMASSERT(pSource);
  1515. // Grab the 64 bit structure
  1516. __m128d vResultd = _mm_load_sd(reinterpret_cast<const double *>(&pSource->v));
  1517. // By shifting down 8 bits, y and z are in seperate 32 bit elements
  1518. __m128i vResulti = _mm_srli_si128(reinterpret_cast<const __m128i *>(&vResultd)[0],8/8);
  1519. // vResultd has x and w, vResulti has y and z, merge into one as x,w,y,z
  1520. XMVECTOR vTemp = _mm_shuffle_ps(reinterpret_cast<const __m128 *>(&vResultd)[0],reinterpret_cast<const __m128 *>(&vResulti)[0],_MM_SHUFFLE(1,0,1,0));
  1521. // Fix the entries to x,y,z,w
  1522. vTemp = _mm_shuffle_ps(vTemp,vTemp,_MM_SHUFFLE(1,3,2,0));
  1523. // Mask x,y,z and w
  1524. vTemp = _mm_and_ps(vTemp,g_XMLoadUIco4And);
  1525. // x and z are unsigned! Flip the bits to convert the order to signed
  1526. vTemp = _mm_xor_ps(vTemp,g_XMLoadUIco4Xor);
  1527. // Convert to floating point numbers
  1528. vTemp = _mm_cvtepi32_ps(reinterpret_cast<const __m128i *>(&vTemp)[0]);
  1529. // x and z - 0x80 to complete the conversion
  1530. vTemp = _mm_add_ps(vTemp,g_XMLoadUIco4Add);
  1531. // Fix y and w because they are too large
  1532. vTemp = _mm_mul_ps(vTemp,g_XMLoadUIco4Mul);
  1533. return vTemp;
  1534. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  1535. #endif // _XM_VMX128_INTRINSICS_
  1536. }
  1537. //------------------------------------------------------------------------------
  1538. XMFINLINE XMVECTOR XMLoadIcoN4
  1539. (
  1540. CONST XMICON4* pSource
  1541. )
  1542. {
  1543. #if defined(_XM_NO_INTRINSICS_)
  1544. XMVECTOR V;
  1545. UINT Element;
  1546. static CONST UINT SignExtend[] = {0x00000000, 0xFFF00000};
  1547. static CONST UINT SignExtendW[] = {0x00000000, 0xFFFFFFF0};
  1548. XMASSERT(pSource);
  1549. Element = (UINT)pSource->v & 0xFFFFF;
  1550. V.v[0] = (FLOAT)(INT)(Element | SignExtend[Element >> 19]) / 524287.0f;
  1551. Element = (UINT)(pSource->v >> 20) & 0xFFFFF;
  1552. V.v[1] = (FLOAT)(INT)(Element | SignExtend[Element >> 19]) / 524287.0f;
  1553. Element = (UINT)(pSource->v >> 40) & 0xFFFFF;
  1554. V.v[2] = (FLOAT)(INT)(Element | SignExtend[Element >> 19]) / 524287.0f;
  1555. Element = (UINT)(pSource->v >> 60);
  1556. V.v[3] = (FLOAT)(INT)(Element | SignExtendW[Element >> 3]) / 7.0f;
  1557. return V;
  1558. #elif defined(_XM_SSE_INTRINSICS_)
  1559. static const XMVECTORI32 g_XMLoadIcoN4And = {0xFFFFF,0xFFFFF000,0xFFFFF,0xF0000000};
  1560. static const XMVECTORI32 g_XMLoadIcoN4Xor = {0x80000,0x00000000,0x80000,0x00000000};
  1561. static const XMVECTORF32 g_XMLoadIcoN4Add = {-8.0f*65536.0f,0,-8.0f*65536.0f,0};
  1562. static const XMVECTORF32 g_XMLoadIcoN4Mul = {1.0f/524287.0f,1.0f/(524287.0f*4096.0f),1.0f/524287.0f,1.0f/(7.0f*4096.0f*65536.0f)};
  1563. XMASSERT(pSource);
  1564. // Grab the 64 bit structure
  1565. __m128d vResultd = _mm_load_sd(reinterpret_cast<const double *>(&pSource->v));
  1566. // By shifting down 8 bits, y and z are in seperate 32 bit elements
  1567. __m128i vResulti = _mm_srli_si128(reinterpret_cast<const __m128i *>(&vResultd)[0],8/8);
  1568. // vResultd has x and w, vResulti has y and z, merge into one as x,w,y,z
  1569. XMVECTOR vTemp = _mm_shuffle_ps(reinterpret_cast<const __m128 *>(&vResultd)[0],reinterpret_cast<const __m128 *>(&vResulti)[0],_MM_SHUFFLE(1,0,1,0));
  1570. // Fix the entries to x,y,z,w
  1571. vTemp = _mm_shuffle_ps(vTemp,vTemp,_MM_SHUFFLE(1,3,2,0));
  1572. // Mask x,y,z and w
  1573. vTemp = _mm_and_ps(vTemp,g_XMLoadIcoN4And);
  1574. // x and z are unsigned! Flip the bits to convert the order to signed
  1575. vTemp = _mm_xor_ps(vTemp,g_XMLoadIcoN4Xor);
  1576. // Convert to floating point numbers
  1577. vTemp = _mm_cvtepi32_ps(reinterpret_cast<const __m128i *>(&vTemp)[0]);
  1578. // x and z - 0x80 to complete the conversion
  1579. vTemp = _mm_add_ps(vTemp,g_XMLoadIcoN4Add);
  1580. // Fix y and w because they are too large
  1581. vTemp = _mm_mul_ps(vTemp,g_XMLoadIcoN4Mul);
  1582. return vTemp;
  1583. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  1584. #endif // _XM_VMX128_INTRINSICS_
  1585. }
  1586. //------------------------------------------------------------------------------
  1587. XMFINLINE XMVECTOR XMLoadIco4
  1588. (
  1589. CONST XMICO4* pSource
  1590. )
  1591. {
  1592. #if defined(_XM_NO_INTRINSICS_)
  1593. XMVECTOR V;
  1594. UINT Element;
  1595. static CONST UINT SignExtend[] = {0x00000000, 0xFFF00000};
  1596. static CONST UINT SignExtendW[] = {0x00000000, 0xFFFFFFF0};
  1597. XMASSERT(pSource);
  1598. Element = (UINT)pSource->v & 0xFFFFF;
  1599. V.v[0] = (FLOAT)(INT)(Element | SignExtend[Element >> 19]);
  1600. Element = (UINT)(pSource->v >> 20) & 0xFFFFF;
  1601. V.v[1] = (FLOAT)(INT)(Element | SignExtend[Element >> 19]);
  1602. Element = (UINT)(pSource->v >> 40) & 0xFFFFF;
  1603. V.v[2] = (FLOAT)(INT)(Element | SignExtend[Element >> 19]);
  1604. Element = (UINT)(pSource->v >> 60);
  1605. V.v[3] = (FLOAT)(INT)(Element | SignExtendW[Element >> 3]);
  1606. return V;
  1607. #elif defined(_XM_SSE_INTRINSICS_)
  1608. static const XMVECTORI32 g_XMLoadIco4And = {0xFFFFF,0xFFFFF000,0xFFFFF,0xF0000000};
  1609. static const XMVECTORI32 g_XMLoadIco4Xor = {0x80000,0x00000000,0x80000,0x00000000};
  1610. static const XMVECTORF32 g_XMLoadIco4Add = {-8.0f*65536.0f,0,-8.0f*65536.0f,0};
  1611. static const XMVECTORF32 g_XMLoadIco4Mul = {1.0f,1.0f/4096.0f,1.0f,1.0f/(4096.0f*65536.0f)};
  1612. XMASSERT(pSource);
  1613. // Grab the 64 bit structure
  1614. __m128d vResultd = _mm_load_sd(reinterpret_cast<const double *>(&pSource->v));
  1615. // By shifting down 8 bits, y and z are in seperate 32 bit elements
  1616. __m128i vResulti = _mm_srli_si128(reinterpret_cast<const __m128i *>(&vResultd)[0],8/8);
  1617. // vResultd has x and w, vResulti has y and z, merge into one as x,w,y,z
  1618. XMVECTOR vTemp = _mm_shuffle_ps(reinterpret_cast<const __m128 *>(&vResultd)[0],reinterpret_cast<const __m128 *>(&vResulti)[0],_MM_SHUFFLE(1,0,1,0));
  1619. // Fix the entries to x,y,z,w
  1620. vTemp = _mm_shuffle_ps(vTemp,vTemp,_MM_SHUFFLE(1,3,2,0));
  1621. // Mask x,y,z and w
  1622. vTemp = _mm_and_ps(vTemp,g_XMLoadIco4And);
  1623. // x and z are unsigned! Flip the bits to convert the order to signed
  1624. vTemp = _mm_xor_ps(vTemp,g_XMLoadIco4Xor);
  1625. // Convert to floating point numbers
  1626. vTemp = _mm_cvtepi32_ps(reinterpret_cast<const __m128i *>(&vTemp)[0]);
  1627. // x and z - 0x80 to complete the conversion
  1628. vTemp = _mm_add_ps(vTemp,g_XMLoadIco4Add);
  1629. // Fix y and w because they are too large
  1630. vTemp = _mm_mul_ps(vTemp,g_XMLoadIco4Mul);
  1631. return vTemp;
  1632. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  1633. #endif // _XM_VMX128_INTRINSICS_
  1634. }
  1635. //------------------------------------------------------------------------------
  1636. XMFINLINE XMVECTOR XMLoadXDecN4
  1637. (
  1638. CONST XMXDECN4* pSource
  1639. )
  1640. {
  1641. #if defined(_XM_NO_INTRINSICS_)
  1642. XMVECTOR V;
  1643. UINT Element;
  1644. static CONST UINT SignExtend[] = {0x00000000, 0xFFFFFC00};
  1645. XMASSERT(pSource);
  1646. XMASSERT((pSource->v & 0x3FF) != 0x200);
  1647. XMASSERT(((pSource->v >> 10) & 0x3FF) != 0x200);
  1648. XMASSERT(((pSource->v >> 20) & 0x3FF) != 0x200);
  1649. Element = pSource->v & 0x3FF;
  1650. V.v[0] = (FLOAT)(SHORT)(Element | SignExtend[Element >> 9]) / 511.0f;
  1651. Element = (pSource->v >> 10) & 0x3FF;
  1652. V.v[1] = (FLOAT)(SHORT)(Element | SignExtend[Element >> 9]) / 511.0f;
  1653. Element = (pSource->v >> 20) & 0x3FF;
  1654. V.v[2] = (FLOAT)(SHORT)(Element | SignExtend[Element >> 9]) / 511.0f;
  1655. V.v[3] = (FLOAT)(pSource->v >> 30) / 3.0f;
  1656. return V;
  1657. #elif defined(_XM_SSE_INTRINSICS_)
  1658. XMASSERT(pSource);
  1659. // Splat the color in all four entries
  1660. __m128 vTemp = _mm_load_ps1(reinterpret_cast<const float *>(&pSource->v));
  1661. // Shift R&0xFF0000, G&0xFF00, B&0xFF, A&0xFF000000
  1662. vTemp = _mm_and_ps(vTemp,g_XMMaskA2B10G10R10);
  1663. // a is unsigned! Flip the bit to convert the order to signed
  1664. vTemp = _mm_xor_ps(vTemp,g_XMFlipA2B10G10R10);
  1665. // Convert to floating point numbers
  1666. vTemp = _mm_cvtepi32_ps(reinterpret_cast<const __m128i *>(&vTemp)[0]);
  1667. // RGB + 0, A + 0x80000000.f to undo the signed order.
  1668. vTemp = _mm_add_ps(vTemp,g_XMFixAA2B10G10R10);
  1669. // Convert 0-255 to 0.0f-1.0f
  1670. return _mm_mul_ps(vTemp,g_XMNormalizeA2B10G10R10);
  1671. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  1672. #endif // _XM_VMX128_INTRINSICS_
  1673. }
  1674. //------------------------------------------------------------------------------
  1675. XMFINLINE XMVECTOR XMLoadXDec4
  1676. (
  1677. CONST XMXDEC4* pSource
  1678. )
  1679. {
  1680. #if defined(_XM_NO_INTRINSICS_)
  1681. XMVECTOR V;
  1682. UINT Element;
  1683. static CONST UINT SignExtend[] = {0x00000000, 0xFFFFFC00};
  1684. XMASSERT(pSource);
  1685. XMASSERT((pSource->v & 0x3FF) != 0x200);
  1686. XMASSERT(((pSource->v >> 10) & 0x3FF) != 0x200);
  1687. XMASSERT(((pSource->v >> 20) & 0x3FF) != 0x200);
  1688. Element = pSource->v & 0x3FF;
  1689. V.v[0] = (FLOAT)(SHORT)(Element | SignExtend[Element >> 9]);
  1690. Element = (pSource->v >> 10) & 0x3FF;
  1691. V.v[1] = (FLOAT)(SHORT)(Element | SignExtend[Element >> 9]);
  1692. Element = (pSource->v >> 20) & 0x3FF;
  1693. V.v[2] = (FLOAT)(SHORT)(Element | SignExtend[Element >> 9]);
  1694. V.v[3] = (FLOAT)(pSource->v >> 30);
  1695. return V;
  1696. #elif defined(_XM_SSE_INTRINSICS_)
  1697. XMASSERT((pSource->v & 0x3FF) != 0x200);
  1698. XMASSERT(((pSource->v >> 10) & 0x3FF) != 0x200);
  1699. XMASSERT(((pSource->v >> 20) & 0x3FF) != 0x200);
  1700. static const XMVECTORI32 g_XMXDec4And = {0x3FF, 0x3FF<<10, 0x3FF<<20, 0x3<<30};
  1701. static const XMVECTORI32 g_XMXDec4Xor = {0x200, 0x200<<10, 0x200<<20, 0x80000000};
  1702. static const XMVECTORF32 g_XMXDec4Add = {-512.0f,-512.0f*1024.0f,-512.0f*1024.0f*1024.0f,32768*65536.0f};
  1703. static const XMVECTORF32 g_XMXDec4Mul = {1.0f,1.0f/1024.0f,1.0f/(1024.0f*1024.0f),1.0f/(1024.0f*1024.0f*1024.0f)};
  1704. XMASSERT(pSource);
  1705. // Splat the color in all four entries
  1706. XMVECTOR vTemp = _mm_load_ps1(reinterpret_cast<const float *>(&pSource->v));
  1707. // Shift R&0xFF0000, G&0xFF00, B&0xFF, A&0xFF000000
  1708. vTemp = _mm_and_ps(vTemp,g_XMXDec4And);
  1709. // a is unsigned! Flip the bit to convert the order to signed
  1710. vTemp = _mm_xor_ps(vTemp,g_XMXDec4Xor);
  1711. // Convert to floating point numbers
  1712. vTemp = _mm_cvtepi32_ps(reinterpret_cast<const __m128i *>(&vTemp)[0]);
  1713. // RGB + 0, A + 0x80000000.f to undo the signed order.
  1714. vTemp = _mm_add_ps(vTemp,g_XMXDec4Add);
  1715. // Convert 0-255 to 0.0f-1.0f
  1716. vTemp = _mm_mul_ps(vTemp,g_XMXDec4Mul);
  1717. return vTemp;
  1718. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  1719. #endif // _XM_VMX128_INTRINSICS_
  1720. }
  1721. //------------------------------------------------------------------------------
  1722. XMFINLINE XMVECTOR XMLoadUDecN4
  1723. (
  1724. CONST XMUDECN4* pSource
  1725. )
  1726. {
  1727. #if defined(_XM_NO_INTRINSICS_)
  1728. XMVECTOR V;
  1729. UINT Element;
  1730. XMASSERT(pSource);
  1731. Element = pSource->v & 0x3FF;
  1732. V.v[0] = (FLOAT)Element / 1023.0f;
  1733. Element = (pSource->v >> 10) & 0x3FF;
  1734. V.v[1] = (FLOAT)Element / 1023.0f;
  1735. Element = (pSource->v >> 20) & 0x3FF;
  1736. V.v[2] = (FLOAT)Element / 1023.0f;
  1737. V.v[3] = (FLOAT)(pSource->v >> 30) / 3.0f;
  1738. return V;
  1739. #elif defined(_XM_SSE_INTRINSICS_)
  1740. XMASSERT(pSource);
  1741. static const XMVECTORI32 g_XMUDecN4And = {0x3FF, 0x3FF<<10, 0x3FF<<20, 0x3<<30};
  1742. static const XMVECTORI32 g_XMUDecN4Xor = {0, 0, 0, 0x80000000};
  1743. static const XMVECTORF32 g_XMUDecN4Add = {0,0,0,32768.0f*65536.0f};
  1744. static const XMVECTORF32 g_XMUDecN4Mul = {1.0f/1023.0f,1.0f/(1023.0f*1024.0f),1.0f/(1023.0f*1024.0f*1024.0f),1.0f/(3.0f*1024.0f*1024.0f*1024.0f)};
  1745. // Splat the color in all four entries
  1746. XMVECTOR vTemp = _mm_load_ps1(reinterpret_cast<const float *>(&pSource->v));
  1747. // Shift R&0xFF0000, G&0xFF00, B&0xFF, A&0xFF000000
  1748. vTemp = _mm_and_ps(vTemp,g_XMUDecN4And);
  1749. // a is unsigned! Flip the bit to convert the order to signed
  1750. vTemp = _mm_xor_ps(vTemp,g_XMUDecN4Xor);
  1751. // Convert to floating point numbers
  1752. vTemp = _mm_cvtepi32_ps(reinterpret_cast<const __m128i *>(&vTemp)[0]);
  1753. // RGB + 0, A + 0x80000000.f to undo the signed order.
  1754. vTemp = _mm_add_ps(vTemp,g_XMUDecN4Add);
  1755. // Convert 0-255 to 0.0f-1.0f
  1756. vTemp = _mm_mul_ps(vTemp,g_XMUDecN4Mul);
  1757. return vTemp;
  1758. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  1759. #endif // _XM_VMX128_INTRINSICS_
  1760. }
  1761. //------------------------------------------------------------------------------
  1762. XMFINLINE XMVECTOR XMLoadUDec4
  1763. (
  1764. CONST XMUDEC4* pSource
  1765. )
  1766. {
  1767. #if defined(_XM_NO_INTRINSICS_)
  1768. XMVECTOR V;
  1769. UINT Element;
  1770. XMASSERT(pSource);
  1771. Element = pSource->v & 0x3FF;
  1772. V.v[0] = (FLOAT)Element;
  1773. Element = (pSource->v >> 10) & 0x3FF;
  1774. V.v[1] = (FLOAT)Element;
  1775. Element = (pSource->v >> 20) & 0x3FF;
  1776. V.v[2] = (FLOAT)Element;
  1777. V.v[3] = (FLOAT)(pSource->v >> 30);
  1778. return V;
  1779. #elif defined(_XM_SSE_INTRINSICS_)
  1780. static const XMVECTORI32 g_XMUDec4And = {0x3FF, 0x3FF<<10, 0x3FF<<20, 0x3<<30};
  1781. static const XMVECTORI32 g_XMUDec4Xor = {0, 0, 0,0x80000000};
  1782. static const XMVECTORF32 g_XMUDec4Add = {0,0,0,32768.0f*65536.0f};
  1783. static const XMVECTORF32 g_XMUDec4Mul = {1.0f,1.0f/1024.0f,1.0f/(1024.0f*1024.0f),1.0f/(1024.0f*1024.0f*1024.0f)};
  1784. XMASSERT(pSource);
  1785. // Splat the color in all four entries
  1786. XMVECTOR vTemp = _mm_load_ps1(reinterpret_cast<const float *>(&pSource->v));
  1787. // Shift R&0xFF0000, G&0xFF00, B&0xFF, A&0xFF000000
  1788. vTemp = _mm_and_ps(vTemp,g_XMUDec4And);
  1789. // a is unsigned! Flip the bit to convert the order to signed
  1790. vTemp = _mm_xor_ps(vTemp,g_XMUDec4Xor);
  1791. // Convert to floating point numbers
  1792. vTemp = _mm_cvtepi32_ps(reinterpret_cast<const __m128i *>(&vTemp)[0]);
  1793. // RGB + 0, A + 0x80000000.f to undo the signed order.
  1794. vTemp = _mm_add_ps(vTemp,g_XMUDec4Add);
  1795. // Convert 0-255 to 0.0f-1.0f
  1796. vTemp = _mm_mul_ps(vTemp,g_XMUDec4Mul);
  1797. return vTemp;
  1798. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  1799. #endif // _XM_VMX128_INTRINSICS_
  1800. }
  1801. //------------------------------------------------------------------------------
  1802. XMFINLINE XMVECTOR XMLoadDecN4
  1803. (
  1804. CONST XMDECN4* pSource
  1805. )
  1806. {
  1807. #if defined(_XM_NO_INTRINSICS_)
  1808. XMVECTOR V;
  1809. UINT Element;
  1810. static CONST UINT SignExtend[] = {0x00000000, 0xFFFFFC00};
  1811. static CONST UINT SignExtendW[] = {0x00000000, 0xFFFFFFFC};
  1812. XMASSERT(pSource);
  1813. XMASSERT((pSource->v & 0x3FF) != 0x200);
  1814. XMASSERT(((pSource->v >> 10) & 0x3FF) != 0x200);
  1815. XMASSERT(((pSource->v >> 20) & 0x3FF) != 0x200);
  1816. XMASSERT(((pSource->v >> 30) & 0x3) != 0x2);
  1817. Element = pSource->v & 0x3FF;
  1818. V.v[0] = (FLOAT)(SHORT)(Element | SignExtend[Element >> 9]) / 511.0f;
  1819. Element = (pSource->v >> 10) & 0x3FF;
  1820. V.v[1] = (FLOAT)(SHORT)(Element | SignExtend[Element >> 9]) / 511.0f;
  1821. Element = (pSource->v >> 20) & 0x3FF;
  1822. V.v[2] = (FLOAT)(SHORT)(Element | SignExtend[Element >> 9]) / 511.0f;
  1823. Element = pSource->v >> 30;
  1824. V.v[3] = (FLOAT)(SHORT)(Element | SignExtendW[Element >> 1]);
  1825. return V;
  1826. #elif defined(_XM_SSE_INTRINSICS_)
  1827. XMASSERT(pSource);
  1828. XMASSERT((pSource->v & 0x3FF) != 0x200);
  1829. XMASSERT(((pSource->v >> 10) & 0x3FF) != 0x200);
  1830. XMASSERT(((pSource->v >> 20) & 0x3FF) != 0x200);
  1831. XMASSERT(((pSource->v >> 30) & 0x3) != 0x2);
  1832. static const XMVECTORI32 g_XMDecN4And = {0x3FF, 0x3FF<<10, 0x3FF<<20, 0x3<<30};
  1833. static const XMVECTORI32 g_XMDecN4Xor = {0x200, 0x200<<10, 0x200<<20, 0};
  1834. static const XMVECTORF32 g_XMDecN4Add = {-512.0f,-512.0f*1024.0f,-512.0f*1024.0f*1024.0f,0};
  1835. static const XMVECTORF32 g_XMDecN4Mul = {1.0f/511.0f,1.0f/(511.0f*1024.0f),1.0f/(511.0f*1024.0f*1024.0f),1.0f/(1024.0f*1024.0f*1024.0f)};
  1836. // Splat the color in all four entries
  1837. XMVECTOR vTemp = _mm_load_ps1(reinterpret_cast<const float *>(&pSource->v));
  1838. // Shift R&0xFF0000, G&0xFF00, B&0xFF, A&0xFF000000
  1839. vTemp = _mm_and_ps(vTemp,g_XMDecN4And);
  1840. // a is unsigned! Flip the bit to convert the order to signed
  1841. vTemp = _mm_xor_ps(vTemp,g_XMDecN4Xor);
  1842. // Convert to floating point numbers
  1843. vTemp = _mm_cvtepi32_ps(reinterpret_cast<const __m128i *>(&vTemp)[0]);
  1844. // RGB + 0, A + 0x80000000.f to undo the signed order.
  1845. vTemp = _mm_add_ps(vTemp,g_XMDecN4Add);
  1846. // Convert 0-255 to 0.0f-1.0f
  1847. vTemp = _mm_mul_ps(vTemp,g_XMDecN4Mul);
  1848. return vTemp;
  1849. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  1850. #endif // _XM_VMX128_INTRINSICS_
  1851. }
  1852. //------------------------------------------------------------------------------
  1853. XMFINLINE XMVECTOR XMLoadDec4
  1854. (
  1855. CONST XMDEC4* pSource
  1856. )
  1857. {
  1858. #if defined(_XM_NO_INTRINSICS_)
  1859. XMVECTOR V;
  1860. UINT Element;
  1861. static CONST UINT SignExtend[] = {0x00000000, 0xFFFFFC00};
  1862. static CONST UINT SignExtendW[] = {0x00000000, 0xFFFFFFFC};
  1863. XMASSERT(pSource);
  1864. XMASSERT((pSource->v & 0x3FF) != 0x200);
  1865. XMASSERT(((pSource->v >> 10) & 0x3FF) != 0x200);
  1866. XMASSERT(((pSource->v >> 20) & 0x3FF) != 0x200);
  1867. XMASSERT(((pSource->v >> 30) & 0x3) != 0x2);
  1868. Element = pSource->v & 0x3FF;
  1869. V.v[0] = (FLOAT)(SHORT)(Element | SignExtend[Element >> 9]);
  1870. Element = (pSource->v >> 10) & 0x3FF;
  1871. V.v[1] = (FLOAT)(SHORT)(Element | SignExtend[Element >> 9]);
  1872. Element = (pSource->v >> 20) & 0x3FF;
  1873. V.v[2] = (FLOAT)(SHORT)(Element | SignExtend[Element >> 9]);
  1874. Element = pSource->v >> 30;
  1875. V.v[3] = (FLOAT)(SHORT)(Element | SignExtendW[Element >> 1]);
  1876. return V;
  1877. #elif defined(_XM_SSE_INTRINSICS_)
  1878. XMASSERT((pSource->v & 0x3FF) != 0x200);
  1879. XMASSERT(((pSource->v >> 10) & 0x3FF) != 0x200);
  1880. XMASSERT(((pSource->v >> 20) & 0x3FF) != 0x200);
  1881. XMASSERT(((pSource->v >> 30) & 0x3) != 0x2);
  1882. static const XMVECTORI32 g_XMDec4And = {0x3FF, 0x3FF<<10, 0x3FF<<20, 0x3<<30};
  1883. static const XMVECTORI32 g_XMDec4Xor = {0x200, 0x200<<10, 0x200<<20, 0};
  1884. static const XMVECTORF32 g_XMDec4Add = {-512.0f,-512.0f*1024.0f,-512.0f*1024.0f*1024.0f,0};
  1885. static const XMVECTORF32 g_XMDec4Mul = {1.0f,1.0f/1024.0f,1.0f/(1024.0f*1024.0f),1.0f/(1024.0f*1024.0f*1024.0f)};
  1886. XMASSERT(pSource);
  1887. // Splat the color in all four entries
  1888. XMVECTOR vTemp = _mm_load_ps1(reinterpret_cast<const float *>(&pSource->v));
  1889. // Shift R&0xFF0000, G&0xFF00, B&0xFF, A&0xFF000000
  1890. vTemp = _mm_and_ps(vTemp,g_XMDec4And);
  1891. // a is unsigned! Flip the bit to convert the order to signed
  1892. vTemp = _mm_xor_ps(vTemp,g_XMDec4Xor);
  1893. // Convert to floating point numbers
  1894. vTemp = _mm_cvtepi32_ps(reinterpret_cast<const __m128i *>(&vTemp)[0]);
  1895. // RGB + 0, A + 0x80000000.f to undo the signed order.
  1896. vTemp = _mm_add_ps(vTemp,g_XMDec4Add);
  1897. // Convert 0-255 to 0.0f-1.0f
  1898. vTemp = _mm_mul_ps(vTemp,g_XMDec4Mul);
  1899. return vTemp;
  1900. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  1901. #endif // _XM_VMX128_INTRINSICS_
  1902. }
  1903. //------------------------------------------------------------------------------
  1904. XMFINLINE XMVECTOR XMLoadUByteN4
  1905. (
  1906. CONST XMUBYTEN4* pSource
  1907. )
  1908. {
  1909. #if defined(_XM_NO_INTRINSICS_)
  1910. XMVECTOR V;
  1911. XMASSERT(pSource);
  1912. V.v[0] = (FLOAT)pSource->x / 255.0f;
  1913. V.v[1] = (FLOAT)pSource->y / 255.0f;
  1914. V.v[2] = (FLOAT)pSource->z / 255.0f;
  1915. V.v[3] = (FLOAT)pSource->w / 255.0f;
  1916. return V;
  1917. #elif defined(_XM_SSE_INTRINSICS_)
  1918. static const XMVECTORI32 g_XMLoadUByteN4And = {0xFF,0xFF00,0xFF0000,0xFF000000};
  1919. static const XMVECTORI32 g_XMLoadUByteN4Xor = {0,0,0,0x80000000};
  1920. static const XMVECTORF32 g_XMLoadUByteN4Add = {0,0,0,32768.0f*65536.0f};
  1921. static const XMVECTORF32 g_XMLoadUByteN4Mul = {1.0f/255.0f,1.0f/(255.0f*256.0f),1.0f/(255.0f*65536.0f),1.0f/(255.0f*65536.0f*256.0f)};
  1922. XMASSERT(pSource);
  1923. // Splat the color in all four entries (x,z,y,w)
  1924. XMVECTOR vTemp = _mm_load1_ps(reinterpret_cast<const float *>(&pSource->x));
  1925. // Mask x&0ff,y&0xff00,z&0xff0000,w&0xff000000
  1926. vTemp = _mm_and_ps(vTemp,g_XMLoadUByteN4And);
  1927. // w is signed! Flip the bits to convert the order to unsigned
  1928. vTemp = _mm_xor_ps(vTemp,g_XMLoadUByteN4Xor);
  1929. // Convert to floating point numbers
  1930. vTemp = _mm_cvtepi32_ps(reinterpret_cast<const __m128i *>(&vTemp)[0]);
  1931. // w + 0x80 to complete the conversion
  1932. vTemp = _mm_add_ps(vTemp,g_XMLoadUByteN4Add);
  1933. // Fix y, z and w because they are too large
  1934. vTemp = _mm_mul_ps(vTemp,g_XMLoadUByteN4Mul);
  1935. return vTemp;
  1936. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  1937. #endif // _XM_VMX128_INTRINSICS_
  1938. }
  1939. //------------------------------------------------------------------------------
  1940. XMFINLINE XMVECTOR XMLoadUByte4
  1941. (
  1942. CONST XMUBYTE4* pSource
  1943. )
  1944. {
  1945. #if defined(_XM_NO_INTRINSICS_)
  1946. XMVECTOR V;
  1947. XMASSERT(pSource);
  1948. V.v[0] = (FLOAT)pSource->x;
  1949. V.v[1] = (FLOAT)pSource->y;
  1950. V.v[2] = (FLOAT)pSource->z;
  1951. V.v[3] = (FLOAT)pSource->w;
  1952. return V;
  1953. #elif defined(_XM_SSE_INTRINSICS_)
  1954. static const XMVECTORI32 g_XMLoadUByte4And = {0xFF,0xFF00,0xFF0000,0xFF000000};
  1955. static const XMVECTORI32 g_XMLoadUByte4Xor = {0,0,0,0x80000000};
  1956. static const XMVECTORF32 g_XMLoadUByte4Add = {0,0,0,32768.0f*65536.0f};
  1957. static const XMVECTORF32 g_XMLoadUByte4Mul = {1.0f,1.0f/256.0f,1.0f/65536.0f,1.0f/(65536.0f*256.0f)};
  1958. XMASSERT(pSource);
  1959. // Splat the color in all four entries (x,z,y,w)
  1960. XMVECTOR vTemp = _mm_load1_ps(reinterpret_cast<const float *>(&pSource->x));
  1961. // Mask x&0ff,y&0xff00,z&0xff0000,w&0xff000000
  1962. vTemp = _mm_and_ps(vTemp,g_XMLoadUByte4And);
  1963. // w is signed! Flip the bits to convert the order to unsigned
  1964. vTemp = _mm_xor_ps(vTemp,g_XMLoadUByte4Xor);
  1965. // Convert to floating point numbers
  1966. vTemp = _mm_cvtepi32_ps(reinterpret_cast<const __m128i *>(&vTemp)[0]);
  1967. // w + 0x80 to complete the conversion
  1968. vTemp = _mm_add_ps(vTemp,g_XMLoadUByte4Add);
  1969. // Fix y, z and w because they are too large
  1970. vTemp = _mm_mul_ps(vTemp,g_XMLoadUByte4Mul);
  1971. return vTemp;
  1972. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  1973. #endif // _XM_VMX128_INTRINSICS_
  1974. }
  1975. //------------------------------------------------------------------------------
  1976. XMFINLINE XMVECTOR XMLoadByteN4
  1977. (
  1978. CONST XMBYTEN4* pSource
  1979. )
  1980. {
  1981. #if defined(_XM_NO_INTRINSICS_)
  1982. XMVECTOR V;
  1983. XMASSERT(pSource);
  1984. XMASSERT(pSource->x != -128);
  1985. XMASSERT(pSource->y != -128);
  1986. XMASSERT(pSource->z != -128);
  1987. XMASSERT(pSource->w != -128);
  1988. V.v[0] = (FLOAT)pSource->x / 127.0f;
  1989. V.v[1] = (FLOAT)pSource->y / 127.0f;
  1990. V.v[2] = (FLOAT)pSource->z / 127.0f;
  1991. V.v[3] = (FLOAT)pSource->w / 127.0f;
  1992. return V;
  1993. #elif defined(_XM_SSE_INTRINSICS_)
  1994. static const XMVECTORI32 g_XMLoadByteN4And = {0xFF,0xFF00,0xFF0000,0xFF000000};
  1995. static const XMVECTORI32 g_XMLoadByteN4Xor = {0x80,0x8000,0x800000,0x00000000};
  1996. static const XMVECTORF32 g_XMLoadByteN4Add = {-128.0f,-128.0f*256.0f,-128.0f*65536.0f,0};
  1997. static const XMVECTORF32 g_XMLoadByteN4Mul = {1.0f/127.0f,1.0f/(127.0f*256.0f),1.0f/(127.0f*65536.0f),1.0f/(127.0f*65536.0f*256.0f)};
  1998. XMASSERT(pSource);
  1999. XMASSERT(pSource->x != -128);
  2000. XMASSERT(pSource->y != -128);
  2001. XMASSERT(pSource->z != -128);
  2002. XMASSERT(pSource->w != -128);
  2003. // Splat the color in all four entries (x,z,y,w)
  2004. XMVECTOR vTemp = _mm_load1_ps(reinterpret_cast<const float *>(&pSource->x));
  2005. // Mask x&0ff,y&0xff00,z&0xff0000,w&0xff000000
  2006. vTemp = _mm_and_ps(vTemp,g_XMLoadByteN4And);
  2007. // x,y and z are unsigned! Flip the bits to convert the order to signed
  2008. vTemp = _mm_xor_ps(vTemp,g_XMLoadByteN4Xor);
  2009. // Convert to floating point numbers
  2010. vTemp = _mm_cvtepi32_ps(reinterpret_cast<const __m128i *>(&vTemp)[0]);
  2011. // x, y and z - 0x80 to complete the conversion
  2012. vTemp = _mm_add_ps(vTemp,g_XMLoadByteN4Add);
  2013. // Fix y, z and w because they are too large
  2014. vTemp = _mm_mul_ps(vTemp,g_XMLoadByteN4Mul);
  2015. return vTemp;
  2016. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  2017. #endif // _XM_VMX128_INTRINSICS_
  2018. }
  2019. //------------------------------------------------------------------------------
  2020. XMFINLINE XMVECTOR XMLoadByte4
  2021. (
  2022. CONST XMBYTE4* pSource
  2023. )
  2024. {
  2025. #if defined(_XM_NO_INTRINSICS_)
  2026. XMVECTOR V;
  2027. XMASSERT(pSource);
  2028. XMASSERT(pSource->x != -128);
  2029. XMASSERT(pSource->y != -128);
  2030. XMASSERT(pSource->z != -128);
  2031. XMASSERT(pSource->w != -128);
  2032. V.v[0] = (FLOAT)pSource->x;
  2033. V.v[1] = (FLOAT)pSource->y;
  2034. V.v[2] = (FLOAT)pSource->z;
  2035. V.v[3] = (FLOAT)pSource->w;
  2036. return V;
  2037. #elif defined(_XM_SSE_INTRINSICS_)
  2038. static const XMVECTORI32 g_XMLoadByte4And = {0xFF,0xFF00,0xFF0000,0xFF000000};
  2039. static const XMVECTORI32 g_XMLoadByte4Xor = {0x80,0x8000,0x800000,0x00000000};
  2040. static const XMVECTORF32 g_XMLoadByte4Add = {-128.0f,-128.0f*256.0f,-128.0f*65536.0f,0};
  2041. static const XMVECTORF32 g_XMLoadByte4Mul = {1.0f,1.0f/256.0f,1.0f/65536.0f,1.0f/(65536.0f*256.0f)};
  2042. XMASSERT(pSource);
  2043. XMASSERT(pSource->x != -128);
  2044. XMASSERT(pSource->y != -128);
  2045. XMASSERT(pSource->z != -128);
  2046. XMASSERT(pSource->w != -128);
  2047. // Splat the color in all four entries (x,z,y,w)
  2048. XMVECTOR vTemp = _mm_load1_ps(reinterpret_cast<const float *>(&pSource->x));
  2049. // Mask x&0ff,y&0xff00,z&0xff0000,w&0xff000000
  2050. vTemp = _mm_and_ps(vTemp,g_XMLoadByte4And);
  2051. // x,y and z are unsigned! Flip the bits to convert the order to signed
  2052. vTemp = _mm_xor_ps(vTemp,g_XMLoadByte4Xor);
  2053. // Convert to floating point numbers
  2054. vTemp = _mm_cvtepi32_ps(reinterpret_cast<const __m128i *>(&vTemp)[0]);
  2055. // x, y and z - 0x80 to complete the conversion
  2056. vTemp = _mm_add_ps(vTemp,g_XMLoadByte4Add);
  2057. // Fix y, z and w because they are too large
  2058. vTemp = _mm_mul_ps(vTemp,g_XMLoadByte4Mul);
  2059. return vTemp;
  2060. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  2061. #endif // _XM_VMX128_INTRINSICS_
  2062. }
  2063. //------------------------------------------------------------------------------
  2064. XMFINLINE XMVECTOR XMLoadColor
  2065. (
  2066. CONST XMCOLOR* pSource
  2067. )
  2068. {
  2069. #if defined(_XM_NO_INTRINSICS_)
  2070. XMASSERT(pSource);
  2071. {
  2072. // INT -> Float conversions are done in one instruction.
  2073. // UINT -> Float calls a runtime function. Keep in INT
  2074. INT iColor = (INT)(pSource->c);
  2075. XMVECTOR vColor = {
  2076. (FLOAT)((iColor >> 16) & 0xFF) * (1.0f/255.0f),
  2077. (FLOAT)((iColor >> 8) & 0xFF) * (1.0f/255.0f),
  2078. (FLOAT)(iColor & 0xFF) * (1.0f/255.0f),
  2079. (FLOAT)((iColor >> 24) & 0xFF) * (1.0f/255.0f)
  2080. };
  2081. return vColor;
  2082. }
  2083. #elif defined(_XM_SSE_INTRINSICS_)
  2084. XMASSERT(pSource);
  2085. // Splat the color in all four entries
  2086. __m128i vInt = _mm_set1_epi32(pSource->c);
  2087. // Shift R&0xFF0000, G&0xFF00, B&0xFF, A&0xFF000000
  2088. vInt = _mm_and_si128(vInt,g_XMMaskA8R8G8B8);
  2089. // a is unsigned! Flip the bit to convert the order to signed
  2090. vInt = _mm_xor_si128(vInt,g_XMFlipA8R8G8B8);
  2091. // Convert to floating point numbers
  2092. XMVECTOR vTemp = _mm_cvtepi32_ps(vInt);
  2093. // RGB + 0, A + 0x80000000.f to undo the signed order.
  2094. vTemp = _mm_add_ps(vTemp,g_XMFixAA8R8G8B8);
  2095. // Convert 0-255 to 0.0f-1.0f
  2096. return _mm_mul_ps(vTemp,g_XMNormalizeA8R8G8B8);
  2097. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  2098. #endif // _XM_VMX128_INTRINSICS_
  2099. }
  2100. //------------------------------------------------------------------------------
  2101. XMFINLINE XMMATRIX XMLoadFloat3x3
  2102. (
  2103. CONST XMFLOAT3X3* pSource
  2104. )
  2105. {
  2106. #if defined(_XM_NO_INTRINSICS_)
  2107. XMMATRIX M;
  2108. XMASSERT(pSource);
  2109. M.r[0].v[0] = pSource->m[0][0];
  2110. M.r[0].v[1] = pSource->m[0][1];
  2111. M.r[0].v[2] = pSource->m[0][2];
  2112. M.r[0].v[3] = 0.0f;
  2113. M.r[1].v[0] = pSource->m[1][0];
  2114. M.r[1].v[1] = pSource->m[1][1];
  2115. M.r[1].v[2] = pSource->m[1][2];
  2116. M.r[1].v[3] = 0.0f;
  2117. M.r[2].v[0] = pSource->m[2][0];
  2118. M.r[2].v[1] = pSource->m[2][1];
  2119. M.r[2].v[2] = pSource->m[2][2];
  2120. M.r[2].v[3] = 0.0f;
  2121. M.r[3].v[0] = 0.0f;
  2122. M.r[3].v[1] = 0.0f;
  2123. M.r[3].v[2] = 0.0f;
  2124. M.r[3].v[3] = 1.0f;
  2125. return M;
  2126. #elif defined(_XM_SSE_INTRINSICS_)
  2127. XMMATRIX M;
  2128. XMVECTOR V1, V2, V3, Z, T1, T2, T3, T4, T5;
  2129. Z = _mm_setzero_ps();
  2130. XMASSERT(pSource);
  2131. V1 = _mm_loadu_ps( &pSource->m[0][0] );
  2132. V2 = _mm_loadu_ps( &pSource->m[1][1] );
  2133. V3 = _mm_load_ss( &pSource->m[2][2] );
  2134. T1 = _mm_unpackhi_ps( V1, Z );
  2135. T2 = _mm_unpacklo_ps( V2, Z );
  2136. T3 = _mm_shuffle_ps( V3, T2, _MM_SHUFFLE( 0, 1, 0, 0 ) );
  2137. T4 = _mm_movehl_ps( T2, T3 );
  2138. T5 = _mm_movehl_ps( Z, T1 );
  2139. M.r[0] = _mm_movelh_ps( V1, T1 );
  2140. M.r[1] = _mm_add_ps( T4, T5 );
  2141. M.r[2] = _mm_shuffle_ps( V2, V3, _MM_SHUFFLE(1, 0, 3, 2) );
  2142. M.r[3] = g_XMIdentityR3;
  2143. return M;
  2144. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  2145. #endif // _XM_VMX128_INTRINSICS_
  2146. }
  2147. //------------------------------------------------------------------------------
  2148. XMFINLINE XMMATRIX XMLoadFloat4x3
  2149. (
  2150. CONST XMFLOAT4X3* pSource
  2151. )
  2152. {
  2153. #if defined(_XM_NO_INTRINSICS_)
  2154. XMMATRIX M;
  2155. XMASSERT(pSource);
  2156. ((UINT *)(&M.r[0].v[0]))[0] = ((const UINT *)(&pSource->m[0][0]))[0];
  2157. ((UINT *)(&M.r[0].v[1]))[0] = ((const UINT *)(&pSource->m[0][1]))[0];
  2158. ((UINT *)(&M.r[0].v[2]))[0] = ((const UINT *)(&pSource->m[0][2]))[0];
  2159. M.r[0].v[3] = 0.0f;
  2160. ((UINT *)(&M.r[1].v[0]))[0] = ((const UINT *)(&pSource->m[1][0]))[0];
  2161. ((UINT *)(&M.r[1].v[1]))[0] = ((const UINT *)(&pSource->m[1][1]))[0];
  2162. ((UINT *)(&M.r[1].v[2]))[0] = ((const UINT *)(&pSource->m[1][2]))[0];
  2163. M.r[1].v[3] = 0.0f;
  2164. ((UINT *)(&M.r[2].v[0]))[0] = ((const UINT *)(&pSource->m[2][0]))[0];
  2165. ((UINT *)(&M.r[2].v[1]))[0] = ((const UINT *)(&pSource->m[2][1]))[0];
  2166. ((UINT *)(&M.r[2].v[2]))[0] = ((const UINT *)(&pSource->m[2][2]))[0];
  2167. M.r[2].v[3] = 0.0f;
  2168. ((UINT *)(&M.r[3].v[0]))[0] = ((const UINT *)(&pSource->m[3][0]))[0];
  2169. ((UINT *)(&M.r[3].v[1]))[0] = ((const UINT *)(&pSource->m[3][1]))[0];
  2170. ((UINT *)(&M.r[3].v[2]))[0] = ((const UINT *)(&pSource->m[3][2]))[0];
  2171. M.r[3].v[3] = 1.0f;
  2172. return M;
  2173. #elif defined(_XM_SSE_INTRINSICS_)
  2174. XMASSERT(pSource);
  2175. // Use unaligned load instructions to
  2176. // load the 12 floats
  2177. // vTemp1 = x1,y1,z1,x2
  2178. XMVECTOR vTemp1 = _mm_loadu_ps(&pSource->m[0][0]);
  2179. // vTemp2 = y2,z2,x3,y3
  2180. XMVECTOR vTemp2 = _mm_loadu_ps(&pSource->m[1][1]);
  2181. // vTemp4 = z3,x4,y4,z4
  2182. XMVECTOR vTemp4 = _mm_loadu_ps(&pSource->m[2][2]);
  2183. // vTemp3 = x3,y3,z3,z3
  2184. XMVECTOR vTemp3 = _mm_shuffle_ps(vTemp2,vTemp4,_MM_SHUFFLE(0,0,3,2));
  2185. // vTemp2 = y2,z2,x2,x2
  2186. vTemp2 = _mm_shuffle_ps(vTemp2,vTemp1,_MM_SHUFFLE(3,3,1,0));
  2187. // vTemp2 = x2,y2,z2,z2
  2188. vTemp2 = _mm_shuffle_ps(vTemp2,vTemp2,_MM_SHUFFLE(1,1,0,2));
  2189. // vTemp1 = x1,y1,z1,0
  2190. vTemp1 = _mm_and_ps(vTemp1,g_XMMask3);
  2191. // vTemp2 = x2,y2,z2,0
  2192. vTemp2 = _mm_and_ps(vTemp2,g_XMMask3);
  2193. // vTemp3 = x3,y3,z3,0
  2194. vTemp3 = _mm_and_ps(vTemp3,g_XMMask3);
  2195. // vTemp4i = x4,y4,z4,0
  2196. __m128i vTemp4i = _mm_srli_si128(reinterpret_cast<const __m128i *>(&vTemp4)[0],32/8);
  2197. // vTemp4i = x4,y4,z4,1.0f
  2198. vTemp4i = _mm_or_si128(vTemp4i,g_XMIdentityR3);
  2199. XMMATRIX M(vTemp1,
  2200. vTemp2,
  2201. vTemp3,
  2202. reinterpret_cast<const __m128 *>(&vTemp4i)[0]);
  2203. return M;
  2204. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  2205. #endif // _XM_VMX128_INTRINSICS_
  2206. }
  2207. //------------------------------------------------------------------------------
  2208. XMFINLINE XMMATRIX XMLoadFloat4x3A
  2209. (
  2210. CONST XMFLOAT4X3A* pSource
  2211. )
  2212. {
  2213. #if defined(_XM_NO_INTRINSICS_)
  2214. XMMATRIX M;
  2215. XMASSERT(pSource);
  2216. XMASSERT(((UINT_PTR)pSource & 0xF) == 0);
  2217. M.r[0].v[0] = pSource->m[0][0];
  2218. M.r[0].v[1] = pSource->m[0][1];
  2219. M.r[0].v[2] = pSource->m[0][2];
  2220. M.r[0].v[3] = 0.0f;
  2221. M.r[1].v[0] = pSource->m[1][0];
  2222. M.r[1].v[1] = pSource->m[1][1];
  2223. M.r[1].v[2] = pSource->m[1][2];
  2224. M.r[1].v[3] = 0.0f;
  2225. M.r[2].v[0] = pSource->m[2][0];
  2226. M.r[2].v[1] = pSource->m[2][1];
  2227. M.r[2].v[2] = pSource->m[2][2];
  2228. M.r[2].v[3] = 0.0f;
  2229. M.r[3].v[0] = pSource->m[3][0];
  2230. M.r[3].v[1] = pSource->m[3][1];
  2231. M.r[3].v[2] = pSource->m[3][2];
  2232. M.r[3].v[3] = 1.0f;
  2233. return M;
  2234. #elif defined(_XM_SSE_INTRINSICS_)
  2235. XMASSERT(pSource);
  2236. // Use aligned load instructions to
  2237. // load the 12 floats
  2238. // vTemp1 = x1,y1,z1,x2
  2239. XMVECTOR vTemp1 = _mm_load_ps(&pSource->m[0][0]);
  2240. // vTemp2 = y2,z2,x3,y3
  2241. XMVECTOR vTemp2 = _mm_load_ps(&pSource->m[1][1]);
  2242. // vTemp4 = z3,x4,y4,z4
  2243. XMVECTOR vTemp4 = _mm_load_ps(&pSource->m[2][2]);
  2244. // vTemp3 = x3,y3,z3,z3
  2245. XMVECTOR vTemp3 = _mm_shuffle_ps(vTemp2,vTemp4,_MM_SHUFFLE(0,0,3,2));
  2246. // vTemp2 = y2,z2,x2,x2
  2247. vTemp2 = _mm_shuffle_ps(vTemp2,vTemp1,_MM_SHUFFLE(3,3,1,0));
  2248. // vTemp2 = x2,y2,z2,z2
  2249. vTemp2 = _mm_shuffle_ps(vTemp2,vTemp2,_MM_SHUFFLE(1,1,0,2));
  2250. // vTemp1 = x1,y1,z1,0
  2251. vTemp1 = _mm_and_ps(vTemp1,g_XMMask3);
  2252. // vTemp2 = x2,y2,z2,0
  2253. vTemp2 = _mm_and_ps(vTemp2,g_XMMask3);
  2254. // vTemp3 = x3,y3,z3,0
  2255. vTemp3 = _mm_and_ps(vTemp3,g_XMMask3);
  2256. // vTemp4i = x4,y4,z4,0
  2257. __m128i vTemp4i = _mm_srli_si128(reinterpret_cast<const __m128i *>(&vTemp4)[0],32/8);
  2258. // vTemp4i = x4,y4,z4,1.0f
  2259. vTemp4i = _mm_or_si128(vTemp4i,g_XMIdentityR3);
  2260. XMMATRIX M(vTemp1,
  2261. vTemp2,
  2262. vTemp3,
  2263. reinterpret_cast<const __m128 *>(&vTemp4i)[0]);
  2264. return M;
  2265. #else // _XM_VMX128_INTRINSICS_
  2266. #endif // _XM_VMX128_INTRINSICS_
  2267. }
  2268. //------------------------------------------------------------------------------
  2269. XMFINLINE XMMATRIX XMLoadFloat4x4
  2270. (
  2271. CONST XMFLOAT4X4* pSource
  2272. )
  2273. {
  2274. #if defined(_XM_NO_INTRINSICS_)
  2275. XMMATRIX M;
  2276. XMASSERT(pSource);
  2277. ((UINT *)(&M.r[0].v[0]))[0] = ((const UINT *)(&pSource->m[0][0]))[0];
  2278. ((UINT *)(&M.r[0].v[1]))[0] = ((const UINT *)(&pSource->m[0][1]))[0];
  2279. ((UINT *)(&M.r[0].v[2]))[0] = ((const UINT *)(&pSource->m[0][2]))[0];
  2280. ((UINT *)(&M.r[0].v[3]))[0] = ((const UINT *)(&pSource->m[0][3]))[0];
  2281. ((UINT *)(&M.r[1].v[0]))[0] = ((const UINT *)(&pSource->m[1][0]))[0];
  2282. ((UINT *)(&M.r[1].v[1]))[0] = ((const UINT *)(&pSource->m[1][1]))[0];
  2283. ((UINT *)(&M.r[1].v[2]))[0] = ((const UINT *)(&pSource->m[1][2]))[0];
  2284. ((UINT *)(&M.r[1].v[3]))[0] = ((const UINT *)(&pSource->m[1][3]))[0];
  2285. ((UINT *)(&M.r[2].v[0]))[0] = ((const UINT *)(&pSource->m[2][0]))[0];
  2286. ((UINT *)(&M.r[2].v[1]))[0] = ((const UINT *)(&pSource->m[2][1]))[0];
  2287. ((UINT *)(&M.r[2].v[2]))[0] = ((const UINT *)(&pSource->m[2][2]))[0];
  2288. ((UINT *)(&M.r[2].v[3]))[0] = ((const UINT *)(&pSource->m[2][3]))[0];
  2289. ((UINT *)(&M.r[3].v[0]))[0] = ((const UINT *)(&pSource->m[3][0]))[0];
  2290. ((UINT *)(&M.r[3].v[1]))[0] = ((const UINT *)(&pSource->m[3][1]))[0];
  2291. ((UINT *)(&M.r[3].v[2]))[0] = ((const UINT *)(&pSource->m[3][2]))[0];
  2292. ((UINT *)(&M.r[3].v[3]))[0] = ((const UINT *)(&pSource->m[3][3]))[0];
  2293. return M;
  2294. #elif defined(_XM_SSE_INTRINSICS_)
  2295. XMASSERT(pSource);
  2296. XMMATRIX M;
  2297. M.r[0] = _mm_loadu_ps( &pSource->_11 );
  2298. M.r[1] = _mm_loadu_ps( &pSource->_21 );
  2299. M.r[2] = _mm_loadu_ps( &pSource->_31 );
  2300. M.r[3] = _mm_loadu_ps( &pSource->_41 );
  2301. return M;
  2302. #elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  2303. #endif // _XM_VMX128_INTRINSICS_
  2304. }
  2305. //------------------------------------------------------------------------------
  2306. XMFINLINE XMMATRIX XMLoadFloat4x4A
  2307. (
  2308. CONST XMFLOAT4X4A* pSource
  2309. )
  2310. {
  2311. #if defined(_XM_NO_INTRINSICS_)
  2312. XMMATRIX M;
  2313. XMASSERT(pSource);
  2314. XMASSERT(((UINT_PTR)pSource & 0xF) == 0);
  2315. M.r[0].v[0] = pSource->m[0][0];
  2316. M.r[0].v[1] = pSource->m[0][1];
  2317. M.r[0].v[2] = pSource->m[0][2];
  2318. M.r[0].v[3] = pSource->m[0][3];
  2319. M.r[1].v[0] = pSource->m[1][0];
  2320. M.r[1].v[1] = pSource->m[1][1];
  2321. M.r[1].v[2] = pSource->m[1][2];
  2322. M.r[1].v[3] = pSource->m[1][3];
  2323. M.r[2].v[0] = pSource->m[2][0];
  2324. M.r[2].v[1] = pSource->m[2][1];
  2325. M.r[2].v[2] = pSource->m[2][2];
  2326. M.r[2].v[3] = pSource->m[2][3];
  2327. M.r[3].v[0] = pSource->m[3][0];
  2328. M.r[3].v[1] = pSource->m[3][1];
  2329. M.r[3].v[2] = pSource->m[3][2];
  2330. M.r[3].v[3] = pSource->m[3][3];
  2331. return M;
  2332. #elif defined(_XM_SSE_INTRINSICS_)
  2333. XMMATRIX M;
  2334. XMASSERT(pSource);
  2335. M.r[0] = _mm_load_ps( &pSource->_11 );
  2336. M.r[1] = _mm_load_ps( &pSource->_21 );
  2337. M.r[2] = _mm_load_ps( &pSource->_31 );
  2338. M.r[3] = _mm_load_ps( &pSource->_41 );
  2339. return M;
  2340. #else // _XM_VMX128_INTRINSICS_
  2341. #endif // _XM_VMX128_INTRINSICS_
  2342. }
  2343. /****************************************************************************
  2344. *
  2345. * Vector and matrix store operations
  2346. *
  2347. ****************************************************************************/
  2348. XMFINLINE VOID XMStoreInt
  2349. (
  2350. UINT* pDestination,
  2351. FXMVECTOR V
  2352. )
  2353. {
  2354. #if defined(_XM_NO_INTRINSICS_) || defined(_XM_SSE_INTRINSICS_)
  2355. XMASSERT(pDestination);
  2356. XMASSERT(((UINT_PTR)pDestination & 3) == 0);
  2357. *pDestination = XMVectorGetIntX( V );
  2358. #else // _XM_VMX128_INTRINSICS_
  2359. #endif // _XM_VMX128_INTRINSICS_
  2360. }
  2361. //------------------------------------------------------------------------------
  2362. XMFINLINE VOID XMStoreFloat
  2363. (
  2364. FLOAT* pDestination,
  2365. FXMVECTOR V
  2366. )
  2367. {
  2368. #if defined(_XM_NO_INTRINSICS_) || defined(_XM_SSE_INTRINSICS_)
  2369. XMASSERT(pDestination);
  2370. XMASSERT(((UINT_PTR)pDestination & 3) == 0);
  2371. *pDestination = XMVectorGetX( V );
  2372. #else // _XM_VMX128_INTRINSICS_
  2373. #endif // _XM_VMX128_INTRINSICS_
  2374. }
  2375. //------------------------------------------------------------------------------
  2376. XMFINLINE VOID XMStoreInt2
  2377. (
  2378. UINT* pDestination,
  2379. FXMVECTOR V
  2380. )
  2381. {
  2382. #if defined(_XM_NO_INTRINSICS_)
  2383. XMASSERT(pDestination);
  2384. XMASSERT(((UINT_PTR)pDestination & 3) == 0);
  2385. pDestination[0] = V.u[0];
  2386. pDestination[1] = V.u[1];
  2387. #elif defined(_XM_SSE_INTRINSICS_)
  2388. XMASSERT(pDestination);
  2389. XMASSERT(((UINT_PTR)pDestination & 3) == 0);
  2390. pDestination[0] = XMVectorGetIntX( V );
  2391. pDestination[1] = XMVectorGetIntY( V );
  2392. #else // _XM_VMX128_INTRINSICS_
  2393. #endif // _XM_VMX128_INTRINSICS_
  2394. }
  2395. //------------------------------------------------------------------------------
  2396. XMFINLINE VOID XMStoreInt2A
  2397. (
  2398. UINT* pDestination,
  2399. FXMVECTOR V
  2400. )
  2401. {
  2402. #if defined(_XM_NO_INTRINSICS_)
  2403. XMASSERT(pDestination);
  2404. XMASSERT(((UINT_PTR)pDestination & 0xF) == 0);
  2405. pDestination[0] = V.u[0];
  2406. pDestination[1] = V.u[1];
  2407. #elif defined(_XM_SSE_INTRINSICS_)
  2408. XMASSERT(pDestination);
  2409. XMASSERT(((UINT_PTR)pDestination & 3) == 0);
  2410. _mm_storel_epi64( (__m128i*)pDestination, reinterpret_cast<const __m128i *>(&V)[0] );
  2411. #else // _XM_VMX128_INTRINSICS_
  2412. #endif // _XM_VMX128_INTRINSICS_
  2413. }
  2414. //------------------------------------------------------------------------------
  2415. XMFINLINE VOID XMStoreFloat2
  2416. (
  2417. XMFLOAT2* pDestination,
  2418. FXMVECTOR V
  2419. )
  2420. {
  2421. #if defined(_XM_NO_INTRINSICS_)
  2422. XMASSERT(pDestination);
  2423. XMASSERT(((UINT_PTR)pDestination & 3) == 0);
  2424. pDestination->x = V.v[0];
  2425. pDestination->y = V.v[1];
  2426. #elif defined(_XM_SSE_INTRINSICS_)
  2427. XMASSERT(pDestination);
  2428. XMASSERT(((UINT_PTR)pDestination & 3) == 0);
  2429. XMVECTOR T = _mm_shuffle_ps( V, V, _MM_SHUFFLE( 1, 1, 1, 1 ) );
  2430. _mm_store_ss( &pDestination->x, V );
  2431. _mm_store_ss( &pDestination->y, T );
  2432. #else // _XM_VMX128_INTRINSICS_
  2433. #endif // _XM_VMX128_INTRINSICS_
  2434. }
  2435. //------------------------------------------------------------------------------
  2436. XMFINLINE VOID XMStoreFloat2A
  2437. (
  2438. XMFLOAT2A* pDestination,
  2439. FXMVECTOR V
  2440. )
  2441. {
  2442. #if defined(_XM_NO_INTRINSICS_)
  2443. XMASSERT(pDestination);
  2444. XMASSERT(((UINT_PTR)pDestination & 0xF) == 0);
  2445. pDestination->x = V.v[0];
  2446. pDestination->y = V.v[1];
  2447. #elif defined(_XM_SSE_INTRINSICS_)
  2448. XMASSERT(pDestination);
  2449. XMASSERT(((UINT_PTR)pDestination & 3) == 0);
  2450. XMVECTOR T = _mm_shuffle_ps( V, V, _MM_SHUFFLE( 1, 1, 1, 1 ) );
  2451. _mm_store_ss( &pDestination->x, V );
  2452. _mm_store_ss( &pDestination->y, T );
  2453. #else // _XM_VMX128_INTRINSICS_
  2454. #endif // _XM_VMX128_INTRINSICS_
  2455. }
  2456. //------------------------------------------------------------------------------
  2457. XMFINLINE VOID XMStoreHalf2
  2458. (
  2459. XMHALF2* pDestination,
  2460. FXMVECTOR V
  2461. )
  2462. {
  2463. #if defined(_XM_NO_INTRINSICS_)
  2464. XMASSERT(pDestination);
  2465. pDestination->x = XMConvertFloatToHalf(V.v[0]);
  2466. pDestination->y = XMConvertFloatToHalf(V.v[1]);
  2467. #elif defined(_XM_SSE_INTRINSICS_)
  2468. XMASSERT(pDestination);
  2469. pDestination->x = XMConvertFloatToHalf(XMVectorGetX(V));
  2470. pDestination->y = XMConvertFloatToHalf(XMVectorGetY(V));
  2471. #else // _XM_VMX128_INTRINSICS_
  2472. #endif // _XM_VMX128_INTRINSICS_
  2473. }
  2474. //------------------------------------------------------------------------------
  2475. XMFINLINE VOID XMStoreShortN2
  2476. (
  2477. XMSHORTN2* pDestination,
  2478. FXMVECTOR V
  2479. )
  2480. {
  2481. #if defined(_XM_NO_INTRINSICS_)
  2482. XMVECTOR N;
  2483. static CONST XMVECTORF32 Scale = {32767.0f, 32767.0f, 32767.0f, 32767.0f};
  2484. XMASSERT(pDestination);
  2485. N = XMVectorClamp(V, g_XMNegativeOne.v, g_XMOne.v);
  2486. N = XMVectorMultiply(N, Scale.v);
  2487. N = XMVectorRound(N);
  2488. pDestination->x = (SHORT)N.v[0];
  2489. pDestination->y = (SHORT)N.v[1];
  2490. #elif defined(_XM_SSE_INTRINSICS_)
  2491. XMASSERT(pDestination);
  2492. static CONST XMVECTORF32 Scale = {32767.0f, 32767.0f, 32767.0f, 32767.0f};
  2493. XMVECTOR vResult = _mm_max_ps(V,g_XMNegativeOne);
  2494. vResult = _mm_min_ps(vResult,g_XMOne);
  2495. vResult = _mm_mul_ps(vResult,Scale);
  2496. __m128i vResulti = _mm_cvtps_epi32(vResult);
  2497. vResulti = _mm_packs_epi32(vResulti,vResulti);
  2498. _mm_store_ss(reinterpret_cast<float *>(&pDestination->x),reinterpret_cast<const __m128 *>(&vResulti)[0]);
  2499. #else // _XM_VMX128_INTRINSICS_
  2500. #endif // _XM_VMX128_INTRINSICS_
  2501. }
  2502. //------------------------------------------------------------------------------
  2503. XMFINLINE VOID XMStoreShort2
  2504. (
  2505. XMSHORT2* pDestination,
  2506. FXMVECTOR V
  2507. )
  2508. {
  2509. #if defined(_XM_NO_INTRINSICS_)
  2510. XMVECTOR N;
  2511. static CONST XMVECTOR Min = {-32767.0f, -32767.0f, -32767.0f, -32767.0f};
  2512. static CONST XMVECTOR Max = {32767.0f, 32767.0f, 32767.0f, 32767.0f};
  2513. XMASSERT(pDestination);
  2514. N = XMVectorClamp(V, Min, Max);
  2515. N = XMVectorRound(N);
  2516. pDestination->x = (SHORT)N.v[0];
  2517. pDestination->y = (SHORT)N.v[1];
  2518. #elif defined(_XM_SSE_INTRINSICS_)
  2519. XMASSERT(pDestination);
  2520. static CONST XMVECTORF32 Min = {-32767.0f, -32767.0f, -32767.0f, -32767.0f};
  2521. static CONST XMVECTORF32 Max = {32767.0f, 32767.0f, 32767.0f, 32767.0f};
  2522. // Bounds check
  2523. XMVECTOR vResult = _mm_max_ps(V,Min);
  2524. vResult = _mm_min_ps(vResult,Max);
  2525. // Convert to int with rounding
  2526. __m128i vInt = _mm_cvtps_epi32(vResult);
  2527. // Pack the ints into shorts
  2528. vInt = _mm_packs_epi32(vInt,vInt);
  2529. _mm_store_ss(reinterpret_cast<float *>(&pDestination->x),reinterpret_cast<const __m128 *>(&vInt)[0]);
  2530. #else // _XM_VMX128_INTRINSICS_
  2531. #endif // _XM_VMX128_INTRINSICS_
  2532. }
  2533. //------------------------------------------------------------------------------
  2534. XMFINLINE VOID XMStoreUShortN2
  2535. (
  2536. XMUSHORTN2* pDestination,
  2537. FXMVECTOR V
  2538. )
  2539. {
  2540. #if defined(_XM_NO_INTRINSICS_)
  2541. XMVECTOR N;
  2542. static CONST XMVECTORF32 Scale = {65535.0f, 65535.0f, 65535.0f, 65535.0f};
  2543. XMASSERT(pDestination);
  2544. N = XMVectorClamp(V, XMVectorZero(), g_XMOne.v);
  2545. N = XMVectorMultiplyAdd(N, Scale.v, g_XMOneHalf.v);
  2546. N = XMVectorTruncate(N);
  2547. pDestination->x = (SHORT)N.v[0];
  2548. pDestination->y = (SHORT)N.v[1];
  2549. #elif defined(_XM_SSE_INTRINSICS_)
  2550. XMASSERT(pDestination);
  2551. static CONST XMVECTORF32 Scale = {65535.0f, 65535.0f, 65535.0f, 65535.0f};
  2552. // Bounds check
  2553. XMVECTOR vResult = _mm_max_ps(V,g_XMZero);
  2554. vResult = _mm_min_ps(vResult,g_XMOne);
  2555. vResult = _mm_mul_ps(vResult,Scale);
  2556. // Convert to int with rounding
  2557. __m128i vInt = _mm_cvtps_epi32(vResult);
  2558. // Since the SSE pack instruction clamps using signed rules,
  2559. // manually extract the values to store them to memory
  2560. pDestination->x = static_cast<SHORT>(_mm_extract_epi16(vInt,0));
  2561. pDestination->y = static_cast<SHORT>(_mm_extract_epi16(vInt,2));
  2562. #else // _XM_VMX128_INTRINSICS_
  2563. #endif // _XM_VMX128_INTRINSICS_
  2564. }
  2565. //------------------------------------------------------------------------------
  2566. XMFINLINE VOID XMStoreUShort2
  2567. (
  2568. XMUSHORT2* pDestination,
  2569. FXMVECTOR V
  2570. )
  2571. {
  2572. #if defined(_XM_NO_INTRINSICS_)
  2573. XMVECTOR N;
  2574. static CONST XMVECTOR Max = {65535.0f, 65535.0f, 65535.0f, 65535.0f};
  2575. XMASSERT(pDestination);
  2576. N = XMVectorClamp(V, XMVectorZero(), Max);
  2577. N = XMVectorRound(N);
  2578. pDestination->x = (SHORT)N.v[0];
  2579. pDestination->y = (SHORT)N.v[1];
  2580. #elif defined(_XM_SSE_INTRINSICS_)
  2581. XMASSERT(pDestination);
  2582. static CONST XMVECTORF32 Max = {65535.0f, 65535.0f, 65535.0f, 65535.0f};
  2583. // Bounds check
  2584. XMVECTOR vResult = _mm_max_ps(V,g_XMZero);
  2585. vResult = _mm_min_ps(vResult,Max);
  2586. // Convert to int with rounding
  2587. __m128i vInt = _mm_cvtps_epi32(vResult);
  2588. // Since the SSE pack instruction clamps using signed rules,
  2589. // manually extract the values to store them to memory
  2590. pDestination->x = static_cast<SHORT>(_mm_extract_epi16(vInt,0));
  2591. pDestination->y = static_cast<SHORT>(_mm_extract_epi16(vInt,2));
  2592. #else // _XM_VMX128_INTRINSICS_
  2593. #endif // _XM_VMX128_INTRINSICS_
  2594. }
  2595. //------------------------------------------------------------------------------
  2596. XMFINLINE VOID XMStoreInt3
  2597. (
  2598. UINT* pDestination,
  2599. FXMVECTOR V
  2600. )
  2601. {
  2602. #if defined(_XM_NO_INTRINSICS_)
  2603. XMASSERT(pDestination);
  2604. XMASSERT(((UINT_PTR)pDestination & 3) == 0);
  2605. pDestination[0] = V.u[0];
  2606. pDestination[1] = V.u[1];
  2607. pDestination[2] = V.u[2];
  2608. #elif defined(_XM_SSE_INTRINSICS_)
  2609. XMASSERT(pDestination);
  2610. XMASSERT(((UINT_PTR)pDestination & 3) == 0);
  2611. pDestination[0] = XMVectorGetIntX( V );
  2612. pDestination[1] = XMVectorGetIntY( V );
  2613. pDestination[2] = XMVectorGetIntZ( V );
  2614. #else // _XM_VMX128_INTRINSICS_
  2615. #endif // _XM_VMX128_INTRINSICS_
  2616. }
  2617. //------------------------------------------------------------------------------
  2618. XMFINLINE VOID XMStoreInt3A
  2619. (
  2620. UINT* pDestination,
  2621. FXMVECTOR V
  2622. )
  2623. {
  2624. #if defined(_XM_NO_INTRINSICS_)
  2625. XMASSERT(pDestination);
  2626. XMASSERT(((UINT_PTR)pDestination & 0xF) == 0);
  2627. pDestination[0] = V.u[0];
  2628. pDestination[1] = V.u[1];
  2629. pDestination[2] = V.u[2];
  2630. #elif defined(_XM_SSE_INTRINSICS_)
  2631. XMASSERT(pDestination);
  2632. XMASSERT(((UINT_PTR)pDestination & 3) == 0);
  2633. pDestination[0] = XMVectorGetIntX( V );
  2634. pDestination[1] = XMVectorGetIntY( V );
  2635. pDestination[2] = XMVectorGetIntZ( V );
  2636. #else // _XM_VMX128_INTRINSICS_
  2637. #endif // _XM_VMX128_INTRINSICS_
  2638. }
  2639. //------------------------------------------------------------------------------
  2640. XMFINLINE VOID XMStoreFloat3
  2641. (
  2642. XMFLOAT3* pDestination,
  2643. FXMVECTOR V
  2644. )
  2645. {
  2646. #if defined(_XM_NO_INTRINSICS_)
  2647. XMASSERT(pDestination);
  2648. XMASSERT(((UINT_PTR)pDestination & 3) == 0);
  2649. pDestination->x = V.v[0];
  2650. pDestination->y = V.v[1];
  2651. pDestination->z = V.v[2];
  2652. #elif defined(_XM_SSE_INTRINSICS_)
  2653. XMASSERT(pDestination);
  2654. XMASSERT(((UINT_PTR)pDestination & 3) == 0);
  2655. XMVECTOR T1 = _mm_shuffle_ps(V,V,_MM_SHUFFLE(1,1,1,1));
  2656. XMVECTOR T2 = _mm_shuffle_ps(V,V,_MM_SHUFFLE(2,2,2,2));
  2657. _mm_store_ss( &pDestination->x, V );
  2658. _mm_store_ss( &pDestination->y, T1 );
  2659. _mm_store_ss( &pDestination->z, T2 );
  2660. #else // _XM_VMX128_INTRINSICS_
  2661. #endif // _XM_VMX128_INTRINSICS_
  2662. }
  2663. //------------------------------------------------------------------------------
  2664. XMFINLINE VOID XMStoreFloat3A
  2665. (
  2666. XMFLOAT3A* pDestination,
  2667. FXMVECTOR V
  2668. )
  2669. {
  2670. #if defined(_XM_NO_INTRINSICS_)
  2671. XMASSERT(pDestination);
  2672. XMASSERT(((UINT_PTR)pDestination & 0xF) == 0);
  2673. pDestination->x = V.v[0];
  2674. pDestination->y = V.v[1];
  2675. pDestination->z = V.v[2];
  2676. #elif defined(_XM_SSE_INTRINSICS_)
  2677. XMASSERT(pDestination);
  2678. XMASSERT(((UINT_PTR)pDestination & 0xF) == 0);
  2679. XMVECTOR T1 = _mm_shuffle_ps( V, V, _MM_SHUFFLE( 1, 1, 1, 1 ) );
  2680. XMVECTOR T2 = _mm_unpackhi_ps( V, V );
  2681. _mm_store_ss( &pDestination->x, V );
  2682. _mm_store_ss( &pDestination->y, T1 );
  2683. _mm_store_ss( &pDestination->z, T2 );
  2684. #else // _XM_VMX128_INTRINSICS_
  2685. #endif // _XM_VMX128_INTRINSICS_
  2686. }
  2687. //------------------------------------------------------------------------------
  2688. XMFINLINE VOID XMStoreUHenDN3
  2689. (
  2690. XMUHENDN3* pDestination,
  2691. FXMVECTOR V
  2692. )
  2693. {
  2694. #if defined(_XM_NO_INTRINSICS_)
  2695. XMVECTOR N;
  2696. static CONST XMVECTORF32 Scale = {2047.0f, 2047.0f, 1023.0f, 0.0f};
  2697. XMASSERT(pDestination);
  2698. N = XMVectorClamp(V, XMVectorZero(), g_XMOne.v);
  2699. N = XMVectorMultiply(N, Scale.v);
  2700. pDestination->v = (((UINT)N.v[2] & 0x3FF) << 22) |
  2701. (((UINT)N.v[1] & 0x7FF) << 11) |
  2702. (((UINT)N.v[0] & 0x7FF));
  2703. #elif defined(_XM_SSE_INTRINSICS_)
  2704. XMASSERT(pDestination);
  2705. static const XMVECTORF32 ScaleUHenDN3 = {2047.0f, 2047.0f*2048.0f,1023.0f*(2048.0f*2048.0f)/2.0f,1.0f};
  2706. static const XMVECTORI32 MaskUHenDN3 = {0x7FF,0x7FF<<11,0x3FF<<(22-1),0};
  2707. // Clamp to bounds
  2708. XMVECTOR vResult = _mm_max_ps(V,g_XMZero);
  2709. vResult = _mm_min_ps(vResult,g_XMOne);
  2710. // Scale by multiplication
  2711. vResult = _mm_mul_ps(vResult,ScaleUHenDN3);
  2712. // Convert to int
  2713. __m128i vResulti = _mm_cvttps_epi32(vResult);
  2714. // Mask off any fraction
  2715. vResulti = _mm_and_si128(vResulti,MaskUHenDN3);
  2716. // Do a horizontal or of 3 entries
  2717. __m128i vResulti2 = _mm_shuffle_epi32(vResulti,_MM_SHUFFLE(0,3,2,1));
  2718. // i = x|y
  2719. vResulti = _mm_or_si128(vResulti,vResulti2);
  2720. // Move Z to the x position
  2721. vResulti2 = _mm_shuffle_epi32(vResulti2,_MM_SHUFFLE(0,3,2,1));
  2722. // Add Z to itself to perform a single bit left shift
  2723. vResulti2 = _mm_add_epi32(vResulti2,vResulti2);
  2724. // i = x|y|z
  2725. vResulti = _mm_or_si128(vResulti,vResulti2);
  2726. _mm_store_ss(reinterpret_cast<float *>(&pDestination->v),reinterpret_cast<const __m128 *>(&vResulti)[0]);
  2727. #else // _XM_VMX128_INTRINSICS_
  2728. #endif // _XM_VMX128_INTRINSICS_
  2729. }
  2730. //------------------------------------------------------------------------------
  2731. XMFINLINE VOID XMStoreUHenD3
  2732. (
  2733. XMUHEND3* pDestination,
  2734. FXMVECTOR V
  2735. )
  2736. {
  2737. #if defined(_XM_NO_INTRINSICS_)
  2738. XMVECTOR N;
  2739. static CONST XMVECTOR Max = {2047.0f, 2047.0f, 1023.0f, 0.0f};
  2740. XMASSERT(pDestination);
  2741. N = XMVectorClamp(V, XMVectorZero(), Max);
  2742. pDestination->v = (((UINT)N.v[2] & 0x3FF) << 22) |
  2743. (((UINT)N.v[1] & 0x7FF) << 11) |
  2744. (((UINT)N.v[0] & 0x7FF));
  2745. #elif defined(_XM_SSE_INTRINSICS_)
  2746. XMASSERT(pDestination);
  2747. static const XMVECTORF32 MaxUHenD3 = { 2047.0f, 2047.0f, 1023.0f, 1.0f};
  2748. static const XMVECTORF32 ScaleUHenD3 = {1.0f, 2048.0f,(2048.0f*2048.0f)/2.0f,1.0f};
  2749. static const XMVECTORI32 MaskUHenD3 = {0x7FF,0x7FF<<11,0x3FF<<(22-1),0};
  2750. // Clamp to bounds
  2751. XMVECTOR vResult = _mm_max_ps(V,g_XMZero);
  2752. vResult = _mm_min_ps(vResult,MaxUHenD3);
  2753. // Scale by multiplication
  2754. vResult = _mm_mul_ps(vResult,ScaleUHenD3);
  2755. // Convert to int
  2756. __m128i vResulti = _mm_cvttps_epi32(vResult);
  2757. // Mask off any fraction
  2758. vResulti = _mm_and_si128(vResulti,MaskUHenD3);
  2759. // Do a horizontal or of 3 entries
  2760. __m128i vResulti2 = _mm_shuffle_epi32(vResulti,_MM_SHUFFLE(0,3,2,1));
  2761. // i = x|y
  2762. vResulti = _mm_or_si128(vResulti,vResulti2);
  2763. // Move Z to the x position
  2764. vResulti2 = _mm_shuffle_epi32(vResulti2,_MM_SHUFFLE(0,3,2,1));
  2765. // Add Z to itself to perform a single bit left shift
  2766. vResulti2 = _mm_add_epi32(vResulti2,vResulti2);
  2767. // i = x|y|z
  2768. vResulti = _mm_or_si128(vResulti,vResulti2);
  2769. _mm_store_ss(reinterpret_cast<float *>(&pDestination->v),reinterpret_cast<const __m128 *>(&vResulti)[0]);
  2770. #else // _XM_VMX128_INTRINSICS_
  2771. #endif // _XM_VMX128_INTRINSICS_
  2772. }
  2773. //------------------------------------------------------------------------------
  2774. XMFINLINE VOID XMStoreHenDN3
  2775. (
  2776. XMHENDN3* pDestination,
  2777. FXMVECTOR V
  2778. )
  2779. {
  2780. #if defined(_XM_NO_INTRINSICS_)
  2781. XMVECTOR N;
  2782. static CONST XMVECTORF32 Scale = {1023.0f, 1023.0f, 511.0f, 1.0f};
  2783. XMASSERT(pDestination);
  2784. N = XMVectorClamp(V, g_XMNegativeOne.v, g_XMOne.v);
  2785. N = XMVectorMultiply(N, Scale.v);
  2786. pDestination->v = (((INT)N.v[2] & 0x3FF) << 22) |
  2787. (((INT)N.v[1] & 0x7FF) << 11) |
  2788. (((INT)N.v[0] & 0x7FF));
  2789. #elif defined(_XM_SSE_INTRINSICS_)
  2790. XMASSERT(pDestination);
  2791. static const XMVECTORF32 ScaleHenDN3 = {1023.0f, 1023.0f*2048.0f,511.0f*(2048.0f*2048.0f),1.0f};
  2792. static const XMVECTORI32 MaskHenDN3 = {0x7FF,0x7FF<<11,0x3FF<<22,0};
  2793. // Clamp to bounds
  2794. XMVECTOR vResult = _mm_max_ps(V,g_XMNegativeOne);
  2795. vResult = _mm_min_ps(vResult,g_XMOne);
  2796. // Scale by multiplication
  2797. vResult = _mm_mul_ps(vResult,ScaleHenDN3);
  2798. // Convert to int
  2799. __m128i vResulti = _mm_cvttps_epi32(vResult);
  2800. // Mask off any fraction
  2801. vResulti = _mm_and_si128(vResulti,MaskHenDN3);
  2802. // Do a horizontal or of all 4 entries
  2803. vResult = _mm_shuffle_ps(reinterpret_cast<const __m128 *>(&vResulti)[0],reinterpret_cast<const __m128 *>(&vResulti)[0],_MM_SHUFFLE(0,3,2,1));
  2804. vResulti = _mm_or_si128(vResulti,reinterpret_cast<const __m128i *>(&vResult)[0]);
  2805. vResult = _mm_shuffle_ps(vResult,vResult,_MM_SHUFFLE(0,3,2,1));
  2806. vResulti = _mm_or_si128(vResulti,reinterpret_cast<const __m128i *>(&vResult)[0]);
  2807. _mm_store_ss(reinterpret_cast<float *>(&pDestination->v),reinterpret_cast<const __m128 *>(&vResulti)[0]);
  2808. #else // _XM_VMX128_INTRINSICS_
  2809. #endif // _XM_VMX128_INTRINSICS_
  2810. }
  2811. //------------------------------------------------------------------------------
  2812. XMFINLINE VOID XMStoreHenD3
  2813. (
  2814. XMHEND3* pDestination,
  2815. FXMVECTOR V
  2816. )
  2817. {
  2818. #if defined(_XM_NO_INTRINSICS_)
  2819. XMVECTOR N;
  2820. static CONST XMVECTOR Min = {-1023.0f, -1023.0f, -511.0f, -1.0f};
  2821. static CONST XMVECTOR Max = {1023.0f, 1023.0f, 511.0f, 1.0f};
  2822. XMASSERT(pDestination);
  2823. N = XMVectorClamp(V, Min, Max);
  2824. pDestination->v = (((INT)N.v[2] & 0x3FF) << 22) |
  2825. (((INT)N.v[1] & 0x7FF) << 11) |
  2826. (((INT)N.v[0] & 0x7FF));
  2827. #elif defined(_XM_SSE_INTRINSICS_)
  2828. XMASSERT(pDestination);
  2829. static const XMVECTORF32 MinHenD3 = {-1023.0f,-1023.0f,-511.0f,-1.0f};
  2830. static const XMVECTORF32 MaxHenD3 = { 1023.0f, 1023.0f, 511.0f, 1.0f};
  2831. static const XMVECTORF32 ScaleHenD3 = {1.0f, 2048.0f,(2048.0f*2048.0f),1.0f};
  2832. static const XMVECTORI32 MaskHenD3 = {0x7FF,0x7FF<<11,0x3FF<<22,0};
  2833. // Clamp to bounds
  2834. XMVECTOR vResult = _mm_max_ps(V,MinHenD3);
  2835. vResult = _mm_min_ps(vResult,MaxHenD3);
  2836. // Scale by multiplication
  2837. vResult = _mm_mul_ps(vResult,ScaleHenD3);
  2838. // Convert to int
  2839. __m128i vResulti = _mm_cvttps_epi32(vResult);
  2840. // Mask off any fraction
  2841. vResulti = _mm_and_si128(vResulti,MaskHenD3);
  2842. // Do a horizontal or of all 4 entries
  2843. vResult = _mm_shuffle_ps(reinterpret_cast<const __m128 *>(&vResulti)[0],reinterpret_cast<const __m128 *>(&vResulti)[0],_MM_SHUFFLE(0,3,2,1));
  2844. vResulti = _mm_or_si128(vResulti,reinterpret_cast<const __m128i *>(&vResult)[0]);
  2845. vResult = _mm_shuffle_ps(vResult,vResult,_MM_SHUFFLE(0,3,2,1));
  2846. vResulti = _mm_or_si128(vResulti,reinterpret_cast<const __m128i *>(&vResult)[0]);
  2847. _mm_store_ss(reinterpret_cast<float *>(&pDestination->v),reinterpret_cast<const __m128 *>(&vResulti)[0]);
  2848. #else // _XM_VMX128_INTRINSICS_
  2849. #endif // _XM_VMX128_INTRINSICS_
  2850. }
  2851. //------------------------------------------------------------------------------
  2852. XMFINLINE VOID XMStoreUDHenN3
  2853. (
  2854. XMUDHENN3* pDestination,
  2855. FXMVECTOR V
  2856. )
  2857. {
  2858. #if defined(_XM_NO_INTRINSICS_)
  2859. XMVECTOR N;
  2860. static CONST XMVECTORF32 Scale = {1023.0f, 2047.0f, 2047.0f, 0.0f};
  2861. XMASSERT(pDestination);
  2862. N = XMVectorClamp(V, XMVectorZero(), g_XMOne.v);
  2863. N = XMVectorMultiply(N, Scale.v);
  2864. pDestination->v = (((UINT)N.v[2] & 0x7FF) << 21) |
  2865. (((UINT)N.v[1] & 0x7FF) << 10) |
  2866. (((UINT)N.v[0] & 0x3FF));
  2867. #elif defined(_XM_SSE_INTRINSICS_)
  2868. XMASSERT(pDestination);
  2869. static const XMVECTORF32 ScaleUDHenN3 = {1023.0f,2047.0f*1024.0f,2047.0f*(1024.0f*2048.0f)/2.0f,1.0f};
  2870. static const XMVECTORI32 MaskUDHenN3 = {0x3FF,0x7FF<<10,0x7FF<<(21-1),0};
  2871. // Clamp to bounds
  2872. XMVECTOR vResult = _mm_max_ps(V,g_XMZero);
  2873. vResult = _mm_min_ps(vResult,g_XMOne);
  2874. // Scale by multiplication
  2875. vResult = _mm_mul_ps(vResult,ScaleUDHenN3);
  2876. // Convert to int
  2877. __m128i vResulti = _mm_cvttps_epi32(vResult);
  2878. // Mask off any fraction
  2879. vResulti = _mm_and_si128(vResulti,MaskUDHenN3);
  2880. // Do a horizontal or of 3 entries
  2881. __m128i vResulti2 = _mm_shuffle_epi32(vResulti,_MM_SHUFFLE(0,3,2,1));
  2882. // i = x|y
  2883. vResulti = _mm_or_si128(vResulti,vResulti2);
  2884. // Move Z to the x position
  2885. vResulti2 = _mm_shuffle_epi32(vResulti2,_MM_SHUFFLE(0,3,2,1));
  2886. // Add Z to itself to perform a single bit left shift
  2887. vResulti2 = _mm_add_epi32(vResulti2,vResulti2);
  2888. // i = x|y|z
  2889. vResulti = _mm_or_si128(vResulti,vResulti2);
  2890. _mm_store_ss(reinterpret_cast<float *>(&pDestination->v),reinterpret_cast<const __m128 *>(&vResulti)[0]);
  2891. #else // _XM_VMX128_INTRINSICS_
  2892. #endif // _XM_VMX128_INTRINSICS_
  2893. }
  2894. //------------------------------------------------------------------------------
  2895. XMFINLINE VOID XMStoreUDHen3
  2896. (
  2897. XMUDHEN3* pDestination,
  2898. FXMVECTOR V
  2899. )
  2900. {
  2901. #if defined(_XM_NO_INTRINSICS_)
  2902. XMVECTOR N;
  2903. static CONST XMVECTOR Max = {1023.0f, 2047.0f, 2047.0f, 0.0f};
  2904. XMASSERT(pDestination);
  2905. N = XMVectorClamp(V, XMVectorZero(), Max);
  2906. pDestination->v = (((UINT)N.v[2] & 0x7FF) << 21) |
  2907. (((UINT)N.v[1] & 0x7FF) << 10) |
  2908. (((UINT)N.v[0] & 0x3FF));
  2909. #elif defined(_XM_SSE_INTRINSICS_)
  2910. XMASSERT(pDestination);
  2911. static const XMVECTORF32 MaxUDHen3 = { 1023.0f, 2047.0f, 2047.0f, 1.0f};
  2912. static const XMVECTORF32 ScaleUDHen3 = {1.0f, 1024.0f,(1024.0f*2048.0f)/2.0f,1.0f};
  2913. static const XMVECTORI32 MaskUDHen3 = {0x3FF,0x7FF<<10,0x7FF<<(21-1),0};
  2914. // Clamp to bounds
  2915. XMVECTOR vResult = _mm_max_ps(V,g_XMZero);
  2916. vResult = _mm_min_ps(vResult,MaxUDHen3);
  2917. // Scale by multiplication
  2918. vResult = _mm_mul_ps(vResult,ScaleUDHen3);
  2919. // Convert to int
  2920. __m128i vResulti = _mm_cvttps_epi32(vResult);
  2921. // Mask off any fraction
  2922. vResulti = _mm_and_si128(vResulti,MaskUDHen3);
  2923. // Do a horizontal or of 3 entries
  2924. __m128i vResulti2 = _mm_shuffle_epi32(vResulti,_MM_SHUFFLE(0,3,2,1));
  2925. // i = x|y
  2926. vResulti = _mm_or_si128(vResulti,vResulti2);
  2927. // Move Z to the x position
  2928. vResulti2 = _mm_shuffle_epi32(vResulti2,_MM_SHUFFLE(0,3,2,1));
  2929. // Add Z to itself to perform a single bit left shift
  2930. vResulti2 = _mm_add_epi32(vResulti2,vResulti2);
  2931. // i = x|y|z
  2932. vResulti = _mm_or_si128(vResulti,vResulti2);
  2933. _mm_store_ss(reinterpret_cast<float *>(&pDestination->v),reinterpret_cast<const __m128 *>(&vResulti)[0]);
  2934. #else // _XM_VMX128_INTRINSICS_
  2935. #endif // _XM_VMX128_INTRINSICS_
  2936. }
  2937. //------------------------------------------------------------------------------
  2938. XMFINLINE VOID XMStoreDHenN3
  2939. (
  2940. XMDHENN3* pDestination,
  2941. FXMVECTOR V
  2942. )
  2943. {
  2944. #if defined(_XM_NO_INTRINSICS_)
  2945. XMVECTOR N;
  2946. static CONST XMVECTORF32 Scale = {511.0f, 1023.0f, 1023.0f, 1.0f};
  2947. XMASSERT(pDestination);
  2948. N = XMVectorClamp(V, g_XMNegativeOne.v, g_XMOne.v);
  2949. N = XMVectorMultiply(N, Scale.v);
  2950. pDestination->v = (((INT)N.v[2] & 0x7FF) << 21) |
  2951. (((INT)N.v[1] & 0x7FF) << 10) |
  2952. (((INT)N.v[0] & 0x3FF));
  2953. #elif defined(_XM_SSE_INTRINSICS_)
  2954. XMASSERT(pDestination);
  2955. static const XMVECTORF32 ScaleDHenN3 = {511.0f, 1023.0f*1024.0f,1023.0f*(1024.0f*2048.0f),1.0f};
  2956. static const XMVECTORI32 MaskDHenN3 = {0x3FF,0x7FF<<10,0x7FF<<21,0};
  2957. // Clamp to bounds
  2958. XMVECTOR vResult = _mm_max_ps(V,g_XMNegativeOne);
  2959. vResult = _mm_min_ps(vResult,g_XMOne);
  2960. // Scale by multiplication
  2961. vResult = _mm_mul_ps(vResult,ScaleDHenN3);
  2962. // Convert to int
  2963. __m128i vResulti = _mm_cvttps_epi32(vResult);
  2964. // Mask off any fraction
  2965. vResulti = _mm_and_si128(vResulti,MaskDHenN3);
  2966. // Do a horizontal or of all 4 entries
  2967. vResult = _mm_shuffle_ps(reinterpret_cast<const __m128 *>(&vResulti)[0],reinterpret_cast<const __m128 *>(&vResulti)[0],_MM_SHUFFLE(0,3,2,1));
  2968. vResulti = _mm_or_si128(vResulti,reinterpret_cast<const __m128i *>(&vResult)[0]);
  2969. vResult = _mm_shuffle_ps(vResult,vResult,_MM_SHUFFLE(0,3,2,1));
  2970. vResulti = _mm_or_si128(vResulti,reinterpret_cast<const __m128i *>(&vResult)[0]);
  2971. _mm_store_ss(reinterpret_cast<float *>(&pDestination->v),reinterpret_cast<const __m128 *>(&vResulti)[0]);
  2972. #else // _XM_VMX128_INTRINSICS_
  2973. #endif // _XM_VMX128_INTRINSICS_
  2974. }
  2975. //------------------------------------------------------------------------------
  2976. XMFINLINE VOID XMStoreDHen3
  2977. (
  2978. XMDHEN3* pDestination,
  2979. FXMVECTOR V
  2980. )
  2981. {
  2982. #if defined(_XM_NO_INTRINSICS_)
  2983. XMVECTOR N;
  2984. static CONST XMVECTOR Min = {-511.0f, -1023.0f, -1023.0f, -1.0f};
  2985. static CONST XMVECTOR Max = {511.0f, 1023.0f, 1023.0f, 1.0f};
  2986. XMASSERT(pDestination);
  2987. N = XMVectorClamp(V, Min, Max);
  2988. pDestination->v = (((INT)N.v[2] & 0x7FF) << 21) |
  2989. (((INT)N.v[1] & 0x7FF) << 10) |
  2990. (((INT)N.v[0] & 0x3FF));
  2991. #elif defined(_XM_SSE_INTRINSICS_)
  2992. XMASSERT(pDestination);
  2993. static const XMVECTORF32 MinDHen3 = {-511.0f,-1023.0f,-1023.0f,-1.0f};
  2994. static const XMVECTORF32 MaxDHen3 = { 511.0f, 1023.0f, 1023.0f, 1.0f};
  2995. static const XMVECTORF32 ScaleDHen3 = {1.0f, 1024.0f,(1024.0f*2048.0f),1.0f};
  2996. static const XMVECTORI32 MaskDHen3 = {0x3FF,0x7FF<<10,0x7FF<<21,0};
  2997. // Clamp to bounds
  2998. XMVECTOR vResult = _mm_max_ps(V,MinDHen3);
  2999. vResult = _mm_min_ps(vResult,MaxDHen3);
  3000. // Scale by multiplication
  3001. vResult = _mm_mul_ps(vResult,ScaleDHen3);
  3002. // Convert to int
  3003. __m128i vResulti = _mm_cvttps_epi32(vResult);
  3004. // Mask off any fraction
  3005. vResulti = _mm_and_si128(vResulti,MaskDHen3);
  3006. // Do a horizontal or of all 4 entries
  3007. vResult = _mm_shuffle_ps(reinterpret_cast<const __m128 *>(&vResulti)[0],reinterpret_cast<const __m128 *>(&vResulti)[0],_MM_SHUFFLE(0,3,2,1));
  3008. vResulti = _mm_or_si128(vResulti,reinterpret_cast<const __m128i *>(&vResult)[0]);
  3009. vResult = _mm_shuffle_ps(vResult,vResult,_MM_SHUFFLE(0,3,2,1));
  3010. vResulti = _mm_or_si128(vResulti,reinterpret_cast<const __m128i *>(&vResult)[0]);
  3011. _mm_store_ss(reinterpret_cast<float *>(&pDestination->v),reinterpret_cast<const __m128 *>(&vResulti)[0]);
  3012. #else // _XM_VMX128_INTRINSICS_
  3013. #endif // _XM_VMX128_INTRINSICS_
  3014. }
  3015. //------------------------------------------------------------------------------
  3016. XMFINLINE VOID XMStoreInt4
  3017. (
  3018. UINT* pDestination,
  3019. FXMVECTOR V
  3020. )
  3021. {
  3022. #if defined(_XM_NO_INTRINSICS_)
  3023. XMASSERT(pDestination);
  3024. pDestination[0] = V.u[0];
  3025. pDestination[1] = V.u[1];
  3026. pDestination[2] = V.u[2];
  3027. pDestination[3] = V.u[3];
  3028. #elif defined(_XM_SSE_INTRINSICS_)
  3029. XMASSERT(pDestination);
  3030. _mm_storeu_si128( (__m128i*)pDestination, reinterpret_cast<const __m128i *>(&V)[0] );
  3031. #else // _XM_VMX128_INTRINSICS_
  3032. #endif // _XM_VMX128_INTRINSICS_
  3033. }
  3034. //------------------------------------------------------------------------------
  3035. XMFINLINE VOID XMStoreInt4A
  3036. (
  3037. UINT* pDestination,
  3038. FXMVECTOR V
  3039. )
  3040. {
  3041. #if defined(_XM_NO_INTRINSICS_)
  3042. XMASSERT(pDestination);
  3043. XMASSERT(((UINT_PTR)pDestination & 0xF) == 0);
  3044. pDestination[0] = V.u[0];
  3045. pDestination[1] = V.u[1];
  3046. pDestination[2] = V.u[2];
  3047. pDestination[3] = V.u[3];
  3048. #elif defined(_XM_SSE_INTRINSICS_)
  3049. XMASSERT(pDestination);
  3050. _mm_store_si128( (__m128i*)pDestination, reinterpret_cast<const __m128i *>(&V)[0] );
  3051. #else // _XM_VMX128_INTRINSICS_
  3052. #endif // _XM_VMX128_INTRINSICS_
  3053. }
  3054. //------------------------------------------------------------------------------
  3055. XMFINLINE VOID XMStoreInt4NC
  3056. (
  3057. UINT* pDestination,
  3058. FXMVECTOR V
  3059. )
  3060. {
  3061. #if defined(_XM_NO_INTRINSICS_)
  3062. XMASSERT(pDestination);
  3063. pDestination[0] = V.u[0];
  3064. pDestination[1] = V.u[1];
  3065. pDestination[2] = V.u[2];
  3066. pDestination[3] = V.u[3];
  3067. #elif defined(_XM_SSE_INTRINSICS_)
  3068. XMASSERT(pDestination);
  3069. _mm_storeu_si128( (__m128i*)pDestination, reinterpret_cast<const __m128i *>(&V)[0] );
  3070. #else // _XM_VMX128_INTRINSICS_
  3071. #endif // _XM_VMX128_INTRINSICS_
  3072. }
  3073. //------------------------------------------------------------------------------
  3074. XMFINLINE VOID XMStoreFloat4
  3075. (
  3076. XMFLOAT4* pDestination,
  3077. FXMVECTOR V
  3078. )
  3079. {
  3080. #if defined(_XM_NO_INTRINSICS_)
  3081. XMASSERT(pDestination);
  3082. pDestination->x = V.v[0];
  3083. pDestination->y = V.v[1];
  3084. pDestination->z = V.v[2];
  3085. pDestination->w = V.v[3];
  3086. #elif defined(_XM_SSE_INTRINSICS_)
  3087. XMASSERT(pDestination);
  3088. _mm_storeu_ps( &pDestination->x, V );
  3089. #else // _XM_VMX128_INTRINSICS_
  3090. #endif // _XM_VMX128_INTRINSICS_
  3091. }
  3092. //------------------------------------------------------------------------------
  3093. XMFINLINE VOID XMStoreFloat4A
  3094. (
  3095. XMFLOAT4A* pDestination,
  3096. FXMVECTOR V
  3097. )
  3098. {
  3099. #if defined(_XM_NO_INTRINSICS_)
  3100. XMASSERT(pDestination);
  3101. XMASSERT(((UINT_PTR)pDestination & 0xF) == 0);
  3102. pDestination->x = V.v[0];
  3103. pDestination->y = V.v[1];
  3104. pDestination->z = V.v[2];
  3105. pDestination->w = V.v[3];
  3106. #elif defined(_XM_SSE_INTRINSICS_)
  3107. XMASSERT(pDestination);
  3108. XMASSERT(((UINT_PTR)pDestination & 0xF) == 0);
  3109. _mm_store_ps( &pDestination->x, V );
  3110. #else // _XM_VMX128_INTRINSICS_
  3111. #endif // _XM_VMX128_INTRINSICS_
  3112. }
  3113. //------------------------------------------------------------------------------
  3114. XMFINLINE VOID XMStoreFloat4NC
  3115. (
  3116. XMFLOAT4* pDestination,
  3117. FXMVECTOR V
  3118. )
  3119. {
  3120. #if defined(_XM_NO_INTRINSICS_)
  3121. XMASSERT(pDestination);
  3122. pDestination->x = V.v[0];
  3123. pDestination->y = V.v[1];
  3124. pDestination->z = V.v[2];
  3125. pDestination->w = V.v[3];
  3126. #elif defined(_XM_SSE_INTRINSICS_)
  3127. XMASSERT(pDestination);
  3128. _mm_storeu_ps( &pDestination->x, V );
  3129. #else // _XM_VMX128_INTRINSICS_
  3130. #endif // _XM_VMX128_INTRINSICS_
  3131. }
  3132. //------------------------------------------------------------------------------
  3133. XMFINLINE VOID XMStoreHalf4
  3134. (
  3135. XMHALF4* pDestination,
  3136. FXMVECTOR V
  3137. )
  3138. {
  3139. #if defined(_XM_NO_INTRINSICS_)
  3140. XMASSERT(pDestination);
  3141. pDestination->x = XMConvertFloatToHalf(V.v[0]);
  3142. pDestination->y = XMConvertFloatToHalf(V.v[1]);
  3143. pDestination->z = XMConvertFloatToHalf(V.v[2]);
  3144. pDestination->w = XMConvertFloatToHalf(V.v[3]);
  3145. #elif defined(_XM_SSE_INTRINSICS_)
  3146. XMASSERT(pDestination);
  3147. pDestination->x = XMConvertFloatToHalf(XMVectorGetX(V));
  3148. pDestination->y = XMConvertFloatToHalf(XMVectorGetY(V));
  3149. pDestination->z = XMConvertFloatToHalf(XMVectorGetZ(V));
  3150. pDestination->w = XMConvertFloatToHalf(XMVectorGetW(V));
  3151. #else // _XM_VMX128_INTRINSICS_
  3152. #endif // _XM_VMX128_INTRINSICS_
  3153. }
  3154. //------------------------------------------------------------------------------
  3155. XMFINLINE VOID XMStoreShortN4
  3156. (
  3157. XMSHORTN4* pDestination,
  3158. FXMVECTOR V
  3159. )
  3160. {
  3161. #if defined(_XM_NO_INTRINSICS_)
  3162. XMVECTOR N;
  3163. static CONST XMVECTORF32 Scale = {32767.0f, 32767.0f, 32767.0f, 32767.0f};
  3164. XMASSERT(pDestination);
  3165. N = XMVectorClamp(V, g_XMNegativeOne.v, g_XMOne.v);
  3166. N = XMVectorMultiply(N, Scale.v);
  3167. N = XMVectorRound(N);
  3168. pDestination->x = (SHORT)N.v[0];
  3169. pDestination->y = (SHORT)N.v[1];
  3170. pDestination->z = (SHORT)N.v[2];
  3171. pDestination->w = (SHORT)N.v[3];
  3172. #elif defined(_XM_SSE_INTRINSICS_)
  3173. XMASSERT(pDestination);
  3174. static CONST XMVECTORF32 Scale = {32767.0f, 32767.0f, 32767.0f, 32767.0f};
  3175. XMVECTOR vResult = _mm_max_ps(V,g_XMNegativeOne);
  3176. vResult = _mm_min_ps(vResult,g_XMOne);
  3177. vResult = _mm_mul_ps(vResult,Scale);
  3178. __m128i vResulti = _mm_cvtps_epi32(vResult);
  3179. vResulti = _mm_packs_epi32(vResulti,vResulti);
  3180. _mm_store_sd(reinterpret_cast<double *>(&pDestination->x),reinterpret_cast<const __m128d *>(&vResulti)[0]);
  3181. #else // _XM_VMX128_INTRINSICS_
  3182. #endif // _XM_VMX128_INTRINSICS_
  3183. }
  3184. //------------------------------------------------------------------------------
  3185. XMFINLINE VOID XMStoreShort4
  3186. (
  3187. XMSHORT4* pDestination,
  3188. FXMVECTOR V
  3189. )
  3190. {
  3191. #if defined(_XM_NO_INTRINSICS_)
  3192. XMVECTOR N;
  3193. static CONST XMVECTOR Min = {-32767.0f, -32767.0f, -32767.0f, -32767.0f};
  3194. static CONST XMVECTOR Max = {32767.0f, 32767.0f, 32767.0f, 32767.0f};
  3195. XMASSERT(pDestination);
  3196. N = XMVectorClamp(V, Min, Max);
  3197. N = XMVectorRound(N);
  3198. pDestination->x = (SHORT)N.v[0];
  3199. pDestination->y = (SHORT)N.v[1];
  3200. pDestination->z = (SHORT)N.v[2];
  3201. pDestination->w = (SHORT)N.v[3];
  3202. #elif defined(_XM_SSE_INTRINSICS_)
  3203. XMASSERT(pDestination);
  3204. static CONST XMVECTORF32 Min = {-32767.0f, -32767.0f, -32767.0f, -32767.0f};
  3205. static CONST XMVECTORF32 Max = {32767.0f, 32767.0f, 32767.0f, 32767.0f};
  3206. // Bounds check
  3207. XMVECTOR vResult = _mm_max_ps(V,Min);
  3208. vResult = _mm_min_ps(vResult,Max);
  3209. // Convert to int with rounding
  3210. __m128i vInt = _mm_cvtps_epi32(vResult);
  3211. // Pack the ints into shorts
  3212. vInt = _mm_packs_epi32(vInt,vInt);
  3213. _mm_store_sd(reinterpret_cast<double *>(&pDestination->x),reinterpret_cast<const __m128d *>(&vInt)[0]);
  3214. #else // _XM_VMX128_INTRINSICS_
  3215. #endif // _XM_VMX128_INTRINSICS_
  3216. }
  3217. //------------------------------------------------------------------------------
  3218. XMFINLINE VOID XMStoreUShortN4
  3219. (
  3220. XMUSHORTN4* pDestination,
  3221. FXMVECTOR V
  3222. )
  3223. {
  3224. #if defined(_XM_NO_INTRINSICS_)
  3225. XMVECTOR N;
  3226. static CONST XMVECTORF32 Scale = {65535.0f, 65535.0f, 65535.0f, 65535.0f};
  3227. XMASSERT(pDestination);
  3228. N = XMVectorClamp(V, XMVectorZero(), g_XMOne.v);
  3229. N = XMVectorMultiplyAdd(N, Scale.v, g_XMOneHalf.v);
  3230. N = XMVectorTruncate(N);
  3231. pDestination->x = (SHORT)N.v[0];
  3232. pDestination->y = (SHORT)N.v[1];
  3233. pDestination->z = (SHORT)N.v[2];
  3234. pDestination->w = (SHORT)N.v[3];
  3235. #elif defined(_XM_SSE_INTRINSICS_)
  3236. XMASSERT(pDestination);
  3237. static CONST XMVECTORF32 Scale = {65535.0f, 65535.0f, 65535.0f, 65535.0f};
  3238. // Bounds check
  3239. XMVECTOR vResult = _mm_max_ps(V,g_XMZero);
  3240. vResult = _mm_min_ps(vResult,g_XMOne);
  3241. vResult = _mm_mul_ps(vResult,Scale);
  3242. // Convert to int with rounding
  3243. __m128i vInt = _mm_cvtps_epi32(vResult);
  3244. // Since the SSE pack instruction clamps using signed rules,
  3245. // manually extract the values to store them to memory
  3246. pDestination->x = static_cast<SHORT>(_mm_extract_epi16(vInt,0));
  3247. pDestination->y = static_cast<SHORT>(_mm_extract_epi16(vInt,2));
  3248. pDestination->z = static_cast<SHORT>(_mm_extract_epi16(vInt,4));
  3249. pDestination->w = static_cast<SHORT>(_mm_extract_epi16(vInt,6));
  3250. #else // _XM_VMX128_INTRINSICS_
  3251. #endif // _XM_VMX128_INTRINSICS_
  3252. }
  3253. //------------------------------------------------------------------------------
  3254. XMFINLINE VOID XMStoreUShort4
  3255. (
  3256. XMUSHORT4* pDestination,
  3257. FXMVECTOR V
  3258. )
  3259. {
  3260. #if defined(_XM_NO_INTRINSICS_)
  3261. XMVECTOR N;
  3262. static CONST XMVECTOR Max = {65535.0f, 65535.0f, 65535.0f, 65535.0f};
  3263. XMASSERT(pDestination);
  3264. N = XMVectorClamp(V, XMVectorZero(), Max);
  3265. N = XMVectorRound(N);
  3266. pDestination->x = (SHORT)N.v[0];
  3267. pDestination->y = (SHORT)N.v[1];
  3268. pDestination->z = (SHORT)N.v[2];
  3269. pDestination->w = (SHORT)N.v[3];
  3270. #elif defined(_XM_SSE_INTRINSICS_)
  3271. XMASSERT(pDestination);
  3272. static CONST XMVECTORF32 Max = {65535.0f, 65535.0f, 65535.0f, 65535.0f};
  3273. // Bounds check
  3274. XMVECTOR vResult = _mm_max_ps(V,g_XMZero);
  3275. vResult = _mm_min_ps(vResult,Max);
  3276. // Convert to int with rounding
  3277. __m128i vInt = _mm_cvtps_epi32(vResult);
  3278. // Since the SSE pack instruction clamps using signed rules,
  3279. // manually extract the values to store them to memory
  3280. pDestination->x = static_cast<SHORT>(_mm_extract_epi16(vInt,0));
  3281. pDestination->y = static_cast<SHORT>(_mm_extract_epi16(vInt,2));
  3282. pDestination->z = static_cast<SHORT>(_mm_extract_epi16(vInt,4));
  3283. pDestination->w = static_cast<SHORT>(_mm_extract_epi16(vInt,6));
  3284. #else // _XM_VMX128_INTRINSICS_
  3285. #endif // _XM_VMX128_INTRINSICS_
  3286. }
  3287. //------------------------------------------------------------------------------
  3288. XMFINLINE VOID XMStoreXIcoN4
  3289. (
  3290. XMXICON4* pDestination,
  3291. FXMVECTOR V
  3292. )
  3293. {
  3294. #if defined(_XM_NO_INTRINSICS_)
  3295. XMVECTOR N;
  3296. static CONST XMVECTORF32 Min = {-1.0f, -1.0f, -1.0f, 0.0f};
  3297. static CONST XMVECTORF32 Scale = {524287.0f, 524287.0f, 524287.0f, 15.0f};
  3298. XMASSERT(pDestination);
  3299. N = XMVectorClamp(V, Min.v, g_XMOne.v);
  3300. N = XMVectorMultiply(N, Scale.v);
  3301. N = XMVectorRound(N);
  3302. pDestination->v = ((UINT64)N.v[3] << 60) |
  3303. (((INT64)N.v[2] & 0xFFFFF) << 40) |
  3304. (((INT64)N.v[1] & 0xFFFFF) << 20) |
  3305. (((INT64)N.v[0] & 0xFFFFF));
  3306. #elif defined(_XM_SSE_INTRINSICS_)
  3307. XMASSERT(pDestination);
  3308. // Note: Masks are x,w,y and z
  3309. static const XMVECTORF32 MinXIcoN4 = {-1.0f, 0.0f,-1.0f,-1.0f};
  3310. static const XMVECTORF32 ScaleXIcoN4 = {524287.0f,15.0f*4096.0f*65536.0f*0.5f,524287.0f*4096.0f,524287.0f};
  3311. static const XMVECTORI32 MaskXIcoN4 = {0xFFFFF,0xF<<((60-32)-1),0xFFFFF000,0xFFFFF};
  3312. // Clamp to bounds
  3313. XMVECTOR vResult = _mm_shuffle_ps(V,V,_MM_SHUFFLE(2,1,3,0));
  3314. vResult = _mm_max_ps(vResult,MinXIcoN4);
  3315. vResult = _mm_min_ps(vResult,g_XMOne);
  3316. // Scale by multiplication
  3317. vResult = _mm_mul_ps(vResult,ScaleXIcoN4);
  3318. // Convert to integer (w is unsigned)
  3319. __m128i vResulti = _mm_cvttps_epi32(vResult);
  3320. // Mask off unused bits
  3321. vResulti = _mm_and_si128(vResulti,MaskXIcoN4);
  3322. // Isolate Y
  3323. __m128i vResulti2 = _mm_and_si128(vResulti,g_XMMaskY);
  3324. // Double Y (Really W) to fixup for unsigned conversion
  3325. vResulti = _mm_add_epi32(vResulti,vResulti2);
  3326. // Shift y and z to straddle the 32-bit boundary
  3327. vResulti2 = _mm_srli_si128(vResulti,(64+12)/8);
  3328. // Shift it into place
  3329. vResulti2 = _mm_slli_si128(vResulti2,20/8);
  3330. // i = x|y<<20|z<<40|w<<60
  3331. vResulti = _mm_or_si128(vResulti,vResulti2);
  3332. _mm_store_sd(reinterpret_cast<double *>(&pDestination->v),reinterpret_cast<const __m128d *>(&vResulti)[0]);
  3333. #else // _XM_VMX128_INTRINSICS_
  3334. #endif // _XM_VMX128_INTRINSICS_
  3335. }
  3336. //------------------------------------------------------------------------------
  3337. XMFINLINE VOID XMStoreXIco4
  3338. (
  3339. XMXICO4* pDestination,
  3340. FXMVECTOR V
  3341. )
  3342. {
  3343. #if defined(_XM_NO_INTRINSICS_)
  3344. XMVECTOR N;
  3345. static CONST XMVECTORF32 Min = {-524287.0f, -524287.0f, -524287.0f, 0.0f};
  3346. static CONST XMVECTORF32 Max = {524287.0f, 524287.0f, 524287.0f, 15.0f};
  3347. XMASSERT(pDestination);
  3348. N = XMVectorClamp(V, Min.v, Max.v);
  3349. pDestination->v = ((UINT64)N.v[3] << 60) |
  3350. (((INT64)N.v[2] & 0xFFFFF) << 40) |
  3351. (((INT64)N.v[1] & 0xFFFFF) << 20) |
  3352. (((INT64)N.v[0] & 0xFFFFF));
  3353. #elif defined(_XM_SSE_INTRINSICS_)
  3354. XMASSERT(pDestination);
  3355. // Note: Masks are x,w,y and z
  3356. static const XMVECTORF32 MinXIco4 = {-524287.0f, 0.0f,-524287.0f,-524287.0f};
  3357. static const XMVECTORF32 MaxXIco4 = { 524287.0f,15.0f, 524287.0f, 524287.0f};
  3358. static const XMVECTORF32 ScaleXIco4 = {1.0f,4096.0f*65536.0f*0.5f,4096.0f,1.0f};
  3359. static const XMVECTORI32 MaskXIco4 = {0xFFFFF,0xF<<((60-1)-32),0xFFFFF000,0xFFFFF};
  3360. // Clamp to bounds
  3361. XMVECTOR vResult = _mm_shuffle_ps(V,V,_MM_SHUFFLE(2,1,3,0));
  3362. vResult = _mm_max_ps(vResult,MinXIco4);
  3363. vResult = _mm_min_ps(vResult,MaxXIco4);
  3364. // Scale by multiplication
  3365. vResult = _mm_mul_ps(vResult,ScaleXIco4);
  3366. // Convert to int
  3367. __m128i vResulti = _mm_cvttps_epi32(vResult);
  3368. // Mask off any fraction
  3369. vResulti = _mm_and_si128(vResulti,MaskXIco4);
  3370. // Isolate Y
  3371. __m128i vResulti2 = _mm_and_si128(vResulti,g_XMMaskY);
  3372. // Double Y (Really W) to fixup for unsigned conversion
  3373. vResulti = _mm_add_epi32(vResulti,vResulti2);
  3374. // Shift y and z to straddle the 32-bit boundary
  3375. vResulti2 = _mm_srli_si128(vResulti,(64+12)/8);
  3376. // Shift it into place
  3377. vResulti2 = _mm_slli_si128(vResulti2,20/8);
  3378. // i = x|y<<20|z<<40|w<<60
  3379. vResulti = _mm_or_si128(vResulti,vResulti2);
  3380. _mm_store_sd(reinterpret_cast<double *>(&pDestination->v),reinterpret_cast<const __m128d *>(&vResulti)[0]);
  3381. #else // _XM_VMX128_INTRINSICS_
  3382. #endif // _XM_VMX128_INTRINSICS_
  3383. }
  3384. //------------------------------------------------------------------------------
  3385. XMFINLINE VOID XMStoreUIcoN4
  3386. (
  3387. XMUICON4* pDestination,
  3388. FXMVECTOR V
  3389. )
  3390. {
  3391. #define XM_URange ((FLOAT)(1 << 20))
  3392. #define XM_URangeDiv2 ((FLOAT)(1 << 19))
  3393. #define XM_UMaxXYZ ((FLOAT)((1 << 20) - 1))
  3394. #define XM_UMaxW ((FLOAT)((1 << 4) - 1))
  3395. #define XM_ScaleXYZ (-(FLOAT)((1 << 20) - 1) / XM_PACK_FACTOR)
  3396. #define XM_ScaleW (-(FLOAT)((1 << 4) - 1) / XM_PACK_FACTOR)
  3397. #define XM_Scale (-1.0f / XM_PACK_FACTOR)
  3398. #define XM_Offset (3.0f)
  3399. #if defined(_XM_NO_INTRINSICS_)
  3400. XMVECTOR N;
  3401. static CONST XMVECTORF32 Scale = {1048575.0f, 1048575.0f, 1048575.0f, 15.0f};
  3402. XMASSERT(pDestination);
  3403. N = XMVectorClamp(V, XMVectorZero(), g_XMOne.v);
  3404. N = XMVectorMultiplyAdd(N, Scale.v, g_XMOneHalf.v);
  3405. pDestination->v = ((UINT64)N.v[3] << 60) |
  3406. (((UINT64)N.v[2] & 0xFFFFF) << 40) |
  3407. (((UINT64)N.v[1] & 0xFFFFF) << 20) |
  3408. (((UINT64)N.v[0] & 0xFFFFF));
  3409. #elif defined(_XM_SSE_INTRINSICS_)
  3410. XMASSERT(pDestination);
  3411. // Note: Masks are x,w,y and z
  3412. static const XMVECTORF32 ScaleUIcoN4 = {1048575.0f,15.0f*4096.0f*65536.0f,1048575.0f*4096.0f,1048575.0f};
  3413. static const XMVECTORI32 MaskUIcoN4 = {0xFFFFF,0xF<<(60-32),0xFFFFF000,0xFFFFF};
  3414. static const XMVECTORF32 AddUIcoN4 = {0.0f,-32768.0f*65536.0f,-32768.0f*65536.0f,0.0f};
  3415. static const XMVECTORI32 XorUIcoN4 = {0x00000000,0x80000000,0x80000000,0x00000000};
  3416. // Clamp to bounds
  3417. XMVECTOR vResult = _mm_shuffle_ps(V,V,_MM_SHUFFLE(2,1,3,0));
  3418. vResult = _mm_max_ps(vResult,g_XMZero);
  3419. vResult = _mm_min_ps(vResult,g_XMOne);
  3420. // Scale by multiplication
  3421. vResult = _mm_mul_ps(vResult,ScaleUIcoN4);
  3422. // Adjust for unsigned entries
  3423. vResult = _mm_add_ps(vResult,AddUIcoN4);
  3424. // Convert to int
  3425. __m128i vResulti = _mm_cvttps_epi32(vResult);
  3426. // Fix the signs on the unsigned entries
  3427. vResulti = _mm_xor_si128(vResulti,XorUIcoN4);
  3428. // Mask off any fraction
  3429. vResulti = _mm_and_si128(vResulti,MaskUIcoN4);
  3430. // Shift y and z to straddle the 32-bit boundary
  3431. __m128i vResulti2 = _mm_srli_si128(vResulti,(64+12)/8);
  3432. // Shift it into place
  3433. vResulti2 = _mm_slli_si128(vResulti2,20/8);
  3434. // i = x|y<<20|z<<40|w<<60
  3435. vResulti = _mm_or_si128(vResulti,vResulti2);
  3436. _mm_store_sd(reinterpret_cast<double *>(&pDestination->v),reinterpret_cast<const __m128d *>(&vResulti)[0]);
  3437. #else // _XM_VMX128_INTRINSICS_
  3438. #endif // _XM_VMX128_INTRINSICS_
  3439. #undef XM_URange
  3440. #undef XM_URangeDiv2
  3441. #undef XM_UMaxXYZ
  3442. #undef XM_UMaxW
  3443. #undef XM_ScaleXYZ
  3444. #undef XM_ScaleW
  3445. #undef XM_Scale
  3446. #undef XM_Offset
  3447. }
  3448. //------------------------------------------------------------------------------
  3449. XMFINLINE VOID XMStoreUIco4
  3450. (
  3451. XMUICO4* pDestination,
  3452. FXMVECTOR V
  3453. )
  3454. {
  3455. #define XM_Scale (-1.0f / XM_PACK_FACTOR)
  3456. #define XM_URange ((FLOAT)(1 << 20))
  3457. #define XM_URangeDiv2 ((FLOAT)(1 << 19))
  3458. #if defined(_XM_NO_INTRINSICS_)
  3459. XMVECTOR N;
  3460. static CONST XMVECTOR Max = {1048575.0f, 1048575.0f, 1048575.0f, 15.0f};
  3461. XMASSERT(pDestination);
  3462. N = XMVectorClamp(V, XMVectorZero(), Max);
  3463. N = XMVectorRound(N);
  3464. pDestination->v = ((UINT64)N.v[3] << 60) |
  3465. (((UINT64)N.v[2] & 0xFFFFF) << 40) |
  3466. (((UINT64)N.v[1] & 0xFFFFF) << 20) |
  3467. (((UINT64)N.v[0] & 0xFFFFF));
  3468. #elif defined(_XM_SSE_INTRINSICS_)
  3469. XMASSERT(pDestination);
  3470. // Note: Masks are x,w,y and z
  3471. static const XMVECTORF32 MaxUIco4 = { 1048575.0f, 15.0f, 1048575.0f, 1048575.0f};
  3472. static const XMVECTORF32 ScaleUIco4 = {1.0f,4096.0f*65536.0f,4096.0f,1.0f};
  3473. static const XMVECTORI32 MaskUIco4 = {0xFFFFF,0xF<<(60-32),0xFFFFF000,0xFFFFF};
  3474. static const XMVECTORF32 AddUIco4 = {0.0f,-32768.0f*65536.0f,-32768.0f*65536.0f,0.0f};
  3475. static const XMVECTORI32 XorUIco4 = {0x00000000,0x80000000,0x80000000,0x00000000};
  3476. // Clamp to bounds
  3477. XMVECTOR vResult = _mm_shuffle_ps(V,V,_MM_SHUFFLE(2,1,3,0));
  3478. vResult = _mm_max_ps(vResult,g_XMZero);
  3479. vResult = _mm_min_ps(vResult,MaxUIco4);
  3480. // Scale by multiplication
  3481. vResult = _mm_mul_ps(vResult,ScaleUIco4);
  3482. vResult = _mm_add_ps(vResult,AddUIco4);
  3483. // Convert to int
  3484. __m128i vResulti = _mm_cvttps_epi32(vResult);
  3485. vResulti = _mm_xor_si128(vResulti,XorUIco4);
  3486. // Mask off any fraction
  3487. vResulti = _mm_and_si128(vResulti,MaskUIco4);
  3488. // Shift y and z to straddle the 32-bit boundary
  3489. __m128i vResulti2 = _mm_srli_si128(vResulti,(64+12)/8);
  3490. // Shift it into place
  3491. vResulti2 = _mm_slli_si128(vResulti2,20/8);
  3492. // i = x|y<<20|z<<40|w<<60
  3493. vResulti = _mm_or_si128(vResulti,vResulti2);
  3494. _mm_store_sd(reinterpret_cast<double *>(&pDestination->v),reinterpret_cast<const __m128d *>(&vResulti)[0]);
  3495. #else // _XM_VMX128_INTRINSICS_
  3496. #endif // _XM_VMX128_INTRINSICS_
  3497. #undef XM_Scale
  3498. #undef XM_URange
  3499. #undef XM_URangeDiv2
  3500. }
  3501. //------------------------------------------------------------------------------
  3502. XMFINLINE VOID XMStoreIcoN4
  3503. (
  3504. XMICON4* pDestination,
  3505. FXMVECTOR V
  3506. )
  3507. {
  3508. #define XM_Scale (-1.0f / XM_PACK_FACTOR)
  3509. #define XM_URange ((FLOAT)(1 << 4))
  3510. #define XM_Offset (3.0f)
  3511. #define XM_UMaxXYZ ((FLOAT)((1 << (20 - 1)) - 1))
  3512. #define XM_UMaxW ((FLOAT)((1 << (4 - 1)) - 1))
  3513. #if defined(_XM_NO_INTRINSICS_)
  3514. XMVECTOR N;
  3515. static CONST XMVECTORF32 Scale = {524287.0f, 524287.0f, 524287.0f, 7.0f};
  3516. XMASSERT(pDestination);
  3517. N = XMVectorClamp(V, g_XMNegativeOne.v, g_XMOne.v);
  3518. N = XMVectorMultiplyAdd(N, Scale.v, g_XMNegativeZero.v);
  3519. N = XMVectorRound(N);
  3520. pDestination->v = ((UINT64)N.v[3] << 60) |
  3521. (((UINT64)N.v[2] & 0xFFFFF) << 40) |
  3522. (((UINT64)N.v[1] & 0xFFFFF) << 20) |
  3523. (((UINT64)N.v[0] & 0xFFFFF));
  3524. #elif defined(_XM_SSE_INTRINSICS_)
  3525. XMASSERT(pDestination);
  3526. // Note: Masks are x,w,y and z
  3527. static const XMVECTORF32 ScaleIcoN4 = {524287.0f,7.0f*4096.0f*65536.0f,524287.0f*4096.0f,524287.0f};
  3528. static const XMVECTORI32 MaskIcoN4 = {0xFFFFF,0xF<<(60-32),0xFFFFF000,0xFFFFF};
  3529. // Clamp to bounds
  3530. XMVECTOR vResult = _mm_shuffle_ps(V,V,_MM_SHUFFLE(2,1,3,0));
  3531. vResult = _mm_max_ps(vResult,g_XMNegativeOne);
  3532. vResult = _mm_min_ps(vResult,g_XMOne);
  3533. // Scale by multiplication
  3534. vResult = _mm_mul_ps(vResult,ScaleIcoN4);
  3535. // Convert to int
  3536. __m128i vResulti = _mm_cvttps_epi32(vResult);
  3537. // Mask off any fraction
  3538. vResulti = _mm_and_si128(vResulti,MaskIcoN4);
  3539. // Shift y and z to straddle the 32-bit boundary
  3540. __m128i vResulti2 = _mm_srli_si128(vResulti,(64+12)/8);
  3541. // Shift it into place
  3542. vResulti2 = _mm_slli_si128(vResulti2,20/8);
  3543. // i = x|y<<20|z<<40|w<<60
  3544. vResulti = _mm_or_si128(vResulti,vResulti2);
  3545. _mm_store_sd(reinterpret_cast<double *>(&pDestination->v),reinterpret_cast<const __m128d *>(&vResulti)[0]);
  3546. #else // _XM_VMX128_INTRINSICS_
  3547. #endif // _XM_VMX128_INTRINSICS_
  3548. #undef XM_Scale
  3549. #undef XM_URange
  3550. #undef XM_Offset
  3551. #undef XM_UMaxXYZ
  3552. #undef XM_UMaxW
  3553. }
  3554. //------------------------------------------------------------------------------
  3555. XMFINLINE VOID XMStoreIco4
  3556. (
  3557. XMICO4* pDestination,
  3558. FXMVECTOR V
  3559. )
  3560. {
  3561. #define XM_Scale (-1.0f / XM_PACK_FACTOR)
  3562. #define XM_URange ((FLOAT)(1 << 4))
  3563. #define XM_Offset (3.0f)
  3564. #if defined(_XM_NO_INTRINSICS_)
  3565. XMVECTOR N;
  3566. static CONST XMVECTOR Min = {-524287.0f, -524287.0f, -524287.0f, -7.0f};
  3567. static CONST XMVECTOR Max = {524287.0f, 524287.0f, 524287.0f, 7.0f};
  3568. XMASSERT(pDestination);
  3569. N = XMVectorClamp(V, Min, Max);
  3570. N = XMVectorRound(N);
  3571. pDestination->v = ((INT64)N.v[3] << 60) |
  3572. (((INT64)N.v[2] & 0xFFFFF) << 40) |
  3573. (((INT64)N.v[1] & 0xFFFFF) << 20) |
  3574. (((INT64)N.v[0] & 0xFFFFF));
  3575. #elif defined(_XM_SSE_INTRINSICS_)
  3576. XMASSERT(pDestination);
  3577. // Note: Masks are x,w,y and z
  3578. static const XMVECTORF32 MinIco4 = {-524287.0f,-7.0f,-524287.0f,-524287.0f};
  3579. static const XMVECTORF32 MaxIco4 = { 524287.0f, 7.0f, 524287.0f, 524287.0f};
  3580. static const XMVECTORF32 ScaleIco4 = {1.0f,4096.0f*65536.0f,4096.0f,1.0f};
  3581. static const XMVECTORI32 MaskIco4 = {0xFFFFF,0xF<<(60-32),0xFFFFF000,0xFFFFF};
  3582. // Clamp to bounds
  3583. XMVECTOR vResult = _mm_shuffle_ps(V,V,_MM_SHUFFLE(2,1,3,0));
  3584. vResult = _mm_max_ps(vResult,MinIco4);
  3585. vResult = _mm_min_ps(vResult,MaxIco4);
  3586. // Scale by multiplication
  3587. vResult = _mm_mul_ps(vResult,ScaleIco4);
  3588. // Convert to int
  3589. __m128i vResulti = _mm_cvttps_epi32(vResult);
  3590. // Mask off any fraction
  3591. vResulti = _mm_and_si128(vResulti,MaskIco4);
  3592. // Shift y and z to straddle the 32-bit boundary
  3593. __m128i vResulti2 = _mm_srli_si128(vResulti,(64+12)/8);
  3594. // Shift it into place
  3595. vResulti2 = _mm_slli_si128(vResulti2,20/8);
  3596. // i = x|y<<20|z<<40|w<<60
  3597. vResulti = _mm_or_si128(vResulti,vResulti2);
  3598. _mm_store_sd(reinterpret_cast<double *>(&pDestination->v),reinterpret_cast<const __m128d *>(&vResulti)[0]);
  3599. #else // _XM_VMX128_INTRINSICS_
  3600. #endif // _XM_VMX128_INTRINSICS_
  3601. #undef XM_Scale
  3602. #undef XM_URange
  3603. #undef XM_Offset
  3604. }
  3605. //------------------------------------------------------------------------------
  3606. XMFINLINE VOID XMStoreXDecN4
  3607. (
  3608. XMXDECN4* pDestination,
  3609. FXMVECTOR V
  3610. )
  3611. {
  3612. #if defined(_XM_NO_INTRINSICS_)
  3613. XMVECTOR N;
  3614. static CONST XMVECTORF32 Min = {-1.0f, -1.0f, -1.0f, 0.0f};
  3615. static CONST XMVECTORF32 Scale = {511.0f, 511.0f, 511.0f, 3.0f};
  3616. XMASSERT(pDestination);
  3617. N = XMVectorClamp(V, Min.v, g_XMOne.v);
  3618. N = XMVectorMultiply(N, Scale.v);
  3619. N = XMVectorRound(N);
  3620. pDestination->v = ((UINT)N.v[3] << 30) |
  3621. (((INT)N.v[2] & 0x3FF) << 20) |
  3622. (((INT)N.v[1] & 0x3FF) << 10) |
  3623. (((INT)N.v[0] & 0x3FF));
  3624. #elif defined(_XM_SSE_INTRINSICS_)
  3625. static const XMVECTORF32 Min = {-1.0f, -1.0f, -1.0f, 0.0f};
  3626. static const XMVECTORF32 Scale = {511.0f, 511.0f*1024.0f, 511.0f*1048576.0f,3.0f*536870912.0f};
  3627. static const XMVECTORI32 ScaleMask = {0x3FF,0x3FF<<10,0x3FF<<20,0x3<<29};
  3628. XMASSERT(pDestination);
  3629. XMVECTOR vResult = _mm_max_ps(V,Min);
  3630. vResult = _mm_min_ps(vResult,g_XMOne);
  3631. // Scale by multiplication
  3632. vResult = _mm_mul_ps(vResult,Scale);
  3633. // Convert to int (W is unsigned)
  3634. __m128i vResulti = _mm_cvtps_epi32(vResult);
  3635. // Mask off any fraction
  3636. vResulti = _mm_and_si128(vResulti,ScaleMask);
  3637. // To fix W, add itself to shift it up to <<30 instead of <<29
  3638. __m128i vResultw = _mm_and_si128(vResulti,g_XMMaskW);
  3639. vResulti = _mm_add_epi32(vResulti,vResultw);
  3640. // Do a horizontal or of all 4 entries
  3641. vResult = _mm_shuffle_ps(reinterpret_cast<const __m128 *>(&vResulti)[0],reinterpret_cast<const __m128 *>(&vResulti)[0],_MM_SHUFFLE(0,3,2,1));
  3642. vResulti = _mm_or_si128(vResulti,reinterpret_cast<const __m128i *>(&vResult)[0]);
  3643. vResult = _mm_shuffle_ps(vResult,vResult,_MM_SHUFFLE(0,3,2,1));
  3644. vResulti = _mm_or_si128(vResulti,reinterpret_cast<const __m128i *>(&vResult)[0]);
  3645. vResult = _mm_shuffle_ps(vResult,vResult,_MM_SHUFFLE(0,3,2,1));
  3646. vResulti = _mm_or_si128(vResulti,reinterpret_cast<const __m128i *>(&vResult)[0]);
  3647. _mm_store_ss(reinterpret_cast<float *>(&pDestination->v),reinterpret_cast<const __m128 *>(&vResulti)[0]);
  3648. #else // _XM_VMX128_INTRINSICS_
  3649. #endif // _XM_VMX128_INTRINSICS_
  3650. }
  3651. //------------------------------------------------------------------------------
  3652. XMFINLINE VOID XMStoreXDec4
  3653. (
  3654. XMXDEC4* pDestination,
  3655. FXMVECTOR V
  3656. )
  3657. {
  3658. #if defined(_XM_NO_INTRINSICS_)
  3659. XMVECTOR N;
  3660. static CONST XMVECTOR Min = {-511.0f, -511.0f, -511.0f, 0.0f};
  3661. static CONST XMVECTOR Max = {511.0f, 511.0f, 511.0f, 3.0f};
  3662. XMASSERT(pDestination);
  3663. N = XMVectorClamp(V, Min, Max);
  3664. pDestination->v = ((UINT)N.v[3] << 30) |
  3665. (((INT)N.v[2] & 0x3FF) << 20) |
  3666. (((INT)N.v[1] & 0x3FF) << 10) |
  3667. (((INT)N.v[0] & 0x3FF));
  3668. #elif defined(_XM_SSE_INTRINSICS_)
  3669. XMASSERT(pDestination);
  3670. static const XMVECTORF32 MinXDec4 = {-511.0f,-511.0f,-511.0f, 0.0f};
  3671. static const XMVECTORF32 MaxXDec4 = { 511.0f, 511.0f, 511.0f, 3.0f};
  3672. static const XMVECTORF32 ScaleXDec4 = {1.0f,1024.0f/2.0f,1024.0f*1024.0f,1024.0f*1024.0f*1024.0f/2.0f};
  3673. static const XMVECTORI32 MaskXDec4= {0x3FF,0x3FF<<(10-1),0x3FF<<20,0x3<<(30-1)};
  3674. // Clamp to bounds
  3675. XMVECTOR vResult = _mm_max_ps(V,MinXDec4);
  3676. vResult = _mm_min_ps(vResult,MaxXDec4);
  3677. // Scale by multiplication
  3678. vResult = _mm_mul_ps(vResult,ScaleXDec4);
  3679. // Convert to int
  3680. __m128i vResulti = _mm_cvttps_epi32(vResult);
  3681. // Mask off any fraction
  3682. vResulti = _mm_and_si128(vResulti,MaskXDec4);
  3683. // Do a horizontal or of 4 entries
  3684. __m128i vResulti2 = _mm_shuffle_epi32(vResulti,_MM_SHUFFLE(3,2,3,2));
  3685. // x = x|z, y = y|w
  3686. vResulti = _mm_or_si128(vResulti,vResulti2);
  3687. // Move Z to the x position
  3688. vResulti2 = _mm_shuffle_epi32(vResulti,_MM_SHUFFLE(1,1,1,1));
  3689. // Perform a single bit left shift on y|w
  3690. vResulti2 = _mm_add_epi32(vResulti2,vResulti2);
  3691. // i = x|y|z|w
  3692. vResulti = _mm_or_si128(vResulti,vResulti2);
  3693. _mm_store_ss(reinterpret_cast<float *>(&pDestination->v),reinterpret_cast<const __m128 *>(&vResulti)[0]);
  3694. #else // _XM_VMX128_INTRINSICS_
  3695. #endif // _XM_VMX128_INTRINSICS_
  3696. }
  3697. //------------------------------------------------------------------------------
  3698. XMFINLINE VOID XMStoreUDecN4
  3699. (
  3700. XMUDECN4* pDestination,
  3701. FXMVECTOR V
  3702. )
  3703. {
  3704. #if defined(_XM_NO_INTRINSICS_)
  3705. XMVECTOR N;
  3706. static CONST XMVECTORF32 Scale = {1023.0f, 1023.0f, 1023.0f, 3.0f};
  3707. XMASSERT(pDestination);
  3708. N = XMVectorClamp(V, XMVectorZero(), g_XMOne.v);
  3709. N = XMVectorMultiply(N, Scale.v);
  3710. pDestination->v = ((UINT)N.v[3] << 30) |
  3711. (((UINT)N.v[2] & 0x3FF) << 20) |
  3712. (((UINT)N.v[1] & 0x3FF) << 10) |
  3713. (((UINT)N.v[0] & 0x3FF));
  3714. #elif defined(_XM_SSE_INTRINSICS_)
  3715. XMASSERT(pDestination);
  3716. static const XMVECTORF32 ScaleUDecN4 = {1023.0f,1023.0f*1024.0f*0.5f,1023.0f*1024.0f*1024.0f,3.0f*1024.0f*1024.0f*1024.0f*0.5f};
  3717. static const XMVECTORI32 MaskUDecN4= {0x3FF,0x3FF<<(10-1),0x3FF<<20,0x3<<(30-1)};
  3718. // Clamp to bounds
  3719. XMVECTOR vResult = _mm_max_ps(V,g_XMZero);
  3720. vResult = _mm_min_ps(vResult,g_XMOne);
  3721. // Scale by multiplication
  3722. vResult = _mm_mul_ps(vResult,ScaleUDecN4);
  3723. // Convert to int
  3724. __m128i vResulti = _mm_cvttps_epi32(vResult);
  3725. // Mask off any fraction
  3726. vResulti = _mm_and_si128(vResulti,MaskUDecN4);
  3727. // Do a horizontal or of 4 entries
  3728. __m128i vResulti2 = _mm_shuffle_epi32(vResulti,_MM_SHUFFLE(3,2,3,2));
  3729. // x = x|z, y = y|w
  3730. vResulti = _mm_or_si128(vResulti,vResulti2);
  3731. // Move Z to the x position
  3732. vResulti2 = _mm_shuffle_epi32(vResulti,_MM_SHUFFLE(1,1,1,1));
  3733. // Perform a left shift by one bit on y|w
  3734. vResulti2 = _mm_add_epi32(vResulti2,vResulti2);
  3735. // i = x|y|z|w
  3736. vResulti = _mm_or_si128(vResulti,vResulti2);
  3737. _mm_store_ss(reinterpret_cast<float *>(&pDestination->v),reinterpret_cast<const __m128 *>(&vResulti)[0]);
  3738. #else // _XM_VMX128_INTRINSICS_
  3739. #endif // _XM_VMX128_INTRINSICS_
  3740. }
  3741. //------------------------------------------------------------------------------
  3742. XMFINLINE VOID XMStoreUDec4
  3743. (
  3744. XMUDEC4* pDestination,
  3745. FXMVECTOR V
  3746. )
  3747. {
  3748. #if defined(_XM_NO_INTRINSICS_)
  3749. XMVECTOR N;
  3750. static CONST XMVECTOR Max = {1023.0f, 1023.0f, 1023.0f, 3.0f};
  3751. XMASSERT(pDestination);
  3752. N = XMVectorClamp(V, XMVectorZero(), Max);
  3753. pDestination->v = ((UINT)N.v[3] << 30) |
  3754. (((UINT)N.v[2] & 0x3FF) << 20) |
  3755. (((UINT)N.v[1] & 0x3FF) << 10) |
  3756. (((UINT)N.v[0] & 0x3FF));
  3757. #elif defined(_XM_SSE_INTRINSICS_)
  3758. XMASSERT(pDestination);
  3759. static const XMVECTORF32 MaxUDec4 = { 1023.0f, 1023.0f, 1023.0f, 3.0f};
  3760. static const XMVECTORF32 ScaleUDec4 = {1.0f,1024.0f/2.0f,1024.0f*1024.0f,1024.0f*1024.0f*1024.0f/2.0f};
  3761. static const XMVECTORI32 MaskUDec4= {0x3FF,0x3FF<<(10-1),0x3FF<<20,0x3<<(30-1)};
  3762. // Clamp to bounds
  3763. XMVECTOR vResult = _mm_max_ps(V,g_XMZero);
  3764. vResult = _mm_min_ps(vResult,MaxUDec4);
  3765. // Scale by multiplication
  3766. vResult = _mm_mul_ps(vResult,ScaleUDec4);
  3767. // Convert to int
  3768. __m128i vResulti = _mm_cvttps_epi32(vResult);
  3769. // Mask off any fraction
  3770. vResulti = _mm_and_si128(vResulti,MaskUDec4);
  3771. // Do a horizontal or of 4 entries
  3772. __m128i vResulti2 = _mm_shuffle_epi32(vResulti,_MM_SHUFFLE(3,2,3,2));
  3773. // x = x|z, y = y|w
  3774. vResulti = _mm_or_si128(vResulti,vResulti2);
  3775. // Move Z to the x position
  3776. vResulti2 = _mm_shuffle_epi32(vResulti,_MM_SHUFFLE(1,1,1,1));
  3777. // Perform a left shift by one bit on y|w
  3778. vResulti2 = _mm_add_epi32(vResulti2,vResulti2);
  3779. // i = x|y|z|w
  3780. vResulti = _mm_or_si128(vResulti,vResulti2);
  3781. _mm_store_ss(reinterpret_cast<float *>(&pDestination->v),reinterpret_cast<const __m128 *>(&vResulti)[0]);
  3782. #else // _XM_VMX128_INTRINSICS_
  3783. #endif // _XM_VMX128_INTRINSICS_
  3784. }
  3785. //------------------------------------------------------------------------------
  3786. XMFINLINE VOID XMStoreDecN4
  3787. (
  3788. XMDECN4* pDestination,
  3789. FXMVECTOR V
  3790. )
  3791. {
  3792. #if defined(_XM_NO_INTRINSICS_)
  3793. XMVECTOR N;
  3794. static CONST XMVECTORF32 Scale = {511.0f, 511.0f, 511.0f, 1.0f};
  3795. XMASSERT(pDestination);
  3796. N = XMVectorClamp(V, g_XMNegativeOne.v, g_XMOne.v);
  3797. N = XMVectorMultiply(N, Scale.v);
  3798. pDestination->v = ((INT)N.v[3] << 30) |
  3799. (((INT)N.v[2] & 0x3FF) << 20) |
  3800. (((INT)N.v[1] & 0x3FF) << 10) |
  3801. (((INT)N.v[0] & 0x3FF));
  3802. #elif defined(_XM_SSE_INTRINSICS_)
  3803. XMASSERT(pDestination);
  3804. static const XMVECTORF32 ScaleDecN4 = {511.0f,511.0f*1024.0f,511.0f*1024.0f*1024.0f,1.0f*1024.0f*1024.0f*1024.0f};
  3805. static const XMVECTORI32 MaskDecN4= {0x3FF,0x3FF<<10,0x3FF<<20,0x3<<30};
  3806. // Clamp to bounds
  3807. XMVECTOR vResult = _mm_max_ps(V,g_XMNegativeOne);
  3808. vResult = _mm_min_ps(vResult,g_XMOne);
  3809. // Scale by multiplication
  3810. vResult = _mm_mul_ps(vResult,ScaleDecN4);
  3811. // Convert to int
  3812. __m128i vResulti = _mm_cvttps_epi32(vResult);
  3813. // Mask off any fraction
  3814. vResulti = _mm_and_si128(vResulti,MaskDecN4);
  3815. // Do a horizontal or of 4 entries
  3816. __m128i vResulti2 = _mm_shuffle_epi32(vResulti,_MM_SHUFFLE(3,2,3,2));
  3817. // x = x|z, y = y|w
  3818. vResulti = _mm_or_si128(vResulti,vResulti2);
  3819. // Move Z to the x position
  3820. vResulti2 = _mm_shuffle_epi32(vResulti,_MM_SHUFFLE(1,1,1,1));
  3821. // i = x|y|z|w
  3822. vResulti = _mm_or_si128(vResulti,vResulti2);
  3823. _mm_store_ss(reinterpret_cast<float *>(&pDestination->v),reinterpret_cast<const __m128 *>(&vResulti)[0]);
  3824. #else // _XM_VMX128_INTRINSICS_
  3825. #endif // _XM_VMX128_INTRINSICS_
  3826. }
  3827. //------------------------------------------------------------------------------
  3828. XMFINLINE VOID XMStoreDec4
  3829. (
  3830. XMDEC4* pDestination,
  3831. FXMVECTOR V
  3832. )
  3833. {
  3834. #if defined(_XM_NO_INTRINSICS_)
  3835. XMVECTOR N;
  3836. static CONST XMVECTOR Min = {-511.0f, -511.0f, -511.0f, -1.0f};
  3837. static CONST XMVECTOR Max = {511.0f, 511.0f, 511.0f, 1.0f};
  3838. XMASSERT(pDestination);
  3839. N = XMVectorClamp(V, Min, Max);
  3840. pDestination->v = ((INT)N.v[3] << 30) |
  3841. (((INT)N.v[2] & 0x3FF) << 20) |
  3842. (((INT)N.v[1] & 0x3FF) << 10) |
  3843. (((INT)N.v[0] & 0x3FF));
  3844. #elif defined(_XM_SSE_INTRINSICS_)
  3845. XMASSERT(pDestination);
  3846. static const XMVECTORF32 MinDec4 = {-511.0f,-511.0f,-511.0f,-1.0f};
  3847. static const XMVECTORF32 MaxDec4 = { 511.0f, 511.0f, 511.0f, 1.0f};
  3848. static const XMVECTORF32 ScaleDec4 = {1.0f,1024.0f,1024.0f*1024.0f,1024.0f*1024.0f*1024.0f};
  3849. static const XMVECTORI32 MaskDec4= {0x3FF,0x3FF<<10,0x3FF<<20,0x3<<30};
  3850. // Clamp to bounds
  3851. XMVECTOR vResult = _mm_max_ps(V,MinDec4);
  3852. vResult = _mm_min_ps(vResult,MaxDec4);
  3853. // Scale by multiplication
  3854. vResult = _mm_mul_ps(vResult,ScaleDec4);
  3855. // Convert to int
  3856. __m128i vResulti = _mm_cvttps_epi32(vResult);
  3857. // Mask off any fraction
  3858. vResulti = _mm_and_si128(vResulti,MaskDec4);
  3859. // Do a horizontal or of 4 entries
  3860. __m128i vResulti2 = _mm_shuffle_epi32(vResulti,_MM_SHUFFLE(3,2,3,2));
  3861. // x = x|z, y = y|w
  3862. vResulti = _mm_or_si128(vResulti,vResulti2);
  3863. // Move Z to the x position
  3864. vResulti2 = _mm_shuffle_epi32(vResulti,_MM_SHUFFLE(1,1,1,1));
  3865. // i = x|y|z|w
  3866. vResulti = _mm_or_si128(vResulti,vResulti2);
  3867. _mm_store_ss(reinterpret_cast<float *>(&pDestination->v),reinterpret_cast<const __m128 *>(&vResulti)[0]);
  3868. #else // _XM_VMX128_INTRINSICS_
  3869. #endif // _XM_VMX128_INTRINSICS_
  3870. }
  3871. //------------------------------------------------------------------------------
  3872. XMFINLINE VOID XMStoreUByteN4
  3873. (
  3874. XMUBYTEN4* pDestination,
  3875. FXMVECTOR V
  3876. )
  3877. {
  3878. #if defined(_XM_NO_INTRINSICS_)
  3879. XMVECTOR N;
  3880. static CONST XMVECTORF32 Scale = {255.0f, 255.0f, 255.0f, 255.0f};
  3881. XMASSERT(pDestination);
  3882. N = XMVectorSaturate(V);
  3883. N = XMVectorMultiply(N, Scale.v);
  3884. N = XMVectorRound(N);
  3885. pDestination->x = (BYTE)N.v[0];
  3886. pDestination->y = (BYTE)N.v[1];
  3887. pDestination->z = (BYTE)N.v[2];
  3888. pDestination->w = (BYTE)N.v[3];
  3889. #elif defined(_XM_SSE_INTRINSICS_)
  3890. XMASSERT(pDestination);
  3891. static const XMVECTORF32 ScaleUByteN4 = {255.0f,255.0f*256.0f*0.5f,255.0f*256.0f*256.0f,255.0f*256.0f*256.0f*256.0f*0.5f};
  3892. static const XMVECTORI32 MaskUByteN4 = {0xFF,0xFF<<(8-1),0xFF<<16,0xFF<<(24-1)};
  3893. // Clamp to bounds
  3894. XMVECTOR vResult = _mm_max_ps(V,g_XMZero);
  3895. vResult = _mm_min_ps(vResult,g_XMOne);
  3896. // Scale by multiplication
  3897. vResult = _mm_mul_ps(vResult,ScaleUByteN4);
  3898. // Convert to int
  3899. __m128i vResulti = _mm_cvttps_epi32(vResult);
  3900. // Mask off any fraction
  3901. vResulti = _mm_and_si128(vResulti,MaskUByteN4);
  3902. // Do a horizontal or of 4 entries
  3903. __m128i vResulti2 = _mm_shuffle_epi32(vResulti,_MM_SHUFFLE(3,2,3,2));
  3904. // x = x|z, y = y|w
  3905. vResulti = _mm_or_si128(vResulti,vResulti2);
  3906. // Move Z to the x position
  3907. vResulti2 = _mm_shuffle_epi32(vResulti,_MM_SHUFFLE(1,1,1,1));
  3908. // Perform a single bit left shift to fix y|w
  3909. vResulti2 = _mm_add_epi32(vResulti2,vResulti2);
  3910. // i = x|y|z|w
  3911. vResulti = _mm_or_si128(vResulti,vResulti2);
  3912. _mm_store_ss(reinterpret_cast<float *>(&pDestination->v),reinterpret_cast<const __m128 *>(&vResulti)[0]);
  3913. #else // _XM_VMX128_INTRINSICS_
  3914. #endif // _XM_VMX128_INTRINSICS_
  3915. }
  3916. //------------------------------------------------------------------------------
  3917. XMFINLINE VOID XMStoreUByte4
  3918. (
  3919. XMUBYTE4* pDestination,
  3920. FXMVECTOR V
  3921. )
  3922. {
  3923. #if defined(_XM_NO_INTRINSICS_)
  3924. XMVECTOR N;
  3925. static CONST XMVECTOR Max = {255.0f, 255.0f, 255.0f, 255.0f};
  3926. XMASSERT(pDestination);
  3927. N = XMVectorClamp(V, XMVectorZero(), Max);
  3928. N = XMVectorRound(N);
  3929. pDestination->x = (BYTE)N.v[0];
  3930. pDestination->y = (BYTE)N.v[1];
  3931. pDestination->z = (BYTE)N.v[2];
  3932. pDestination->w = (BYTE)N.v[3];
  3933. #elif defined(_XM_SSE_INTRINSICS_)
  3934. XMASSERT(pDestination);
  3935. static const XMVECTORF32 MaxUByte4 = { 255.0f, 255.0f, 255.0f, 255.0f};
  3936. static const XMVECTORF32 ScaleUByte4 = {1.0f,256.0f*0.5f,256.0f*256.0f,256.0f*256.0f*256.0f*0.5f};
  3937. static const XMVECTORI32 MaskUByte4 = {0xFF,0xFF<<(8-1),0xFF<<16,0xFF<<(24-1)};
  3938. // Clamp to bounds
  3939. XMVECTOR vResult = _mm_max_ps(V,g_XMZero);
  3940. vResult = _mm_min_ps(vResult,MaxUByte4);
  3941. // Scale by multiplication
  3942. vResult = _mm_mul_ps(vResult,ScaleUByte4);
  3943. // Convert to int
  3944. __m128i vResulti = _mm_cvttps_epi32(vResult);
  3945. // Mask off any fraction
  3946. vResulti = _mm_and_si128(vResulti,MaskUByte4);
  3947. // Do a horizontal or of 4 entries
  3948. __m128i vResulti2 = _mm_shuffle_epi32(vResulti,_MM_SHUFFLE(3,2,3,2));
  3949. // x = x|z, y = y|w
  3950. vResulti = _mm_or_si128(vResulti,vResulti2);
  3951. // Move Z to the x position
  3952. vResulti2 = _mm_shuffle_epi32(vResulti,_MM_SHUFFLE(1,1,1,1));
  3953. // Perform a single bit left shift to fix y|w
  3954. vResulti2 = _mm_add_epi32(vResulti2,vResulti2);
  3955. // i = x|y|z|w
  3956. vResulti = _mm_or_si128(vResulti,vResulti2);
  3957. _mm_store_ss(reinterpret_cast<float *>(&pDestination->v),reinterpret_cast<const __m128 *>(&vResulti)[0]);
  3958. #else // _XM_VMX128_INTRINSICS_
  3959. #endif // _XM_VMX128_INTRINSICS_
  3960. }
  3961. //------------------------------------------------------------------------------
  3962. XMFINLINE VOID XMStoreByteN4
  3963. (
  3964. XMBYTEN4* pDestination,
  3965. FXMVECTOR V
  3966. )
  3967. {
  3968. #if defined(_XM_NO_INTRINSICS_)
  3969. XMVECTOR N;
  3970. static CONST XMVECTORF32 Scale = {127.0f, 127.0f, 127.0f, 127.0f};
  3971. XMASSERT(pDestination);
  3972. N = XMVectorMultiply(V, Scale.v);
  3973. N = XMVectorRound(N);
  3974. pDestination->x = (CHAR)N.v[0];
  3975. pDestination->y = (CHAR)N.v[1];
  3976. pDestination->z = (CHAR)N.v[2];
  3977. pDestination->w = (CHAR)N.v[3];
  3978. #elif defined(_XM_SSE_INTRINSICS_)
  3979. XMASSERT(pDestination);
  3980. static const XMVECTORF32 ScaleByteN4 = {127.0f,127.0f*256.0f,127.0f*256.0f*256.0f,127.0f*256.0f*256.0f*256.0f};
  3981. static const XMVECTORI32 MaskByteN4 = {0xFF,0xFF<<8,0xFF<<16,0xFF<<24};
  3982. // Clamp to bounds
  3983. XMVECTOR vResult = _mm_max_ps(V,g_XMNegativeOne);
  3984. vResult = _mm_min_ps(vResult,g_XMOne);
  3985. // Scale by multiplication
  3986. vResult = _mm_mul_ps(vResult,ScaleByteN4);
  3987. // Convert to int
  3988. __m128i vResulti = _mm_cvttps_epi32(vResult);
  3989. // Mask off any fraction
  3990. vResulti = _mm_and_si128(vResulti,MaskByteN4);
  3991. // Do a horizontal or of 4 entries
  3992. __m128i vResulti2 = _mm_shuffle_epi32(vResulti,_MM_SHUFFLE(3,2,3,2));
  3993. // x = x|z, y = y|w
  3994. vResulti = _mm_or_si128(vResulti,vResulti2);
  3995. // Move Z to the x position
  3996. vResulti2 = _mm_shuffle_epi32(vResulti,_MM_SHUFFLE(1,1,1,1));
  3997. // i = x|y|z|w
  3998. vResulti = _mm_or_si128(vResulti,vResulti2);
  3999. _mm_store_ss(reinterpret_cast<float *>(&pDestination->v),reinterpret_cast<const __m128 *>(&vResulti)[0]);
  4000. #else // _XM_VMX128_INTRINSICS_
  4001. #endif // _XM_VMX128_INTRINSICS_
  4002. }
  4003. //------------------------------------------------------------------------------
  4004. XMFINLINE VOID XMStoreByte4
  4005. (
  4006. XMBYTE4* pDestination,
  4007. FXMVECTOR V
  4008. )
  4009. {
  4010. #if defined(_XM_NO_INTRINSICS_)
  4011. XMVECTOR N;
  4012. static CONST XMVECTOR Min = {-127.0f, -127.0f, -127.0f, -127.0f};
  4013. static CONST XMVECTOR Max = {127.0f, 127.0f, 127.0f, 127.0f};
  4014. XMASSERT(pDestination);
  4015. N = XMVectorClamp(V, Min, Max);
  4016. N = XMVectorRound(N);
  4017. pDestination->x = (CHAR)N.v[0];
  4018. pDestination->y = (CHAR)N.v[1];
  4019. pDestination->z = (CHAR)N.v[2];
  4020. pDestination->w = (CHAR)N.v[3];
  4021. #elif defined(_XM_SSE_INTRINSICS_)
  4022. XMASSERT(pDestination);
  4023. static const XMVECTORF32 MinByte4 = {-127.0f,-127.0f,-127.0f,-127.0f};
  4024. static const XMVECTORF32 MaxByte4 = { 127.0f, 127.0f, 127.0f, 127.0f};
  4025. static const XMVECTORF32 ScaleByte4 = {1.0f,256.0f,256.0f*256.0f,256.0f*256.0f*256.0f};
  4026. static const XMVECTORI32 MaskByte4 = {0xFF,0xFF<<8,0xFF<<16,0xFF<<24};
  4027. // Clamp to bounds
  4028. XMVECTOR vResult = _mm_max_ps(V,MinByte4);
  4029. vResult = _mm_min_ps(vResult,MaxByte4);
  4030. // Scale by multiplication
  4031. vResult = _mm_mul_ps(vResult,ScaleByte4);
  4032. // Convert to int
  4033. __m128i vResulti = _mm_cvttps_epi32(vResult);
  4034. // Mask off any fraction
  4035. vResulti = _mm_and_si128(vResulti,MaskByte4);
  4036. // Do a horizontal or of 4 entries
  4037. __m128i vResulti2 = _mm_shuffle_epi32(vResulti,_MM_SHUFFLE(3,2,3,2));
  4038. // x = x|z, y = y|w
  4039. vResulti = _mm_or_si128(vResulti,vResulti2);
  4040. // Move Z to the x position
  4041. vResulti2 = _mm_shuffle_epi32(vResulti,_MM_SHUFFLE(1,1,1,1));
  4042. // i = x|y|z|w
  4043. vResulti = _mm_or_si128(vResulti,vResulti2);
  4044. _mm_store_ss(reinterpret_cast<float *>(&pDestination->v),reinterpret_cast<const __m128 *>(&vResulti)[0]);
  4045. #else // _XM_VMX128_INTRINSICS_
  4046. #endif // _XM_VMX128_INTRINSICS_
  4047. }
  4048. //------------------------------------------------------------------------------
  4049. XMFINLINE VOID XMStoreColor
  4050. (
  4051. XMCOLOR* pDestination,
  4052. FXMVECTOR V
  4053. )
  4054. {
  4055. #if defined(_XM_NO_INTRINSICS_)
  4056. XMVECTOR N;
  4057. static CONST XMVECTORF32 Scale = {255.0f, 255.0f, 255.0f, 255.0f};
  4058. XMASSERT(pDestination);
  4059. N = XMVectorSaturate(V);
  4060. N = XMVectorMultiply(N, Scale.v);
  4061. N = XMVectorRound(N);
  4062. pDestination->c = ((UINT)N.v[3] << 24) |
  4063. ((UINT)N.v[0] << 16) |
  4064. ((UINT)N.v[1] << 8) |
  4065. ((UINT)N.v[2]);
  4066. #elif defined(_XM_SSE_INTRINSICS_)
  4067. XMASSERT(pDestination);
  4068. static CONST XMVECTORF32 Scale = {255.0f,255.0f,255.0f,255.0f};
  4069. // Set <0 to 0
  4070. XMVECTOR vResult = _mm_max_ps(V,g_XMZero);
  4071. // Set>1 to 1
  4072. vResult = _mm_min_ps(vResult,g_XMOne);
  4073. // Convert to 0-255
  4074. vResult = _mm_mul_ps(vResult,Scale);
  4075. // Shuffle RGBA to ARGB
  4076. vResult = _mm_shuffle_ps(vResult,vResult,_MM_SHUFFLE(2,1,0,3));
  4077. // Convert to int
  4078. __m128i vInt = _mm_cvtps_epi32(vResult);
  4079. // Mash to shorts
  4080. vInt = _mm_packs_epi32(vInt,vInt);
  4081. // Mash to bytes
  4082. vInt = _mm_packs_epi16(vInt,vInt);
  4083. // Store the color
  4084. _mm_store_ss(reinterpret_cast<float *>(&pDestination->c),reinterpret_cast<__m128 *>(&vInt)[0]);
  4085. #else // _XM_VMX128_INTRINSICS_
  4086. #endif // _XM_VMX128_INTRINSICS_
  4087. }
  4088. //------------------------------------------------------------------------------
  4089. XMFINLINE VOID XMStoreFloat3x3
  4090. (
  4091. XMFLOAT3X3* pDestination,
  4092. CXMMATRIX M
  4093. )
  4094. {
  4095. #if defined(_XM_NO_INTRINSICS_) || defined(XM_NO_MISALIGNED_VECTOR_ACCESS) || defined(_XM_SSE_INTRINSICS_)
  4096. XMStoreFloat3x3NC(pDestination, M);
  4097. #else // _XM_VMX128_INTRINSICS_
  4098. #endif // _XM_VMX128_INTRINSICS_
  4099. }
  4100. //------------------------------------------------------------------------------
  4101. XMFINLINE VOID XMStoreFloat3x3NC
  4102. (
  4103. XMFLOAT3X3* pDestination,
  4104. CXMMATRIX M
  4105. )
  4106. {
  4107. #if defined(_XM_NO_INTRINSICS_)
  4108. XMASSERT(pDestination);
  4109. pDestination->m[0][0] = M.r[0].v[0];
  4110. pDestination->m[0][1] = M.r[0].v[1];
  4111. pDestination->m[0][2] = M.r[0].v[2];
  4112. pDestination->m[1][0] = M.r[1].v[0];
  4113. pDestination->m[1][1] = M.r[1].v[1];
  4114. pDestination->m[1][2] = M.r[1].v[2];
  4115. pDestination->m[2][0] = M.r[2].v[0];
  4116. pDestination->m[2][1] = M.r[2].v[1];
  4117. pDestination->m[2][2] = M.r[2].v[2];
  4118. #elif defined(_XM_SSE_INTRINSICS_)
  4119. XMASSERT(pDestination);
  4120. XMVECTOR vTemp1 = M.r[0];
  4121. XMVECTOR vTemp2 = M.r[1];
  4122. XMVECTOR vTemp3 = M.r[2];
  4123. XMVECTOR vWork = _mm_shuffle_ps(vTemp1,vTemp2,_MM_SHUFFLE(0,0,2,2));
  4124. vTemp1 = _mm_shuffle_ps(vTemp1,vWork,_MM_SHUFFLE(2,0,1,0));
  4125. _mm_storeu_ps(&pDestination->m[0][0],vTemp1);
  4126. vTemp2 = _mm_shuffle_ps(vTemp2,vTemp3,_MM_SHUFFLE(1,0,2,1));
  4127. _mm_storeu_ps(&pDestination->m[1][1],vTemp2);
  4128. vTemp3 = _mm_shuffle_ps(vTemp3,vTemp3,_MM_SHUFFLE(2,2,2,2));
  4129. _mm_store_ss(&pDestination->m[2][2],vTemp3);
  4130. #else // _XM_VMX128_INTRINSICS_
  4131. #endif // _XM_VMX128_INTRINSICS_
  4132. }
  4133. //------------------------------------------------------------------------------
  4134. XMFINLINE VOID XMStoreFloat4x3
  4135. (
  4136. XMFLOAT4X3* pDestination,
  4137. CXMMATRIX M
  4138. )
  4139. {
  4140. #if defined(_XM_NO_INTRINSICS_) || defined(XM_NO_MISALIGNED_VECTOR_ACCESS) || defined(_XM_SSE_INTRINSICS_)
  4141. XMStoreFloat4x3NC(pDestination, M);
  4142. #else // _XM_VMX128_INTRINSICS_
  4143. #endif // _XM_VMX128_INTRINSICS_
  4144. }
  4145. //------------------------------------------------------------------------------
  4146. XMFINLINE VOID XMStoreFloat4x3A
  4147. (
  4148. XMFLOAT4X3A* pDestination,
  4149. CXMMATRIX M
  4150. )
  4151. {
  4152. #if defined(_XM_NO_INTRINSICS_)
  4153. XMASSERT(pDestination);
  4154. XMASSERT(((UINT_PTR)pDestination & 0xF) == 0);
  4155. pDestination->m[0][0] = M.r[0].v[0];
  4156. pDestination->m[0][1] = M.r[0].v[1];
  4157. pDestination->m[0][2] = M.r[0].v[2];
  4158. pDestination->m[1][0] = M.r[1].v[0];
  4159. pDestination->m[1][1] = M.r[1].v[1];
  4160. pDestination->m[1][2] = M.r[1].v[2];
  4161. pDestination->m[2][0] = M.r[2].v[0];
  4162. pDestination->m[2][1] = M.r[2].v[1];
  4163. pDestination->m[2][2] = M.r[2].v[2];
  4164. pDestination->m[3][0] = M.r[3].v[0];
  4165. pDestination->m[3][1] = M.r[3].v[1];
  4166. pDestination->m[3][2] = M.r[3].v[2];
  4167. #elif defined(_XM_SSE_INTRINSICS_)
  4168. XMASSERT(pDestination);
  4169. XMASSERT(((UINT_PTR)pDestination & 0xF) == 0);
  4170. // x1,y1,z1,w1
  4171. XMVECTOR vTemp1 = M.r[0];
  4172. // x2,y2,z2,w2
  4173. XMVECTOR vTemp2 = M.r[1];
  4174. // x3,y3,z3,w3
  4175. XMVECTOR vTemp3 = M.r[2];
  4176. // x4,y4,z4,w4
  4177. XMVECTOR vTemp4 = M.r[3];
  4178. // z1,z1,x2,y2
  4179. XMVECTOR vTemp = _mm_shuffle_ps(vTemp1,vTemp2,_MM_SHUFFLE(1,0,2,2));
  4180. // y2,z2,x3,y3 (Final)
  4181. vTemp2 = _mm_shuffle_ps(vTemp2,vTemp3,_MM_SHUFFLE(1,0,2,1));
  4182. // x1,y1,z1,x2 (Final)
  4183. vTemp1 = _mm_shuffle_ps(vTemp1,vTemp,_MM_SHUFFLE(2,0,1,0));
  4184. // z3,z3,x4,x4
  4185. vTemp3 = _mm_shuffle_ps(vTemp3,vTemp4,_MM_SHUFFLE(0,0,2,2));
  4186. // z3,x4,y4,z4 (Final)
  4187. vTemp3 = _mm_shuffle_ps(vTemp3,vTemp4,_MM_SHUFFLE(2,1,2,0));
  4188. // Store in 3 operations
  4189. _mm_store_ps(&pDestination->m[0][0],vTemp1);
  4190. _mm_store_ps(&pDestination->m[1][1],vTemp2);
  4191. _mm_store_ps(&pDestination->m[2][2],vTemp3);
  4192. #else // _XM_VMX128_INTRINSICS_
  4193. #endif // _XM_VMX128_INTRINSICS_
  4194. }
  4195. //------------------------------------------------------------------------------
  4196. XMFINLINE VOID XMStoreFloat4x3NC
  4197. (
  4198. XMFLOAT4X3* pDestination,
  4199. CXMMATRIX M
  4200. )
  4201. {
  4202. #if defined(_XM_NO_INTRINSICS_)
  4203. XMASSERT(pDestination);
  4204. pDestination->m[0][0] = M.r[0].v[0];
  4205. pDestination->m[0][1] = M.r[0].v[1];
  4206. pDestination->m[0][2] = M.r[0].v[2];
  4207. pDestination->m[1][0] = M.r[1].v[0];
  4208. pDestination->m[1][1] = M.r[1].v[1];
  4209. pDestination->m[1][2] = M.r[1].v[2];
  4210. pDestination->m[2][0] = M.r[2].v[0];
  4211. pDestination->m[2][1] = M.r[2].v[1];
  4212. pDestination->m[2][2] = M.r[2].v[2];
  4213. pDestination->m[3][0] = M.r[3].v[0];
  4214. pDestination->m[3][1] = M.r[3].v[1];
  4215. pDestination->m[3][2] = M.r[3].v[2];
  4216. #elif defined(_XM_SSE_INTRINSICS_)
  4217. XMASSERT(pDestination);
  4218. XMVECTOR vTemp1 = M.r[0];
  4219. XMVECTOR vTemp2 = M.r[1];
  4220. XMVECTOR vTemp3 = M.r[2];
  4221. XMVECTOR vTemp4 = M.r[3];
  4222. XMVECTOR vTemp2x = _mm_shuffle_ps(vTemp2,vTemp3,_MM_SHUFFLE(1,0,2,1));
  4223. vTemp2 = _mm_shuffle_ps(vTemp2,vTemp1,_MM_SHUFFLE(2,2,0,0));
  4224. vTemp1 = _mm_shuffle_ps(vTemp1,vTemp2,_MM_SHUFFLE(0,2,1,0));
  4225. vTemp3 = _mm_shuffle_ps(vTemp3,vTemp4,_MM_SHUFFLE(0,0,2,2));
  4226. vTemp3 = _mm_shuffle_ps(vTemp3,vTemp4,_MM_SHUFFLE(2,1,2,0));
  4227. _mm_storeu_ps(&pDestination->m[0][0],vTemp1);
  4228. _mm_storeu_ps(&pDestination->m[1][1],vTemp2x);
  4229. _mm_storeu_ps(&pDestination->m[2][2],vTemp3);
  4230. #else // _XM_VMX128_INTRINSICS_
  4231. #endif // _XM_VMX128_INTRINSICS_
  4232. }
  4233. //------------------------------------------------------------------------------
  4234. XMFINLINE VOID XMStoreFloat4x4
  4235. (
  4236. XMFLOAT4X4* pDestination,
  4237. CXMMATRIX M
  4238. )
  4239. {
  4240. #if defined(_XM_NO_INTRINSICS_) || defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
  4241. XMStoreFloat4x4NC(pDestination, M);
  4242. #elif defined(_XM_SSE_INTRINSICS_)
  4243. XMASSERT(pDestination);
  4244. _mm_storeu_ps( &pDestination->_11, M.r[0] );
  4245. _mm_storeu_ps( &pDestination->_21, M.r[1] );
  4246. _mm_storeu_ps( &pDestination->_31, M.r[2] );
  4247. _mm_storeu_ps( &pDestination->_41, M.r[3] );
  4248. #else // _XM_VMX128_INTRINSICS_
  4249. #endif // _XM_VMX128_INTRINSICS_
  4250. }
  4251. //------------------------------------------------------------------------------
  4252. XMFINLINE VOID XMStoreFloat4x4A
  4253. (
  4254. XMFLOAT4X4A* pDestination,
  4255. CXMMATRIX M
  4256. )
  4257. {
  4258. #if defined(_XM_NO_INTRINSICS_)
  4259. XMASSERT(pDestination);
  4260. XMASSERT(((UINT_PTR)pDestination & 0xF) == 0);
  4261. pDestination->m[0][0] = M.r[0].v[0];
  4262. pDestination->m[0][1] = M.r[0].v[1];
  4263. pDestination->m[0][2] = M.r[0].v[2];
  4264. pDestination->m[0][3] = M.r[0].v[3];
  4265. pDestination->m[1][0] = M.r[1].v[0];
  4266. pDestination->m[1][1] = M.r[1].v[1];
  4267. pDestination->m[1][2] = M.r[1].v[2];
  4268. pDestination->m[1][3] = M.r[1].v[3];
  4269. pDestination->m[2][0] = M.r[2].v[0];
  4270. pDestination->m[2][1] = M.r[2].v[1];
  4271. pDestination->m[2][2] = M.r[2].v[2];
  4272. pDestination->m[2][3] = M.r[2].v[3];
  4273. pDestination->m[3][0] = M.r[3].v[0];
  4274. pDestination->m[3][1] = M.r[3].v[1];
  4275. pDestination->m[3][2] = M.r[3].v[2];
  4276. pDestination->m[3][3] = M.r[3].v[3];
  4277. #elif defined(_XM_SSE_INTRINSICS_)
  4278. XMASSERT(pDestination);
  4279. _mm_store_ps( &pDestination->_11, M.r[0] );
  4280. _mm_store_ps( &pDestination->_21, M.r[1] );
  4281. _mm_store_ps( &pDestination->_31, M.r[2] );
  4282. _mm_store_ps( &pDestination->_41, M.r[3] );
  4283. #else // _XM_VMX128_INTRINSICS_
  4284. #endif // _XM_VMX128_INTRINSICS_
  4285. }
  4286. //------------------------------------------------------------------------------
  4287. XMFINLINE VOID XMStoreFloat4x4NC
  4288. (
  4289. XMFLOAT4X4* pDestination,
  4290. CXMMATRIX M
  4291. )
  4292. {
  4293. #if defined(_XM_NO_INTRINSICS_)
  4294. XMASSERT(pDestination);
  4295. pDestination->m[0][0] = M.r[0].v[0];
  4296. pDestination->m[0][1] = M.r[0].v[1];
  4297. pDestination->m[0][2] = M.r[0].v[2];
  4298. pDestination->m[0][3] = M.r[0].v[3];
  4299. pDestination->m[1][0] = M.r[1].v[0];
  4300. pDestination->m[1][1] = M.r[1].v[1];
  4301. pDestination->m[1][2] = M.r[1].v[2];
  4302. pDestination->m[1][3] = M.r[1].v[3];
  4303. pDestination->m[2][0] = M.r[2].v[0];
  4304. pDestination->m[2][1] = M.r[2].v[1];
  4305. pDestination->m[2][2] = M.r[2].v[2];
  4306. pDestination->m[2][3] = M.r[2].v[3];
  4307. pDestination->m[3][0] = M.r[3].v[0];
  4308. pDestination->m[3][1] = M.r[3].v[1];
  4309. pDestination->m[3][2] = M.r[3].v[2];
  4310. pDestination->m[3][3] = M.r[3].v[3];
  4311. #elif defined(_XM_SSE_INTRINSICS_)
  4312. XMASSERT(pDestination);
  4313. _mm_storeu_ps(&pDestination->m[0][0],M.r[0]);
  4314. _mm_storeu_ps(&pDestination->m[1][0],M.r[1]);
  4315. _mm_storeu_ps(&pDestination->m[2][0],M.r[2]);
  4316. _mm_storeu_ps(&pDestination->m[3][0],M.r[3]);
  4317. #else // _XM_VMX128_INTRINSICS_
  4318. #endif // _XM_VMX128_INTRINSICS_
  4319. }
  4320. #endif // __XNAMATHCONVERT_INL__