public static bool TryRunHalf(Half value, int requestedDigits, ref NumberBuffer number) { Half v = Half.IsNegative(value) ? Half.Negate(value) : value; Debug.Assert((double)v > 0); Debug.Assert(Half.IsFinite(v)); int length; int decimalExponent; bool result; if (requestedDigits == -1) { DiyFp w = DiyFp.CreateAndGetBoundaries(v, out DiyFp boundaryMinus, out DiyFp boundaryPlus).Normalize(); result = TryRunShortest(in boundaryMinus, in w, in boundaryPlus, number.Digits, out length, out decimalExponent); } else { DiyFp w = new DiyFp(v).Normalize(); result = TryRunCounted(in w, requestedDigits, number.Digits, out length, out decimalExponent); } if (result) { Debug.Assert((requestedDigits == -1) || (length == requestedDigits)); number.Scale = length + decimalExponent; number.Digits[length] = (byte)('\0'); number.DigitsCount = length; } return(result); }
public static unsafe void Dragon4Half(Half value, int cutoffNumber, bool isSignificantDigits, ref NumberBuffer number) { Half v = Half.IsNegative(value) ? Half.Negate(value) : value; Debug.Assert((double)v > 0.0); Debug.Assert(Half.IsFinite(v)); ushort mantissa = ExtractFractionAndBiasedExponent(value, out int exponent); uint mantissaHighBitIdx; bool hasUnequalMargins = false; if ((mantissa >> DiyFp.HalfImplicitBitIndex) != 0) { mantissaHighBitIdx = DiyFp.HalfImplicitBitIndex; hasUnequalMargins = (mantissa == (1U << DiyFp.HalfImplicitBitIndex)); } else { Debug.Assert(mantissa != 0); mantissaHighBitIdx = (uint)BitOperations.Log2(mantissa); } int length = (int)(Dragon4(mantissa, exponent, mantissaHighBitIdx, hasUnequalMargins, cutoffNumber, isSignificantDigits, number.Digits, out int decimalExponent)); number.Scale = decimalExponent + 1; number.Digits[length] = (byte)('\0'); number.DigitsCount = length; }
public DiyFp(Half value) { Debug.Assert(Half.IsFinite(value)); Debug.Assert((float)value > 0.0f); f = ExtractFractionAndBiasedExponent(value, out e); }