-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathxnamathconvert.inl
5785 lines (4828 loc) · 185 KB
/
xnamathconvert.inl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*++
Copyright (c) Microsoft Corporation. All rights reserved.
Module Name:
xnamathconvert.inl
Abstract:
XNA math library for Windows and Xbox 360: Conversion, loading, and storing functions.
--*/
#if defined(_MSC_VER) && (_MSC_VER > 1000)
#pragma once
#endif
#ifndef __XNAMATHCONVERT_INL__
#define __XNAMATHCONVERT_INL__
#define XM_PACK_FACTOR (FLOAT)(1 << 22)
#define XM_UNPACK_FACTOR_UNSIGNED (FLOAT)(1 << 23)
#define XM_UNPACK_FACTOR_SIGNED XM_PACK_FACTOR
#define XM_UNPACK_UNSIGNEDN_OFFSET(BitsX, BitsY, BitsZ, BitsW) \
{-XM_UNPACK_FACTOR_UNSIGNED / (FLOAT)((1 << (BitsX)) - 1), \
-XM_UNPACK_FACTOR_UNSIGNED / (FLOAT)((1 << (BitsY)) - 1), \
-XM_UNPACK_FACTOR_UNSIGNED / (FLOAT)((1 << (BitsZ)) - 1), \
-XM_UNPACK_FACTOR_UNSIGNED / (FLOAT)((1 << (BitsW)) - 1)}
#define XM_UNPACK_UNSIGNEDN_SCALE(BitsX, BitsY, BitsZ, BitsW) \
{XM_UNPACK_FACTOR_UNSIGNED / (FLOAT)((1 << (BitsX)) - 1), \
XM_UNPACK_FACTOR_UNSIGNED / (FLOAT)((1 << (BitsY)) - 1), \
XM_UNPACK_FACTOR_UNSIGNED / (FLOAT)((1 << (BitsZ)) - 1), \
XM_UNPACK_FACTOR_UNSIGNED / (FLOAT)((1 << (BitsW)) - 1)}
#define XM_UNPACK_SIGNEDN_SCALE(BitsX, BitsY, BitsZ, BitsW) \
{-XM_UNPACK_FACTOR_SIGNED / (FLOAT)((1 << ((BitsX) - 1)) - 1), \
-XM_UNPACK_FACTOR_SIGNED / (FLOAT)((1 << ((BitsY) - 1)) - 1), \
-XM_UNPACK_FACTOR_SIGNED / (FLOAT)((1 << ((BitsZ) - 1)) - 1), \
-XM_UNPACK_FACTOR_SIGNED / (FLOAT)((1 << ((BitsW) - 1)) - 1)}
//#define XM_UNPACK_SIGNEDN_OFFSET(BitsX, BitsY, BitsZ, BitsW) \
// {-XM_UNPACK_FACTOR_SIGNED / (FLOAT)((1 << ((BitsX) - 1)) - 1) * 3.0f, \
// -XM_UNPACK_FACTOR_SIGNED / (FLOAT)((1 << ((BitsY) - 1)) - 1) * 3.0f, \
// -XM_UNPACK_FACTOR_SIGNED / (FLOAT)((1 << ((BitsZ) - 1)) - 1) * 3.0f, \
// -XM_UNPACK_FACTOR_SIGNED / (FLOAT)((1 << ((BitsW) - 1)) - 1) * 3.0f}
#define XM_PACK_UNSIGNEDN_SCALE(BitsX, BitsY, BitsZ, BitsW) \
{-(FLOAT)((1 << (BitsX)) - 1) / XM_PACK_FACTOR, \
-(FLOAT)((1 << (BitsY)) - 1) / XM_PACK_FACTOR, \
-(FLOAT)((1 << (BitsZ)) - 1) / XM_PACK_FACTOR, \
-(FLOAT)((1 << (BitsW)) - 1) / XM_PACK_FACTOR}
#define XM_PACK_SIGNEDN_SCALE(BitsX, BitsY, BitsZ, BitsW) \
{-(FLOAT)((1 << ((BitsX) - 1)) - 1) / XM_PACK_FACTOR, \
-(FLOAT)((1 << ((BitsY) - 1)) - 1) / XM_PACK_FACTOR, \
-(FLOAT)((1 << ((BitsZ) - 1)) - 1) / XM_PACK_FACTOR, \
-(FLOAT)((1 << ((BitsW) - 1)) - 1) / XM_PACK_FACTOR}
#define XM_PACK_OFFSET XMVectorSplatConstant(3, 0)
//#define XM_UNPACK_OFFSET XM_PACK_OFFSET
/****************************************************************************
*
* Data conversion
*
****************************************************************************/
//------------------------------------------------------------------------------
XMFINLINE FLOAT XMConvertHalfToFloat
(
HALF Value
)
{
#if defined(_XM_NO_INTRINSICS_) || defined(_XM_SSE_INTRINSICS_)
UINT Mantissa;
UINT Exponent;
UINT Result;
Mantissa = (UINT)(Value & 0x03FF);
if ((Value & 0x7C00) != 0) // The value is normalized
{
Exponent = (UINT)((Value >> 10) & 0x1F);
}
else if (Mantissa != 0) // The value is denormalized
{
// Normalize the value in the resulting float
Exponent = 1;
do
{
Exponent--;
Mantissa <<= 1;
} while ((Mantissa & 0x0400) == 0);
Mantissa &= 0x03FF;
}
else // The value is zero
{
Exponent = (UINT)-112;
}
Result = ((Value & 0x8000) << 16) | // Sign
((Exponent + 112) << 23) | // Exponent
(Mantissa << 13); // Mantissa
return *(FLOAT*)&Result;
#elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
#endif
}
//------------------------------------------------------------------------------
XMINLINE FLOAT* XMConvertHalfToFloatStream
(
FLOAT* pOutputStream,
UINT OutputStride,
CONST HALF* pInputStream,
UINT InputStride,
UINT HalfCount
)
{
#if defined(_XM_NO_INTRINSICS_) || defined(_XM_SSE_INTRINSICS_)
UINT i;
BYTE* pHalf = (BYTE*)pInputStream;
BYTE* pFloat = (BYTE*)pOutputStream;
XMASSERT(pOutputStream);
XMASSERT(pInputStream);
for (i = 0; i < HalfCount; i++)
{
*(FLOAT*)pFloat = XMConvertHalfToFloat(*(HALF*)pHalf);
pHalf += InputStride;
pFloat += OutputStride;
}
return pOutputStream;
#elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
#endif // _XM_VMX128_INTRINSICS_
}
//------------------------------------------------------------------------------
XMFINLINE HALF XMConvertFloatToHalf
(
FLOAT Value
)
{
#if defined(_XM_NO_INTRINSICS_) || defined(_XM_SSE_INTRINSICS_)
UINT Result;
UINT IValue = ((UINT *)(&Value))[0];
UINT Sign = (IValue & 0x80000000U) >> 16U;
IValue = IValue & 0x7FFFFFFFU; // Hack off the sign
if (IValue > 0x47FFEFFFU)
{
// The number is too large to be represented as a half. Saturate to infinity.
Result = 0x7FFFU;
}
else
{
if (IValue < 0x38800000U)
{
// The number is too small to be represented as a normalized half.
// Convert it to a denormalized value.
UINT Shift = 113U - (IValue >> 23U);
IValue = (0x800000U | (IValue & 0x7FFFFFU)) >> Shift;
}
else
{
// Rebias the exponent to represent the value as a normalized half.
IValue += 0xC8000000U;
}
Result = ((IValue + 0x0FFFU + ((IValue >> 13U) & 1U)) >> 13U)&0x7FFFU;
}
return (HALF)(Result|Sign);
#elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
#endif
}
//------------------------------------------------------------------------------
XMINLINE HALF* XMConvertFloatToHalfStream
(
HALF* pOutputStream,
UINT OutputStride,
CONST FLOAT* pInputStream,
UINT InputStride,
UINT FloatCount
)
{
#if defined(_XM_NO_INTRINSICS_) || defined(_XM_SSE_INTRINSICS_)
UINT i;
BYTE* pFloat = (BYTE*)pInputStream;
BYTE* pHalf = (BYTE*)pOutputStream;
XMASSERT(pOutputStream);
XMASSERT(pInputStream);
for (i = 0; i < FloatCount; i++)
{
*(HALF*)pHalf = XMConvertFloatToHalf(*(FLOAT*)pFloat);
pFloat += InputStride;
pHalf += OutputStride;
}
return pOutputStream;
#elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
#endif // _XM_VMX128_INTRINSICS_
}
//------------------------------------------------------------------------------
#if defined(_XM_NO_INTRINSICS_) || defined(_XM_SSE_INTRINSICS_)
// For VMX128, these routines are all defines in the main header
#pragma warning(push)
#pragma warning(disable:4701) // Prevent warnings about 'Result' potentially being used without having been initialized
XMINLINE XMVECTOR XMConvertVectorIntToFloat
(
FXMVECTOR VInt,
UINT DivExponent
)
{
#if defined(_XM_NO_INTRINSICS_)
UINT ElementIndex;
FLOAT fScale;
XMVECTOR Result;
XMASSERT(DivExponent<32);
fScale = 1.0f / (FLOAT)(1U << DivExponent);
ElementIndex = 0;
do {
INT iTemp = (INT)VInt.vector4_u32[ElementIndex];
Result.vector4_f32[ElementIndex] = ((FLOAT)iTemp) * fScale;
} while (++ElementIndex<4);
return Result;
#else // _XM_SSE_INTRINSICS_
XMASSERT(DivExponent<32);
// Convert to floats
XMVECTOR vResult = _mm_cvtepi32_ps(reinterpret_cast<const __m128i *>(&VInt)[0]);
// Convert DivExponent into 1.0f/(1<<DivExponent)
UINT uScale = 0x3F800000U - (DivExponent << 23);
// Splat the scalar value
__m128i vScale = _mm_set1_epi32(uScale);
vResult = _mm_mul_ps(vResult,reinterpret_cast<const __m128 *>(&vScale)[0]);
return vResult;
#endif
}
//------------------------------------------------------------------------------
XMINLINE XMVECTOR XMConvertVectorFloatToInt
(
FXMVECTOR VFloat,
UINT MulExponent
)
{
#if defined(_XM_NO_INTRINSICS_)
UINT ElementIndex;
XMVECTOR Result;
FLOAT fScale;
XMASSERT(MulExponent<32);
// Get the scalar factor.
fScale = (FLOAT)(1U << MulExponent);
ElementIndex = 0;
do {
INT iResult;
FLOAT fTemp = VFloat.vector4_f32[ElementIndex]*fScale;
if (fTemp <= -(65536.0f*32768.0f)) {
iResult = (-0x7FFFFFFF)-1;
} else if (fTemp > (65536.0f*32768.0f)-128.0f) {
iResult = 0x7FFFFFFF;
} else {
iResult = (INT)fTemp;
}
Result.vector4_u32[ElementIndex] = (UINT)iResult;
} while (++ElementIndex<4);
return Result;
#else // _XM_SSE_INTRINSICS_
XMASSERT(MulExponent<32);
static const XMVECTORF32 MaxInt = {65536.0f*32768.0f-128.0f,65536.0f*32768.0f-128.0f,65536.0f*32768.0f-128.0f,65536.0f*32768.0f-128.0f};
XMVECTOR vResult = _mm_set_ps1((FLOAT)(1U << MulExponent));
vResult = _mm_mul_ps(vResult,VFloat);
// In case of positive overflow, detect it
XMVECTOR vOverflow = _mm_cmpgt_ps(vResult,MaxInt);
// Float to int conversion
__m128i vResulti = _mm_cvttps_epi32(vResult);
// If there was positive overflow, set to 0x7FFFFFFF
vResult = _mm_and_ps(vOverflow,g_XMAbsMask);
vOverflow = _mm_andnot_ps(vOverflow,reinterpret_cast<const __m128 *>(&vResulti)[0]);
vOverflow = _mm_or_ps(vOverflow,vResult);
return vOverflow;
#endif
}
//------------------------------------------------------------------------------
XMINLINE XMVECTOR XMConvertVectorUIntToFloat
(
FXMVECTOR VUInt,
UINT DivExponent
)
{
#if defined(_XM_NO_INTRINSICS_)
UINT ElementIndex;
FLOAT fScale;
XMVECTOR Result;
XMASSERT(DivExponent<32);
fScale = 1.0f / (FLOAT)(1U << DivExponent);
ElementIndex = 0;
do {
Result.vector4_f32[ElementIndex] = (FLOAT)VUInt.vector4_u32[ElementIndex] * fScale;
} while (++ElementIndex<4);
return Result;
#else // _XM_SSE_INTRINSICS_
XMASSERT(DivExponent<32);
static const XMVECTORF32 FixUnsigned = {32768.0f*65536.0f,32768.0f*65536.0f,32768.0f*65536.0f,32768.0f*65536.0f};
// For the values that are higher than 0x7FFFFFFF, a fixup is needed
// Determine which ones need the fix.
XMVECTOR vMask = _mm_and_ps(VUInt,g_XMNegativeZero);
// Force all values positive
XMVECTOR vResult = _mm_xor_ps(VUInt,vMask);
// Convert to floats
vResult = _mm_cvtepi32_ps(reinterpret_cast<const __m128i *>(&vResult)[0]);
// Convert 0x80000000 -> 0xFFFFFFFF
__m128i iMask = _mm_srai_epi32(reinterpret_cast<const __m128i *>(&vMask)[0],31);
// For only the ones that are too big, add the fixup
vMask = _mm_and_ps(reinterpret_cast<const __m128 *>(&iMask)[0],FixUnsigned);
vResult = _mm_add_ps(vResult,vMask);
// Convert DivExponent into 1.0f/(1<<DivExponent)
UINT uScale = 0x3F800000U - (DivExponent << 23);
// Splat
iMask = _mm_set1_epi32(uScale);
vResult = _mm_mul_ps(vResult,reinterpret_cast<const __m128 *>(&iMask)[0]);
return vResult;
#endif
}
//------------------------------------------------------------------------------
XMINLINE XMVECTOR XMConvertVectorFloatToUInt
(
FXMVECTOR VFloat,
UINT MulExponent
)
{
#if defined(_XM_NO_INTRINSICS_)
UINT ElementIndex;
XMVECTOR Result;
FLOAT fScale;
XMASSERT(MulExponent<32);
// Get the scalar factor.
fScale = (FLOAT)(1U << MulExponent);
ElementIndex = 0;
do {
UINT uResult;
FLOAT fTemp = VFloat.vector4_f32[ElementIndex]*fScale;
if (fTemp <= 0.0f) {
uResult = 0;
} else if (fTemp >= (65536.0f*65536.0f)) {
uResult = 0xFFFFFFFFU;
} else {
uResult = (UINT)fTemp;
}
Result.vector4_u32[ElementIndex] = uResult;
} while (++ElementIndex<4);
return Result;
#else // _XM_SSE_INTRINSICS_
XMASSERT(MulExponent<32);
static const XMVECTORF32 MaxUInt = {65536.0f*65536.0f-256.0f,65536.0f*65536.0f-256.0f,65536.0f*65536.0f-256.0f,65536.0f*65536.0f-256.0f};
static const XMVECTORF32 UnsignedFix = {32768.0f*65536.0f,32768.0f*65536.0f,32768.0f*65536.0f,32768.0f*65536.0f};
XMVECTOR vResult = _mm_set_ps1(static_cast<float>(1U << MulExponent));
vResult = _mm_mul_ps(vResult,VFloat);
// Clamp to >=0
vResult = _mm_max_ps(vResult,g_XMZero);
// Any numbers that are too big, set to 0xFFFFFFFFU
XMVECTOR vOverflow = _mm_cmpgt_ps(vResult,MaxUInt);
XMVECTOR vValue = UnsignedFix;
// Too large for a signed integer?
XMVECTOR vMask = _mm_cmpge_ps(vResult,vValue);
// Zero for number's lower than 0x80000000, 32768.0f*65536.0f otherwise
vValue = _mm_and_ps(vValue,vMask);
// Perform fixup only on numbers too large (Keeps low bit precision)
vResult = _mm_sub_ps(vResult,vValue);
__m128i vResulti = _mm_cvttps_epi32(vResult);
// Convert from signed to unsigned pnly if greater than 0x80000000
vMask = _mm_and_ps(vMask,g_XMNegativeZero);
vResult = _mm_xor_ps(reinterpret_cast<const __m128 *>(&vResulti)[0],vMask);
// On those that are too large, set to 0xFFFFFFFF
vResult = _mm_or_ps(vResult,vOverflow);
return vResult;
#endif
}
#pragma warning(pop)
#endif // _XM_NO_INTRINSICS_ || _XM_SSE_INTRINSICS_
/****************************************************************************
*
* Vector and matrix load operations
*
****************************************************************************/
//------------------------------------------------------------------------------
XMFINLINE XMVECTOR XMLoadInt(CONST UINT* pSource)
{
#if defined(_XM_NO_INTRINSICS_)
XMVECTOR V;
XMASSERT(pSource);
XMASSERT(((UINT_PTR)pSource & 3) == 0);
V.vector4_u32[0] = *pSource;
return V;
#elif defined(_XM_SSE_INTRINSICS_)
XMASSERT(pSource);
XMASSERT(((UINT_PTR)pSource & 3) == 0);
return _mm_load_ss( (const float*)pSource );
#elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
#endif // _XM_VMX128_INTRINSICS_
}
//------------------------------------------------------------------------------
XMFINLINE XMVECTOR XMLoadFloat(CONST FLOAT* pSource)
{
#if defined(_XM_NO_INTRINSICS_)
XMVECTOR V;
XMASSERT(pSource);
XMASSERT(((UINT_PTR)pSource & 3) == 0);
V.vector4_f32[0] = *pSource;
return V;
#elif defined(_XM_SSE_INTRINSICS_)
XMASSERT(pSource);
XMASSERT(((UINT_PTR)pSource & 3) == 0);
return _mm_load_ss( pSource );
#elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
#endif // _XM_VMX128_INTRINSICS_
}
//------------------------------------------------------------------------------
XMFINLINE XMVECTOR XMLoadInt2
(
CONST UINT* pSource
)
{
#if defined(_XM_NO_INTRINSICS_)
XMVECTOR V;
XMASSERT(pSource);
V.vector4_u32[0] = pSource[0];
V.vector4_u32[1] = pSource[1];
return V;
#elif defined(_XM_SSE_INTRINSICS_)
XMASSERT(pSource);
__m128 x = _mm_load_ss( (const float*)pSource );
__m128 y = _mm_load_ss( (const float*)(pSource+1) );
return _mm_unpacklo_ps( x, y );
#elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
#endif // _XM_VMX128_INTRINSICS_
}
//------------------------------------------------------------------------------
XMFINLINE XMVECTOR XMLoadInt2A
(
CONST UINT* pSource
)
{
#if defined(_XM_NO_INTRINSICS_)
XMVECTOR V;
XMASSERT(pSource);
XMASSERT(((UINT_PTR)pSource & 0xF) == 0);
V.vector4_u32[0] = pSource[0];
V.vector4_u32[1] = pSource[1];
return V;
#elif defined(_XM_SSE_INTRINSICS_)
XMASSERT(pSource);
XMASSERT(((UINT_PTR)pSource & 0xF) == 0);
__m128i V = _mm_loadl_epi64( (const __m128i*)pSource );
return reinterpret_cast<__m128 *>(&V)[0];
#else // _XM_VMX128_INTRINSICS_
#endif // _XM_VMX128_INTRINSICS_
}
//------------------------------------------------------------------------------
XMFINLINE XMVECTOR XMLoadFloat2
(
CONST XMFLOAT2* pSource
)
{
#if defined(_XM_NO_INTRINSICS_)
XMVECTOR V;
XMASSERT(pSource);
((UINT *)(&V.vector4_f32[0]))[0] = ((const UINT *)(&pSource->x))[0];
((UINT *)(&V.vector4_f32[1]))[0] = ((const UINT *)(&pSource->y))[0];
return V;
#elif defined(_XM_SSE_INTRINSICS_)
XMASSERT(pSource);
__m128 x = _mm_load_ss( &pSource->x );
__m128 y = _mm_load_ss( &pSource->y );
return _mm_unpacklo_ps( x, y );
#elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
#endif // _XM_VMX128_INTRINSICS_
}
//------------------------------------------------------------------------------
XMFINLINE XMVECTOR XMLoadFloat2A
(
CONST XMFLOAT2A* pSource
)
{
#if defined(_XM_NO_INTRINSICS_)
XMVECTOR V;
XMASSERT(pSource);
XMASSERT(((UINT_PTR)pSource & 0xF) == 0);
V.vector4_f32[0] = pSource->x;
V.vector4_f32[1] = pSource->y;
return V;
#elif defined(_XM_SSE_INTRINSICS_)
XMASSERT(pSource);
XMASSERT(((UINT_PTR)pSource & 0xF) == 0);
__m128i V = _mm_loadl_epi64( (const __m128i*)pSource );
return reinterpret_cast<__m128 *>(&V)[0];
#else // _XM_VMX128_INTRINSICS_
#endif // _XM_VMX128_INTRINSICS_
}
//------------------------------------------------------------------------------
XMFINLINE XMVECTOR XMLoadHalf2
(
CONST XMHALF2* pSource
)
{
#if defined(_XM_NO_INTRINSICS_)
XMASSERT(pSource);
{
XMVECTOR vResult = {
XMConvertHalfToFloat(pSource->x),
XMConvertHalfToFloat(pSource->y),
0.0f,
0.0f
};
return vResult;
}
#elif defined(_XM_SSE_INTRINSICS_)
XMASSERT(pSource);
XMVECTOR vResult = {
XMConvertHalfToFloat(pSource->x),
XMConvertHalfToFloat(pSource->y),
0.0f,
0.0f
};
return vResult;
#elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
#endif // _XM_VMX128_INTRINSICS_
}
//------------------------------------------------------------------------------
XMFINLINE XMVECTOR XMLoadShortN2
(
CONST XMSHORTN2* pSource
)
{
#if defined(_XM_NO_INTRINSICS_)
XMASSERT(pSource);
XMASSERT(pSource->x != -32768);
XMASSERT(pSource->y != -32768);
{
XMVECTOR vResult = {
(FLOAT)pSource->x * (1.0f/32767.0f),
(FLOAT)pSource->y * (1.0f/32767.0f),
0.0f,
0.0f
};
return vResult;
}
#elif defined(_XM_SSE_INTRINSICS_)
XMASSERT(pSource);
XMASSERT(pSource->x != -32768);
XMASSERT(pSource->y != -32768);
// Splat the two shorts in all four entries (WORD alignment okay,
// DWORD alignment preferred)
__m128 vTemp = _mm_load_ps1(reinterpret_cast<const float *>(&pSource->x));
// Mask x&0xFFFF, y&0xFFFF0000,z&0,w&0
vTemp = _mm_and_ps(vTemp,g_XMMaskX16Y16);
// x needs to be sign extended
vTemp = _mm_xor_ps(vTemp,g_XMFlipX16Y16);
// Convert to floating point numbers
vTemp = _mm_cvtepi32_ps(reinterpret_cast<const __m128i *>(&vTemp)[0]);
// x - 0x8000 to undo the signed order.
vTemp = _mm_add_ps(vTemp,g_XMFixX16Y16);
// Convert 0-32767 to 0.0f-1.0f
return _mm_mul_ps(vTemp,g_XMNormalizeX16Y16);
#elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
#endif // _XM_VMX128_INTRINSICS_
}
//------------------------------------------------------------------------------
XMFINLINE XMVECTOR XMLoadShort2
(
CONST XMSHORT2* pSource
)
{
#if defined(_XM_NO_INTRINSICS_)
XMVECTOR V;
XMASSERT(pSource);
XMASSERT(pSource->x != -32768);
XMASSERT(pSource->y != -32768);
V.vector4_f32[0] = (FLOAT)pSource->x;
V.vector4_f32[1] = (FLOAT)pSource->y;
return V;
#elif defined(_XM_SSE_INTRINSICS_)
XMASSERT(pSource);
XMASSERT(pSource->x != -32768);
XMASSERT(pSource->y != -32768);
// Splat the two shorts in all four entries (WORD alignment okay,
// DWORD alignment preferred)
__m128 vTemp = _mm_load_ps1(reinterpret_cast<const float *>(&pSource->x));
// Mask x&0xFFFF, y&0xFFFF0000,z&0,w&0
vTemp = _mm_and_ps(vTemp,g_XMMaskX16Y16);
// x needs to be sign extended
vTemp = _mm_xor_ps(vTemp,g_XMFlipX16Y16);
// Convert to floating point numbers
vTemp = _mm_cvtepi32_ps(reinterpret_cast<const __m128i *>(&vTemp)[0]);
// x - 0x8000 to undo the signed order.
vTemp = _mm_add_ps(vTemp,g_XMFixX16Y16);
// Y is 65536 too large
return _mm_mul_ps(vTemp,g_XMFixupY16);
#elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
#endif // _XM_VMX128_INTRINSICS_
}
//------------------------------------------------------------------------------
XMFINLINE XMVECTOR XMLoadUShortN2
(
CONST XMUSHORTN2* pSource
)
{
#if defined(_XM_NO_INTRINSICS_)
XMVECTOR V;
XMASSERT(pSource);
V.vector4_f32[0] = (FLOAT)pSource->x / 65535.0f;
V.vector4_f32[1] = (FLOAT)pSource->y / 65535.0f;
return V;
#elif defined(_XM_SSE_INTRINSICS_)
static const XMVECTORF32 FixupY16 = {1.0f/65535.0f,1.0f/(65535.0f*65536.0f),0.0f,0.0f};
static const XMVECTORF32 FixaddY16 = {0,32768.0f*65536.0f,0,0};
XMASSERT(pSource);
// Splat the two shorts in all four entries (WORD alignment okay,
// DWORD alignment preferred)
__m128 vTemp = _mm_load_ps1(reinterpret_cast<const float *>(&pSource->x));
// Mask x&0xFFFF, y&0xFFFF0000,z&0,w&0
vTemp = _mm_and_ps(vTemp,g_XMMaskX16Y16);
// y needs to be sign flipped
vTemp = _mm_xor_ps(vTemp,g_XMFlipY);
// Convert to floating point numbers
vTemp = _mm_cvtepi32_ps(reinterpret_cast<const __m128i *>(&vTemp)[0]);
// y + 0x8000 to undo the signed order.
vTemp = _mm_add_ps(vTemp,FixaddY16);
// Y is 65536 times too large
vTemp = _mm_mul_ps(vTemp,FixupY16);
return vTemp;
#elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
#endif // _XM_VMX128_INTRINSICS_
}
//------------------------------------------------------------------------------
XMFINLINE XMVECTOR XMLoadUShort2
(
CONST XMUSHORT2* pSource
)
{
#if defined(_XM_NO_INTRINSICS_)
XMVECTOR V;
XMASSERT(pSource);
V.vector4_f32[0] = (FLOAT)pSource->x;
V.vector4_f32[1] = (FLOAT)pSource->y;
return V;
#elif defined(_XM_SSE_INTRINSICS_)
static const XMVECTORF32 FixaddY16 = {0,32768.0f,0,0};
XMASSERT(pSource);
// Splat the two shorts in all four entries (WORD alignment okay,
// DWORD alignment preferred)
__m128 vTemp = _mm_load_ps1(reinterpret_cast<const float *>(&pSource->x));
// Mask x&0xFFFF, y&0xFFFF0000,z&0,w&0
vTemp = _mm_and_ps(vTemp,g_XMMaskX16Y16);
// y needs to be sign flipped
vTemp = _mm_xor_ps(vTemp,g_XMFlipY);
// Convert to floating point numbers
vTemp = _mm_cvtepi32_ps(reinterpret_cast<const __m128i *>(&vTemp)[0]);
// Y is 65536 times too large
vTemp = _mm_mul_ps(vTemp,g_XMFixupY16);
// y + 0x8000 to undo the signed order.
vTemp = _mm_add_ps(vTemp,FixaddY16);
return vTemp;
#elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
#endif // _XM_VMX128_INTRINSICS_
}
//------------------------------------------------------------------------------
XMFINLINE XMVECTOR XMLoadInt3
(
CONST UINT* pSource
)
{
#if defined(_XM_NO_INTRINSICS_)
XMVECTOR V;
XMASSERT(pSource);
V.vector4_u32[0] = pSource[0];
V.vector4_u32[1] = pSource[1];
V.vector4_u32[2] = pSource[2];
return V;
#elif defined(_XM_SSE_INTRINSICS_)
XMASSERT(pSource);
#ifdef _XM_ISVS2005_
__m128i V = _mm_set_epi32( 0, *(pSource+2), *(pSource+1), *pSource );
return reinterpret_cast<__m128 *>(&V)[0];
#else
__m128 x = _mm_load_ss( (const float*)pSource );
__m128 y = _mm_load_ss( (const float*)(pSource+1) );
__m128 z = _mm_load_ss( (const float*)(pSource+2) );
__m128 xy = _mm_unpacklo_ps( x, y );
return _mm_movelh_ps( xy, z );
#endif // !_XM_ISVS2005_
#elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
#endif // _XM_VMX128_INTRINSICS_
}
//------------------------------------------------------------------------------
XMFINLINE XMVECTOR XMLoadInt3A
(
CONST UINT* pSource
)
{
#if defined(_XM_NO_INTRINSICS_)
XMVECTOR V;
XMASSERT(pSource);
XMASSERT(((UINT_PTR)pSource & 0xF) == 0);
V.vector4_u32[0] = pSource[0];
V.vector4_u32[1] = pSource[1];
V.vector4_u32[2] = pSource[2];
return V;
#elif defined(_XM_SSE_INTRINSICS_)
XMASSERT(pSource);
// Reads an extra integer that is 'undefined'
__m128i V = _mm_load_si128( (const __m128i*)pSource );
return reinterpret_cast<__m128 *>(&V)[0];
#else // _XM_VMX128_INTRINSICS_
#endif // _XM_VMX128_INTRINSICS_
}
//------------------------------------------------------------------------------
XMFINLINE XMVECTOR XMLoadFloat3
(
CONST XMFLOAT3* pSource
)
{
#if defined(_XM_NO_INTRINSICS_)
XMVECTOR V;
XMASSERT(pSource);
((UINT *)(&V.vector4_f32[0]))[0] = ((const UINT *)(&pSource->x))[0];
((UINT *)(&V.vector4_f32[1]))[0] = ((const UINT *)(&pSource->y))[0];
((UINT *)(&V.vector4_f32[2]))[0] = ((const UINT *)(&pSource->z))[0];
return V;
#elif defined(_XM_SSE_INTRINSICS_)
XMASSERT(pSource);
#ifdef _XM_ISVS2005_
// This reads 1 floats past the memory that should be ignored.
// Need to continue to do this for VS 2005 due to compiler issue but prefer new method
// to avoid triggering issues with memory debug tools (like AV)
return _mm_loadu_ps( &pSource->x );
#else
__m128 x = _mm_load_ss( &pSource->x );
__m128 y = _mm_load_ss( &pSource->y );
__m128 z = _mm_load_ss( &pSource->z );
__m128 xy = _mm_unpacklo_ps( x, y );
return _mm_movelh_ps( xy, z );
#endif // !_XM_ISVS2005_
#elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
#endif // _XM_VMX128_INTRINSICS_
}
//------------------------------------------------------------------------------
XMFINLINE XMVECTOR XMLoadFloat3A
(
CONST XMFLOAT3A* pSource
)
{
#if defined(_XM_NO_INTRINSICS_)
XMVECTOR V;
XMASSERT(pSource);
XMASSERT(((UINT_PTR)pSource & 0xF) == 0);
V.vector4_f32[0] = pSource->x;
V.vector4_f32[1] = pSource->y;
V.vector4_f32[2] = pSource->z;
return V;
#elif defined(_XM_SSE_INTRINSICS_)
XMASSERT(pSource);
XMASSERT(((UINT_PTR)pSource & 0xF) == 0);
// This reads 1 floats past the memory that should be ignored.
return _mm_load_ps( &pSource->x );
#else // _XM_VMX128_INTRINSICS_
#endif // _XM_VMX128_INTRINSICS_
}
//------------------------------------------------------------------------------
XMFINLINE XMVECTOR XMLoadUHenDN3
(
CONST XMUHENDN3* pSource
)
{
#if defined(_XM_NO_INTRINSICS_)
XMVECTOR V;
UINT Element;
XMASSERT(pSource);
Element = pSource->v & 0x7FF;
V.vector4_f32[0] = (FLOAT)Element / 2047.0f;
Element = (pSource->v >> 11) & 0x7FF;
V.vector4_f32[1] = (FLOAT)Element / 2047.0f;
Element = (pSource->v >> 22) & 0x3FF;
V.vector4_f32[2] = (FLOAT)Element / 1023.0f;
return V;
#elif defined(_XM_SSE_INTRINSICS_)
static const XMVECTORF32 UHenDN3Mul = {1.0f/2047.0f,1.0f/(2047.0f*2048.0f),1.0f/(1023.0f*2048.0f*2048.0f),0};
XMASSERT(pSource);
// Get the 32 bit value and splat it
XMVECTOR vResult = _mm_load_ps1(reinterpret_cast<const float *>(&pSource->v));
// Mask off x, y and z
vResult = _mm_and_ps(vResult,g_XMMaskHenD3);
// Convert x and y to unsigned
vResult = _mm_xor_ps(vResult,g_XMFlipZ);
// Convert to float
vResult = _mm_cvtepi32_ps(reinterpret_cast<const __m128i *>(&vResult)[0]);
// Convert x and y back to signed
vResult = _mm_add_ps(vResult,g_XMAddUHenD3);
// Normalize x,y and z to -1.0f-1.0f
vResult = _mm_mul_ps(vResult,UHenDN3Mul);
return vResult;
#elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
#endif // _XM_VMX128_INTRINSICS_
}
//------------------------------------------------------------------------------
XMFINLINE XMVECTOR XMLoadUHenD3
(
CONST XMUHEND3* pSource
)
{
#if defined(_XM_NO_INTRINSICS_)
XMVECTOR V;
UINT Element;
XMASSERT(pSource);
Element = pSource->v & 0x7FF;
V.vector4_f32[0] = (FLOAT)Element;
Element = (pSource->v >> 11) & 0x7FF;
V.vector4_f32[1] = (FLOAT)Element;
Element = (pSource->v >> 22) & 0x3FF;
V.vector4_f32[2] = (FLOAT)Element;
return V;
#elif defined(_XM_SSE_INTRINSICS_)
XMASSERT(pSource);
// Get the 32 bit value and splat it
XMVECTOR vResult = _mm_load_ps1(reinterpret_cast<const float *>(&pSource->v));
// Mask off x, y and z
vResult = _mm_and_ps(vResult,g_XMMaskHenD3);
// Convert x and y to unsigned
vResult = _mm_xor_ps(vResult,g_XMFlipZ);
// Convert to float
vResult = _mm_cvtepi32_ps(reinterpret_cast<const __m128i *>(&vResult)[0]);
// Convert x and y back to signed
vResult = _mm_add_ps(vResult,g_XMAddUHenD3);
// Normalize x and y to -1024-1023.0f and z to -512-511.0f
vResult = _mm_mul_ps(vResult,g_XMMulHenD3);
return vResult;
#elif defined(XM_NO_MISALIGNED_VECTOR_ACCESS)
#endif // _XM_VMX128_INTRINSICS_
}
//------------------------------------------------------------------------------
XMFINLINE XMVECTOR XMLoadHenDN3
(
CONST XMHENDN3* pSource
)
{
#if defined(_XM_NO_INTRINSICS_)
XMVECTOR V;
UINT Element;
static CONST UINT SignExtendXY[] = {0x00000000, 0xFFFFF800};
static CONST UINT SignExtendZ[] = {0x00000000, 0xFFFFFC00};
XMASSERT(pSource);
XMASSERT((pSource->v & 0x7FF) != 0x400);