Browse Source

Portable: Union for better double-long conversion perf

la-knuth
Christoph Ruegg 14 years ago
parent
commit
2fd5f6aaac
  1. 23
      src/Numerics/Precision.cs

23
src/Numerics/Precision.cs

@ -34,6 +34,10 @@ namespace MathNet.Numerics
using System.Collections.Generic; using System.Collections.Generic;
using System.Numerics; using System.Numerics;
#if PORTABLE
using System.Runtime.InteropServices;
#endif
/// <summary> /// <summary>
/// Utilities for working with floating point numbers. /// Utilities for working with floating point numbers.
/// </summary> /// </summary>
@ -1822,17 +1826,30 @@ namespace MathNet.Numerics
#if PORTABLE #if PORTABLE
internal static long DoubleToInt64Bits(double value) internal static long DoubleToInt64Bits(double value)
{ {
return BitConverter.ToInt64(BitConverter.GetBytes(value), 0); var union = new DoubleLongUnion {Double = value};
return union.Int64;
} }
internal static double Int64BitsToDouble(long value) internal static double Int64BitsToDouble(long value)
{ {
return BitConverter.ToDouble(BitConverter.GetBytes(value), 0); var union = new DoubleLongUnion {Int64 = value};
return union.Double;
} }
internal static double Truncate(double value){ internal static double Truncate(double value)
{
return value >= 0.0 ? Math.Floor(value) : Math.Ceiling(value); return value >= 0.0 ? Math.Floor(value) : Math.Ceiling(value);
} }
[StructLayout(LayoutKind.Explicit)]
public struct DoubleLongUnion
{
[FieldOffset(0)]
public double Double;
[FieldOffset(0)]
public long Int64;
}
#endif #endif
} }
} }
Loading…
Cancel
Save