Esempio n. 1
0
        public int ReadInt(void *statePtr)
        {
            Debug.Assert(sizeInBits != 0);

            var valuePtr = (byte *)statePtr + (int)byteOffset;

            var fmt = (int)format;

            switch (fmt)
            {
            case kFormatBit:
                if (sizeInBits == 1)
                {
                    return(MemoryHelpers.ReadSingleBit(valuePtr, bitOffset) ? 1 : 0);
                }
                return((int)MemoryHelpers.ReadMultipleBitsAsUInt(valuePtr, bitOffset, sizeInBits));

            case kFormatSBit:
                if (sizeInBits == 1)
                {
                    return(MemoryHelpers.ReadSingleBit(valuePtr, bitOffset) ? 1 : -1);
                }
                return(MemoryHelpers.ReadExcessKMultipleBitsAsInt(valuePtr, bitOffset, sizeInBits));

            case kFormatInt:
            case kFormatUInt:
                Debug.Assert(sizeInBits == 32, "INT and UINT state must have sizeInBits=32");
                Debug.Assert(bitOffset == 0, "INT and UINT state must be byte-aligned");
                if (fmt == kFormatUInt)
                {
                    Debug.Assert(*(uint *)valuePtr <= int.MaxValue, "UINT must fit in the int");
                }
                return(*(int *)valuePtr);

            case kFormatShort:
                Debug.Assert(sizeInBits == 16, "SHRT state must have sizeInBits=16");
                Debug.Assert(bitOffset == 0, "SHRT state must be byte-aligned");
                return(*(short *)valuePtr);

            case kFormatUShort:
                Debug.Assert(sizeInBits == 16, "USHT state must have sizeInBits=16");
                Debug.Assert(bitOffset == 0, "USHT state must be byte-aligned");
                return(*(ushort *)valuePtr);

            case kFormatByte:
                Debug.Assert(sizeInBits == 8, "BYTE state must have sizeInBits=8");
                Debug.Assert(bitOffset == 0, "BYTE state must be byte-aligned");
                return(*valuePtr);

            case kFormatSByte:
                Debug.Assert(sizeInBits == 8, "SBYT state must have sizeInBits=8");
                Debug.Assert(bitOffset == 0, "SBYT state must be byte-aligned");
                return(*(sbyte *)valuePtr);

            // Not supported:
            // - kFormatLong
            // - kFormatULong
            // - kFormatFloat
            // - kFormatDouble
            default:
                throw new InvalidOperationException($"State format '{format}' is not supported as integer format");
            }
        }