Created
May 23, 2019 00:03
-
-
Save EgorBo/866a49334291c1ac3b108eb9341681ae to your computer and use it in GitHub Desktop.
test.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
using System; | |
using System.Collections.Generic; | |
using System.Runtime.CompilerServices; | |
class Program | |
{ | |
static void Main() | |
{ | |
// some corner cases | |
var values = new List<float>( | |
new float[] {0, 1, MathF.PI, MathF.E, float.MinValue, float.MinValue, int.MaxValue, int.MinValue }); | |
var random = new Random(); | |
// Also, add some random values jic | |
for (int i = 0; i < 100000; i++) | |
values.Add((float)random.NextDouble()); | |
for (int i = 0; i < values.Count; i++) | |
Test(values[i]); | |
Console.WriteLine("Done."); | |
} | |
public static void Test(float x) | |
{ | |
// the whole method contains 46 vdivss and 46 vmulss (as expected) | |
// ConstToVar(x) to generate old vdivss | |
AssertEquals(expected: x / ConstToVar(2), actual: x / 2); | |
AssertEquals(expected: x / ConstToVar(4), actual: x / 4); | |
AssertEquals(expected: x / ConstToVar(8), actual: x / 8); | |
AssertEquals(expected: x / ConstToVar(16), actual: x / 16); | |
AssertEquals(expected: x / ConstToVar(32), actual: x / 32); | |
AssertEquals(expected: x / ConstToVar(64), actual: x / 64); | |
AssertEquals(expected: x / ConstToVar(128), actual: x / 128); | |
AssertEquals(expected: x / ConstToVar(256), actual: x / 256); | |
AssertEquals(expected: x / ConstToVar(512), actual: x / 512); | |
AssertEquals(expected: x / ConstToVar(1024), actual: x / 1024); | |
AssertEquals(expected: x / ConstToVar(2048), actual: x / 2048); | |
AssertEquals(expected: x / ConstToVar(4096), actual: x / 4096); | |
AssertEquals(expected: x / ConstToVar(8192), actual: x / 8192); | |
AssertEquals(expected: x / ConstToVar(16384), actual: x / 16384); | |
AssertEquals(expected: x / ConstToVar(32768), actual: x / 32768); | |
AssertEquals(expected: x / ConstToVar(65536), actual: x / 65536); | |
AssertEquals(expected: x / ConstToVar(131072), actual: x / 131072); | |
AssertEquals(expected: x / ConstToVar(262144), actual: x / 262144); | |
AssertEquals(expected: x / ConstToVar(524288), actual: x / 524288); | |
AssertEquals(expected: x / ConstToVar(1048576), actual: x / 1048576); | |
AssertEquals(expected: x / ConstToVar(2097152), actual: x / 2097152); | |
AssertEquals(expected: x / ConstToVar(4194304), actual: x / 4194304); | |
AssertEquals(expected: x / ConstToVar(8388608), actual: x / 8388608); | |
AssertEquals(expected: x / ConstToVar(16777216), actual: x / 16777216); | |
AssertEquals(expected: x / ConstToVar(33554432), actual: x / 33554432); | |
AssertEquals(expected: x / ConstToVar(67108864), actual: x / 67108864); | |
AssertEquals(expected: x / ConstToVar(134217728), actual: x / 134217728); | |
AssertEquals(expected: x / ConstToVar(268435456), actual: x / 268435456); | |
AssertEquals(expected: x / ConstToVar(536870912), actual: x / 536870912); | |
AssertEquals(expected: x / ConstToVar(1073741824), actual: x / 1073741824); | |
// < 1 | |
AssertEquals(expected: x / ConstToVar(0.5f), actual: x / 0.5f); | |
AssertEquals(expected: x / ConstToVar(0.25f), actual: x / 0.25f); | |
AssertEquals(expected: x / ConstToVar(0.125f), actual: x / 0.125f); | |
AssertEquals(expected: x / ConstToVar(0.0625f), actual: x / 0.0625f); | |
AssertEquals(expected: x / ConstToVar(0.03125f), actual: x / 0.03125f); | |
AssertEquals(expected: x / ConstToVar(0.015625f), actual: x / 0.015625f); | |
AssertEquals(expected: x / ConstToVar(0.0078125f), actual: x / 0.0078125f); | |
AssertEquals(expected: x / ConstToVar(0.00390625f), actual: x / 0.00390625f); | |
AssertEquals(expected: x / ConstToVar(0.001953125f), actual: x / 0.001953125f); | |
AssertEquals(expected: x / ConstToVar(0.0009765625f), actual: x / 0.0009765625f); | |
AssertEquals(expected: x / ConstToVar(0.00048828125f), actual: x / 0.00048828125f); | |
AssertEquals(expected: x / ConstToVar(0.00024414062f), actual: x / 0.00024414062f); | |
AssertEquals(expected: x / ConstToVar(0.00012207031f), actual: x / 0.00012207031f); | |
// < 0 | |
AssertEquals(expected: x / ConstToVar(-1073741824), actual: x / -1073741824); | |
AssertEquals(expected: x / ConstToVar(-0.00012207031f), actual: x / -0.00012207031f); | |
AssertEquals(expected: x / ConstToVar(-2147483648), actual: x / -2147483648); | |
} | |
[MethodImpl(MethodImplOptions.NoInlining)] | |
private static void AssertEquals(float expected, float actual) | |
{ | |
var expectedi = BitConverter.SingleToInt32Bits(expected); | |
var actuali = BitConverter.SingleToInt32Bits(actual); | |
if (expectedi != actuali || expected != actual) | |
throw new InvalidOperationException(); | |
} | |
[MethodImpl(MethodImplOptions.NoInlining)] | |
private static T ConstToVar<T>(T v) => v; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment