Skip to content

Commit

Permalink
Merge branch 'main' into reyang/sanity
Browse files Browse the repository at this point in the history
  • Loading branch information
reyang authored Aug 5, 2022
2 parents 8f2b978 + ce99d44 commit 6e1cc4d
Show file tree
Hide file tree
Showing 3 changed files with 264 additions and 53 deletions.
16 changes: 7 additions & 9 deletions src/OpenTelemetry/Metrics/ExponentialBucketHistogram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
// limitations under the License.
// </copyright>

#if NET6_0_OR_GREATER

using System;
using System.Diagnostics;
using OpenTelemetry.Internal;
Expand All @@ -30,8 +28,6 @@ namespace OpenTelemetry.Metrics;
/// </summary>
internal class ExponentialBucketHistogram
{
private static readonly double Log2E = Math.Log2(Math.E); // 1 / Math.Log(2)

private int scale;
private double scalingFactor; // 2 ^ scale / log(2)

Expand Down Expand Up @@ -107,13 +103,17 @@ internal int Scale
{
get => this.scale;

private set
set
{
this.scale = value;
this.scalingFactor = Math.ScaleB(Log2E, value);

// A subset of Math.ScaleB(Math.Log2(Math.E), value)
this.scalingFactor = BitConverter.Int64BitsToDouble(0x71547652B82FEL | ((0x3FFL + value) << 52 /* fraction width */));
}
}

internal double ScalingFactor => this.scalingFactor;

internal CircularBufferBuckets PositiveBuckets { get; }

internal long ZeroCount { get; private set; }
Expand All @@ -135,7 +135,7 @@ public int MapToIndex(double value)
{
Debug.Assert(MathHelper.IsFinite(value), "IEEE-754 +Inf, -Inf and NaN should be filtered out before calling this method.");
Debug.Assert(value != 0, "IEEE-754 zero values should be handled by ZeroCount.");
Debug.Assert(!double.IsNegative(value), "IEEE-754 negative values should be normalized before calling this method.");
Debug.Assert(value > 0, "IEEE-754 negative values should be normalized before calling this method.");

var bits = BitConverter.DoubleToInt64Bits(value);
var fraction = bits & 0xFFFFFFFFFFFFFL /* fraction mask */;
Expand Down Expand Up @@ -202,5 +202,3 @@ public void Record(double value)
Debug.Assert(n == 0, "Increment should always succeed after scale down.");
}
}

#endif
Loading

0 comments on commit 6e1cc4d

Please sign in to comment.