forked from Pryaxis/TSAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added HalfVector2, HalfUtils to XNA structures.
- Loading branch information
Showing
4 changed files
with
165 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using System; | ||
|
||
internal static class HalfUtils | ||
{ | ||
public unsafe static ushort Pack(float value) | ||
{ | ||
uint num = *(uint*)(&value); | ||
uint num2 = (num & 2147483648u) >> 16; | ||
uint num3 = num & 2147483647u; | ||
ushort result; | ||
if (num3 > 1207955455u) | ||
{ | ||
result = (ushort)(num2 | 32767u); | ||
} | ||
else if (num3 < 947912704u) | ||
{ | ||
uint num4 = (num3 & 8388607u) | 8388608u; | ||
int num5 = (int)(113u - (num3 >> 23)); | ||
num3 = ((num5 > 31) ? 0u : (num4 >> num5)); | ||
result = (ushort)(num2 | num3 + 4095u + (num3 >> 13 & 1u) >> 13); | ||
} | ||
else | ||
{ | ||
result = (ushort)(num2 | num3 + 3355443200u + 4095u + (num3 >> 13 & 1u) >> 13); | ||
} | ||
return result; | ||
} | ||
|
||
public unsafe static float Unpack(ushort value) | ||
{ | ||
uint num3; | ||
if (((int)value & -33792) == 0) | ||
{ | ||
if ((value & 1023) != 0) | ||
{ | ||
uint num = 4294967282u; | ||
uint num2 = (uint)(value & 1023); | ||
while ((num2 & 1024u) == 0u) | ||
{ | ||
num -= 1u; | ||
num2 <<= 1; | ||
} | ||
num2 &= 4294966271u; | ||
num3 = (uint)((int)(value & 32768) << 16 | (int)((int)(num + 127u) << 23) | (int)((int)num2 << 13)); | ||
} | ||
else | ||
{ | ||
num3 = (uint)((uint)(value & 32768) << 16); | ||
} | ||
} | ||
else | ||
{ | ||
num3 = (uint)((int)(value & 32768) << 16 | (value >> 10 & 31) - 15 + 127 << 23 | (int)(value & 1023) << 13); | ||
} | ||
return *(float*)(&num3); | ||
} | ||
|
||
private const uint BiasDiffo = 3355443200u; | ||
|
||
private const int cExpBias = 15; | ||
|
||
private const int cExpBits = 5; | ||
|
||
private const int cFracBits = 10; | ||
|
||
private const int cFracBitsDiff = 13; | ||
|
||
private const uint cFracMask = 1023u; | ||
|
||
private const uint cRoundBit = 4096u; | ||
|
||
private const int cSignBit = 15; | ||
|
||
private const uint cSignMask = 32768u; | ||
|
||
private const uint eMax = 16u; | ||
|
||
private const int eMin = -14; | ||
|
||
private const uint wMaxNormal = 1207955455u; | ||
|
||
private const uint wMinNormal = 947912704u; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using System; | ||
|
||
[Serializable] | ||
public struct HalfVector2 : IPackedVector<uint>, IPackedVector, IEquatable<HalfVector2> | ||
{ | ||
public HalfVector2(Vector2 vector) | ||
{ | ||
this.packedValue = HalfVector2.PackHelper(vector.X, vector.Y); | ||
} | ||
|
||
public HalfVector2(float x, float y) | ||
{ | ||
this.packedValue = HalfVector2.PackHelper(x, y); | ||
} | ||
|
||
public override bool Equals(object obj) | ||
{ | ||
return obj is HalfVector2 && this.Equals((HalfVector2)obj); | ||
} | ||
|
||
public bool Equals(HalfVector2 other) | ||
{ | ||
return this.packedValue.Equals(other.packedValue); | ||
} | ||
|
||
public override int GetHashCode() | ||
{ | ||
return this.packedValue.GetHashCode(); | ||
} | ||
|
||
void IPackedVector.PackFromVector4(Vector4 vector) | ||
{ | ||
this.packedValue = HalfVector2.PackHelper(vector.X, vector.Y); | ||
} | ||
|
||
Vector4 IPackedVector.ToVector4() | ||
{ | ||
Vector2 vector = this.ToVector2(); | ||
return new Vector4(vector.X, vector.Y, 0f, 1f); | ||
} | ||
|
||
public static bool operator ==(HalfVector2 a, HalfVector2 b) | ||
{ | ||
return a.Equals(b); | ||
} | ||
|
||
public static bool operator !=(HalfVector2 a, HalfVector2 b) | ||
{ | ||
return !a.Equals(b); | ||
} | ||
|
||
private static uint PackHelper(float vectorX, float vectorY) | ||
{ | ||
uint num = (uint)HalfUtils.Pack(vectorX); | ||
uint num2 = (uint)((uint)HalfUtils.Pack(vectorY) << 16); | ||
return num | num2; | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
return this.ToVector2().ToString(); | ||
} | ||
|
||
public Vector2 ToVector2() | ||
{ | ||
Vector2 result; | ||
result.X = HalfUtils.Unpack((ushort)this.packedValue); | ||
result.Y = HalfUtils.Unpack((ushort)(this.packedValue >> 16)); | ||
return result; | ||
} | ||
|
||
public uint PackedValue | ||
{ | ||
get { return this.packedValue; } | ||
set { this.packedValue = value; } | ||
} | ||
|
||
private uint packedValue; | ||
} |