public void Utilities_NumberHelpers_CanConvertIntToNormalizedFloatAndBack(int value, int minValue, int maxValue, float expected) { var result = NumberHelpers.IntToNormalizedFloat(value, minValue, maxValue); Assert.That(result, Is.EqualTo(expected).Within(float.Epsilon)); var integer = NumberHelpers.NormalizedFloatToInt(result, minValue, maxValue); Assert.That(integer, Is.EqualTo(Mathf.Clamp(value, minValue, maxValue))); }
public void WriteDouble(void *statePtr, double value) { var valuePtr = (byte *)statePtr + (int)byteOffset; var fmt = (int)format; switch (fmt) { case kFormatBit: if (sizeInBits == 1) { MemoryHelpers.WriteSingleBit(valuePtr, bitOffset, value >= 0.5f); } else { MemoryHelpers.WriteNormalizedUIntAsMultipleBits(valuePtr, bitOffset, sizeInBits, (float)value); } break; case kFormatSBit: if (sizeInBits == 1) { MemoryHelpers.WriteSingleBit(valuePtr, bitOffset, value >= 0.0f); } else { MemoryHelpers.WriteNormalizedUIntAsMultipleBits(valuePtr, bitOffset, sizeInBits, (float)value * 0.5f + 0.5f); } break; case kFormatInt: Debug.Assert(sizeInBits == 32, "INT state must have sizeInBits=16"); Debug.Assert(bitOffset == 0, "INT state must be byte-aligned"); *(int *)valuePtr = NumberHelpers.NormalizedFloatToInt((float)value * 0.5f + 0.5f, int.MinValue, int.MaxValue); break; case kFormatUInt: Debug.Assert(sizeInBits == 32, "UINT state must have sizeInBits=16"); Debug.Assert(bitOffset == 0, "UINT state must be byte-aligned"); *(uint *)valuePtr = NumberHelpers.NormalizedFloatToUInt((float)value, uint.MinValue, uint.MaxValue); break; case kFormatShort: Debug.Assert(sizeInBits == 16, "SHRT state must have sizeInBits=16"); Debug.Assert(bitOffset == 0, "SHRT state must be byte-aligned"); *(short *)valuePtr = (short)NumberHelpers.NormalizedFloatToInt((float)value * 0.5f + 0.5f, short.MinValue, short.MaxValue); break; case kFormatUShort: Debug.Assert(sizeInBits == 16, "USHT state must have sizeInBits=16"); Debug.Assert(bitOffset == 0, "USHT state must be byte-aligned"); *(ushort *)valuePtr = (ushort)NumberHelpers.NormalizedFloatToUInt((float)value, ushort.MinValue, ushort.MaxValue); break; case kFormatByte: Debug.Assert(sizeInBits == 8, "BYTE state must have sizeInBits=8"); Debug.Assert(bitOffset == 0, "BYTE state must be byte-aligned"); *valuePtr = (byte)NumberHelpers.NormalizedFloatToUInt((float)value, byte.MinValue, byte.MaxValue); break; case kFormatSByte: Debug.Assert(sizeInBits == 8, "SBYT state must have sizeInBits=8"); Debug.Assert(bitOffset == 0, "SBYT state must be byte-aligned"); *(sbyte *)valuePtr = (sbyte)NumberHelpers.NormalizedFloatToInt((float)value * 0.5f + 0.5f, sbyte.MinValue, sbyte.MaxValue); break; case kFormatFloat: Debug.Assert(sizeInBits == 32, "FLT state must have sizeInBits=32"); Debug.Assert(bitOffset == 0, "FLT state must be byte-aligned"); *(float *)valuePtr = (float)value; break; case kFormatDouble: Debug.Assert(sizeInBits == 64, "DBL state must have sizeInBits=64"); Debug.Assert(bitOffset == 0, "DBL state must be byte-aligned"); *(double *)valuePtr = value; break; // Not supported: // - kFormatLong // - kFormatULong // - kFormatFloat // - kFormatDouble default: throw new InvalidOperationException($"State format '{format}' is not supported as floating-point format"); } }
internal PrimitiveValue FloatToPrimitiveValue(float value) { var fmt = (int)format; switch (fmt) { case kFormatBit: if (sizeInBits == 1) { return(value >= 0.5f); } ////FIXME: is this supposed to be int or uint? return((int)NumberHelpers.NormalizedFloatToUInt(value, 0, (uint)((1UL << (int)sizeInBits) - 1))); case kFormatSBit: { if (sizeInBits == 1) { return(value >= 0.0f); } var minValue = (int)-(long)(1UL << ((int)sizeInBits - 1)); var maxValue = (int)((1UL << ((int)sizeInBits - 1)) - 1); return(NumberHelpers.NormalizedFloatToInt(value, minValue, maxValue)); } case kFormatInt: Debug.Assert(sizeInBits == 32, "INT state must have sizeInBits=32"); Debug.Assert(bitOffset == 0, "INT state must be byte-aligned"); return(NumberHelpers.NormalizedFloatToInt(value * 0.5f + 0.5f, int.MinValue, int.MaxValue)); case kFormatUInt: Debug.Assert(sizeInBits == 32, "UINT state must have sizeInBits=32"); Debug.Assert(bitOffset == 0, "UINT state must be byte-aligned"); return(NumberHelpers.NormalizedFloatToUInt(value, uint.MinValue, uint.MaxValue)); case kFormatShort: Debug.Assert(sizeInBits == 16, "SHRT state must have sizeInBits=16"); Debug.Assert(bitOffset == 0, "SHRT state must be byte-aligned"); return((short)NumberHelpers.NormalizedFloatToInt(value * 0.5f + 0.5f, short.MinValue, short.MaxValue)); case kFormatUShort: Debug.Assert(sizeInBits == 16, "USHT state must have sizeInBits=16"); Debug.Assert(bitOffset == 0, "USHT state must be byte-aligned"); return((ushort)NumberHelpers.NormalizedFloatToUInt(value, ushort.MinValue, ushort.MaxValue)); case kFormatByte: Debug.Assert(sizeInBits == 8, "BYTE state must have sizeInBits=8"); Debug.Assert(bitOffset == 0, "BYTE state must be byte-aligned"); return((byte)NumberHelpers.NormalizedFloatToUInt(value, byte.MinValue, byte.MaxValue)); case kFormatSByte: Debug.Assert(sizeInBits == 8, "SBYT state must have sizeInBits=8"); Debug.Assert(bitOffset == 0, "SBYT state must be byte-aligned"); return((sbyte)NumberHelpers.NormalizedFloatToInt(value * 0.5f + 0.5f, sbyte.MinValue, sbyte.MaxValue)); case kFormatFloat: Debug.Assert(sizeInBits == 32, "FLT state must have sizeInBits=32"); Debug.Assert(bitOffset == 0, "FLT state must be byte-aligned"); return(value); case kFormatDouble: Debug.Assert(sizeInBits == 64, "DBL state must have sizeInBits=64"); Debug.Assert(bitOffset == 0, "DBL state must be byte-aligned"); return(value); // Not supported: // - kFormatLong // - kFormatULong default: throw new Exception($"State format '{format}' is not supported as floating-point format"); } }