static long ApplyIntegerCastMode(long value, IntDsdlType type) { var maxValue = type.MaxBitlen == 64 ? unchecked ((long)ulong.MaxValue) : (long)((1UL << type.MaxBitlen) - 1); switch (type.CastMode) { case CastMode.Truncated: return(value & maxValue); case CastMode.Saturated: var range = TypeLimits.GetIntRange(type.MaxBitlen); if (value < range.Minimum) { return(range.Minimum); } else if (value > range.Maximum) { return(range.Maximum); } return(value); default: throw new ArgumentOutOfRangeException(nameof(type.CastMode)); } }
static void WriteFloatPrimitive(BitStreamWriter writer, double value, FloatDsdlType type) { var range = TypeLimits.GetFloatRange(type.MaxBitlen); switch (type.CastMode) { case CastMode.Saturated: if (value > range.Maximum) { value = range.Maximum; } if (value < range.Minimum) { value = range.Minimum; } break; case CastMode.Truncated: if (!double.IsNaN(value) && value > range.Maximum) { value = double.PositiveInfinity; } if (!double.IsNaN(value) && value < range.Maximum) { value = double.NegativeInfinity; } break; default: throw new ArgumentOutOfRangeException(); } switch (type.MaxBitlen) { case 16: var val = BitSerializer.Float32ToUInt16((float)value); BitSerializer.Write(writer, val, 16); break; case 32: BitSerializer.Write(writer, (float)value, 32); break; case 64: BitSerializer.Write(writer, value, 64); break; default: throw new InvalidOperationException($"Unexpected float bit lenght: {type.MaxBitlen}."); } }