-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimized how floats are calculated in Battalion Wars.
- Loading branch information
1 parent
904eea5
commit 722de03
Showing
4 changed files
with
81 additions
and
32 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
FinModelUtility/Formats/Modl/Modl Tests/WeirdFloatMathTests.cs
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,31 @@ | ||
namespace modl.schema.anim { | ||
public class WeirdFloatMathTests { | ||
[Test] | ||
[TestCase(3100000000, -0.00009453f)] | ||
[TestCase(3150000000, -0.005895f)] | ||
[TestCase(3200000000, -0.367431f)] | ||
[TestCase(3250000000, -22.883056f)] | ||
[TestCase(3300000000, -1424.03125f)] | ||
[TestCase((uint) 0x0229C4AB, WeirdFloatMath.C_ZERO)] | ||
[TestCase((uint) 0x38800000, WeirdFloatMath.C_6_10351_EN5)] | ||
[TestCase((uint) 0x3F000000, WeirdFloatMath.C_HALF)] | ||
[TestCase((uint) 0x40400000, WeirdFloatMath.C_3)] | ||
[TestCase((uint) 0x46800000, WeirdFloatMath.C_16384)] | ||
public void TestInterpretAsSingle(uint input, float expectedOutput) | ||
=> Assert.AreEqual(expectedOutput, | ||
WeirdFloatMath.InterpretAsSingle(input), | ||
.000001f); | ||
|
||
[Test] | ||
[TestCase(13746744073709551615, -2.6518952414567028E-06d)] | ||
[TestCase(13796744073709551615, -0.0058304183539235046d)] | ||
[TestCase(13846744073709551615, -12.758538758847861d)] | ||
[TestCase(13896744073709551615, -27804.427732706066d)] | ||
[TestCase(13946744073709551615, -60373745.84277343d)] | ||
[TestCase((ulong) 0x4330000000000000, WeirdFloatMath.C_4503599627370496)] | ||
public void TestInterpretAsDouble(ulong input, double expectedOutput) | ||
=> Assert.AreEqual(expectedOutput, | ||
WeirdFloatMath.InterpretAsDouble(input), | ||
.0000001); | ||
} | ||
} |
42 changes: 30 additions & 12 deletions
42
FinModelUtility/Formats/Modl/Modl/src/schema/anim/WeirdFloatMath.cs
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 |
---|---|---|
@@ -1,23 +1,41 @@ | ||
namespace modl.schema.anim { | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace modl.schema.anim { | ||
public static class WeirdFloatMath { | ||
/// <summary> 0x38800000 </summary> | ||
public const float C_6_10351_EN5 = 6.103515625E-05f; | ||
|
||
/// <summary> 0x0229C4AB </summary> | ||
public const float C_ZERO = 0; | ||
|
||
/// <summary> 0x3F000000 </summary> | ||
public const float C_HALF = 0.5f; | ||
|
||
/// <summary> 0x40400000 </summary> | ||
public const float C_3 = 3; | ||
|
||
/// <summary> 0x46800000 </summary> | ||
public const float C_16384 = 16384f; | ||
|
||
/// <summary> 0x4330000000000000 </summary> | ||
public const double C_4503599627370496 = 4503599627370496; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static ulong Concat44(uint first, uint second) | ||
=> ((ulong) first << 32) | second; | ||
|
||
public static double InterpretAsDouble(ulong value) { | ||
Span<byte> bytes = stackalloc byte[8]; | ||
BitConverter.TryWriteBytes(bytes, value); | ||
return BitConverter.ToDouble(bytes); | ||
} | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static unsafe double InterpretAsDouble(ulong value) | ||
=> *(double*) (&value); | ||
|
||
public static float InterpretAsSingle(uint value) { | ||
Span<byte> bytes = stackalloc byte[4]; | ||
BitConverter.TryWriteBytes(bytes, value); | ||
return BitConverter.ToSingle(bytes); | ||
} | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static unsafe float InterpretAsSingle(uint value) | ||
=> *(float*) (&value); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static double CreateWeirdDoubleFromUInt32(uint value) | ||
=> WeirdFloatMath.InterpretAsDouble( | ||
WeirdFloatMath.Concat44(0x43300000, value)) - | ||
WeirdFloatMath.InterpretAsDouble(0x4330000000000000); | ||
C_4503599627370496; | ||
} | ||
} |
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
3 changes: 1 addition & 2 deletions
3
FinModelUtility/Formats/Modl/Modl/src/schema/modl/bw2/Bw2Modl.cs
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