Skip to content

Replace FloatUtils Conversion to Integral types through unions with casting - #7676

Draft
abhishekdahad-1 wants to merge 1 commit into
dotnet:mainfrom
abhishekdahad-1:HashRoundRegisterSpill
Draft

Replace FloatUtils Conversion to Integral types through unions with casting#7676
abhishekdahad-1 wants to merge 1 commit into
dotnet:mainfrom
abhishekdahad-1:HashRoundRegisterSpill

Conversation

@abhishekdahad-1

Copy link
Copy Markdown

FloatUtils.cs contains an overloaded function GetBits that takes Single/Double precision floating point values and converts them into the corresponding unsigned integral types with the same width. The disassembly for this function will have a store-load dependency which can be reduced to a single mov instruction.

The function utilizes the internal union that is stored in the class, by writing into the floating point field, and then reading from the integral field. This can be observed in the Microsoft.ML.PerformanceTests.HashBench.HashScalarDouble testcase, when inspecting the HashRound function in Hashing.cs:

private uint HashRound(uint seed, double value, bool old)
{
      ulong v = FloatUtils.GetBits(value == 0 ? 0 : value);
      var hash = Hashing.MurmurRound(seed, Utils.GetLo(v));
      var hi = Utils.GetHi(v);
      if (old && hi == 0)
          return hash;
      return Hashing.MurmurRound(hash, hi);
}

When value is converted from a double to a ulong, the corresponding assembly code will look like this because of union semantics:

vmovsd qword ptr [rsp+0x28], xmm1		
mov rdx, qword ptr [rsp+0x28]		

This can be collapsed into a single instruction like so:

movq rdx, xmm1

When benchmarked on both Microsoft.ML.PerformanceTests.HashBench.HashScalarDouble and Microsoft.ML.PerformanceTests.HashBench.HashScalarFloat, it shows a performance improvement.
Before:

Method Mean Error StdDev Extra Metric
HashScalarDouble 485.0 us 9.43 us 10.09 us -
HashScalarFloat 376.4 us 0.71 us 0.63 us -

After:

Method Mean Error StdDev Extra Metric
HashScalarDouble 472.2 us 2.44 us 2.16 us -
HashScalarFloat 362.3 us 0.82 us 0.77 us -

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant